Brakebein / prisma-generator-nestjs-dto

Generates NestJS DTO classes from Prisma Schema
Apache License 2.0
51 stars 28 forks source link

Ignore unique in connect objects? #28

Open m1212e opened 1 year ago

m1212e commented 1 year ago

Is it possible to implement the ignore annotations for the unique constraints like this?

model Role {
  id        String @id
  name      Json
  attribute String

  companyUserRoles CompanyUserRole[]

  /// @DtoUpdateHidden
  @@unique([name, attribute])
}
@ApiExtraModels(RoleNameAttributeUniqueInputDto)
export class ConnectRoleDto {
  @ApiProperty({
    required: false,
    nullable: true,
  })
  @IsOptional()
  @IsString()
  id?: string;

//  @ApiProperty({
//    required: false,
//    nullable: true,
//  })
//  @IsOptional()
//  @ValidateNested()
//  @Type(() => RoleNameAttributeUniqueInputDto)
//  name_attribute?: RoleNameAttributeUniqueInputDto;
}
Brakebein commented 1 year ago

I'm sorry, the prisma generator only parses triple-slash comments on fields and not on any @@ attributes. However, I just found out that triple-slash comments on models are parsed.

/// some annotation
model Role {
  id        String @id
  name      Json
  attribute String

  companyUserRoles CompanyUserRole[]

  @@unique([name, attribute])
}

So basically, a model could be annotated with, for example, /// @DtoConnectHideUnique to mark the desired behavior. I'm not sure if this is still comprehensible.

m1212e commented 1 year ago

Hm I dont 't think this is a good way to do this, tbh. What if someone wants to only hide on a specific unique. Maybe its worth asking over at prisma if they every thought about implementing something like this.

m1212e commented 1 year ago

https://github.com/prisma/prisma/discussions/20899

m1212e commented 1 year ago

https://github.com/prisma/prisma/issues/18561

paultannenbaum commented 8 months ago

@Brakebein I see in an earlier comment you mentioned a /// @DtoConnectHideUnique. Does that field exist? I tried with no luck, but is something I want to use.

To expand on that, when I add a second @unique to my models schema (in my case its a slug field), both my id and slug get marked as optional in my connect-dto's. But I would like my id field to stay required. How would I go about achieving that?

Brakebein commented 8 months ago

No, the annotation @DtoConnectHideUnique above was only a possible idea on how a model could be annotated. It hasn't been implemented yet.

In general, if there are more than one unique field, then, of course, the ConnectDto properties are optional, because you can identify an entry by either one of the fields. In this regard, it just replicates what the Prisma client expects for connect clauses.

There are two possible options to deal with your issue. It depends how you want it to be. Assuming following model:

model Page {
  id   String @id
  slug String @unique
}

Do you want the slug field to be ignored, with only the id field remaining?

export class ConnectPageDto {
  id: string;
}

Or do you just want the id field to be mandatory, the slug field is still optional (which doesn't make to much sense actually):

export class ConnectPageDto {
  id: string;
  slug?: string;
}

In both cases, I would need to implement some additional annotation to control this behavior. For example @DtoIgnoreConnect (for the first solution) or something similar.

paultannenbaum commented 8 months ago

In my case, the first scenario described would be preferred, where the connect dto would look like:

export class ConnectPageDto {
  id: string;
}

I dont want to be able to query by slug, but I do want it to be a unique constraint in the DB. Thanks!

Brakebein commented 7 months ago

Please check out version 1.21.0-beta. If an @id or @unique fields is annotated with the @DtoIgnoreOnConnect annotation, it will be ignored in the ConnectDto.

paultannenbaum commented 7 months ago

Hi @Brakebein, just pulled down the beta version and it works perfect for my use case. Thank you for adding this in!

Brakebein commented 7 months ago

With the latest release, I changed the annotation to @DtoConnectHidden to be consistent with the other annotations that omit fields, like @DtoCreateHidden etc. See readme.