PrismaP
Prisma5mo ago
3 replies
Toscanah

Prisma filtering VS pure JS filtering

Greetings!

I have a decently sized dataset (10k+ records) with a couple of 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:

where: {
  OR: [
    {
      created_at: {
        gte: new Date("2025-08-28T22:00:00.000Z"),
        lte: new Date("2025-08-28T23:00:00.000Z"),
      },
    },
    {
      created_at: {
        gte: new Date("2025-08-29T22:00:00.000Z"),
        lte: new Date("2025-08-29T23:00:00.000Z"),
      },
    },
    // ... one per matching day
  ],
}


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!
Was this page helpful?