PrismaP
Prisma14mo ago
1 reply
riccardolardi

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?
Screenshot_2024-12-03_at_10.00.38.png
Gist
Prisma count related posts for each available attribute option - schema.prisma
Prisma count related posts for each available attribute option
Was this page helpful?