Node
Nest.js
Typeorm

Typeorm

Installation

pnpm i @nestjs/typeorm @nestjs/config typeorm mysql2 joi
  • mysql2 : MySQL 연동을 위한 드라이버
  • pg : PostgresSQL 연동을 위한 드라이버
  • joi : 환경변수 검증기
    • Joi import시 주의사항
import * as Joi from "joi";
app.module.ts
    ConfigModule.forRoot({
      isGlobal: true,
      validationSchema: Joi.object({
        ENV: Joi.string().required(),
        DB_TYPE: Joi.string().required(),
        DB_HOST: Joi.string().required(),
        DB_PORT: Joi.number().required(),
        DB_USERNAME: Joi.string().required(),
        DB_PASSWORD: Joi.string().required(),
        DB_DATABASE: Joi.string().required(),
      }),
    }),

Lazy Loading

  • Typeorm은 기본적으로 Lazy Loading을 지원하지 않음
  • Lazy Loading을 하고 싶으면 타입을 Promise<T> 타입으로 해줘야 함
  • 참고 Commit (opens in a new tab)

연관관계

ManyToMany

Category와 Question의 ManyToMany 관계 설정, JoinTable()은 양방향의 한 쪽에만 사용해야 한다.

@Entity()
export class Category {
  @PrimaryGeneratedColumn()
  id: number;
 
  @Column()
  name: string;
 
  @ManyToMany(() => Question, (question) => question.categories)
  questions: Question[];
}
@Entity()
export class Question {
  @PrimaryGeneratedColumn()
  id: number;
 
  @Column()
  title: string;
 
  @Column()
  text: string;
 
  @ManyToMany(() => Category, (category) => category.questions)
  @JoinTable()
  categories: Category[];
}

JoinTable()

export class BatchNotification {
  ...
 
  // 다대다 관계 설정 (BatchNotification -> User)
  @ManyToMany(() => User, user => user.batchNotifications)
  @JoinTable({
    name: 'batch_notification_user', // 조인 테이블 이름
    joinColumn: {
      name: 'batch_notification_id', // 현재 엔티티(BatchNotification)의 컬럼 이름
      referencedColumnName: 'id', // BatchNotification 엔티티의 기본 키
    },
    inverseJoinColumn: {
      name: 'user_id', // 반대 엔티티(User)의 컬럼 이름
      referencedColumnName: 'id', // User 엔티티의 기본 키
    },
  })
  users: User[];
}

함수

Create Update

create(), save(), upsert(), delete(), softDelete(), restore()

Update

update(), increment(), decrement()

Find

find(), findAndCount(), findOne(), exists(), preload(), query()

통계관련 및 기타

count(), sum(), average(), minimum(), maximum(), query()

N + 1

relations

  async getAllBoards(): Promise<BoardResp[]> {
    const boards = await this.boardRepository.find({
      relations: ['comments'],
    });

find 메서드에 relations 넣어서 어떤 관계를 eager loading 할지 결정

Error

Circular Delete

연관관계를 설정할 때 양쪽 다 Casecade 설정을 하면 발생