1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Nest.js TypeORMとDBの接続設定について

Posted at

開発環境での手順

  1. Docker等でDBを作成

  2. 以下コマンドでライブラリをインストール

    npm i typeorm@0.2.45 @nestjs/typeorm@8.0.2 pg
    
  3. src/entities/○○.entity.tsを作成し記述

  4. 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',
        },
    }
    
  5. src/app.module.tsにtypeOrmの設定を記載

    import { Module } from '@nestjs/common';
    import { TypeOrmModule } from '@nestjs/typeorm';
    
    @Module({
      imports: [TypeOrmModule.forRoot()],
    })
    export class AppModule {}
    
  6. 下記コマンドを実行

    # migrationファイルを作成
    npx typeorm migration:generate -n Create○○
    
    # distディレクトリにコンパイルさせるため一度ローカルでrun
    npm run start:dev
    
    # typeormのmigrationを実行
    npx typeorm migrations:run
    
  7. pgAdminなどで確認
    typeOrm実行後のDB.jpg

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?