How to solve this in Prisma, count related entries of complex query
Consider a filtering mechanism as suggested in the linked image. We would like to show the results count depending on each "attribute option". See the prisma schema and trpc route here: https://gist.github.com/riccardolardi/7ab7c9a7792ed9428298630c3b30f22c
This approach is evidently very inefficient since it introduces a classic n+1 problem, where we fetch the attributes for the selected category and then loop over each of them and again fetch the count for each attribute option.
I can't seem any other solution other than writing a raw SQL query for this kind of problem. Or am I missing something?
Gist
Prisma count related posts for each available attribute option
Prisma count related posts for each available attribute option - schema.prisma
1 Reply
I have responded to this on Github
https://github.com/prisma/prisma/discussions/25782
GitHub
How to solve this in Prisma, count related entries of complex query...
Question Consider a filtering mechanism as suggested in the linked image. We would like to show the results count depending on each "attribute option". See the prisma schema and trpc rout...