🧬 Advanced

CQRS Handlers Reference

Command Query Responsibility Segregation patterns in Ever Gauzy.

Overview

Gauzy uses NestJS CQRS (@nestjs/cqrs) to separate write operations (commands) from read operations (queries). This provides:

  • Clear separation of concerns
  • Easy testing of business logic
  • Event-driven side effects

Command Pattern

Defining a Command

export class CreateOrganizationSprintCommand implements ICommand {
  constructor(public readonly input: IOrganizationSprintCreateInput) {}
}

Handling a Command

@CommandHandler(CreateOrganizationSprintCommand)
export class CreateOrganizationSprintHandler implements ICommandHandler<CreateOrganizationSprintCommand> {
  constructor(private readonly sprintService: OrganizationSprintService) {}

  async execute(command: CreateOrganizationSprintCommand) {
    const { input } = command;
    return await this.sprintService.create(input);
  }
}

Dispatching a Command

@Post('/')
async create(@Body() entity: CreateSprintDTO) {
  return await this.commandBus.execute(
    new CreateOrganizationSprintCommand(entity)
  );
}

Command Flow

Diagram; its source is available below
Diagram source
sequenceDiagram
    Controller->>CommandBus: execute(Command)
    CommandBus->>CommandHandler: handle(Command)
    CommandHandler->>Service: business logic
    Service->>Repository: database operation
    Repository-->>Service: result
    Service-->>CommandHandler: result
    CommandHandler-->>Controller: result

Common Patterns

PatternDescription
Create commandsEntity creation with side effects
Update commandsEntity updates with validation
Composite commandsMulti-step operations
Event-emitting commandsTrigger events after execution

Edit this page

Copyright © 2023-Present Ever Co. LTD.Docs by Sourcey