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));
}
}이벤트 소싱
상태 변경을 이벤트 시퀀스로 저장하여 완전한 히스토리를 유지합니다.
댓글 0