1.
Nestjs 프로젝트 생성
nest new typeorm2023
Shell
복사
2.
TypeORM 관련 패키지 설치
npm install --save @nestjs/typeorm typeorm mysql2
Shell
복사
3.
root 폴더에 orm.config.ts 추가 (수정필요)
import { TypeOrmModuleOptions } from "@nestjs/typeorm";
function ormConfig(): TypeOrmModuleOptions {
const commonConf = {
SYNCRONIZE: false,
ENTITIES: [__dirname + '/domain/*{.ts,.js}'],
MIGRATIONS: [__dirname + '/migrations/**/*{.ts,.js}'],
MIGRATIONS_RUN: false,
};
//
return {
name: 'default',
type: 'mysql',
database: 'test',
host: 'localhost',
port: Number(3306),
username: 'root',
password: 'root',
logging: true,
synchronize: commonConf.SYNCRONIZE,
entities: commonConf.ENTITIES,
migrations: commonConf.MIGRATIONS,
migrationsRun: commonConf.MIGRATIONS_RUN,
};
}
export { ormConfig };
TypeScript
복사
4.
app.module.ts에 ormConfig 추가
import { ormConfig } from './orm.config';
import { TypeOrmModule } from '@nestjs/typeorm';
@Module({
imports: [
TypeOrmModule.forRootAsync({ useFactory: ormConfig }),
...
],
...
})
TypeScript
복사