typescript - NestJS-Prisma,如何编写与 prisma 一对多类型匹配的 DTO
问题描述
我是 NestJS 和 Prisma 的新手。我正在尝试为相应的棱镜模型编写一个 API。
这是我的棱镜模型:
model orderable_test {
id Int @id @unique @default(autoincrement())
test_name String
test_id Int
price Int
is_orderable Boolean
is_active Boolean
orderable_bundle orderable_bundle? @relation(fields: [orderable_bundleId], references: [id])
orderable_bundleId Int?
}
model orderable_bundle {
id Int @id @unique @default(autoincrement())
bundle_name String
bundle_id Int
price Int
is_orderable Boolean
is_active Boolean
orderable_tests orderable_test[]
}
对于 orderable_test,我的 DTO 运行良好,orderable_test 的 DTO 是:
class OrderableTestDTO {
@ApiProperty()
test_name: string;
@ApiProperty()
test_id: number;
@ApiProperty()
price: number;
@ApiProperty()
is_orderable: boolean;
@ApiProperty()
is_active: boolean;
@ApiPropertyOptional({default: null})
orderable_bundleId:number|null;
}
对于 orderable_bundle DTO,我有
class OrderableBundleDTO {
@ApiProperty()
bundle_name: string;
@ApiProperty()
bundle_id: number;
@ApiProperty()
price: number;
@ApiProperty()
is_orderable: boolean;
@ApiProperty()
is_active: boolean;
@ApiPropertyOptional({type: () => OrderableTestDTO})
orderable_tests: OrderableTestDTO | null
}
根据 Prisma 官方文档:我需要我的 DTO 像
const createBundle = await prisma.bundle.create({
data: {
bundle_name: 'Bob',
bundle_id: 1
............
orderable_tests: {
create: [
{
id: 'String',
test_name: 'String',
test_id: 1,
price: 0
.....
},
],
},
},
})
但目前,我的 DTO 将如下所示:缺少create:
const createBundle = await prisma.bundle.create({
data: {
bundle_name: 'Bob',
bundle_id: 1
............
orderable_tests:
{
id: 'String',
test_name: 'String',
test_id: 1,
price: 0
.....
},
},
},
})
对于自动生成的 Prisma 类型:它看起来像:
export type orderable_bundleCreateInput = {
bundle_name: string
bundle_id: number
price: number
is_orderable: boolean
is_active: boolean
orderable_tests?: orderable_testCreateNestedManyWithoutOrderable_bundleInput
}
export type orderable_testCreateNestedManyWithoutOrderable_bundleInput = {
create?: XOR<Enumerable<orderable_testCreateWithoutOrderable_bundleInput>, Enumerable<orderable_testUncheckedCreateWithoutOrderable_bundleInput>>
connectOrCreate?: Enumerable<orderable_testCreateOrConnectWithoutOrderable_bundleInput>
createMany?: orderable_testCreateManyOrderable_bundleInputEnvelope
connect?: Enumerable<orderable_testWhereUniqueInput>
}
我真的是类型脚本和棱镜的新手,是否有可能有一个看起来与自动生成的棱镜类型完全一致的 DTO,如果没有,我该如何添加 create: 在 orderable_bundle DTO 下的内部 orderable_test 之前。感谢您查看我的问题!
解决方案
我只是自己想通了。我可以将自动生成的 prisma 类型中的类似格式调整为 DTO。
例如,我试图匹配这样的棱镜类型:
export type orderable_bundleUncheckedCreateInput = {
id?: number
bundle_name: string
bundle_id: number
price: number
is_orderable: boolean
is_active: boolean
order_infoId?: number | null
orderable_tests?: orderable_testUncheckedCreateNestedManyWithoutOrderable_bundleInput
}
export type orderable_testUncheckedCreateNestedManyWithoutOrderable_bundleInput = {
create?: XOR<Enumerable<orderable_testCreateWithoutOrderable_bundleInput>, Enumerable<orderable_testUncheckedCreateWithoutOrderable_bundleInput>>
connectOrCreate?: Enumerable<orderable_testCreateOrConnectWithoutOrderable_bundleInput>
createMany?: orderable_testCreateManyOrderable_bundleInputEnvelope
connect?: Enumerable<orderable_testWhereUniqueInput>
}
export type orderable_testCreateWithoutOrderable_bundleInput = {
test_name: string
test_id: number
price: number
is_orderable: boolean
is_active: boolean
}
.........
这种类型可以让我选择创建或连接到我在创建此数据时设置关系的其他模型。
对于 DTO,我可以写一个来匹配:
import {ApiExtraModels,ApiProperty} from '@nestjs/swagger'
import {CreateOrderInfoDto} from './create-orderInfo.dto'
import {ConnectOrderInfoDto} from './connect-orderInfo.dto'
export class CreateOrderableBundleOrderInfoRelationInputDto {
create?: CreateOrderInfoDto;
connect?: ConnectOrderInfoDto;
}
@ApiExtraModels(CreateOrderInfoDto,ConnectOrderInfoDto,CreateOrderableBundleOrderInfoRelationInputDto)
export class CreateOrderableBundleDto {
@ApiProperty()
bundle_name: string;
@ApiProperty()
bundle_id: number;
@ApiProperty()
price: number;
@ApiProperty()
is_orderable: boolean;
@ApiProperty()
is_active: boolean;
@ApiProperty()
order_info: CreateOrderableBundleOrderInfoRelationInputDto;
}
export class CreateOrderInfoDto {
sample_id: number;
sample_barcode: number;
}
export class ConnectOrderInfoDto {
id?: number;
sample_id?: number;
sample_barcode?: number;
}
推荐阅读
- woocommerce - 如何从另一家 woocommerce 商店获取 woocommerce 库存数量
- swift - Swift 中判断字符串中的字符是否为单词字符的最简单方法是什么?
- php - (errno:150“外键约束不正确”)在Laravel中的多对多关系中
- python - 在 Matplotlib 中单独排序堆积条形图
- angular - 启动项目时如何在Angular中调试随机404错误
- python - 是否可以使用二维数组在图形模式下屏蔽张量像素?
- java - 如何使用 JUnit 测试来测试这个类?
- php - phpmailer 不发布错误或发送电子邮件
- python - 如何在python中找到从任何索引开始并在任何结束索引处结束而不排序的整数列表的最大总和
- ios - Swift 错误:一行上的连续语句必须用 ';' 分隔