開発環境での手順
-
Docker等でDBを作成
-
以下コマンドでライブラリをインストール
npm i typeorm@0.2.45 @nestjs/typeorm@8.0.2 pg
-
src/entities/○○.entity.tsを作成し記述
-
ormconfig.jsに接続情報を記載
module.exports = { type: 'postgres', host: 'localhost', port: 5432, username: 'postgres', password: 'postgres', database: 'postgres', autoLoadEntities: true, entities: ['dist/entities/*.entity.js'], migrations: ['dist/migrations/*.js'], cli: { entitiesDir: 'src/entities', migrationsDir: 'src/migrations', }, }
-
src/app.module.tsにtypeOrmの設定を記載
import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; @Module({ imports: [TypeOrmModule.forRoot()], }) export class AppModule {}
-
下記コマンドを実行
# migrationファイルを作成 npx typeorm migration:generate -n Create○○ # distディレクトリにコンパイルさせるため一度ローカルでrun npm run start:dev # typeormのmigrationを実行 npx typeorm migrations:run