링크

설치해야 할 모듈들

@nestjs/typeorm
[email protected]
pg

npm install pg [email protected] @nestjs/typeorm --save

typeorm은 0.3.0 이상 버전에서 Repository 를 사용할 수가 없고 커스텀 레포지토리를 사용해야 한다.

0.2 버전으로 설치할 것

설정 파일 생성하기

import { TypeOrmModuleOptions } from '@nestjs/typeorm';

export const typeORMConfig: TypeOrmModuleOptions = {
  type: 'postgres',
  host: 'localhost',
  port: 5432,
  username: 'postgres',
  password: 'postgres',
  database: 'board-app',
  entities: [__dirname + '/../**/*.entity.{js,ts}'],
  synchronize: true,
};

루트 Module에서 Import 하기!

@Module({
  imports: [typeORMConfig.forRoot(typeORMConfig), BoardsModule],
  controllers: [],
  providers: [],
})

export class AppModule {}

Entity 생성하기

import { BaseEntity, Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
import { BoardStatus } from './board.model';

@Entity()
export class Board extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  title: string;

  @Column()
  description: string;

  @Column()
  status: BoardStatus;
}