Association

Report 다루기 Report는 자동차를 판 사람이 자기가 판매한 가격과 정보를 보고하는 것. 이것을 바탕으로 자신의 중고차 가치를 추정받는 시스템의 정확도를 높일 수 있다. 리포트 Entity 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 // reports.entity.ts import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; @Entity() export class Report { @PrimaryGeneratedColumn() id: number; @Column() price: number; @Column() make: string; @Column() model: string; @Column() year: number; @Column() // 판매지역 나타내는 경도 lng: number; @Column() // 판매지역 나타내는 위도 lat: number; @Column() mileage: number; } DTO 생성 컨트롤러를 수정하자 ...

2023년 10월 10일 · 7 분 · 배준수

데이터 직렬화

Custom Data Serialization 응답에서 특정 속성 제거하기 비밀번호는 보안에서 중요한 만큼 응답에서 제외하는 것이 맞다. 이 과정을 진행해보자. 어떻게 다룰지는 Nest Document를 살펴볼 것. 현재 요청-응답 Flow는 다음과 같다. 요청이 발생한다. 컨트롤러로 이동 서비스로 이동 서비스가 Repository 이용하여 Entity instance를 컨트롤러에게 반환 이 인스턴스는 JSON으로 변환된다. Document에서 권장하는 방법 서비스에서 컨트롤러로 이동하는 Entity instance에 인스턴스를 plain object로 변경하는 것에 관한 규칙 라이브러리를 추가한다. class serializer interceptor 라는 decorator를 컨트롤러에서 추가한다. 이 interceptor는 들어오는 요청이나 내보내는 응답을 가로채서 entity를 plain object로 변경해준다. 우리의 경우 나가는 응답을 가로채서 entity를 라이브러리로 추가한 규칙에 맞게 plain object로 변경해줄것 결론적으로 우리는 일단 password는 빼고 response를 보내도록 코드를 추가해줄 예정이다. ...

2023년 10월 8일 · 7 분 · 배준수

TypeORM으로 데이터 처리하기

데이터베이스 ORM TypeORM SQLite Postgres MySQL MongoDB Mongoose MongoDB 아무거나 사용해도 된다. 여기서는 TypeORM과 SQLite를 사용할 것. 마지막에는 Postegres로 변경할 계획 Entity 우리는 AppModule 내부에[ 두가지 모듈(users, reports)을 만들기로 계획했었다. 이 모듈에는 각각 User Entity와 ReportEntity 파일을 만들 것이다. Entity 파일은 애플리케이션 내부에 저장하려는 한 종류의 리소스나 항목을 정의하고, 갖고 있는 모든 속성을 나열하는 리스트이다. 예를 들어 User 모듈에는 사용자가 입력한 email과 password가 있어야 하는데, User Entitiy에 나와 있어야 한다. ...

2023년 10월 7일 · 7 분 · 배준수

요청 데이터의 Validation

요청 데이터의 Validation(타당성) 검사 Decorator로 요청 데이터 접근하기 HTTP 요청 3가지로 구성 Start line : POST /messages/5?validate=true HTTP/1.1 Headers : Host: localhost:3000, Content-Type: application/json Body : {"content": "hi there"} Nest의 경우 decorator 이용 POST /messages/5?validate=true HTTP/1.1 5 : @Param(‘id’) validate=true : @Query() Headers는 @Headers() Body 는 @Body() 위와 같이 decorator를 이용하면 된다. messages.controller.ts 의 컨트롤러를 다시 작성해 보자. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // messages.controller.ts import { Controller, Get, Post, Body, Param } from '@nestjs/common'; @Controller('messages') // class decorator export class MessagesController { @Get() // method decorator listMessages() {} @Post() createMessage(@Body() body: any) { // @Body, @Param : argument decorator console.log(body); } @Get('/:id') getMessage(@Param('id') id: string) { console.log(id); } } 이제 API Client로 확인해보자. ...

2023년 10월 5일 · 4 분 · 배준수