Skip to content

Commit

Permalink
fix: filter collections when reassigning results
Browse files Browse the repository at this point in the history
  • Loading branch information
darkbasic committed Dec 5, 2023
1 parent 5e17832 commit 2ac3bc0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-ducks-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mikro-orm-find-dataloader": minor
---

fix: filter collections when reassigning results
10 changes: 5 additions & 5 deletions packages/find/src/findDataloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ export function getFindBatchLoadFn<Entity extends object>(
for (const [key, value] of Object.entries(filter)) {
const entityValue = entity[key as keyof K];
if (Array.isArray(value)) {
// Our current filter is an array
if (Array.isArray(entityValue)) {
// Collection
if (!value.every((el) => entityValue.includes(el))) {
Expand All @@ -500,8 +501,10 @@ export function getFindBatchLoadFn<Entity extends object>(
}
}
} else {
// Object: recursion
if (!filterResult(entityValue as object, value)) {
// Our current filter is an object
if (entityValue instanceof Collection) {
entityValue.find((entity) => filterResult(entity, value));
} else if (!filterResult(entityValue as object, value)) {
return false;
}
}
Expand All @@ -522,9 +525,6 @@ export function optsMapToQueries<Entity extends object>(
populate: options.populate === true ? ["*"] : Array.from(options.populate),
}),
} satisfies Pick<FindOptions<any, any>, "populate">;
console.log("entityName", entityName);
console.log("filter", filter);
console.log("findOptions", findOptions);
const entities = await em.find(entityName, filter, findOptions);
return [key, entities];
});
Expand Down

0 comments on commit 2ac3bc0

Please sign in to comment.