@@ -37,13 +37,34 @@ export abstract class AbstractServiceSchema extends AbstractService implements S
3737 projection ?: ProjectionType < T > | null | undefined ,
3838 options ?: QueryOptions < T > | null | undefined ,
3939 ) : Promise < Query < Array < T > , T , any , T > [ ] > {
40- //TODO: add event emitter
40+ if ( this . eventEmitter ) {
41+ const beforeEvents = await this . eventEmitter ?. emitAsync (
42+ [ this . moduleName . toLowerCase ( ) , this . serviceName . toLowerCase ( ) , 'service' , 'beforeFind' ] . join ( EventEmitterSeparator ) ,
43+ { filter, projection, options } ,
44+ )
45+ for ( const beforeEvent of beforeEvents ) {
46+ if ( beforeEvent ?. stop ) throw beforeEvent ?. stop
47+ if ( beforeEvent ?. filter ) filter = { ...filter , ...beforeEvent . filter }
48+ if ( beforeEvent ?. projection ) projection = { ...( typeof projection === 'object' ? projection : { } ) , ...beforeEvent . projection }
49+ if ( beforeEvent ?. options ) options = { ...options , ...beforeEvent . options }
50+ }
51+ }
4152 this . logger . debug ( [ 'find' , JSON . stringify ( Object . values ( arguments ) ) ] . join ( ' ' ) )
4253 return await this . _model . find < Query < Array < T > , T , any , T > > ( filter , projection , options ) . exec ( )
4354 }
4455
4556 public async count < T extends AbstractSchema | Document > ( filter ?: FilterQuery < T > , options ?: ( mongodb . CountOptions & MongooseBaseQueryOptions < T > ) | null ) : Promise < number > {
46- //TODO: add event emitter
57+ if ( this . eventEmitter ) {
58+ const beforeEvents = await this . eventEmitter ?. emitAsync (
59+ [ this . moduleName . toLowerCase ( ) , this . serviceName . toLowerCase ( ) , 'service' , 'beforeCount' ] . join ( EventEmitterSeparator ) ,
60+ { filter, options } ,
61+ )
62+ for ( const beforeEvent of beforeEvents ) {
63+ if ( beforeEvent ?. stop ) throw beforeEvent ?. stop
64+ if ( beforeEvent ?. filter ) filter = { ...filter , ...beforeEvent . filter }
65+ if ( beforeEvent ?. options ) options = { ...options , ...beforeEvent . options }
66+ }
67+ }
4768 this . logger . debug ( [ 'count' , JSON . stringify ( Object . values ( arguments ) ) ] . join ( ' ' ) )
4869 return await this . _model . countDocuments ( filter , options ) . exec ( )
4970 }
@@ -64,11 +85,11 @@ export abstract class AbstractServiceSchema extends AbstractService implements S
6485 ) : Promise < [ Array < T & Query < T , T , any , T > > , number ] > {
6586 this . logger . debug ( [ 'findAndCount' , JSON . stringify ( Object . values ( arguments ) ) ] . join ( ' ' ) )
6687 if ( this . eventEmitter ) {
88+ console . log ( 'de' , [ this . moduleName . toLowerCase ( ) , this . serviceName . toLowerCase ( ) , 'service' , 'beforeFindAndCount' ] . join ( EventEmitterSeparator ) )
6789 const beforeEvents = await this . eventEmitter ?. emitAsync (
6890 [ this . moduleName . toLowerCase ( ) , this . serviceName . toLowerCase ( ) , 'service' , 'beforeFindAndCount' ] . join ( EventEmitterSeparator ) ,
6991 { filter, projection, options } ,
7092 )
71- // noinspection DuplicatedCode
7293 for ( const beforeEvent of beforeEvents ) {
7394 if ( beforeEvent ?. stop ) throw beforeEvent ?. stop
7495 if ( beforeEvent ?. filter ) filter = { ...filter , ...beforeEvent . filter }
@@ -80,6 +101,7 @@ export abstract class AbstractServiceSchema extends AbstractService implements S
80101 filter = { ...filter , ...softDelete }
81102 let count = await this . _model . countDocuments ( filter ) . exec ( )
82103 let data = await this . _model . find < T & Query < T , T , any , T > > ( filter , projection , options ) . exec ( )
104+
83105 if ( this . eventEmitter ) {
84106 const afterEvents = await this . eventEmitter ?. emitAsync (
85107 [ this . moduleName . toLowerCase ( ) , this . serviceName . toLowerCase ( ) , 'service' , 'afterFindAndCount' ] . join ( EventEmitterSeparator ) ,
@@ -90,6 +112,7 @@ export abstract class AbstractServiceSchema extends AbstractService implements S
90112 if ( afterEvent ?. count ) count += afterEvent . count
91113 }
92114 }
115+
93116 return [ data , count ]
94117 }
95118
@@ -99,6 +122,7 @@ export abstract class AbstractServiceSchema extends AbstractService implements S
99122 options ?: QueryOptions < T > | null | undefined ,
100123 ) : Promise < Query < T , T , any , T > > {
101124 this . logger . debug ( [ 'findById' , JSON . stringify ( Object . values ( arguments ) ) ] . join ( ' ' ) )
125+
102126 if ( this . eventEmitter ) {
103127 const beforeEvents = await this . eventEmitter ?. emitAsync (
104128 [ this . moduleName . toLowerCase ( ) , this . serviceName . toLowerCase ( ) , 'service' , 'beforeFindById' ] . join ( EventEmitterSeparator ) ,
@@ -110,7 +134,9 @@ export abstract class AbstractServiceSchema extends AbstractService implements S
110134 if ( beforeEvent ?. options ) options = { ...options , ...beforeEvent . options }
111135 }
112136 }
137+
113138 let data = await this . _model . findById < Query < T | null , T , any , T > > ( _id , projection , options ) . exec ( )
139+
114140 if ( this . eventEmitter ) {
115141 const afterEvents = await this . eventEmitter ?. emitAsync (
116142 [ this . moduleName . toLowerCase ( ) , this . serviceName . toLowerCase ( ) , 'service' , 'afterFindById' ] . join ( EventEmitterSeparator ) ,
@@ -120,6 +146,7 @@ export abstract class AbstractServiceSchema extends AbstractService implements S
120146 if ( afterEvent ?. data ) data = { ...data , ...afterEvent . data }
121147 }
122148 }
149+
123150 if ( ! data ) {
124151 this . logger . debug ( [ 'findById' , JSON . stringify ( Object . values ( arguments ) ) ] . join ( ' ' ) )
125152 throw new NotFoundException ( )
0 commit comments