-
-
Notifications
You must be signed in to change notification settings - Fork 625
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
This PR adds support for .transaction() #814
base: master
Are you sure you want to change the base?
Changes from 5 commits
cd0447b
0c921d2
90b96ec
9b80926
b3073f9
33347a1
f0c9e0d
85cd056
88d49b6
5c373ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -214,6 +214,47 @@ PromiseConnection.prototype.changeUser = function(options) { | |
}); | ||
}); | ||
}; | ||
PromiseConnection.prototype.transaction = function(options,userPromise) { | ||
const self = this; | ||
if (! userPromise) { | ||
userPromise = options; | ||
options = {}; | ||
} | ||
options = options || {}; | ||
|
||
var promiseChain = Promise.resolve(); | ||
|
||
if (options.autoCommit !== true) { | ||
promiseChain = promiseChain.then(function() { | ||
return self.query( | ||
"START TRANSACTION" + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You might also want to support isolation levels https://dev.mysql.com/doc/refman/8.0/en/innodb-transaction-isolation-levels.html A constant type can be exported directly by |
||
( options.consistentSnapshot === true ? " WITH CONSISTENT SNAPSHOT" : | ||
( options.readWrite !== false ? " READ WRITE" : " READ ONLY" ) | ||
) | ||
); | ||
}); | ||
} | ||
|
||
promiseChain = promiseChain.then(function() { | ||
return userPromise(self); | ||
}); | ||
|
||
if (options.autoCommit === false) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You meant There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually I meant !== true, as I don't want truthy objects to enable autocommit, only the exact value autoCommit: true |
||
promiseChain = promiseChain.then(function(res) { | ||
return self.query("COMMIT") | ||
.then(function() { | ||
return res; | ||
}); | ||
}) | ||
.catch(function(err) { | ||
return self.query("ROLLBACK") | ||
.catch(function() { | ||
throw err; | ||
}); | ||
}) | ||
} | ||
return promiseChain; | ||
}; | ||
|
||
function PromisePreparedStatementInfo(statement, promiseImpl) { | ||
this.statement = statement; | ||
|
@@ -339,6 +380,21 @@ PromisePool.prototype.getConnection = function() { | |
}); | ||
}; | ||
|
||
PromisePool.prototype.transaction = function(options,userPromise) { | ||
return this.getConnection() | ||
.then(function(con) { | ||
return this.Promise.resolve(con.transaction(options,userPromise)) | ||
.then(function(res) { | ||
con.release(); | ||
return res; | ||
}) | ||
.catch(function(err) { | ||
con.release(); | ||
throw err; | ||
}); | ||
}); | ||
}; | ||
|
||
PromisePool.prototype.query = function(sql, args) { | ||
const corePool = this.pool; | ||
const localErr = new Error(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/nitpick
userPromise
is not a promise, its a callback that will return a promise, name should reflect thatThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick accepted