首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >NestJS- prisma,如何编写与prisma一对多类型匹配的DTO

NestJS- prisma,如何编写与prisma一对多类型匹配的DTO
EN

Stack Overflow用户
提问于 2021-10-15 23:48:36
回答 1查看 873关注 0票数 1

我是NestJS和Prisma的新手。我正在尝试为相应的prisma模型编写一个API。

这是我的prisma模型:

代码语言:javascript
运行
复制
    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是:

代码语言:javascript
运行
复制
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,我有

代码语言:javascript
运行
复制
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是这样的

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
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类型:它看起来像:

代码语言:javascript
运行
复制
  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>
  }

我真的很喜欢脚本和prisma类型,有没有可能有一个DTO看起来完全像自动生成的prisma类型,如果不是,我如何在orderable_bundle DTO下的内部orderable_test之前添加create:。感谢您查看我的问题!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-19 01:26:44

我自己才弄明白的。我可以将类似的格式从自动生成的prisma类型改编为DTO。

例如,我尝试像这样匹配prisma类型:

代码语言:javascript
运行
复制
  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,我可以编写以下代码来匹配:

代码语言:javascript
运行
复制
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;
  }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69591631

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档