Me and my colleague are working on a client project currently. Where using NestJS as the backend and Angular in the frontend, the database is a MySQL.
we're struggling with the following issue and we both dont know how to fix this:
- In the Entity of the NestJS Backend we have a Column setup as a number (int) like this:
//imports
@Entity()
export class Movie {
@Column({ nullable: true })
dfffCopies: number;
//more stuff
}
- in the Angular frontend my colleague is setting up a form with the angular formbuilder with the initial value of 0
this.form = this.fb.group({ dfffCopies: new FormControl(0)})
- when he sends this form out with the formbuilder it hits the backend controller, landing in the dto
//imports
export class CreateMovieDto {
@ApiProperty()
@IsOptional()
dfffCopies: number;
//more stuff
}
the issue now is, that when the user inputs nothing, my colleague wants to send out a NULL, undefined, empty string or something like that, not a 0 as a number so that the backend can save this value as empty. however when he does that the backend throws an error
"incorrect integer value: 'null'"
i know that i could do something like Number(dfffCopies)
in the backend before saving and we already tried that, but the problem with that is, that we have probably around 50+ more integer values to save within this movie-entity and i pretty much save the whole dto into the database like this:
//when saving
const movie = this.create(createMovieDto);
await this.save(movie);
//when editing
await this.update({ id }, createMovieDto);
if i would do that i would have to wrap every single value from the dto into an if-statement to check wether its there or not
Question now is: How can i or my colleague change the code to accept NULL, undefined, NaN or something else on the integer field and save it empty into the database? He said that we could change the backend from number/integer on all fields to String but tbh that doesnt feel like it is the solution