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

Ignore unique in connect objects? #28

Open
m1212e opened this issue Aug 24, 2023 · 10 comments
Open

Ignore unique in connect objects? #28

m1212e opened this issue Aug 24, 2023 · 10 comments

Comments

@m1212e
Copy link

m1212e commented Aug 24, 2023

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

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

m1212e commented Aug 30, 2023

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

m1212e commented Aug 30, 2023

prisma/prisma#20899

@m1212e
Copy link
Author

m1212e commented Sep 22, 2023

prisma/prisma#18561

@paultannenbaum
Copy link

paultannenbaum commented Mar 28, 2024

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

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

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

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

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

@Brakebein
Copy link
Owner

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.

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

3 participants