Skip to content

Commit 4ce7f14

Browse files
authored
perf: ensure computed queries get processed in parallel (#3)
1 parent 9bd67c3 commit 4ce7f14

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

.changeset/chilled-elephants-try.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"mikro-orm-find-dataloader": minor
3+
---
4+
5+
Ensure computed queries get processed in parallel

packages/find/src/EntityDataLoader.ts

+11-18
Original file line numberDiff line numberDiff line change
@@ -95,24 +95,17 @@ export class EntityDataLoader<T extends AnyEntity<T> = any, P extends string = n
9595
>(async (dataloaderFinds) => {
9696
const queriesMap = groupFindQueries(dataloaderFinds);
9797
assertHasNewFilterAndMapKey(dataloaderFinds);
98-
const resultsMap = new Map<string, any[] | Error>();
99-
await Promise.all(
100-
Array.from(queriesMap, async ([key, [filter, options]]): Promise<void> => {
101-
const entityName = key.substring(0, key.indexOf("|"));
102-
let entitiesOrError: any[] | Error;
103-
const findOptions = {
104-
...(options?.populate != null && {
105-
populate: options.populate === true ? ["*"] : Array.from(options.populate),
106-
}),
107-
} satisfies Pick<FindOptions<any, any>, "populate">;
108-
try {
109-
entitiesOrError = await em.getRepository(entityName).find(filter, findOptions);
110-
} catch (e) {
111-
entitiesOrError = e as Error;
112-
}
113-
resultsMap.set(key, entitiesOrError);
114-
}),
115-
);
98+
const promises = Array.from(queriesMap, async ([key, [filter, options]]): Promise<[string, any[]]> => {
99+
const entityName = key.substring(0, key.indexOf("|"));
100+
const findOptions = {
101+
...(options?.populate != null && {
102+
populate: options.populate === true ? ["*"] : Array.from(options.populate),
103+
}),
104+
} satisfies Pick<FindOptions<any, any>, "populate">;
105+
const entities = await em.getRepository(entityName).find(filter, findOptions);
106+
return [key, entities];
107+
});
108+
const resultsMap = new Map(await Promise.all(promises));
116109

117110
return dataloaderFinds.map(({ filtersAndKeys, many }) => {
118111
const res = filtersAndKeys.reduce<any[]>((acc, { key, newFilter }) => {

0 commit comments

Comments
 (0)