Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

Commit d9f27b5

Browse files
Robb Sadleretsuo
authored andcommitted
work in progress
1 parent e44ef9f commit d9f27b5

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/core/@model/model-operators/from-db.spec.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe('@Model.fromDb', () => {
5656
expect(result.val).toBe(777);
5757
});
5858

59-
it('returns an object of the correct instaceOf', () => {
59+
it('returns an object of the correct instanceOf', () => {
6060
const result = Test.fromDb({});
6161
expect(result instanceof Test).toBe(true);
6262
});
@@ -252,6 +252,9 @@ describe('@Model.fromDb', () => {
252252

253253
@Db({field: 'addr', model: Address})
254254
address = new Address();
255+
256+
@Db()
257+
tag: string;
255258
}
256259

257260
@Model()
@@ -468,7 +471,7 @@ describe('@Model.fromDb', () => {
468471
expect(result.leads.length).toBe(0);
469472
});
470473

471-
it('maps an array of sub documents from the DB to a resulting array of type model', () => {
474+
fit('maps an array of sub documents from the DB to a resulting array of type model', () => {
472475

473476
const leadSubDocs = [
474477
{
@@ -479,7 +482,8 @@ describe('@Model.fromDb', () => {
479482
st: '123',
480483
z: '90277'
481484
},
482-
n: '1'
485+
n: '1',
486+
tag: 'abc'
483487
},
484488
{
485489
addr: {
@@ -514,6 +518,9 @@ describe('@Model.fromDb', () => {
514518
expect((result.leads[1].address || {} as any).street).toBe(leadSubDocs[1].addr.st);
515519
expect((result.leads[1].address || {} as any).zip).toBe(leadSubDocs[1].addr.z);
516520

521+
expect(result.leads[0].tag).toBe(leadSubDocs[0].tag);
522+
expect(result.leads[1].tag).toBeUndefined('Undefined sub-document field should not be defined');
523+
517524
});
518525
});
519526

src/core/@model/model-operators/from-db.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export function fromDb(json: any, options?: IFromDbOptions): object {
5454
return source;
5555
}
5656

57+
console.log('=================='.zebra.green, JSON.stringify(target, null, 2).green);
5758
const dbOptionsByFieldName: Map<string, IDbOptions>
5859
= Reflect.getMetadata(dbSymbols.dbByFieldName, target) || new Map<string, IDbOptions>();
5960

@@ -63,6 +64,7 @@ export function fromDb(json: any, options?: IFromDbOptions): object {
6364

6465
// convert the DB key name to the Model key name
6566
const mapper = map(key, source[key], dbOptionsByFieldName);
67+
if (key === 'tag') console.log('================== mapper'.cyan, JSON.stringify(mapper, null, 2).cyan);
6668
const model = mapper.model;
6769

6870
let nextTarget;
@@ -73,7 +75,9 @@ export function fromDb(json: any, options?: IFromDbOptions): object {
7375
} catch (err) {
7476
throw new Error(`Model '${modelName}' has a property '${key}' that defines its model with a value that`
7577
+ ` cannot be constructed`);
76-
}
78+
}
79+
80+
console.log('================== 111111'.zebra.green, JSON.stringify(target, null, 2).green);
7781

7882
if (model || shouldRecurse(source[key])) {
7983

@@ -87,12 +91,13 @@ export function fromDb(json: any, options?: IFromDbOptions): object {
8791
const values = [];
8892

8993
for (const src of source[key]) {
94+
console.log('==================src'.cyan, src);
95+
nextTarget = (model);
9096
values.push(Object.assign(new model(), mapDbToModel(src, nextTarget, map)));
9197
}
9298
value = values;
9399

94100
} else {
95-
96101
value = mapDbToModel(source[key], nextTarget, map, ++depth);
97102

98103
if (model) {
@@ -103,20 +108,23 @@ export function fromDb(json: any, options?: IFromDbOptions): object {
103108
}
104109
}
105110
}
106-
111+
console.log('================== >>>>>>>'.cyan, mapper.newKey);
107112
target[mapper.newKey] = value;
108113
}
109114

110115
} else {
111116
// otherwise, map a property that has a primitive value or an ObjectID value
112117
if (mapper.newKey !== undefined) {
118+
console.log('================== %%%%%%'.cyan, `assigning ${source[key]} to ${mapper.newKey}`);
113119
target[mapper.newKey] = (source[key] !== undefined && source[key] !== null)
114120
? source[key]
115121
: nextTarget; // resolves issue #94
116122
}
117123
}
124+
console.log('================== 222222'.zebra.green, JSON.stringify(target, null, 2).green);
118125
}
119126

127+
if (!!target.order) console.log('=================='.zebra.red, JSON.stringify(target, null, 2).red);
120128
return target;
121129
}
122130

0 commit comments

Comments
 (0)