簡體   English   中英

NestJS-Prisma,如何編寫與prisma一對多類型匹配的DTO

[英]NestJS-Prisma, How to write a DTO that matches the prisma one to many type

我是 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 之前。 感謝您查看我的問題!

我只是自己想出來的。 我可以將自動生成的棱鏡類型的類似格式調整為 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;
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM