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

Implement DtoRelationCanDisconnectOnUpdate annotation #25

Merged
merged 6 commits into from
Aug 9, 2023
Merged

Implement DtoRelationCanDisconnectOnUpdate annotation #25

merged 6 commits into from
Aug 9, 2023

Conversation

m1212e
Copy link

@m1212e m1212e commented Aug 1, 2023

Add a "@DtoRelationCanDisconnectOnUpdate" annotation which enables to auto generate disconnect fields on the connect DTO

@m1212e m1212e mentioned this pull request Aug 1, 2023
@m1212e
Copy link
Author

m1212e commented Aug 1, 2023

@Brakebein it would be super cool if you could check and merge this, thanks for you work!

@Brakebein
Copy link
Owner

Thanks! I will be able to look at it next week.

@Brakebein
Copy link
Owner

After reading the docs (https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#disconnect) and playing around with the Prisma client, I found some caveats:

According to the docs, the disconnect update field is only available for optional relation fields. Hence, for the sample schema, it should not be possible to create a disconnect field for company, which is declared as a mandatory field. Though, it would be possible, for example, for the optional photo field.

prisma.product.update({
  where: { id: '123' },
  data: {
    photo: {
      disconnect: true,
    },
    company: {
      // disconnect not available
    },
  },
});

The question is, if it should be skipped automatically in this case (maybe with a warning), or if the user should be responsible to not add this annotation to the company field?

However, it seems to be possible to do it the other way round. I don't know what happens to the mandatory field then. But this should not be our problem in the scope of this generator. I was just wondering.

prisma.company.update({
  where: { id: '456' },
  data: {
    product: {
      disconnect: { id: '123' },
    },
  },
});

Adding the annotation to a relation list field produces the correct disconnect field 👍

model Photo {
  id      String    @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  /// @DtoRelationCanDisconnectOnUpdate
  product Product[]
}

@m1212e
Copy link
Author

m1212e commented Aug 9, 2023

Good point, I think this is a thing the library should address! I've added a check which throws an error if a relation is required and is not a list.

@m1212e
Copy link
Author

m1212e commented Aug 9, 2023

Regarding the required 1:n where you disconnect from the n side: I would agree that this is beyond the scope of the generator.

@Brakebein Brakebein merged commit 7257569 into Brakebein:main Aug 9, 2023
@m1212e
Copy link
Author

m1212e commented Aug 24, 2023

Thanks for merging!
After updating to the newest version I get a slightly wrong output:

  @ApiProperty({
    type: Product,
    isArray: true,
    required: false,
    nullable: true,
    type: ConnectProductDto,
  })
  @IsOptional()
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => ConnectProductDto)
  disconnect?: ConnectProductDto[];

The type field of the @ApiProperty decorator is set twice. This did not happen before the merge, as far as I can see. Would you mind looking into this @Brakebein ? Maybe you have an idea where the issue lies. If you're short on time, hit me up again, I can also investigate but thought you might already know whats causing this, so just wanted to let you know!

@m1212e
Copy link
Author

m1212e commented Aug 24, 2023

I think I found the cause, I'll open a PR

@m1212e
Copy link
Author

m1212e commented Aug 24, 2023

#29

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

Successfully merging this pull request may close these issues.

2 participants