Bird
0
0

You want to fetch all users with their posts, but only include posts created after 2023-01-01. How do you write the Prisma query?

hard📝 Application Q8 of 15
NestJS - Database with Prisma
You want to fetch all users with their posts, but only include posts created after 2023-01-01. How do you write the Prisma query?
Aprisma.user.findMany({ where: { posts: { createdAt: { gt: new Date('2023-01-01') } } } })
Bprisma.user.findMany({ include: { posts: true, where: { createdAt: { gt: new Date('2023-01-01') } } } })
Cprisma.user.findMany({ include: { posts: { where: { createdAt: { gt: new Date('2023-01-01') } } } } })
Dprisma.user.findMany({ include: { posts: { createdAt: { gt: new Date('2023-01-01') } } } })
Step-by-Step Solution
Solution:
  1. Step 1: Understand filtering related records in include

    To filter related posts, use include with posts and a where clause inside it.
  2. Step 2: Analyze options

    prisma.user.findMany({ include: { posts: { where: { createdAt: { gt: new Date('2023-01-01') } } } } }) correctly places where inside posts in include to filter posts by createdAt.
  3. Final Answer:

    prisma.user.findMany with include posts filtered by where -> Option C
  4. Quick Check:

    Filter related records inside include with where [OK]
Quick Trick: Filter related records inside include using where clause [OK]
Common Mistakes:
  • Placing where outside include
  • Omitting where clause inside posts
  • Using invalid syntax for filtering

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes