33const fp = require ( 'fastify-plugin' )
44var pg = require ( 'pg' )
55
6- function transactionUtil ( pool , query , values , cb ) {
6+ function transactionUtil ( pool , fn , cb ) {
77 pool . connect ( ( err , client , done ) => {
88 if ( err ) return cb ( err )
99
@@ -18,32 +18,33 @@ function transactionUtil (pool, query, values, cb) {
1818
1919 client . query ( 'BEGIN' , ( err ) => {
2020 if ( shouldAbort ( err ) ) return cb ( err )
21- client . query ( query , values , ( err , res ) => {
22- if ( shouldAbort ( err ) ) return cb ( err )
2321
22+ fn ( client ) . then ( res => {
2423 client . query ( 'COMMIT' , ( err ) => {
2524 done ( )
2625 if ( err ) {
2726 return cb ( err )
2827 }
2928 return cb ( null , res )
3029 } )
30+ } ) . catch ( err => {
31+ if ( shouldAbort ( err ) ) return cb ( err )
3132 } )
3233 } )
3334 } )
3435}
3536
36- function transact ( query , values , cb ) {
37+ function transact ( fn , cb ) {
3738 if ( ! cb ) {
3839 return new Promise ( ( resolve , reject ) => {
39- transactionUtil ( this , query , values , function ( err , res ) {
40+ transactionUtil ( this , fn , function ( err , res ) {
4041 if ( err ) { return reject ( err ) }
4142 return resolve ( res )
4243 } )
4344 } )
4445 }
4546
46- return transactionUtil ( this , query , values , cb )
47+ return transactionUtil ( this , fn , cb )
4748}
4849
4950function fastifyPostgres ( fastify , options , next ) {
0 commit comments