NestJS 에서 TypeORM 연동 (코드 요약)

M·2023년 12월 1일

TIL

목록 보기
42/42
// posts.entity.ts
import { Column, Entity } from 'typeorm';

@Entity()
export class PostsModel {
  @PrimaryGeneratedColumn() // 프라이머리키 지정
  id: number;

  @Column() 
  author: string;

  @Column()
  title: string;

  @Column()
  content: string;

  @Column()
  likeCount: number;

  @Column()
  commentCount: number;
}
// app.module.ts
@Module({
  imports: [
    PostsModule,
    TypeOrmModule.forRoot({
      // 데이터베이스 타입
      type: 'postgres',
      // 엔드포인트
      host: '127.0.0.1',
      // 포트 (postgres 기본 5432)
      port: 5432,
      // 사용자 이름
      username: 'postgres',
      // 비밀번호
      password: 'postgres',
      // 데이터베이스 이름
      database: 'postgres',
      entities: [PostModel],
      // 싱크 연동여부
      synchronize: true, // 개발환경에서는 true
    }),
  ],
profile
자바스크립트부터 공부하는 사람

0개의 댓글