Skip to content

Commit a904df2

Browse files
authoredDec 4, 2023
fix: make sure we always add populate options to the queryMap (#9)
1 parent 98246f0 commit a904df2

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed
 

‎.changeset/cold-actors-know.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"mikro-orm-find-dataloader": patch
3+
---
4+
5+
Make sure we always add populate options to the queryMap

‎packages/find/src/findDataloader.ts

+15-7
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,15 @@ function getNewFiltersAndMapKeys<K extends object>(
248248
}
249249
}
250250

251+
// The purpose of this function on a freshly created query map is just to add populate options
252+
// to the query map. A brand new query map already contains an array with the current element
253+
// as its sole value so there is no need to update it, otherwise we would get the cur element twice.
254+
// TODO: use Sets to avoid duplicates even in subsequent updates.
251255
function updateQueryFilter<K extends object, P extends string = never>(
252256
[acc, accOptions]: [FilterQueryDataloader<K>, { populate?: true | Set<any> }?],
253257
cur: FilterQueryDataloader<K>,
254258
options?: Pick<FindOptions<K, P>, "populate">,
259+
newQueryMap?: boolean,
255260
): void {
256261
if (options?.populate != null && accOptions != null && accOptions.populate !== true) {
257262
if (Array.isArray(options.populate) && options.populate[0] === "*") {
@@ -266,13 +271,15 @@ function updateQueryFilter<K extends object, P extends string = never>(
266271
}
267272
}
268273
}
269-
for (const [key, value] of Object.entries(acc)) {
270-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
271-
const curValue = (cur as Record<string, any[]>)[key]!;
272-
if (Array.isArray(value)) {
273-
value.push(...curValue.reduce<any[]>((acc, cur) => acc.concat(cur), []));
274-
} else {
275-
updateQueryFilter([value], curValue);
274+
if (newQueryMap !== true) {
275+
for (const [key, value] of Object.entries(acc)) {
276+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
277+
const curValue = (cur as Record<string, any[]>)[key]!;
278+
if (Array.isArray(value)) {
279+
value.push(...curValue.reduce<any[]>((acc, cur) => acc.concat(cur), []));
280+
} else {
281+
updateQueryFilter([value], curValue);
282+
}
276283
}
277284
}
278285
}
@@ -299,6 +306,7 @@ export function groupFindQueriesByOpts(
299306
let queryMap = queriesMap.get(key);
300307
if (queryMap == null) {
301308
queryMap = [structuredClone(newFilter), {}];
309+
updateQueryFilter(queryMap, newFilter, options, true);
302310
queriesMap.set(key, queryMap);
303311
} else {
304312
updateQueryFilter(queryMap, newFilter, options);

0 commit comments

Comments
 (0)
Please sign in to comment.