name: cqrs-event-projection-basics description: Applies practical CQRS and event projection fundamentals in TypeScript systems by separating write and read concerns with deterministic projections. Use when read models diverge from write workflows or event-driven views are needed.
CQRS + Event Projection Basics
Rule Anchor
AGENTS.md> "Execution Safety"
Use This Skill When
- Read queries and write commands have different shapes and scaling needs.
- You need projection-based views from event streams.
- Event-driven workflows require deterministic read models.
Core Principles
- Commands mutate state, queries read optimized views.
- Events are immutable facts emitted after successful command handling.
- Projections update read models incrementally from events.
- Projection logic must be deterministic and idempotent by design.
Workflow
- Define command contracts and validation rules.
- Define emitted event names and payload contracts.
- Build projection handlers per event name.
- Maintain projection checkpoints (e.g., sequenceId).
- Rebuild read models from event logs when required.
Projection Handler Rules
- Handle one event contract at a time.
- Avoid hidden state inference from IDs or naming conventions.
- Use explicit payload fields only.
- Fail fast on contract violations.
Checklist
- Command and query models are separated.
- Event schemas are explicit and version-aware.
- Projection updates are deterministic.
- Replay from checkpoint produces identical read state.
- Observability exists for processed/failed event counts.
Anti-Patterns
- Querying write models directly for all reads.
- Projections that depend on runtime global mutable state.
- Silent fallback behavior on malformed events.