이 강의는 인프런의 '따라하며 배우는 NestJS' 강의를 보고 학습한 글입니다.
[무료] 따라하며 배우는 NestJS - 인프런 | 강의
이 강의를 통해 NestJS에 대해서 배울 수 있습니다., - 강의 소개 | 인프런
www.inflearn.com
Pipe
- Injectable 데코레이터가 달린 클래스
- 클라이언트의 request에 대한 data transformation, data validation 수행
- Nest는 메소드가 호출되기 직전에 파이프를 삽입하고, 파이프는 메소드로 향하는 인수를 수신하고 이에 대해 동작
파이프의 종류
- Handler-level Pipes, 핸들러에 적용
- Parameter-level Pipes, 파라메터에 적용
- Global-level Pipes, main.ts에 적용
Bulit-in Pipes
- ValidationPipe
- ParseIntPipe
- ParseBoolPipe
- ParseArrayPipe
- ParseUUIDPipe
- DefaultValuePipe
실습에서 사용한 모듈
GitHub - typestack/class-validator: Decorator-based property validation for classes.
Decorator-based property validation for classes. Contribute to typestack/class-validator development by creating an account on GitHub.
github.com
// DTO
export class CreateBoardDto {
@IsNotEmpty()
title: string;
@IsNotEmpty()
description: string;
}
// Controller
@Post()
@UsePipes(ValidationPipe)
createBoard(@Body() createBoarddto : CreateBoardDto): Board {
return this.boardsService.createBoard(createBoarddto);
}
커스텀 파이프 구현
- PipeTransform 인터페이스 구현
- 구현할 transform메소드는 파라메터로 (인자의 값, 메타데이터)를 받으며 해당 메소드에서 Return된 값은 Route 핸들러로 전해진다. 만약 예외가 발생한다면 클라이언트에 바로 전해진다.
Postgres & TypeORM
- postgresSQL 설치
- TypeORM 이용
- 유저 요청 -> Controller -> Service -> Repository(DB조작) -> Service -> Controller -> Response -> 유저 응답
Documentation | NestJS - A progressive Node.js framework
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Rea
docs.nestjs.com
Repository | typeorm
typeorm.delightful.studio
데이터베이스를 이용한 CRUD 구현
- 현재 강의와 TypeORM 버전이슈로 인한 문제가 있는데, 아래 글을 참고하셔서 해결하시면 됩니다
typeorm 0.3.x @EntityRepository 돌려줘~~
typeorm 0.3.x 에서는 기존에 사용하던 @EntityRepository가 deprecated 되었다.그 말인 즉슨 커스텀 레포지토리 패턴을 사용할 수 없게되었다. 눈물이 난다.. 눈물이 나 하지만 의지의 한국인은 방법을 찾아
velog.io
'프로그래밍 > NestJS' 카테고리의 다른 글
NestJS 입문 - 권한, 로그, 설정 (0) | 2023.06.06 |
---|---|
NestJS 입문 - 인증기능 (0) | 2023.06.06 |
NestJS 입문 - 기본 요소, CRUD (0) | 2023.05.27 |