Home > Software design >  Typescript TypeOrm best way to create and save Data
Typescript TypeOrm best way to create and save Data

Time:09-23

im currently working on a NestJS Project in which i have to store loads of data in a single database table. so i created an entity, a repository, a dto and the route in order to store everything. my dto is, just as my entity, extremely long and packed with alot of variables i have to store. pretty much every variable in my dto is optional and in the entity everything is nullable so i have to verify wether that value exists or not before creating a dataset for it.

my entity:

import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
import { distributionSponsors } from '../enums/distributionSponsors';
import { fbw } from '../enums/fbw';
import { MovieFskHf } from '../enums/movieFskHf';
import { MovieFskTr } from '../enums/movieFskTr';
import { MovieGenre } from '../enums/movieGenre';

@Entity()
export class Movie {
    @PrimaryGeneratedColumn()
    id: number;

    @Column({ nullable: true })
    isPublished: boolean;

    @Column({ nullable: true })
    cinemaRelease: Date;

    ...// more following the same format
}

import { ParseIntPipe } from '@nestjs/common';
import { ApiProperty } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import { IsOptional, IsString } from 'class-validator';
import { distributionSponsors } from '../enums/distributionSponsors';
import { fbw } from '../enums/fbw';
import { MovieFskHf } from '../enums/movieFskHf';
import { MovieFskTr } from '../enums/movieFskTr';
import { MovieGenre } from '../enums/movieGenre';

export class CreateMovieDto {
    @ApiProperty()
    @IsOptional()
    isPublished: boolean;

    @ApiProperty()
    @IsOptional()
    cinemaRelease: Date;

    ... // more following the same format
}

now in my repository im destructuring the dto. i dont want to wrap every variable coming out of my dto into an if-statement. is there an easier way of writing everything more compact?

@EntityRepository(Movie)
export class MoviesRepository extends Repository<Movie> {
    async createMovie(
        createMovieDto: CreateMovieDto,
    ) {
        const movie = this.create();

        const {
            isPublished,
            cinemaRelease,
            title,
            orgTitle,
            ...//more following
        } = createMovieDto;


       /* need logic here to do something like
       *  if(title){
       *  movie.title = title
       *  }
       *  just for every single variable from the dto
       */

        try {
            await this.save(movie);
        } catch (error) {
            //blah
        }
    }

CodePudding user response:

You can check the DTO object and delete the empty properties: here's the solution:

    async createMovie(
    createMovieDto: CreateMovieDto,
) {
    Object.keys(createMovieDto).forEach(
        k =>
        createMovieDto[k] == null ||
          (createMovieDto[k] == '' && delete createMovieDto[k]), 
      );

    try {
        await this.save(createMovieDto);
    } catch (error) {
        //blah
    }
}
  • Related