@@ -18,7 +18,7 @@ import {
1818 MongoClient ,
1919 ObjectId ,
2020 TransactionOptions ,
21- UpdateOptions ,
21+ UpdateOptions
2222} from 'mongodb' ;
2323
2424import { DEBUG } from '../constants' ;
@@ -30,7 +30,7 @@ import { CascadeType } from '../relationship/interfaces';
3030import {
3131 getRelationshipMetadata ,
3232 getRelationshipsCascadesMetadata ,
33- setRelationshipsCascadesMetadata ,
33+ setRelationshipsCascadesMetadata
3434} from '../relationship/metadata' ;
3535import { SessionLoaderService } from '../session/service' ;
3636import { fromPlain , merge } from '../transformer/utils' ;
@@ -185,6 +185,7 @@ export class EntityManager {
185185 if ( ! isEmpty ( proxy . _id ) ) {
186186 proxy . updatedAt = new Date ( ) ;
187187 const $unset : any = { } ;
188+ // must be done in a recursive way to allow to delete props from subobject
188189 for ( const p in entity ) {
189190 if ( Object . prototype . hasOwnProperty . call ( proxy , p ) === true ) {
190191 const v : any = proxy [ p ] ;
@@ -215,7 +216,7 @@ export class EntityManager {
215216 // merge the proxy changes back to the entity
216217 this . merge ( entity , proxy ) ;
217218
218- this . log ( '%s %s saved' , Model . name , entity . _id . toHexString ( ) ) ;
219+ this . log ( '%s %s saved' , Model . name , entity . _id ? .toHexString ( ) ) ;
219220 return entity ;
220221 } catch ( e ) {
221222 this . log ( 'error saving %s' , entityName ) ;
@@ -234,10 +235,7 @@ export class EntityManager {
234235 ...( ctx !== undefined ? { session : ctx . session } : { } ) ,
235236 ...options
236237 } ) ;
237- return cursor . map ( ( data ) => {
238- const entity = this . fromPlain < Model > ( classType , data ) ;
239- return entity ;
240- } ) ;
238+ return cursor . map ( ( data ) => this . fromPlain < Model > ( classType , data ) ) ;
241239 }
242240
243241 async findOne < Model extends EntityInterface > (
@@ -251,7 +249,7 @@ export class EntityManager {
251249 ...( ctx !== undefined ? { session : ctx . session } : { } ) ,
252250 ...options
253251 } ) ;
254- if ( obj !== undefined ) {
252+ if ( obj !== null ) {
255253 return this . fromPlain < Model > ( classType , obj ) ;
256254 }
257255 }
@@ -384,8 +382,10 @@ export class EntityManager {
384382 ) ;
385383 }
386384
387- const value = obj [ property ] ;
388- const relationship = await this . findOne < R > ( relationMetadata . type , { _id : value } , options ) ;
385+ const id : ObjectId = obj [ property ] ;
386+ const filter : Filter < R > = { } ;
387+ filter . _id = id ;
388+ const relationship = await this . findOne < R > ( relationMetadata . type , filter ) ;
389389
390390 return relationship ;
391391 }
@@ -394,7 +394,7 @@ export class EntityManager {
394394 obj : any ,
395395 property : string ,
396396 options : FindOptions = { }
397- ) : Promise < Array < R | Error > > {
397+ ) : Promise < R [ ] > {
398398 this . log ( 'getRelationships %s on %s' , property , obj ) ;
399399
400400 const relationMetadata = getRelationshipMetadata < R > ( obj . constructor , property , this ) ;
@@ -415,7 +415,7 @@ export class EntityManager {
415415 }
416416
417417 const value = obj [ property ] ;
418- const relationshipsCursor = await this . find (
418+ const relationshipsCursor = await this . find < R > (
419419 relationMetadata . type ,
420420 {
421421 _id : { $in : value }
0 commit comments