-
-
Notifications
You must be signed in to change notification settings - Fork 619
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 9 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 |
---|---|---|
|
@@ -119,15 +119,47 @@ class PromiseConnection extends EventEmitter { | |
}); | ||
} | ||
|
||
beginTransaction() { | ||
beginTransaction(options) { | ||
const c = this.connection; | ||
const localErr = new Error(); | ||
return new this.Promise((resolve, reject) => { | ||
const done = makeDoneCb(resolve, reject, localErr); | ||
c.beginTransaction(done); | ||
c.beginTransaction(options,done); | ||
}); | ||
} | ||
|
||
transaction(options,promiseCallback) { | ||
if (! promiseCallback) { | ||
promiseCallback = options; | ||
options = {}; | ||
} | ||
options = options || {}; | ||
|
||
let promiseChain = this.Promise.resolve(); | ||
|
||
if (options.autoCommit !== true) { | ||
promiseChain = promiseChain.then(() => this.beginTransaction(options)); | ||
} | ||
|
||
promiseChain = promiseChain.then(() => promiseCallback(this)); | ||
|
||
if (options.autoCommit !== true) { | ||
promiseChain = promiseChain | ||
.then( res => | ||
this.commit() | ||
.then( | ||
() => res | ||
) | ||
) | ||
.catch( err => | ||
this.rollback() | ||
.catch(() => null) | ||
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. This should probably emit an error or even override the previous error, it certainly shouldn't be swallowed. |
||
.then(() => { throw err }) | ||
) | ||
} | ||
return promiseChain; | ||
} | ||
|
||
commit() { | ||
const c = this.connection; | ||
const localErr = new Error(); | ||
|
@@ -330,6 +362,21 @@ class PromisePool extends EventEmitter { | |
}); | ||
} | ||
|
||
transaction(options,promiseCallback) { | ||
return this.getConnection() | ||
.then(con => | ||
con.transaction(options,promiseCallback) | ||
.then(res => { | ||
con.release(); | ||
return res; | ||
}) | ||
.catch(err => { | ||
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. Nit, you probably want to merge the |
||
con.release(); | ||
throw err; | ||
}) | ||
); | ||
} | ||
|
||
execute(sql, values) { | ||
const corePool = this.pool; | ||
const localErr = new Error(); | ||
|
@@ -403,3 +450,4 @@ module.exports.raw = core.raw; | |
module.exports.PromisePool = PromisePool; | ||
module.exports.PromiseConnection = PromiseConnection; | ||
module.exports.PromisePoolConnection = PromisePoolConnection; | ||
module.exports.ISOLATION_LEVEL = core.ISOLATION_LEVEL; |
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.
Inconsistent quotes, apparently lint isn't enforcing this.