是否可以使用 `where` 和 `count` 来 return 指定字段的计数?

is it possible to user `where` and `count` to return the count for a specified field?

我正在尝试使用 Prisma return 布尔字段的计数,它等于 'true'。

为了提供一些背景信息,在前端,我试图计算用户已完成的锻炼的百分比,因此理想情况下,我希望 prisma return 对总锻炼进行计数(我已经成功完成)和 'isCompleted' 等于 true 的 userWorkouts 计数(我无法实现),目前计数是 returning 所有 userWorkouts 而不仅仅是完成的。

这是我当前的 Prisma 查询:

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);
};

使用 Primsa 可以实现吗?或者我应该 return 所有 userWorkouts 和过滤器 isCompleted: true 在前端?

我已经与 Prisma 团队谈过,但目前还无法实现,尽管有一个功能请求正在为它打开。

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

如果您想帮助添加此功能,请将您 +1 添加到功能请求中。