개발일지
Nest.js DB연동
index.ys
2023. 8. 13. 02:28
Nest.js RDS 연동
config파일 설정
- config폴더에 typeorm.cofig.ts 파일 설정
- db와 연동 되는지 확인
import { DataSource, DataSourceOptions } from "typeorm";
export const dataSourceOptions: DataSourceOptions = {
type: 'mysql',
host: 'HOST',
port: 3306,
username: '이름',
password: '비밀번호',
database: 'diedie_backend',
entities: [__dirname + '/../**/*.entity.{js,ts}'],
synchronize: false, // true로 설정할 경우 서버가 구동될 때마다 테이블이 자동으로 생성됨
migrations: [__dirname + '/../migrations/*.{js,ts}'], // migration 수행할 파일
// "migrations": ["dist/migrations/*{.ts,.js}"], // migration 수행할 파일
// cli: {
// migrationsDir: "src/migrations" // migration 파일을 생성할 디렉토리
// },
migrationsTableName: "migrations" // migration 내용이 기록될 테이블명(default = migration)
}
const dataSource = new DataSource(dataSourceOptions)
export default dataSource
- 정상적으로 db와 연결
마이그레이션 파일 생성
typeorm migration:create ./migrations/이름
DB에 마이그레이션
- db에 마이그레이션 파일과 동일한 테이블이 존재하면 에러발생
- 마이그레이션 파일의 경로를 지정 해주거나 package.json에서 명령어 옵션을 변경
npm run typeorm migration:run
참고 블로그
TypeORM in NESTJS — Cannot use import statement outside a module.
Hi there, I guess if you arrived here it’s because as I did, you struggled with configuring TypeORM Migration with NestJS. Here is how to…
vincentwargnier.medium.com