【问题标题】:NestJS-Prisma How to Apply Count RelationsNestJS-Prisma 如何应用计数关系
【发布时间】:2021-12-13 05:18:00
【问题描述】:

我试图按照官方 prisma 文档来实现与 prisma client 的计数关系。在官方文档中,提供了一个例子:

const usersWithCount = await prisma.user.findMany({
  select: {
    _count: {
      select: { posts: true },
    },
  },
})

按照这个文档,我在我的 NestJS 项目中创建了以下代码:

@ApiDefaultResponse({status: 200, description:'Successfully returned customer count'})
@ApiBadRequestResponse({status:400, description: "Bad Request"})
@Get('Count')
async getCustomersCount(){
    return this.prismaService.clinic.findMany({
        select: {
            _count: {
                select: {customers: true}
            }
        }
    })

}

但是,这给了我一个错误

Type '{ _count: { select: { customers: true; }; }; }' is not assignable to type 'clinicSelect'.
  Object literal may only specify known properties, and '_count' does not exist in type 'clinicSelect'.

对于自动生成的诊所选择类型:我有

  export type clinicSelect = {
    clinic_id?: boolean
    clinic_name?: boolean
    clinic_address?: boolean
    customers?: boolean | customerFindManyArgs
    isActive?: boolean
  }

我这里使用的模型类似于文档中使用的一个棱镜,诊所和客户是多对多的关系。

除了这个,我可以应用官方文档中显示的其他方法,对于这个计数关系,

计算关系的功能在 2.20.0 版和 稍后。

而我的 prisma 版本是 2.30。

prisma -v
Environment variables loaded from .env
prisma                : 2.30.0
@prisma/client        : 2.30.0
Current platform      : windows

所以我的问题是,为什么我不能在我的 NestJS 代码中应用这种计数关系?这只是 prisma 客户端的问题还是应该以其他方式编写?感谢您的帮助!

这是我的方案:

model clinic {
  clinic_id      Int        @id @unique
  clinic_name    String
  clinic_address String
  /// @DtoRelationRequired
  /// @DtoRelationCanConnectOnCreate
  /// @DtoRelationCanConnectOnUpdate
  customers      customer[]
  isActive       Boolean    @default(true)
}

model customer {
  customer_id         Int          @id @unique
  customer_first_name String?      @db.VarChar(50)
  customer_last_name  String?      @db.VarChar(50)
  customer_address    String?      @db.VarChar(250)
  /// @DtoRelationRequired
  /// @DtoRelationCanConnectOnCreate
  /// @DtoRelationCanConnectOnUpdate
  clinics             clinic[]
  orders              order_info[]
  patient_info        patient[]
  payment_method      String?
  phone               String?
  email               String?
  isActive Boolean @default(true)
}

【问题讨论】:

  • 如果有帮助,您生成的类型 ClinicSelect 应该看起来像 export type ClinicSelect = { Clinic_id?: boolean Clinic_name?: boolean Clinic_address?: boolean customers?: boolean | customerFindManyArgs isActive?: boolean _count?: boolean | ClinicCountOutputTypeArgs }。你能分享你的棱镜架构吗?
  • @RomainTAILLANDIER 您好 Romain,感谢您的帮助。我刚刚更新了我的架构。我理解你的评论,根据modelSelect里面的官方文档,应该有一个_count,在那个model_count里面,里面应该有一个select。但是我在自动生成的类型中找不到那种关系。或许我自己可以写一篇。但是这些类型是由 Prisma 自动生成的,每次我生成 prisma 时都会刷新。所以我想知道是否有一种方法可以像官方文档中那样执行计数关系。

标签: nestjs prisma


【解决方案1】:

我自己想通了。这是 prisma 2.30 中的预览功能。要解决这个问题,只需将 prisma 和 prisma/client 版本升级到 3.30 即可

npm install prisma@3 @prisma/client@3 

即使从 prisma 2.21 开始添加计数关系。但是从那里的发行说明开始,此功能通常在 prisma 3.01 中可用。因此,在 2.30 中,此功能可能不可用。要在 prisma 2.30 中使用它,the preview feature needs to be enabled

启用 Prisma Client 预览功能启用 Prisma Client 预览功能:

将预览功能标志添加到生成器块:

生成器客户端 { 提供者 = "prisma-client-js"
previewFeatures = ["mongoDb"] } 重新生成客户端:

npx prisma generate 如果您使用的是 Visual Studio Code 和 生成 .ts 文件后,预览功能不可用 客户端,运行 TypeScript: Restart TS server 命令。

Prisma 3.30 发行说明

选择关系计数已普遍可用 选择关系计数 允许您通过传递 _count 来计算相关记录的数量 选择或包含选项,然后指定哪个关系 计数应通过另一个选择包含在结果对象中。

选择关系计数可帮助您查询相关模型的计数,例如 例如,计算每个用户的帖子数:

const users = await prisma.user.findMany({ 包括:{ _数数: { 选择:{帖子:真}, }, }, }) 展开查看返回的users的结构如果你一直在使用这个预览功能,你可以去掉 从您的 Prisma Schema 中的 previewFeatures 中选择RelationCount 标志。

请参阅升级参考here。 请参阅 Prisma 发行说明here

【讨论】:

    猜你喜欢
    • 2022-01-04
    • 2022-01-06
    • 2023-02-06
    • 1970-01-01
    • 2021-12-09
    • 2019-08-05
    • 2023-01-31
    • 2022-11-01
    • 1970-01-01
    相关资源
    最近更新 更多