【问题标题】:NestJS-Prisma, How to write a DTO that matches the prisma one to many typeNestJS-Prisma,如何编写与 prisma 一对多类型匹配的 DTO
【发布时间】:2021-12-04 01:35:26
【问题描述】:

我是 NestJS 和 Prisma 的新手。我正在尝试为相应的 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,如果没有,我如何在 orderable_bundle DTO 下的内部 orderable_test 之前添加 create:。感谢您查看我的问题!

【问题讨论】:

  • 我认为您在某些代码 sn-ps 中复制粘贴了错误信息。您能看一下吗(您两次发布了架构和创建查询)。你的问题对我来说不是很清楚,你能试着澄清一下吗?为什么不能只使用自动生成的棱镜类型?
  • 感谢您的评论,我刚刚发布了我的 DTO(以前,我错误地将模型发布为 dto)。我的问题是是否可以创建一个 DTO 类,就像那些可以自动转向创建、连接、连接或创建的自动生成类型取决于逻辑。除了那些自动生成的类型之外,我还想使用 DTO,因为在 DTO 中,我可以对其应用更灵活的管道、验证器或守卫。
  • 嘿抱歉回复晚了,我昨天有点忙。我看了你的解决方案。这是一个也可以工作的库:github.com/tpdewolf/prisma-nestjs-dto-generator,如果您不想手动生成 DTO。 (不过,我无法评论这将如何保持下去:/)
  • @TasinIshmam 感谢分享这个包。这似乎很有帮助。一开始我很困惑,因为 NestJS 官网没有说明如何编写一个可以很好地适应关系模型的 DTO,而且我找不到任何关于这部分的文章或文档。大多数 NestJS-Prisma 教程文章只是简单的关系或无关系模型。
  • 乐于助人????

标签: typescript nestjs prisma


【解决方案1】:

我只是自己想出来的。我可以将自动生成的 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;
  }

【讨论】:

    猜你喜欢
    • 2023-01-31
    • 2022-01-06
    • 2021-10-20
    • 2021-10-28
    • 2021-11-08
    • 2021-09-13
    • 2021-10-22
    • 2020-08-18
    • 2021-12-04
    相关资源
    最近更新 更多