Repository navigation
Replies: 2 comments 4 replies
|
Hi @KiriLucas 👋 You can dynamically build the where object based on the presence of filters. For example: const where = {
name: {
in: filters.names,
},
};
if (filters.tag1 || filters.tag2) {
where.tags = {
some: {
AND: [],
},
};
if (filters.tag1) {
where.tags.some.AND.push({
tagTypes: {
name: filters.tag1,
},
value: filters.tag1,
});
}
if (filters.tag2) {
where.tags.some.AND.push({
tagTypes: {
name: filters.tag2,
},
value: filters.tag2,
});
}
}
await this.prisma.modules.findMany({
where: {
AND: [where],
},
});we first define the base where object with the name filter. If either |
4 replies
|
Thanks a lot! Object.assign() instead of push() that's what I needed! |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hey! So I've got this:
I have two records that match the
filters.namesfilter (let's say I've got two records with the name 'Potato'), but sincetag1andtag2might be undefined (they're optional query params), whenever I send anamesbut notag1ortag2, I've got a single result.Debugging my code, I found out that it's because only one of those records has a relation with the
tagstable, so, by doingtags: { some: // ........ }I mess my query up, and instead of fetching 2 records (with name 'Potato') I only fetch that with a relation totagstable... Since that relation is optional, something like this is necessary:tags: (filters.tag1 || filters.tag2) ? { some: // ........ } : undefinedThat way, I only build my query if at least one filter is present, but that would be weird if I had 20+ filters, even if I used a function only for that. So, my question is, on my case, is there any way to make things a little bit cleaner?
// Edit: Forgot to mention that my filters are meant to narrow down results, so I must use an
ANDstatement, asORwould widen up my query results.All reactions