Brakebein / prisma-generator-nestjs-dto

Generates NestJS DTO classes from Prisma Schema
Apache License 2.0
49 stars 26 forks source link

Support for CompoundUniqueInput #8

Closed ChristopherCapito closed 1 year ago

ChristopherCapito commented 1 year ago

With Prisma, for example if using postgres, you can use a compound of unique fields.

This schema:

model Bike {
  id String @id @default(uuid())

  chassis_number String

  model String

  vendor String

  year_from Int

  year_to Int

  products Product[]

  @@unique([vendor, chassis_number])
}

produces the following connect-dto:

export class ConnectBikeDto {
  id: string;
}

wheras it should be this, as it is in Prisma:

export class ConnectBikeDto {
  id: string;
  vendor_chassis_number?: {
    vendor: string;
    chassis_number: string;
  };
}

For now I am editing the DTO after the fact, although using prisma generate obviously overrides the field.

Brakebein commented 1 year ago

In my project, I don't really use the connect DTOs. But yes, it would be possible. However, id: string; should also be optional id?: string;, because you could either connect by id or vendor_chassis_number.

I also just realize that it doesn't generate any ApiProperty or class validation decorators yet. Hence, for vendor_chassis_number an additional class would be needed. So the output would look something like this:

export class VendorChassisNumber {
  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  vendor: string;
  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  chassis_number: string;
}

export class ConnectBikeDto {
  @ApiProperty({
    required: false,
    nullable: true,
  })
  @IsOptional()
  @IsString()
  id?: string;
  @ApiProperty({
    required: false,
    nullable: true,
    type: VendorChassisNumber,
  })
  @IsOptional()
  @ValidateNested()
  @Type(() => VendorChassisNumber)
  vendor_chassis_number?: VendorChassisNumber;
}

So, it seems a bit more complex at second glance.

ChristopherCapito commented 1 year ago

Well. For now I just keep in mind to always edit the connect DTO to fit my purpose. It would be a great help to have the generator do this, even if it does not include the API documentation right away. I really need to get more into your generator to understand how its working. Maybe I can implement it myself. :D

Brakebein commented 1 year ago

I finally found some time to implement it. Can you please test it? https://www.npmjs.com/package/@brakebein/prisma-generator-nestjs-dto/v/1.17.0-beta0