Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for CompoundUniqueInput #8

Closed
ChristopherCapito opened this issue Nov 20, 2022 · 3 comments
Closed

Support for CompoundUniqueInput #8

ChristopherCapito opened this issue Nov 20, 2022 · 3 comments

Comments

@ChristopherCapito
Copy link

ChristopherCapito commented Nov 20, 2022

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
Copy link
Owner

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
Copy link
Author

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 added a commit that referenced this issue Jan 28, 2023
@Brakebein
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants