PrismaP
Prisma12mo ago
4 replies
Salmon Bhai

How do I get the count in nested relations.

Here I am trying to retrieve comment count for every post. How would I go about it?

  const posts = await prisma.posts.findMany({
    where: {
      userId,
    },
    orderBy: [
      { startDate: 'asc' },
    ],
    select: {
      id: true,
      title: true,
      image: true,
      publish: true,
      comments: {
        _count: {
            id: true
          }
      }
    }
  });
Was this page helpful?