CQRS Pattern (@xndrjs/cqrs)
Command Query Responsibility Segregation (CQRS) separates commands (mutations) from queries (reads) for better scalability and maintainability.
Installation
npm install @xndrjs/cqrs
Overview
The @xndrjs/cqrs package provides three main buses:
- CommandBus - Handles commands (mutations/actions)
- QueryBus - Handles queries (reads)
- EventBus - Publishes and subscribes to domain events
Key Concepts
Commands
Commands represent actions that change state. They don't return values, only success/error.
class CreateTodoCommand extends Command<"CreateTodo", { text: string }> {
constructor(payload: { text: string }) {
super("CreateTodo", payload);
}
}
Queries
Queries represent read operations. They return results.
class GetTodosQuery extends Query<"GetTodos", {}, Todo[]> {
constructor() {
super("GetTodos", {});
}
}
Events
Events represent something that happened in the domain.
class TodoCreatedEvent extends DomainEvent<"TodoCreated", { id: string; text: string }> {
constructor(payload: { id: string; text: string }) {
super("TodoCreated", payload);
}
}
Next Steps
- Learn about CommandBus
- Explore QueryBus
- Check out EventBus