Skip to content

Commit

Permalink
fix: Use Promise.all in toJSON iteration. (#1077)
Browse files Browse the repository at this point in the history
* fix: Use Promise.all in toJSON iteration.

* Only normalize the hint once.
  • Loading branch information
stephenh committed May 7, 2024
1 parent 2328b6e commit 9deb08d
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions packages/orm/src/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,21 @@ export async function toJSON<T extends Entity, const H extends JsonHint<T>>(
const entity = Array.isArray(entityOrList) ? entityOrList[0] : entityOrList;
const loadHint = convertToLoadHint(getMetadata(entity), hint as any, true);
await entity.em.populate(entityOrList, loadHint);
const normHint = normalizeHint(hint as any);

if (Array.isArray(entityOrList)) {
const list = [];
for (const entity of entityOrList) {
const json = {};
await copyToPayload(json, entity, normalizeHint(hint as any));
list.push(json);
}
return list as any;
const list = [] as any;
await Promise.all(
entityOrList.map((entity) => {
const json = {};
list.push(json);
return copyToPayload(json, entity, normHint);
}),
);
return list;
} else {
const json = {};
await copyToPayload(json, entity, normalizeHint(hint as any));
await copyToPayload(json, entity, normHint);
return json as any;
}
}
Expand Down

0 comments on commit 9deb08d

Please sign in to comment.