Nestjs- Typeorm 0.3에서 migration 사용하기
typeorm.config.ts경로 : src/config/typeorm.config.ts
migration 명령어 사용을 위한 package.json 추가
// package.json
...
"scripts": {
...
"typeorm": "npx typeorm -d src/config/typeorm.config.ts",
"migration:generate": "npm run typeorm -- migration:generate",
"migration:run": "npm run typeorm -- migration:run",
"migration:revert": "npm run typeorm -- migration:revert"
...
}
...
migration 파일 생성 위한 cli
npm run migration:generate -- ./src/database/migrations/migrations
- 에러발생
Error: Unable to open file: "/Users/bugibugi/Project2/NEST/yanglog/yanglog-server/src/config/typeorm.config.ts". Cannot use import statement outside a module
//typeorm.config.ts
import { TypeOrmModuleOptions } from '@nestjs/typeorm';
import { DataSource, DataSourceOptions } from 'typeorm';
export const typeORMConfig: DataSourceOptions = {
type: 'postgres',
host: '호스트',
port: 포트,
username: '디비유저',
password: '디비유저비밀번호',
database: '데이터베이스',
entities: [__dirname + '/../**/*.entity.{js,ts}'],
migrations: ['/dist/db/migrations/*{.ts,.js}'],
synchronize: false,
};
const dataSource = new DataSource(typeORMConfig);
export default dataSource;
typeorm.config.ts를 이렇게 수정하니 해결됬고! migration 파일이 정상적으로 생성되었다.
$ npm run migration:run
해당 명령어로 dist/db/migrations/*.js ts
경로에 있는 migration 파일을 실행하여 테이블을 만들줄 알았으나 기본 migration 테이블만 만들어졌다...
이해가 안된다... 왜일까... ㅠㅠ
우선 typeorm -d datasorce인스턴스를 생성하는 파일이 루트경로에 있어야 하는것 같다.
루트경로에 migration 명령어를 사용을 위한 data-source.ts파일을 생성한다.
//data-source.ts
import { ConfigService } from '@nestjs/config';
import { config } from 'dotenv';
import { DataSource } from 'typeorm';
config();
const configService = new ConfigService();
export default new DataSource({
type: 'postgres',
host: configService.get<string>('DATABASE_HOST'),
port: configService.get<number>('DATABASE_PORT'),
username: configService.get<string>('DATABASE_USERNAME'),
password: configService.get<string>('DATABASE_PASSWORD'),
database: configService.get<string>('DATABASE_NAME'),
synchronize: false,
entities: ['src/**/*.entity.ts'],
migrations: ['src/database/migrations/*.ts'],
migrationsTableName: 'migrations',
});
주의점은 data-source.ts는 package.json typeorm 명령어 -d datasource인스턴스를 하는역할로 사용.
기존에 TypeOrmModule.forRoot(typeORMConfig) 해당부분은 원래 사용하던 typeORMConfig.ts파일을 사용했다.
// package.json
...
"scripts": {
...
"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --dataSource ./src/config/typeorm.config.ts",
"migration:create": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js migration:create",
"migration:generate": "npm run typeorm migration:generate ./src/database/migrations/Migration",
"migration:run": "npm run typeorm migration:run",
"migration:revert": "npm run typeorm migration:revert"
...
}
...
주의점
tsconfig.build.json
{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "test", "dist", "**/*spec.ts", "data-source.ts"]
}
나같은 경우에 data-source.ts를 nest build 할때 제외 해주었다 왜냐하면 그렇게 하지 않으면 원래는 dist/main 이런식으로 빌드 되는데
data-source를 제외 안시키면 dist/src/main, dist/data-source.js 이렇게 되기 때문에 다른것들 경로가 꼬인다.
$ npm run migration:create ./src/database/migrations/MigrationPostData
명령어로 MigrationPostData에 up부분에 post data를 넣는 부분을 구현하고
$ npm run migration:run
하면 성공!!!