@@ -16,35 +16,43 @@ function transactionUtil (pool, fn, cb) {
1616 return ! ! err
1717 }
1818
19- client . query ( 'BEGIN' , ( err ) => {
19+ const commit = ( err , res ) => {
2020 if ( shouldAbort ( err ) ) return cb ( err )
2121
22- fn ( client ) . then ( res => {
23- client . query ( 'COMMIT' , ( err ) => {
24- done ( )
25- if ( err ) {
26- return cb ( err )
27- }
28- return cb ( null , res )
29- } )
30- } ) . catch ( err => {
31- if ( shouldAbort ( err ) ) return cb ( err )
22+ client . query ( 'COMMIT' , ( err ) => {
23+ done ( )
24+ if ( err ) {
25+ return cb ( err )
26+ }
27+ return cb ( null , res )
3228 } )
29+ }
30+
31+ client . query ( 'BEGIN' , ( err ) => {
32+ if ( shouldAbort ( err ) ) return cb ( err )
33+
34+ const promise = fn ( client , commit )
35+
36+ if ( promise && typeof promise . then === 'function' ) {
37+ promise . then (
38+ ( res ) => commit ( null , res ) ,
39+ ( e ) => commit ( e ) )
40+ }
3341 } )
3442 } )
3543}
3644
3745function transact ( fn , cb ) {
38- if ( ! cb ) {
39- return new Promise ( ( resolve , reject ) => {
40- transactionUtil ( this , fn , function ( err , res ) {
41- if ( err ) { return reject ( err ) }
42- return resolve ( res )
43- } )
44- } )
46+ if ( cb && typeof cb === 'function' ) {
47+ return transactionUtil ( this , fn , cb )
4548 }
4649
47- return transactionUtil ( this , fn , cb )
50+ return new Promise ( ( resolve , reject ) => {
51+ transactionUtil ( this , fn , function ( err , res ) {
52+ if ( err ) { return reject ( err ) }
53+ return resolve ( res )
54+ } )
55+ } )
4856}
4957
5058function fastifyPostgres ( fastify , options , next ) {
0 commit comments