Day19(4.7)

ShinJuYong·2022년 4월 7일
0

camp

목록 보기
19/44
post-thumbnail

HttpExceptionHandling

Main.ts

http-exception-filter.ts

Soft-Delete

물리적으로 DB에서 지우게된다면 아래와같이 작동하게된다.

  @Mutation(() => Boolean)
  deleteProduct(
    @Args('productId') productId: string, //
  ) {
    // 제품을 삭제한다.
    return this.productService.delete({ productId });
    // 제품이 삭제됐다면 True or False
  }
  async delete({ productId }) {
    // 제품을 삭제하는 비즈니스로직

    // 1. 실제 삭제(db의 데이터를 물리적으로 삭제한다.)
    const result = await this.productRepository.delete({ id: productId });
    return result.affected ? true : false;
    // 삭제가됐다면 1이기때문에 true
    // 아니라면 false가 반환된다.
  }

하지만 Soft-Delete를 하게된다면 컬럼으로 삭제상태를 나타내, 물리적으로 삭제가아닌 삭제상태를 알려주는식으로 삭제한다

  @Column({ default: null })
  @Field(() => Date)
  deletedAt: Date;

제품을 가져오는 fetchProducts를 했을때 deleteAt이 비어있냐 비어있지 않냐를 체크해 가져온다면 삭제상태 구분이 가능하다.

Nest.js와 TypeORM에는 내장된기능

  @DeleteDateColumn()
  deletedAt: Date;

this.productRepository.softRemove({ id: productId }); // id로만 삭제가능
this.productRepository.softDelete({ id: productId }); // 다양한 조건으로 삭제가능.

Join(Table)

아래와같이 사용하면 join테이블도 FE에서 받아 사용이 가능하다.

    // 2. 상품과 상품거래위치를 같이 등록하는경우 (Join)
    const { productSaleslocation, ...product } = createProductInput;

    const result = await this.productSaleslocationRepository.save({
      ...productSaleslocation,
    });

    return this.productRepository.save({
      ...product,
      productSaleslocation: result, //{ id: result.id }, 동일하게 인식한다.
    });

TIL Git

강의-깃허브
숙제-깃허브

0개의 댓글