[Typescript] yarn 환경에서 TypeORM Migration 세팅

이정우·2023년 4월 5일
0

TypeORM이 Cannot import from outside in module 에러를 내면서 migration runmigration revert를 실행시키지 못하는 경우가 있어 tsconfig-paths 모듈을 설치 후 ts-node를 통해 runrevert를 실행시켜주어야 한다.

yarn add tsconfig-paths

나는 index.tsTypeORM DataSource가 다음과 같이 정의되어 있는 상태이다.

export const AppDataSource = new DataSource({
  type: 'mysql',
  ...
  synchronize: process.env.NODE_ENV === 'dev' ? true : false,
  logging: true,
  entities: [Video, VideoFile],
  migrations: ['./src/migration/*.ts'],
  subscribers: [],
});

그리고 package.json에 다음과 같이 커맨드를 정의하였다.

"migration:create": "yarn typeorm migration:create './src/migration/migrate'",
"migration:run": "yarn ts-node --compilerOptions '{\"experimentalDecorators\": true}' -r tsconfig-paths/register ./node_modules/.bin/typeorm migration:run -d index.ts",
"migration:revert": "yarn ts-node --compilerOptions '{\"experimentalDecorators\": true}' -r tsconfig-paths/register ./node_modules/.bin/typeorm migration:revert -d index.ts"

ts-node로 실행하는 runrevert의 경우, 같은 경로에 존재하는 tsconfig.json을 읽지 못하는 경우가 발생하여 compilerOptions을 직접 명시해주었다.

profile
공부

0개의 댓글