@@ -10,30 +10,12 @@ let doQuery = require('../query');
1010let doSqliteFunction = require ( '../sqliteFunction' ) ;
1111let releaseDbClient = require ( '../table/releaseDbClient' ) ;
1212
13- function newDatabase ( connectionString , poolOptions , hooks ) {
13+ function newDatabase ( connectionString , poolOptions ) {
1414 if ( ! connectionString )
1515 throw new Error ( 'Connection string cannot be empty' ) ;
1616 poolOptions = poolOptions || { min : 1 } ;
1717 var pool = newPool ( connectionString , poolOptions ) ;
1818
19- // Normalize hooks with safe no-ops
20- hooks = hooks || { } ;
21- const transactionHooks = hooks . transaction || { } ;
22- const getTransactionHook = ( name ) =>
23- ( transactionHooks && transactionHooks [ name ] ) || ( hooks && hooks [ name ] ) ;
24- const callHook = async ( name , ...args ) => {
25- try {
26- const fn = getTransactionHook ( name ) ;
27- if ( typeof fn === 'function' ) {
28- return await fn ( ...args ) ;
29- }
30- } catch ( e ) {
31- // Hooks should not break core flow; log and continue
32- // Using console.error to avoid introducing new deps
33- console . error ( `[orange-orm] Hook ${ name } failed:` , e ) ;
34- }
35- } ;
36-
3719 let c = { poolFactory : pool , hostLocal, express} ;
3820
3921 c . transaction = function ( options , fn ) {
@@ -42,17 +24,12 @@ function newDatabase(connectionString, poolOptions, hooks) {
4224 options = undefined ;
4325 }
4426 let domain = createDomain ( ) ;
45- const req = domain && domain . req ;
46-
4727 if ( ! fn )
4828 throw new Error ( 'transaction requires a function' ) ;
4929 return domain . run ( runInTransaction ) ;
5030
5131 function begin ( ) {
52- return Promise . resolve ( )
53- . then ( ( ) => callHook ( 'beforeBegin' , c , req ) )
54- . then ( ( ) => _begin ( domain , options ) )
55- . then ( ( res ) => Promise . resolve ( callHook ( 'afterBegin' , c , req ) ) . then ( ( ) => res ) ) ;
32+ return _begin ( domain , options ) ;
5633 }
5734
5835 async function runInTransaction ( ) {
@@ -62,12 +39,8 @@ function newDatabase(connectionString, poolOptions, hooks) {
6239 . then ( begin )
6340 . then ( ( ) => fn ( domain ) )
6441 . then ( ( res ) => result = res )
65- . then ( ( ) => Promise . resolve ( callHook ( 'beforeCommit' , c , req ) ) )
6642 . then ( ( ) => commit ( domain ) )
67- . then ( ( ) => callHook ( 'afterCommit' , c , req ) )
68- . then ( null , ( e ) => Promise . resolve ( rollback ( domain , e ) )
69- . then ( ( ) => callHook ( 'afterRollback' , c , req , e ) )
70- ) ;
43+ . then ( null , ( e ) => Promise . resolve ( rollback ( domain , e ) ) ) ;
7144 return result ;
7245 }
7346
@@ -79,27 +52,19 @@ function newDatabase(connectionString, poolOptions, hooks) {
7952 let domain = createDomain ( ) ;
8053 let transaction = newTransaction ( domain , pool ) ;
8154 let p = domain . run ( ( ) => new Promise ( transaction ) . then ( begin ) ) ;
82- const req = domain && domain . req ;
83-
8455 function run ( fn ) {
8556 return p . then ( ( ) => fn ( domain ) ) ;
8657 }
8758 run . rollback = function ( error ) {
88- return Promise . resolve ( rollback ( domain , error ) )
89- . then ( ( ) => callHook ( 'afterRollback' , c , req , error ) ) ;
59+ return Promise . resolve ( rollback ( domain , error ) ) ;
9060 } ;
9161 run . commit = function ( ) {
92- return Promise . resolve ( callHook ( 'beforeCommit' , c , req ) )
93- . then ( ( ) => commit ( domain ) )
94- . then ( ( ) => callHook ( 'afterCommit' , c , req ) ) ;
62+ return Promise . resolve ( commit ( domain ) ) ;
9563 } ;
9664 return run ;
9765
9866 function begin ( ) {
99- return Promise . resolve ( )
100- . then ( ( ) => callHook ( 'beforeBegin' , c , req ) )
101- . then ( ( ) => _begin ( domain , options ) )
102- . then ( ( res ) => Promise . resolve ( callHook ( 'afterBegin' , c , req ) ) . then ( ( ) => res ) ) ;
67+ return _begin ( domain , options ) ;
10368 }
10469 } ;
10570
0 commit comments