Home > Back-end >  is it possible to user `where` and `count` to return the count for a specified field?
is it possible to user `where` and `count` to return the count for a specified field?

Time:01-23

I am trying to use Prisma to return a count for a boolean field where it equals 'true'.

To give some context, on the frontend I am trying to calculate the workouts that have been completed by a user as a percentage, so ideally I would like prisma to return a count for the total workouts (which I have successfully done) and the count for the userWorkouts where 'isCompleted' equal true (which I am unable to achieve), currently the count is returning all userWorkouts not just the completed ones.

Here is my current Prisma Query:

const response = await prisma.user.findUnique({
  where: {
    id: 1,
  },
  select: {
    id: true,
    programs: {
      select: {
        program: {
          select: {
            name: true,
            blocks: {
              select: {
                id: true,
                name: true,
                week: {
                  select: {
                    id: true,
                    number: true,
                    workouts: {
                      select: {
                        userWorkouts: {
                          where: {
                            isCompleted: true,
                          },
                        },
                        _count: {
                          select: {
                            userWorkouts: true,
                          },
                        },
                      },
                    },
                    _count: {
                      select: {
                        workouts: true,
                      },
                    },
                  },
                },
              },
            },
          },
        },
      },
    },
  },
});
res.json(response);
};

Is this possible to achieve using Primsa? Or should I just return all userWorkouts and filter for isCompleted: true on the frontend?

CodePudding user response:

I have spoken with the Prisma team and this can't be achieved yet, although there is a feature request open for it.

https://github.com/prisma/prisma/issues/8413

If you would like to help get this feature added please add you 1 to the feature request.

  •  Tags:  
  • Related