
문제 nanoid로 작성한 서비스 로직을 unit test하기 위해, jest로 코드 작성하고 테스트 실행시켰더니 아래와 같은 에러 메시지 발생 SyntaxError: Cannot use import statement outside a module 실행시키려는 코드 import { nanoid } from "nanoid"; export class userService { ... async createInviteCode(userId: string) { const code = nanoid(10); // 10글자의 nanaoId 생성 await this.respository.update(userId, { code }); }; } 원인 jest - jest는 commonJS 로 트랜스파일됨 nanoid - cr..
문제 User entity 를 수정하는데 @BeforeUpdate 로 만든 메서드가 해당 Entity에서 적용되지 않음 user.entity.ts ... @Column() password: string; @BeforeInsert() @BeforeUpdate() private async hashPassword() { const rounds = 10; const salt = await bcrypt.genSalt(rounds); this.password = await bcrypt.hash(this.password, salt); } user.controller.ts @Patch(":id") async update(@Param("id") id: string, @Body() updateUserDto: UpdateU..
TypeORM 을 사용해서 특정 데이터를 삽입할 때 주로 create 로 객체를 만들고 save로 저장하는 로직을 쓰곤 했다. 그러나, 회사에서 insert를 쓰는 게 더 낫지 않냐라는 이야기가 나왔고 그 둘의 차이를 비교해보았다. 공통점 insert과 save 메소드 모두 데이터베이스에서 엔티티를 생성하는데 사용한다. 차이점 두 메소드의 차이점은 엔티티의 존재 여부에 따라 다른 동작을 한다는 것과 반환 값에 있다. save 생성한 엔티티가 있다면 새로 삽입한다. 그러나, 이미 존재한다면 기존 엔티티의 데이터를 업데이트 한다. 즉, insert()와 update()의 기능을 한 번에 적용한다. 생성한 엔티티를 반환한다. Argument : entity | entity[] Return : the saved..
TypeORM을 사용해서 DB에서 (조건에 맞는) 데이터를 조회할 때 findOne 을 쓰곤 했었다. 그러다 이번에 자료를 찾아보다 findOneOrFail 라는 메서드가 나와 간단하지만 어떤 차이가 있는지 정리해보았다. 공통점 findOne과 findOneOrFail 메소드 모두 데이터베이스에서 어떤 조건에 맞는 하나의 엔티티를 찾는데 사용한다. 차이점 두 메소드의 차이점은 엔티티가 없을 때 어떻게 상황을 다루냐에 있다. findOne 주어진 옵션(조건)과 일치하는 첫 번째 엔티티를 조회한다.만약 특정 조건에 맞는 엔티티가 없으면, undefined을 반환한다. 즉, undefined 값을 반환할 때를 별도로 확인해야 하고 케이스를 처리해야 한다. Parameters Optional id: string..
구 버전의 NestJS 포함, 관련 라이브러리 버전을 업데이트하는 방법을 찾아보았다. 직접 삭제 후 일일이 재설치 하는 방법이 있긴 하지만, 보다 효율적인 방법을 모색하는 차원에서 조사를 해보았다. 업데이트 명령어 패키지 관리자 업데이트 $ npm update $ yarn upgrade-interactive npm-check-updates (nc) 기존 패키지를 모두 체크하여 현재 releas된 버전과 비교해서 출력해주는 툴 Install globally: npm install -g npm-check-updates ncu 위에서 언급된 명령어를 활용해, 일괄 update 하고 재설치 및 실행하는 과정에서 겪은 트러블슈팅을 기록해보았다. 트러블슈팅 1. all connections to the npm re..
문제 특정 패키지를 전역으로 설치하려는데 permission Error 가 발생함 npm install -g npm-check-update path /usr/local/lib/node_modules npm ERR! code EACCES npm ERR! errno -13 npm ERR! syscall access npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules' # 중략 npm ERR! The operation was rejected by your operating system. npm ERR! It is likely you do not have the permissions to access this file as..
문제 아래처럼 A라는 클래스의 type에 B 또는 C 가 들어갈 수 있음 그러나, Swagger로 데코러이터를 추가하면 B 와 C 부분은 하위 속성이 보여지지 않고 단순 객체 {} 형태만 나타남 interface A { id: string; type: B | C; } interface B { x: number; y: number; } interface C { name: string; count: number; } 해결 공식문서의 ExtraModels 적용하기 getSchemaPath and ApiExtraModels를 활용해 참고할 스키마의 값을 가져오기 import { ApiExtraModels, ApiResponse, getSchemaPath } from '@nestjs/swagger'; @ApiEx..