1

i need to get user details with post count(Number of posts for today).

const usersWithCount = await prisma.user.findMany({
      select: {
        _count: {
          select: {
            posts: {
             where: {
                       createdAt: moment().format("YYYY-MM-DD"),
                    },
                  },
            recipes: true,
          },
        },
      },
    })

1 Answer 1

1

You cannot filter in relations count, this is not currently supported by prisma. Here is the Feature Request for adding filters in relations count. In your use case you can get filtered relations as described below:

  const usersWithCount = await prisma.user.findMany({
    select: {
      posts: {
        where: {
          createdAt: moment().format("YYYY-MM-DD"),
        },
      },
      recipes: true
    },
  });

In the response of the above query you will get posts array with records satisfying your where condition, you can use the array's length as a count.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.