Skip to content

Commit

Permalink
fix: check upsert populate ability before adding to population
Browse files Browse the repository at this point in the history
  • Loading branch information
etienne-bechara committed Jan 4, 2022
1 parent cb67c42 commit 034c210
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions source/orm/orm.repository/orm.repository.update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export abstract class OrmUpdateRepository<Entity> extends OrmCreateRepository<En
* @param data
* @param options
*/
// eslint-disable-next-line complexity
public async upsert(
data: EntityData<Entity> | EntityData<Entity>[], options: OrmUpsertOptions<Entity> = { },
): Promise<Entity[]> {
Expand All @@ -176,10 +177,16 @@ export abstract class OrmUpdateRepository<Entity> extends OrmCreateRepository<En
return clause;
});

// Find matching data, ensure to populate array data which most likely are 1:m or m:n relations
// Find matching data, ensure to populate array data that are 1:m or m:n relations
const populate = Array.isArray(options.populate) ? options.populate : [ ];
const sampleData = dataArray[0];
for (const key in sampleData) Array.isArray(sampleData[key]) ? populate.push(key) : undefined;

for (const key in sampleData) {
if (Array.isArray(sampleData[key]) && this.canPopulate(key)) {
populate.push(key);
}
}

const matchingEntities = await this.readBy({ $or: clauses }, { populate });

// Find matching entities for each item on original data
Expand Down

0 comments on commit 034c210

Please sign in to comment.