NestJS-Prisma 如何应用计数关系
NestJS-Prisma How to Apply Count Relations
我试图按照有关使用 prisma client 实现计数关系的官方 Prisma 文档进行操作。在官方文档中,它提供了一个例子:
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'.
对于自动生成的 clinicSelect 类型:我有
export type clinicSelect = {
clinic_id?: boolean
clinic_name?: boolean
clinic_address?: boolean
customers?: boolean | customerFindManyArgs
isActive?: boolean
}
我在这里使用的模型类似于文档中使用的一个棱镜,诊所和客户是多对多关系。
除了这个,我还可以使用官方文档中的其他方法,对于这个计数关系,
The ability to count relations is available in version 2.20.0 and
later.
而我的 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)
}
我想通了。这是 prisma 2.30 中的预览功能。要解决这个问题,只需将 prisma 和 prisma/client 版本升级到 3.30 with
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.
Enabling a Prisma Client preview feature To enable a Prisma Client
Preview feature:
Add the Preview feature flag to the generator block:
generator client { provider = "prisma-client-js"
previewFeatures = ["mongoDb"] } Re-generate the client:
npx prisma generate If you are using Visual Studio Code and the
Preview feature is not available in your .ts file after generating the
client, run the TypeScript: Restart TS server command.
Prisma 3.30 发行说明
Select Relation Count is Generally Available Select Relation Count
allows you to count the number of related records by passing _count to
the select or include options and then specifying which relation
counts should be included in the resulting objects via another select.
Select Relation Count helps you query counts on related models, for
example, counting the number of posts per user:
const users = await prisma.user.findMany({ include: {
_count: {
select: { posts: true },
}, }, }) Expand to view the structure of the returned users
If you've been using this Preview feature, you can remove the
selectRelationCount flag from previewFeatures in your Prisma Schema.
我试图按照有关使用 prisma client 实现计数关系的官方 Prisma 文档进行操作。在官方文档中,它提供了一个例子:
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'.
对于自动生成的 clinicSelect 类型:我有
export type clinicSelect = {
clinic_id?: boolean
clinic_name?: boolean
clinic_address?: boolean
customers?: boolean | customerFindManyArgs
isActive?: boolean
}
我在这里使用的模型类似于文档中使用的一个棱镜,诊所和客户是多对多关系。
除了这个,我还可以使用官方文档中的其他方法,对于这个计数关系,
The ability to count relations is available in version 2.20.0 and later.
而我的 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)
}
我想通了。这是 prisma 2.30 中的预览功能。要解决这个问题,只需将 prisma 和 prisma/client 版本升级到 3.30 with
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.
Enabling a Prisma Client preview feature To enable a Prisma Client Preview feature:
Add the Preview feature flag to the generator block:
generator client { provider = "prisma-client-js"
previewFeatures = ["mongoDb"] } Re-generate the client:npx prisma generate If you are using Visual Studio Code and the Preview feature is not available in your .ts file after generating the client, run the TypeScript: Restart TS server command.
Prisma 3.30 发行说明
Select Relation Count is Generally Available Select Relation Count allows you to count the number of related records by passing _count to the select or include options and then specifying which relation counts should be included in the resulting objects via another select.
Select Relation Count helps you query counts on related models, for example, counting the number of posts per user:
const users = await prisma.user.findMany({ include: { _count: { select: { posts: true }, }, }, }) Expand to view the structure of the returned
users
If you've been using this Preview feature, you can remove the selectRelationCount flag from previewFeatures in your Prisma Schema.