Nest.js
-
class-validator의 `@ValidateNested` 데코레이터의 오해와 진실Nest.js/class-validator 2024. 7. 9. 16:45
이러한 중첩 객체가 있다면 @ValidateNested 데코레이터와 @Type 데코레이터를 사용 해주어야 한다. 그러나 만약 request-dto가 아닌 response-dto라면 어떻게 해야할까? GorupEventItemResDto인데 해당 Response Dto에는 response 하기 때문에 @Type 데코레이터만 사용해도 중첩 객체의 @Trsnfomer 데코레이터나 @Exclude @Expose 작업 같은것들이 정상적으로 실행된다. 자세한건 아래 Reference에 class-transformer 공식문서를 참고하면 된다. 결론적으로 @ValidateNested 데코레이터는 애초에 class-validator에서 가져오는것이고 중첩객체의 validation이 필요할때 사용 하는 데코레이터이고 만..
-
class-transformer 사용시 plainToInstance 메서드 사용시 주의할점Nest.js/class-transformer 2024. 7. 7. 16:48
TestEntity@Entity({ name: 'test' })export class TestEntity { @PrimaryGeneratedColumn() id!: number; @Column({ name: 'content', type: 'text', }) content!: string; @Column({ type: 'timestamptz', transformer: new LocalDateTimeTransformer(), nullable: true, }) orderDateTime!: LocalDateTime; @Transform( ({ value }: { value: Date }) => { console.log('*******호출!!!******'); return value.toISOS..
-
Javascript Date타입 대신 date, time, date-time을 다루기Nest.js/class-transformer 2024. 7. 7. 15:02
- TypeOrm과 js-joda를 이용한 Date타입 대신 LocalDateTime 타입 사용하기 이전에 작성한 글을 참고 하면 좋다. createdAt, updatedAt 의 date-time 형식 createdAt나 updatedAt 칼럼을 database에서 가져와서 일련의 데이터 가공 작업을 하고 api response할때 ISO 8601 포맷으로 리턴을 해주어야 한다. 꽤나 오랜시간 고민을 많이 했다. repository에서 createdAt나 updatedAt 관련된 프로퍼티를 return 하는 방법을 고민 해보았다가 이것은 꽤나 많은 수정을 거쳐야 될것 같아서 아닌것 같았다.필요한 기능은 다음과 같다. database에서 select 해서 가져왔을때는 js-joda 타입으로 손쉽게 불변..
-
TypeOrm과 js-joda를 이용한 Date타입 대신 LocalDateTime 타입 사용하기Nest.js/TypeOrm 2024. 7. 6. 20:40
js-joda에서 제공 하는 타입은 다음과 같다.LocalDate : 년,월,일 타입LocalTime : 분,초 타입LocalDateTime : 년,월,시,분,초 타입ZonedDateTime: joda/timezone과 함께 사용하며 특정 국가의 timezone에 따른 타입이외에 다른 기능들은 공식문서에 설명이 되어 있다. @js-joda/core 라이브러리를 설치 한다. TypeOrm의 column 데코레이터의 transformer 프로퍼티와 ValueTransformer를 이용하여 js-joda를 이용하여 날짜 데이터 타입을 보다 효율적으로 다룰 수 있다.js-joda에서 Convert from/ to native js 부분을 참고 utils/convert-date-time.ts transforme..
-
TypeOrm과 Postgresql에서 timestamp을 UpdateDateColumn 데코레이터 사용시 transformer 이슈Nest.js/TypeOrm 2024. 7. 6. 19:01
@UpdateDateColumn 사용시 transformer 프로퍼티 옵션을 사용하면 onUpdate CURRENT_TIMESTAMP가 동작하지 않는다.무슨 말이냐면 @CreateDateColumn, @UpdateDateColumn 데코레이터 사용해서 데이터를 생성 하면 현재 date-time을 자동으로 데이터를 입력 해주는데 만약 필드에 변경이 일어날 경우에 UpdateDateColumn에서 업데이트 된 기준 일자로 다시 해당 칼럼을 업데이트 해주는데 만약에 UpdateDateColumn에 transformer 프로퍼티 옵션을 사용하게 된다면 해당 기능이 제대로 동작 하지 않는다. @CreateDateColumn 데코레이터와 @UpdateDateColumn 데코레이터를 사용 했을때해당 코드는 cre..
-
-
Nestjs ConfigModule 사용시 환경변수 boolean값을 가져올때 string으로 가져오는 문제Nest.js 2024. 6. 24. 14:09
let cookieOptions: CookieOptions = { maxAge: Number(this.configService.get(ENV_COOKIE_MAX_AGE)), secure: this.configService.get(ENV_COOKIE_SECURE),}; CookieOptions에서 secure 프로퍼티의 옵션에는 boolean 값만 올 수 있는데typeof this.configService.get('COOKIE_SECURE')typeof this.configService.get(ENV_COOKIE_SECURE)> this.configService.get으로 .env 에 true나 false값을 가져오면 ***cookie_secure=*** string ***cookie_secur..