11import { shouldRecurse } from '../../lib' ;
22import { dbSymbols , IDbOptions } from '../db' ;
3+ import { idSymbols } from '../id' ;
34import { modelSymbols } from '../model' ;
45import { debug } from './index' ;
56
@@ -27,71 +28,59 @@ export function toDb(changeSet?: any): object {
2728 debug . normal ( `.toDb called, target '${ ( constructor || { } as any ) . name } '` ) ;
2829
2930 changeSet = changeSet || this ;
30-
31- const dbObj = mapModelToDb . call ( this , changeSet ) ;
32-
33- delete ( dbObj as any ) . id ;
34- if ( ! ( dbObj as any ) . _id && this . _id ) {
35- ( dbObj as any ) . _id = this . _id ;
36- }
37-
38- return dbObj ;
31+ return mapModelToDb . call ( this , changeSet ) ;
3932}
4033
41- function mapModelToDb ( source , depth = 0 ) {
34+ function mapModelToDb ( model ) {
4235
43- const result = { } as any ;
44- if ( ! source ) {
36+ const dbo = { } as any ;
37+ if ( ! model ) {
4538 return ;
4639 }
4740
48- const dbOptionsByPropertyName : Map < string , IDbOptions > = Reflect . getMetadata ( dbSymbols . dbByPropertyName , source ) ;
41+ const dbOptionsByPropertyName : Map < string , IDbOptions > = Reflect . getMetadata ( dbSymbols . dbByPropertyName , model ) ;
4942
5043 // iterate over each property
51- const keys = Object . getOwnPropertyNames ( source ) ;
44+ const keys = Object . getOwnPropertyNames ( model ) ;
5245
5346 for ( const key of keys ) {
5447
55- const map = keyMapper . call ( this , key , source [ key ] , dbOptionsByPropertyName ) || { } as any ;
56- const model = map . model ;
48+ const map = keyMapper . call ( this , key , model [ key ] , dbOptionsByPropertyName ) || { } as any ;
49+ const subModel = map . model ;
5750
5851 if ( ! map . newKey ) {
59- // field is not mapped with @Db , and model is not promiscuous, skip it
52+ // field is not mapped with @Db or @Id , and model is not promiscuous, skip it
6053 continue ;
6154 }
6255
6356 let value ;
64- if ( model || shouldRecurse ( source [ key ] ) ) {
57+ if ( subModel || shouldRecurse ( model [ key ] ) ) {
6558
66- if ( Array . isArray ( source [ key ] ) ) {
67- ++ depth ;
59+ if ( Array . isArray ( model [ key ] ) ) {
6860 const values = [ ] ;
69- for ( const src of source [ key ] ) {
70- values . push ( mapModelToDb . call ( this , src , depth ) ) ;
61+ for ( const src of model [ key ] ) {
62+ values . push ( mapModelToDb . call ( this , src ) ) ;
7163 }
7264 value = values ;
7365
7466 } else if ( map . newKey !== undefined ) {
7567
76- value = mapModelToDb . call ( this , source [ key ] , ++ depth ) ;
68+ value = mapModelToDb . call ( this , model [ key ] ) ;
7769
7870 }
7971
8072 } else if ( map . newKey !== undefined ) {
8173
8274 // if a newKey (db key) has been defined, then set the value to the source [key] so that
8375 // result[map.newKey] will get source[key] - otherwise, result[map.newKey] will be undefined.
84- value = source [ key ] ;
76+ value = model [ key ] ;
8577
8678 }
8779
88- result [ map . newKey ] = value ;
89- }
90- if ( depth > 0 && ( result . _id && result . id ) ) { // resolves #106
91- delete result . id ;
80+ dbo [ map . newKey ] = value ;
9281 }
9382
94- return result ;
83+ return dbo ;
9584}
9685
9786function keyMapper ( key , value , dbMeta ) : { model : any , newKey : string } {
@@ -103,8 +92,13 @@ function keyMapper(key, value, dbMeta): { model: any, newKey: string } {
10392 }
10493
10594 let fieldName : string ;
106- // if there's @Db meta data on the property
107- if ( dbMeta && dbMeta . get ) {
95+ const idProperty = Reflect . getMetadata ( idSymbols . idByPropertyName , this ) ;
96+
97+ if ( idProperty && idProperty === key ) { // if this is an @Id field
98+
99+ fieldName = '_id' ;
100+
101+ } else if ( dbMeta && dbMeta . get ) { // if there's @Db meta data on the property
108102 const dbOptions = ( dbMeta . get ( key ) ) as IDbOptions ;
109103
110104 if ( ( dbOptions || { } ) . field ) {
@@ -118,7 +112,7 @@ function keyMapper(key, value, dbMeta): { model: any, newKey: string } {
118112
119113 // if the model's promiscuous use the property name for the field name if @Db wasn't found...
120114 // otherwise leave the field out of the results
121- if ( ! fieldName && ( ( modelOptions . dbConfig || { } ) . promiscuous || ( this [ modelSymbols . modelOptions ] || { } as any ) . promiscuous ) ) {
115+ if ( ! fieldName && ( ( modelOptions . dbConfig || { } ) . promiscuous || ( modelOptions || { } as any ) . promiscuous ) ) {
122116 fieldName = key ;
123117 }
124118
0 commit comments