0

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

Audwin Oyong
  • 2,247
  • 3
  • 15
  • 32
Exo
  • 237
  • 1
  • 2
  • 13
  • Is it possible that you have `Colum` instead of `Column`? Seems like it would throw an error, but it would explain why the column isn't nullable. Also check the db schema to make sure the column is actually nullable in the db. – DemiPixel Oct 07 '21 at 23:50
  • sorry its just a typo, i have Column, i shortened everything down and somewhere i somehow delete the N – Exo Oct 08 '21 at 06:34
  • fixed my typo and yes, within the database the field is nullable – Exo Oct 08 '21 at 06:34

1 Answers1

0

okay so i basically figured out that i can solve the issue by creating my own decorator which is heavily inspired by this toBoolean Decorator and now, when my colleague sends me an emtpy string, ``, or null it converts it to being NULL on the database. before the dto

import { Transform } from 'class-transformer';

const ToInteger = () => {
    const toPlain = Transform(
        ({ value }) => {
            return value;
        },
        {
            toPlainOnly: true,
        },
    );
    const toClass = (target: any, key: string) => {
        return Transform(
            ({ obj }) => {
                return valueToInteger(obj[key]);
            },
            {
                toClassOnly: true,
            },
        )(target, key);
    };
    return function (target: any, key: string) {
        toPlain(target, key);
        toClass(target, key);
    };
};

const valueToInteger = (value: any) => {
    if (value === undefined) {
        return undefined;
    }
    if (isNaN(value)) {
        return null;
    }
    if (value === '') {
        return null;
    }
    if (value === 'null') {
        return null;
    }
    const x = parseInt(value);

    return x;
};

export { ToInteger };

after that i just had to import the decorator into my dto and set it for this variable

    //imports
    
    export class CreateMovieDto {
    
    @ApiProperty()
    @IsOptional()
    @ToInteger()
    dfffCopies: number;
    
    //more stuff
    
    }
Exo
  • 237
  • 1
  • 2
  • 13