Backend2025년 5월 29일2분 읽기

NestJS 심화 — CQRS 패턴과 이벤트 소싱

YS
YoungSam
조회 109

CQRS란

Command Query Responsibility Segregation. 읽기와 쓰기 모델을 분리하여 각각 최적화합니다.

NestJS CQRS 모듈

// Command
export class CreateOrderCommand {
  constructor(public readonly userId: string, public readonly items: Item[]) {}
}

// Handler
@CommandHandler(CreateOrderCommand)
export class CreateOrderHandler implements ICommandHandler<CreateOrderCommand> {
  async execute(command: CreateOrderCommand) {
    const order = Order.create(command.userId, command.items);
    await this.repository.save(order);
    this.eventBus.publish(new OrderCreatedEvent(order.id));
  }
}

이벤트 소싱

상태 변경을 이벤트 시퀀스로 저장하여 완전한 히스토리를 유지합니다.

NestJSCQRSEvent Sourcing

댓글 0

아직 댓글이 없습니다.