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

Add @DtoCreateRequired decorator #55

Open
gwesterman opened this issue Oct 21, 2024 · 5 comments
Open

Add @DtoCreateRequired decorator #55

gwesterman opened this issue Oct 21, 2024 · 5 comments

Comments

@gwesterman
Copy link

I need to generate a CreateDTO that contains a required field for the id of an optional relation that can be set to null after the entry is created. This would typically happen via onDelete: SetNull.

My use-case is a chat in which messages require a reference to a user/author on creation, but this reference can be null once the user is deleted, which in turn should not cause the message to be deleted.

In the model below this works well for Channel, which is a non-optional reference. The channelId field on the CreateMessageDto is required. But this is not the case for authorId.

This is a simplified/partial version of my model:

// schema.prisma
model User {
   id        String   @id @unique
   createdAt DateTime @default(now())
   updatedAt DateTime @updatedAt

   name      String

   messages  Message[]
}

model Message {
   id        String   @id @default(cuid())
   createdAt DateTime @default(now())
   updatedAt DateTime @updatedAt

   author    User?   @relation(fields: [authorId], references: [id], onDelete: SetNull)
   /// @DtoRelationIncludeId
   /// @IsRequired
   authorId  String?

   channel   Channel @relation(fields: [channelId], references: [id], onDelete: Cascade)
   /// @DtoRelationIncludeId
   /// @IsRequired
   channelId String

   content   String
}

The generated CreateMessageDto looks like this:

// create-message.dto
export class CreateMessageDto {
   @ApiProperty({
      type: 'string',
      required: false,
      nullable: true
   })
   @IsOptional()
   @IsString()
   authorId?: string | null;
   @ApiProperty({
      type: 'string'
   })
   @IsNotEmpty()
   @IsString()
   channelId!: string;
   @ApiProperty({
      type: 'string'
   })
   @IsNotEmpty()
   @IsString()
   content!: string;
}

I've tried different combinations of decorators, but cannot get it to work. Am I missing something?

Could a new decorator, for example @DtoCreateRequired help in this case?

@Brakebein
Copy link
Owner

Yes, similar to the @DtoUpdateRequired annotation there should be an equivalent annotation for the CreateDto. I will add it within the next days.

Brakebein added a commit that referenced this issue Oct 26, 2024
@Brakebein
Copy link
Owner

@DtoCreateRequired annotation has been added in 1.24.0-beta3.

(I'm waiting for some feedback on other features before releasing the next proper version.)

@gwesterman
Copy link
Author

Thank you for implementing this so quickly!

I can confirm, that @DtoCreateRequired is working for me, but only in conjunction with @DtoRelationIncludeId. Is this intended?

@Brakebein
Copy link
Owner

Currently, each annotation fulfills a different task: @DtoRelationIncludeId only makes the ID field visible, which is otherwise hidden by default. @DtoCreateRequired only turns an optional field to required.

But if I think about it, you are probably right. similar to @DtoCreateOptional which unhides an omitted field, you would expect that @DtoCreateRequired unhides the field as well.

What I need to check is, if @DtoCreateOptional only works for normal fields, but also for relation-related fields making @DtoRelationIncludeId kind of redundant?

@Brakebein
Copy link
Owner

Sorry, it took some time to come back on this, but I now checked the behavior. For now, @DtoRelationIncludeId is not really redundant. It unhides the relation-related ID field in CreateDto, UpdateDto, and PlainDto. So, it affects 3 files. While @DtoCreateOptional and @DtoCreateRequired only affects the CreateDto. Hence, in your special use case, you'll need both @DtoRelationIncludeId and @DtoCreateOptional keeping it optional in the UpdateDto.

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