Home > database >  nestJS set nullable value null in mysql database
nestJS set nullable value null in mysql database

Time:06-11

I have the following problem. In my entity I have set the refine_set and refined_vnum fields to nullabe & in my CreateItemProtoDto to IsOptional(). In create everything works now and I don't have to pass these two values.

export class CreateItemProtoDto {
  @IsNumber()
  @IsOptional()
  readonly refined_vnum: number;

  @IsNumber()
  @IsOptional()
  readonly refine_set: number;
}

But if I set them once and don't give them in a patch request I want them to be set to null again in the mysql database. But this does not happen because the Dto does not consider the record because it is zero.

export class UpdateItemProtoDto extends PartialType(CreateItemProtoDto) {}

CodePudding user response:

You can simply update your DTO class to have default values.

When passing the actual value from the request that one will be overridden otherwise it will remain null.

export class CreateItemProtoDto {
  @IsNumber()
  @IsOptional()
  readonly refined_vnum: number | null = null;

  @IsNumber()
  @IsOptional()
  readonly refine_set: number | null = null;
}
  • Related