-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(datasource-mongoose): error on a nested field when requesting a c…
…hild property on a missing value (#860)
- Loading branch information
Guillaume Gautreau
authored
Oct 26, 2023
1 parent
f25e4b0
commit 6a04be7
Showing
5 changed files
with
136 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
function addNullValuesOnRecord( | ||
record: Record<string, unknown>, | ||
projection: string[], | ||
): Record<string, unknown> { | ||
if (!record) return record; | ||
|
||
const result = { ...record }; | ||
|
||
for (const field of projection) { | ||
const fieldPrefix = field.split(':')[0]; | ||
|
||
if (result[fieldPrefix] === undefined) { | ||
result[fieldPrefix] = null; | ||
} | ||
} | ||
|
||
const nestedPrefixes = new Set( | ||
projection.filter(field => field.includes(':')).map(field => field.split(':')[0]), | ||
); | ||
|
||
for (const nestedPrefix of nestedPrefixes) { | ||
const childPaths = projection | ||
.filter(field => field.startsWith(`${nestedPrefix}:`)) | ||
.map(field => field.substring(nestedPrefix.length + 1)); | ||
|
||
if (result[nestedPrefix] !== null && result[nestedPrefix] !== undefined) { | ||
if (Array.isArray(result[nestedPrefix])) { | ||
result[nestedPrefix] = (result[nestedPrefix] as Record<string, unknown>[]).map( | ||
childRecord => addNullValuesOnRecord(childRecord, childPaths), | ||
); | ||
} else if (typeof result[nestedPrefix] === 'object') { | ||
result[nestedPrefix] = addNullValuesOnRecord( | ||
result[nestedPrefix] as Record<string, unknown>, | ||
childPaths, | ||
); | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
export default function addNullValues( | ||
records: Record<string, unknown>[], | ||
projection: string[], | ||
): Record<string, unknown>[] { | ||
return records.map(record => addNullValuesOnRecord(record, projection)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
packages/datasource-mongoose/test/utils/add-null-values.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import addNullValues from '../../src/utils/add-null-values'; | ||
|
||
describe('addNullValues', () => { | ||
it('should return a different object with null values added', () => { | ||
const input = { | ||
id: 1, | ||
name: 'John', | ||
}; | ||
const projection = ['id', 'name', 'address']; | ||
|
||
const result = addNullValues([input], projection); | ||
|
||
expect(result).not.toBe(input); | ||
expect(result).toEqual([{ id: 1, name: 'John', address: null }]); | ||
}); | ||
|
||
it('should add null values in nested objects', () => { | ||
const input = { | ||
id: 1, | ||
name: 'John', | ||
address: { | ||
street: 'Main Street', | ||
}, | ||
}; | ||
const projection = ['id', 'name', 'address:street', 'address:city']; | ||
|
||
const result = addNullValues([input], projection); | ||
|
||
expect(result).toEqual([ | ||
{ | ||
id: 1, | ||
name: 'John', | ||
address: { | ||
street: 'Main Street', | ||
city: null, | ||
}, | ||
}, | ||
]); | ||
}); | ||
|
||
it('should add null values in nested arrays', () => { | ||
const input = { | ||
id: 1, | ||
name: 'John', | ||
addresses: [ | ||
{ | ||
street: 'Main Street', | ||
}, | ||
], | ||
}; | ||
const projection = ['id', 'name', 'addresses:street', 'addresses:city']; | ||
|
||
const result = addNullValues([input], projection); | ||
|
||
expect(result).toEqual([ | ||
{ | ||
id: 1, | ||
name: 'John', | ||
addresses: [ | ||
{ | ||
street: 'Main Street', | ||
city: null, | ||
}, | ||
], | ||
}, | ||
]); | ||
}); | ||
}); |