Open m1212e opened 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.
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.
@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?
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.
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!
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.
Hi @Brakebein, just pulled down the beta version and it works perfect for my use case. Thank you for adding this in!
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.
Is it possible to implement the ignore annotations for the unique constraints like this?