Skip to content

Commit

Permalink
feat(datamapper): enabling nested entity saving with improved null an…
Browse files Browse the repository at this point in the history
…d undefined handling

collectionFieldsWithValues method modified to work with nested entities, filter null and undefined
extracted to a method.
collectionFields method modified
to not remove entities
  • Loading branch information
vitorgamer58 committed Jul 10, 2023
1 parent d5e0de1 commit d62cb04
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/dataMapper.js
Expand Up @@ -58,15 +58,31 @@ class DataMapper {

collectionFields() {
return this.allFields
.filter((i) => !i.isEntity)
.map((i) => i.nameDb)
}

filterNullAndUndefined(i, instance) {
if (instance[i.name] === null || instance[i.name] === undefined) return null
if (i.isEntity) {
const entity = instance[i.name]
const newObject = Object.keys(entity).reduce((acc, key) => {
if (entity[key] === null || entity[key] === undefined) return acc

acc[key] = entity[key]

return acc
}, {})

return { [i.nameDb]: newObject }
}
return { [i.nameDb]: instance[i.name] }
}

collectionFieldsWithValue(instance) {

let collectionFields = this.allFields
.filter((i) => !i.isEntity)
.map(i => ({ [i.nameDb]: instance[i.name] }))
.map(i => this.filterNullAndUndefined(i, instance))
.filter(Boolean)
.reduce((x, y) => ({ ...x, ...y }))

if (instance.id === undefined) {
Expand Down

0 comments on commit d62cb04

Please sign in to comment.