@@ -18,7 +18,8 @@ import {
18
18
MongoClient ,
19
19
ObjectId ,
20
20
TransactionOptions ,
21
- UpdateOptions
21
+ UpdateOptions ,
22
+ WithId
22
23
} from 'mongodb' ;
23
24
24
25
import { DEBUG } from '../constants' ;
@@ -140,7 +141,11 @@ export class EntityManager {
140
141
return this . getDatabase ( databaseName ) . collection ( this . getCollectionName ( nameOrInstance ) ) ;
141
142
}
142
143
143
- async validate ( obj : any , validatorOptions : ValidatorOptions = { } , throwError : boolean = false ) {
144
+ async validate < Model extends EntityInterface = any > (
145
+ obj : Model ,
146
+ validatorOptions : ValidatorOptions = { } ,
147
+ throwError : boolean = false
148
+ ) {
144
149
const errors = await validate ( obj , {
145
150
validationError : { target : true , value : true } ,
146
151
whitelist : true ,
@@ -155,12 +160,12 @@ export class EntityManager {
155
160
}
156
161
157
162
async save < Model extends EntityInterface > (
158
- entity : Model ,
163
+ entity : Model | WithId < Model > ,
159
164
options : ( InsertOneOptions | UpdateOptions ) & {
160
165
skipValidation ?: boolean ;
161
166
validatorOptions ?: ValidatorOptions ;
162
167
} = { }
163
- ) : Promise < Model > {
168
+ ) : Promise < WithId < Model > > {
164
169
const entityName = entity . constructor . name ;
165
170
const ctx = this . getSessionContext ( ) ;
166
171
try {
@@ -174,8 +179,8 @@ export class EntityManager {
174
179
if ( Model === undefined ) {
175
180
throw new Error ( `Can not find model ${ entityName } ` ) ;
176
181
}
177
- const proxy = new Model ( ) ;
178
- this . merge ( proxy , entity ) ;
182
+
183
+ const proxy = this . merge ( new Model ( ) , entity ) ;
179
184
180
185
const operationOptions = {
181
186
...( ctx !== undefined ? { session : ctx . session } : { } ) ,
@@ -217,7 +222,7 @@ export class EntityManager {
217
222
this . merge ( entity , proxy ) ;
218
223
219
224
this . log ( '%s %s saved' , Model . name , entity . _id ?. toHexString ( ) ) ;
220
- return entity ;
225
+ return entity as WithId < Model > ;
221
226
} catch ( e ) {
222
227
this . log ( 'error saving %s' , entityName ) ;
223
228
throw e ;
@@ -228,29 +233,29 @@ export class EntityManager {
228
233
classType : ClassConstructor < Model > ,
229
234
query : Filter < Model > ,
230
235
options : FindOptions < Model > = { }
231
- ) : Promise < FindCursor < Model > > {
236
+ ) : Promise < FindCursor < WithId < Model > > > {
232
237
this . log ( 'find %s %o' , classType . name , query ) ;
233
238
const ctx = this . getSessionContext ( ) ;
234
239
const cursor = this . getCollection ( classType ) . find ( query , {
235
240
...( ctx !== undefined ? { session : ctx . session } : { } ) ,
236
241
...options
237
242
} ) ;
238
- return cursor . map ( ( data ) => this . fromPlain < Model > ( classType , data ) ) ;
243
+ return cursor . map ( ( data ) => this . fromPlain ( classType , data ) as WithId < Model > ) ;
239
244
}
240
245
241
246
async findOne < Model extends EntityInterface > (
242
247
classType : ClassConstructor < Model > ,
243
248
query : Filter < Model > ,
244
249
options : FindOptions = { }
245
- ) : Promise < Model | undefined > {
250
+ ) : Promise < WithId < Model > | undefined > {
246
251
this . log ( 'findOne %s %o' , classType . name , query , options ) ;
247
252
const ctx = this . getSessionContext ( ) ;
248
253
const obj = await this . getCollection ( classType ) . findOne ( query , {
249
254
...( ctx !== undefined ? { session : ctx . session } : { } ) ,
250
255
...options
251
256
} ) ;
252
257
if ( obj !== null ) {
253
- return this . fromPlain < Model > ( classType , obj ) ;
258
+ return this . fromPlain < Model > ( classType , obj ) as WithId < Model > ;
254
259
}
255
260
}
256
261
@@ -269,14 +274,17 @@ export class EntityManager {
269
274
}
270
275
271
276
isIdQuery ( query : any ) : boolean {
272
- return Object . keys ( query ) . length === 1 && query . _id ;
277
+ return Object . keys ( query ) . length === 1 && query . _id instanceof ObjectId ;
273
278
}
274
279
275
280
isIdsQuery ( query : any ) : boolean {
276
281
return this . isIdQuery ( query ) && Array . isArray ( query . _id . $in ) ;
277
282
}
278
283
279
- protected async deleteCascade < Model extends EntityInterface > ( classType : ClassConstructor < Model > , entity : Model ) {
284
+ protected async deleteCascade < Model extends EntityInterface > (
285
+ classType : ClassConstructor < Model > ,
286
+ entity : WithId < Model >
287
+ ) {
280
288
const relationshipsCascades = getRelationshipsCascadesMetadata ( classType ) ;
281
289
282
290
if ( ! Array . isArray ( relationshipsCascades ) ) {
@@ -326,7 +334,7 @@ export class EntityManager {
326
334
327
335
async deleteMany < Model extends EntityInterface > (
328
336
classType : ClassConstructor < Model > ,
329
- query : any ,
337
+ query : Filter < Model > ,
330
338
options : DeleteOptions = { }
331
339
) {
332
340
this . log ( 'deleteMany %s %o' , classType . name , query ) ;
@@ -355,7 +363,7 @@ export class EntityManager {
355
363
) {
356
364
this . log ( 'watch %o' , pipes ) ;
357
365
const ctx = this . getSessionContext ( ) ;
358
- return this . getCollection ( classType ) . watch < Model > ( pipes , {
366
+ return this . getCollection ( classType ) . watch < WithId < Model > > ( pipes , {
359
367
...( ctx !== undefined ? { session : ctx . session } : { } ) ,
360
368
...options
361
369
} ) ;
@@ -365,7 +373,7 @@ export class EntityManager {
365
373
obj : any ,
366
374
property : string ,
367
375
options : FindOptions = { }
368
- ) : Promise < R | undefined > {
376
+ ) : Promise < WithId < R > | undefined > {
369
377
this . log ( 'getRelationship %s on %s' , property , obj ) ;
370
378
const relationMetadata = getRelationshipMetadata < R > ( obj . constructor , property , this , obj ) ;
371
379
if ( isEmpty ( relationMetadata ) ) {
@@ -385,7 +393,7 @@ export class EntityManager {
385
393
const id : ObjectId = obj [ property ] ;
386
394
const filter : Filter < R > = { } ;
387
395
filter . _id = id ;
388
- const relationship = await this . findOne < R > ( relationMetadata . type , filter ) ;
396
+ const relationship = await this . findOne < R > ( relationMetadata . type , filter , options ) ;
389
397
390
398
return relationship ;
391
399
}
@@ -394,7 +402,7 @@ export class EntityManager {
394
402
obj : any ,
395
403
property : string ,
396
404
options : FindOptions = { }
397
- ) : Promise < R [ ] > {
405
+ ) : Promise < Array < WithId < R > > > {
398
406
this . log ( 'getRelationships %s on %s' , property , obj ) ;
399
407
400
408
const relationMetadata = getRelationshipMetadata < R > ( obj . constructor , property , this ) ;
@@ -457,14 +465,14 @@ export class EntityManager {
457
465
classType : ClassConstructor < Model > ,
458
466
data : object ,
459
467
options ?: ClassTransformOptions
460
- ) {
468
+ ) : Model {
461
469
this . log ( 'transform fromPlain %s' , classType . name ) ;
462
- return fromPlain ( classType , data , options ) ;
470
+ return fromPlain < Model > ( classType , data , options ) ;
463
471
}
464
472
465
- merge < Model extends EntityInterface > ( entity : Model , data : Model , options ?: ClassTransformOptions ) {
473
+ merge < Model extends EntityInterface > ( entity : Model , data : Model , excludePrefixes ?: string [ ] ) {
466
474
this . log ( '%s transform merge' , getObjectName ( entity ) ) ;
467
- return merge ( entity , data , options ) ;
475
+ return merge ( entity , data , excludePrefixes ) ;
468
476
}
469
477
470
478
async createIndexs < Model extends EntityInterface > ( model : ClassConstructor < Model > ) {
0 commit comments