@@ -18,9 +18,12 @@ function newDatabase(connectionString, poolOptions, hooks) {
1818
1919 // Normalize hooks with safe no-ops
2020 hooks = hooks || { } ;
21+ const transactionHooks = hooks . transaction || { } ;
22+ const getTransactionHook = ( name ) =>
23+ ( transactionHooks && transactionHooks [ name ] ) || ( hooks && hooks [ name ] ) ;
2124 const callHook = async ( name , ...args ) => {
2225 try {
23- const fn = hooks && hooks [ name ] ;
26+ const fn = getTransactionHook ( name ) ;
2427 if ( typeof fn === 'function' ) {
2528 return await fn ( ...args ) ;
2629 }
@@ -34,7 +37,6 @@ function newDatabase(connectionString, poolOptions, hooks) {
3437 let c = { poolFactory : pool , hostLocal, express} ;
3538
3639 c . transaction = function ( options , fn ) {
37- console . dir ( 'trancation.........yes' ) ;
3840 if ( ( arguments . length === 1 ) && ( typeof options === 'function' ) ) {
3941 fn = options ;
4042 options = undefined ;
@@ -44,14 +46,13 @@ function newDatabase(connectionString, poolOptions, hooks) {
4446
4547 if ( ! fn )
4648 throw new Error ( 'transaction requires a function' ) ;
47- console . dir ( 'run fn in trans........' ) ;
4849 return domain . run ( runInTransaction ) ;
4950
5051 function begin ( ) {
5152 return Promise . resolve ( )
52- . then ( ( ) => callHook ( 'beforeTransactionBegin ' , c , req ) )
53+ . then ( ( ) => callHook ( 'beforeBegin ' , c , req ) )
5354 . then ( ( ) => _begin ( domain , options ) )
54- . then ( ( res ) => Promise . resolve ( callHook ( 'afterTransactionBegin ' , c , req ) ) . then ( ( ) => res ) ) ;
55+ . then ( ( res ) => Promise . resolve ( callHook ( 'afterBegin ' , c , req ) ) . then ( ( ) => res ) ) ;
5556 }
5657
5758 async function runInTransaction ( ) {
@@ -61,11 +62,11 @@ function newDatabase(connectionString, poolOptions, hooks) {
6162 . then ( begin )
6263 . then ( ( ) => fn ( domain ) )
6364 . then ( ( res ) => result = res )
64- . then ( ( ) => Promise . resolve ( callHook ( 'beforeTransactionCommit ' , c , req ) ) )
65+ . then ( ( ) => Promise . resolve ( callHook ( 'beforeCommit ' , c , req ) ) )
6566 . then ( ( ) => commit ( domain ) )
66- . then ( ( ) => callHook ( 'afterTransactionCommit ' , c , req ) )
67+ . then ( ( ) => callHook ( 'afterCommit ' , c , req ) )
6768 . then ( null , ( e ) => Promise . resolve ( rollback ( domain , e ) )
68- . then ( ( ) => callHook ( 'afterTransactionRollback ' , c , req , e ) )
69+ . then ( ( ) => callHook ( 'afterRollback ' , c , req , e ) )
6970 ) ;
7071 return result ;
7172 }
@@ -85,20 +86,20 @@ function newDatabase(connectionString, poolOptions, hooks) {
8586 }
8687 run . rollback = function ( error ) {
8788 return Promise . resolve ( rollback ( domain , error ) )
88- . then ( ( ) => callHook ( 'afterTransactionRollback ' , c , req , error ) ) ;
89+ . then ( ( ) => callHook ( 'afterRollback ' , c , req , error ) ) ;
8990 } ;
9091 run . commit = function ( ) {
91- return Promise . resolve ( callHook ( 'beforeTransactionCommit ' , c , req ) )
92+ return Promise . resolve ( callHook ( 'beforeCommit ' , c , req ) )
9293 . then ( ( ) => commit ( domain ) )
93- . then ( ( ) => callHook ( 'afterTransactionCommit ' , c , req ) ) ;
94+ . then ( ( ) => callHook ( 'afterCommit ' , c , req ) ) ;
9495 } ;
9596 return run ;
9697
9798 function begin ( ) {
9899 return Promise . resolve ( )
99- . then ( ( ) => callHook ( 'beforeTransactionBegin ' , c , req ) )
100+ . then ( ( ) => callHook ( 'beforeBegin ' , c , req ) )
100101 . then ( ( ) => _begin ( domain , options ) )
101- . then ( ( res ) => Promise . resolve ( callHook ( 'afterTransactionBegin ' , c , req ) ) . then ( ( ) => res ) ) ;
102+ . then ( ( res ) => Promise . resolve ( callHook ( 'afterBegin ' , c , req ) ) . then ( ( ) => res ) ) ;
102103 }
103104 } ;
104105
0 commit comments