diff --git a/.changeset/cold-actors-know.md b/.changeset/cold-actors-know.md new file mode 100644 index 0000000..d4eea4e --- /dev/null +++ b/.changeset/cold-actors-know.md @@ -0,0 +1,5 @@ +--- +"mikro-orm-find-dataloader": patch +--- + +Make sure we always add populate options to the queryMap diff --git a/packages/find/src/findDataloader.ts b/packages/find/src/findDataloader.ts index e174240..6d0fd22 100644 --- a/packages/find/src/findDataloader.ts +++ b/packages/find/src/findDataloader.ts @@ -248,10 +248,15 @@ function getNewFiltersAndMapKeys( } } +// The purpose of this function on a freshly created query map is just to add populate options +// to the query map. A brand new query map already contains an array with the current element +// as its sole value so there is no need to update it, otherwise we would get the cur element twice. +// TODO: use Sets to avoid duplicates even in subsequent updates. function updateQueryFilter( [acc, accOptions]: [FilterQueryDataloader, { populate?: true | Set }?], cur: FilterQueryDataloader, options?: Pick, "populate">, + newQueryMap?: boolean, ): void { if (options?.populate != null && accOptions != null && accOptions.populate !== true) { if (Array.isArray(options.populate) && options.populate[0] === "*") { @@ -266,13 +271,15 @@ function updateQueryFilter( } } } - for (const [key, value] of Object.entries(acc)) { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const curValue = (cur as Record)[key]!; - if (Array.isArray(value)) { - value.push(...curValue.reduce((acc, cur) => acc.concat(cur), [])); - } else { - updateQueryFilter([value], curValue); + if (newQueryMap !== true) { + for (const [key, value] of Object.entries(acc)) { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const curValue = (cur as Record)[key]!; + if (Array.isArray(value)) { + value.push(...curValue.reduce((acc, cur) => acc.concat(cur), [])); + } else { + updateQueryFilter([value], curValue); + } } } } @@ -299,6 +306,7 @@ export function groupFindQueriesByOpts( let queryMap = queriesMap.get(key); if (queryMap == null) { queryMap = [structuredClone(newFilter), {}]; + updateQueryFilter(queryMap, newFilter, options, true); queriesMap.set(key, queryMap); } else { updateQueryFilter(queryMap, newFilter, options);