관리 메뉴

Mini

[Nest JS] Postgres SQL 설치, type ORM, 엔티티(테이블) 생성 본문

JS/Nest.js

[Nest JS] Postgres SQL 설치, type ORM, 엔티티(테이블) 생성

Mini_96 2024. 8. 17. 21:49

설치후 pgAdmin4 실행후, db 생성

* type ORM ?

ORM 안쓰고 해보던지 ㅋㅋ

 

* 모듈 설치

npm install pg typeorm @nestjs/typeorm --save

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

export const typeORMConfig : TypeOrmModuleOptions = {
  type: 'postgres',
  host: 'localhost',
  port: 5432,
  username: 'postgres',
  password: '123456',
  database: 'board-app',
  entities: [__dirname + '/../**/*.entity.{js,ts}'], //엔티티 파일이 어디있는지(엔티티 이용해서 db table 생성함)
  synchronize: true //true -> app 재실행시, 수정된 컬럼의 테이블 drop후 재생성
}

import { Module } from '@nestjs/common';
import { BoardsModule } from './boards/boards.module';
import { typeORMConfig } from "./configs/typeorm.config";
import { TypeOrmModule } from "@nestjs/typeorm";

@Module({
  imports: [
    TypeOrmModule.forRoot(typeORMConfig),
    BoardsModule
  ]
})
export class AppModule {}

 

* 엔티티 생성

이거 완전 스프링...?

BaseEntity 상속받아야함.

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
}