카테고리 없음

DTO(Data Transfer Object)

namu445 2022. 6. 1. 11:07
  • 값을 담아서 주고 받기 위한 객체 (주로 클아이언트의 입출력 데이터를 주고 받을 때 사용한다.)
  • 기능, 목적 별로 데이터를 주거나 받을 때 주고받는 데이터의 형태(객체)를 정의할 수 있다.
  • 유지보수성, 재사용성을 높일 수 있다.

 

  • 타입 스크립트를 사용하면 DTO를 이용해서 타입체크도 가능하다.
  • nestjs에서 라이브러리(class-validator)를 사용하면 DTO에 들어오는 값에 더 많은 제한 설정을 할 수 있다.
//GraphQL에서 같이 사용할 수 있는 DTO, HTTP API에서만 사용할 때는 데코레이터를 제거하고 사용한다.
import { Field, InputType, Int } from '@nestjs/graphql';
import { Min } from 'class-validator';

@InputType()
export class CreateProductInput {
  @Field(() => String)
  name: string;

  @Field(() => String)
  description: string;

  @Field(() => Int)
  @Min(0) //최소값 지정
  price: number;
}

 

 

 

 

<참고>

https://stackoverflow.com/questions/59397687/what-is-the-purpose-of-a-data-transfer-object-in-nestjs

 

What is the purpose of a Data Transfer Object in NestJS?

Im struggling with a problem. Im following the documentation of NestJS. The back-end framework for NodeJS. The documentation mentions a DTO (Data Transfer Object). I created a DTO for creating a us...

stackoverflow.com