Nest.js
[Nestjs]PatialType이란
Dev갱이
2022. 12. 21. 23:32
728x90
POST, UPDATE 요청을 처리할 때 데이터 삽입에 대한 DTO, 데이터 수정에 대한 DTO를 만든다. 일반적으로 데이터 수정 DTO는 삽입 시의 DTO에 종속된다. 수정시의 DTO에 들어가는 개체가 삽입시의 DTO에 들어있는 개체에 포함된다는 것이다.
그런 상황에서 사용할 수 있는 것이 PartialType다.
POST 처리를 위한 DTO가 CreateDto라고 가정하면,
import { IsNumber, IsOptional, IsString } from 'class-validator';
export class CreateDto {
@IsString()
readonly title: string;
@IsNumber()
readonly year: number;
@IsString({ each: true })
@IsOptional()
readonly genres: string[];
}
이런식이 된다. 이제 이 클래스를 UpdateDto에도 재사용할 수 있다.
import { PartialType } from '@nestjs/mapped-types';
import { CreateDto } from './create.dto';
export class UpdateDto extends PartialType(CreateDto) {}
위처럼 extends PartialType(상위DTO)를 통해 선택적으로 종속받을 수 있다.
728x90