프레임워크/Nest.js

NestJS TypeORM 설정 및 연결 with PostgreSQL

muyeon 2023. 12. 14. 10:50

설치

npm install @nestjs/typeorm typeorm pg

typeORM 세팅

 

app.module.ts 에 세팅한다.

 

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { PostsModule } from './posts/posts.module';
import { TypeOrmModule } from '@nestjs/typeorm';

@Module({
  imports: [
    PostsModule,
    TypeOrmModule.forRoot({
      type: 'postgres', // 데이터베이스 타입
      host: '127.0.0.1',
      port: 5432,
      username: 'postgres',
      password: 'postgres',
      database: 'postgres',
      entities: [], // 나중에 생성할 모델들을 넣는다.
      synchronize: true, // NestJS에서 작성하는 TypeORM 코드와 데이터베이스의 싱크를 자동으로 맞출건지, production 에서는 false
    }),
  ], // imports 는 다른 모듈을 불러올 때 사용한다.
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}