This skill should be used when the user asks about "cqrs pattern", "command query responsibility segregation", "when to use cqrs", "separate read write", "event sourcing", or mentions needing different models for reading and writing data.
How this skill is triggered — by the user, by Claude, or both
Slash command
/design-pattern-skills:cqrsThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
CQRS separates read operations (Queries) from write operations (Commands) into different models. This allows each side to be optimized independently - the write model for complex business logic, and the read model for fast queries.
CQRS separates read operations (Queries) from write operations (Commands) into different models. This allows each side to be optimized independently - the write model for complex business logic, and the read model for fast queries.
// Command
interface CreateUserCommand {
type: 'CreateUser';
payload: { name: string; email: string };
}
// Query
interface GetUserQuery {
type: 'GetUser';
payload: { id: string };
}
// Command Handler
class UserCommandHandler {
async handle(command: CreateUserCommand): Promise<string> {
// Validate, apply business rules
const user = this.userAggregate.create(command.payload);
await this.repository.save(user);
return user.id;
}
}
// Query Handler
class UserQueryHandler {
async handle(query: GetUserQuery): Promise<UserDTO> {
// Read from optimized read model
return this.readDb.query('SELECT * FROM user_views WHERE id = ?', [query.payload.id]);
}
}
See examples/ directory for runnable TypeScript implementations:
examples/cqrs.ts - Basic implementation with separate handlersexamples/cqrs-advanced.ts - Real-world example with event projectionsnpx claudepluginhub keohanoi/design-pattern-skills --plugin design-pattern-skillsImplements CQRS (Command Query Responsibility Segregation) for scalable architectures. Use when separating read/write models, optimizing query performance, or building event-sourced systems.
Implements CQRS (Command Query Responsibility Segregation) for scalable architectures. Use when separating read/write models, optimizing query performance, or building event-sourced systems.
Separate command (write) and query (read) models for complex domains. Use when read/write patterns diverge significantly or when audit/consistency requirements demand immutability.