Prisma filtering VS pure JS filtering
Greetings!
I have a decently sized dataset (10k+ records) with a couple of
I need to filter this dataset by three dimensions:
- Period -> a simple date range
- TimeRange -> a "clock" range
- Weekdays -> an array of weekdays (e.g. only Fridays and Mondays)
All filters apply to the same field:
I see two approaches:
1. Hybrid approach
- Apply
- Apply
- Simpler query, but more rows fetched -> extra work JS needs to do. 2. Full Prisma approach
- Walk from
- Keep only days that match the selected
- For each valid day, build a Prisma condition combining that day with the
- Result: a big
So I’m wondering: is it better to push everything into Prisma (option 2), or keep the query simple and filter
includes.I need to filter this dataset by three dimensions:
- Period -> a simple date range
{ from: Date, to: Date }- TimeRange -> a "clock" range
{ from: "HH:mm", to: "HH:mm" }- Weekdays -> an array of weekdays (e.g. only Fridays and Mondays)
All filters apply to the same field:
created_at.I see two approaches:
1. Hybrid approach
- Apply
Period directly in Prisma (gte / lte is trivial).- Apply
TimeRange and Weekdays later in JS after fetching the data.- Simpler query, but more rows fetched -> extra work JS needs to do. 2. Full Prisma approach
- Walk from
Period.from to Period.to day by day.- Keep only days that match the selected
Weekdays.- For each valid day, build a Prisma condition combining that day with the
TimeRange.- Result: a big
OR array of ranges like this:So I’m wondering: is it better to push everything into Prisma (option 2), or keep the query simple and filter
TimeRange + Weekdays in JS (option 1)?
Performance-wise, what are the trade-offs at ~10k–20k or more rows with includes? I'm currently using PostgreSQL as my DB.
Update: option 3 -> use raw query with PostgreSQL built-in functions. Great but I loose Prisma type safety.
Thanks!3 Replies
You decided to hold for human wisdom. We'll chime in soon! Meanwhile,
#ask-ai is there if you need a quick second opinion.kapa.ai leans more towards the raw SQL option which I guess fine. Though, I'd still like a human suggestion :)
To achieve best of raw SQL and Prisma, I would recommend using TypedSQL: https://www.prisma.io/docs/orm/prisma-client/using-raw-sql/typedsql
Writing Type-safe SQL with TypedSQL and Prisma Client | Prisma Docu...
Learn how to use TypedSQL to write fully type-safe SQL queries that are compatible with any SQL console and Prisma Client.