Jeferson Costa
Jeferson Costa

Reputation: 31

Filter a query in Prisma with results

I am trying to find a way that with PRISMA I could filter a query with the query results... Something like this:

prisma.card.findMany({
    include: {
      user: true,
      project: true,
      transaction: {
        where: {
          date_created: {
            lte: getInterval(card.interval),
            gte: getInterval(card.interval),
          },
        },
      },
      snapshots: true,
    },
})

Basically I am querying cards and including transactions, then I have a function to get an interval, but I will need the prop from the card (interval) to be used in the function.

The way bellow is not working, just an example.

Do you know if is there any other way? I am using Mongodb.

Upvotes: 0

Views: 1481

Answers (1)

Shea Hunter Belsky
Shea Hunter Belsky

Reputation: 3218

Replace include with select to be able to apply filters to a relation when you are searching for something in Prisma.

Docs: https://www.prisma.io/docs/concepts/components/prisma-client/relation-queries#filter-a-list-of-relations

Your query will look something like this:

prisma.card.findMany({
    // include => select, make sure to select any other fields you need here too
    select: {
      user: true,
      project: true,
      transaction: {
        where: {
          date_created: {
            lte: getInterval(card.interval),
            gte: getInterval(card.interval),
          },
        },
      },
      snapshots: true,
    },
})

Your TypeScript types should also be able to guide you on what types are available on the transaction property and what's not. The emitted Prisma Client is REALLY helpful in that regard!

Upvotes: 1

Related Questions