@@ -18,7 +18,8 @@ import {
1818 MongoClient ,
1919 ObjectId ,
2020 TransactionOptions ,
21- UpdateOptions
21+ UpdateOptions ,
22+ WithId
2223} from 'mongodb' ;
2324
2425import { DEBUG } from '../constants' ;
@@ -140,7 +141,11 @@ export class EntityManager {
140141 return this . getDatabase ( databaseName ) . collection ( this . getCollectionName ( nameOrInstance ) ) ;
141142 }
142143
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+ ) {
144149 const errors = await validate ( obj , {
145150 validationError : { target : true , value : true } ,
146151 whitelist : true ,
@@ -155,12 +160,12 @@ export class EntityManager {
155160 }
156161
157162 async save < Model extends EntityInterface > (
158- entity : Model ,
163+ entity : Model | WithId < Model > ,
159164 options : ( InsertOneOptions | UpdateOptions ) & {
160165 skipValidation ?: boolean ;
161166 validatorOptions ?: ValidatorOptions ;
162167 } = { }
163- ) : Promise < Model > {
168+ ) : Promise < WithId < Model > > {
164169 const entityName = entity . constructor . name ;
165170 const ctx = this . getSessionContext ( ) ;
166171 try {
@@ -174,8 +179,8 @@ export class EntityManager {
174179 if ( Model === undefined ) {
175180 throw new Error ( `Can not find model ${ entityName } ` ) ;
176181 }
177- const proxy = new Model ( ) ;
178- this . merge ( proxy , entity ) ;
182+
183+ const proxy = this . merge ( new Model ( ) , entity ) ;
179184
180185 const operationOptions = {
181186 ...( ctx !== undefined ? { session : ctx . session } : { } ) ,
@@ -217,7 +222,7 @@ export class EntityManager {
217222 this . merge ( entity , proxy ) ;
218223
219224 this . log ( '%s %s saved' , Model . name , entity . _id ?. toHexString ( ) ) ;
220- return entity ;
225+ return entity as WithId < Model > ;
221226 } catch ( e ) {
222227 this . log ( 'error saving %s' , entityName ) ;
223228 throw e ;
@@ -228,29 +233,29 @@ export class EntityManager {
228233 classType : ClassConstructor < Model > ,
229234 query : Filter < Model > ,
230235 options : FindOptions < Model > = { }
231- ) : Promise < FindCursor < Model > > {
236+ ) : Promise < FindCursor < WithId < Model > > > {
232237 this . log ( 'find %s %o' , classType . name , query ) ;
233238 const ctx = this . getSessionContext ( ) ;
234239 const cursor = this . getCollection ( classType ) . find ( query , {
235240 ...( ctx !== undefined ? { session : ctx . session } : { } ) ,
236241 ...options
237242 } ) ;
238- return cursor . map ( ( data ) => this . fromPlain < Model > ( classType , data ) ) ;
243+ return cursor . map ( ( data ) => this . fromPlain ( classType , data ) as WithId < Model > ) ;
239244 }
240245
241246 async findOne < Model extends EntityInterface > (
242247 classType : ClassConstructor < Model > ,
243248 query : Filter < Model > ,
244249 options : FindOptions = { }
245- ) : Promise < Model | undefined > {
250+ ) : Promise < WithId < Model > | undefined > {
246251 this . log ( 'findOne %s %o' , classType . name , query , options ) ;
247252 const ctx = this . getSessionContext ( ) ;
248253 const obj = await this . getCollection ( classType ) . findOne ( query , {
249254 ...( ctx !== undefined ? { session : ctx . session } : { } ) ,
250255 ...options
251256 } ) ;
252257 if ( obj !== null ) {
253- return this . fromPlain < Model > ( classType , obj ) ;
258+ return this . fromPlain < Model > ( classType , obj ) as WithId < Model > ;
254259 }
255260 }
256261
@@ -269,14 +274,17 @@ export class EntityManager {
269274 }
270275
271276 isIdQuery ( query : any ) : boolean {
272- return Object . keys ( query ) . length === 1 && query . _id ;
277+ return Object . keys ( query ) . length === 1 && query . _id instanceof ObjectId ;
273278 }
274279
275280 isIdsQuery ( query : any ) : boolean {
276281 return this . isIdQuery ( query ) && Array . isArray ( query . _id . $in ) ;
277282 }
278283
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+ ) {
280288 const relationshipsCascades = getRelationshipsCascadesMetadata ( classType ) ;
281289
282290 if ( ! Array . isArray ( relationshipsCascades ) ) {
@@ -326,7 +334,7 @@ export class EntityManager {
326334
327335 async deleteMany < Model extends EntityInterface > (
328336 classType : ClassConstructor < Model > ,
329- query : any ,
337+ query : Filter < Model > ,
330338 options : DeleteOptions = { }
331339 ) {
332340 this . log ( 'deleteMany %s %o' , classType . name , query ) ;
@@ -355,7 +363,7 @@ export class EntityManager {
355363 ) {
356364 this . log ( 'watch %o' , pipes ) ;
357365 const ctx = this . getSessionContext ( ) ;
358- return this . getCollection ( classType ) . watch < Model > ( pipes , {
366+ return this . getCollection ( classType ) . watch < WithId < Model > > ( pipes , {
359367 ...( ctx !== undefined ? { session : ctx . session } : { } ) ,
360368 ...options
361369 } ) ;
@@ -365,7 +373,7 @@ export class EntityManager {
365373 obj : any ,
366374 property : string ,
367375 options : FindOptions = { }
368- ) : Promise < R | undefined > {
376+ ) : Promise < WithId < R > | undefined > {
369377 this . log ( 'getRelationship %s on %s' , property , obj ) ;
370378 const relationMetadata = getRelationshipMetadata < R > ( obj . constructor , property , this , obj ) ;
371379 if ( isEmpty ( relationMetadata ) ) {
@@ -385,7 +393,7 @@ export class EntityManager {
385393 const id : ObjectId = obj [ property ] ;
386394 const filter : Filter < R > = { } ;
387395 filter . _id = id ;
388- const relationship = await this . findOne < R > ( relationMetadata . type , filter ) ;
396+ const relationship = await this . findOne < R > ( relationMetadata . type , filter , options ) ;
389397
390398 return relationship ;
391399 }
@@ -394,7 +402,7 @@ export class EntityManager {
394402 obj : any ,
395403 property : string ,
396404 options : FindOptions = { }
397- ) : Promise < R [ ] > {
405+ ) : Promise < Array < WithId < R > > > {
398406 this . log ( 'getRelationships %s on %s' , property , obj ) ;
399407
400408 const relationMetadata = getRelationshipMetadata < R > ( obj . constructor , property , this ) ;
@@ -457,14 +465,14 @@ export class EntityManager {
457465 classType : ClassConstructor < Model > ,
458466 data : object ,
459467 options ?: ClassTransformOptions
460- ) {
468+ ) : Model {
461469 this . log ( 'transform fromPlain %s' , classType . name ) ;
462- return fromPlain ( classType , data , options ) ;
470+ return fromPlain < Model > ( classType , data , options ) ;
463471 }
464472
465- merge < Model extends EntityInterface > ( entity : Model , data : Model , options ?: ClassTransformOptions ) {
473+ merge < Model extends EntityInterface > ( entity : Model , data : Model , excludePrefixes ?: string [ ] ) {
466474 this . log ( '%s transform merge' , getObjectName ( entity ) ) ;
467- return merge ( entity , data , options ) ;
475+ return merge ( entity , data , excludePrefixes ) ;
468476 }
469477
470478 async createIndexs < Model extends EntityInterface > ( model : ClassConstructor < Model > ) {
0 commit comments