Skip to content

Commit

Permalink
fix: populate collections before update diff
Browse files Browse the repository at this point in the history
  • Loading branch information
etienne-bechara committed Apr 23, 2021
1 parent 94e2dc3 commit 41a67e8
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions source/orm/orm.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export abstract class OrmService<Entity> {

/**
* Update target entities based on provided data.
* In canse of multiple, target amount must match data amount.
* In cane of multiple, target amount must match data amount.
* @param entities
* @param data
*/
Expand All @@ -187,7 +187,6 @@ export abstract class OrmService<Entity> {

const entityArray = Array.isArray(entities) ? entities : [ entities ];
const dataArray = Array.isArray(data) ? data : [ data ];
const updatedEntities = entityArray.map((e, i) => this.entityRepository.assign(e, dataArray[i]));

if (entityArray.length !== dataArray.length) {
throw new InternalServerErrorException({
Expand All @@ -197,6 +196,19 @@ export abstract class OrmService<Entity> {
});
}

// Before assignment, ensure collections were populated
const updatedEntities = await Promise.all(
entityArray.map(async (entity, i) => {
for (const key in entity as any) {
if (dataArray[i]?.[key] && entity[key]?.isInitialized && !entity[key].isInitialized()) {
await entity[key].init();
}
}

return this.entityRepository.assign(entity, dataArray[i]);
}),
);

try {
await this.entityRepository.persistAndFlush(updatedEntities);
}
Expand Down

0 comments on commit 41a67e8

Please sign in to comment.