$npx -y skills add wshobson/agents --skill projection-patternsBuild read models and projections from event streams. Use when implementing CQRS read sides, building materialized views, or optimizing query performance in event-sourced systems.
| 1 | # Projection Patterns |
| 2 | |
| 3 | Comprehensive guide to building projections and read models for event-sourced systems. |
| 4 | |
| 5 | ## When to Use This Skill |
| 6 | |
| 7 | - Building CQRS read models |
| 8 | - Creating materialized views from events |
| 9 | - Optimizing query performance |
| 10 | - Implementing real-time dashboards |
| 11 | - Building search indexes from events |
| 12 | - Aggregating data across streams |
| 13 | |
| 14 | ## Core Concepts |
| 15 | |
| 16 | ### 1. Projection Architecture |
| 17 | |
| 18 | ``` |
| 19 | ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ |
| 20 | │ Event Store │────►│ Projector │────►│ Read Model │ |
| 21 | │ │ │ │ │ (Database) │ |
| 22 | │ ┌─────────┐ │ │ ┌─────────┐ │ │ ┌─────────┐ │ |
| 23 | │ │ Events │ │ │ │ Handler │ │ │ │ Tables │ │ |
| 24 | │ └─────────┘ │ │ │ Logic │ │ │ │ Views │ │ |
| 25 | │ │ │ └─────────┘ │ │ │ Cache │ │ |
| 26 | └─────────────┘ └─────────────┘ └─────────────┘ |
| 27 | ``` |
| 28 | |
| 29 | ### 2. Projection Types |
| 30 | |
| 31 | | Type | Description | Use Case | |
| 32 | | -------------- | --------------------------- | ---------------------- | |
| 33 | | **Live** | Real-time from subscription | Current state queries | |
| 34 | | **Catchup** | Process historical events | Rebuilding read models | |
| 35 | | **Persistent** | Stores checkpoint | Resume after restart | |
| 36 | | **Inline** | Same transaction as write | Strong consistency | |
| 37 | |
| 38 | ## Templates and detailed worked examples |
| 39 | |
| 40 | Full template library and detailed worked examples live in `references/details.md`. Read that file when you need the concrete templates. |
| 41 | |
| 42 | ## Best Practices |
| 43 | |
| 44 | ### Do's |
| 45 | |
| 46 | - **Make projections idempotent** - Safe to replay |
| 47 | - **Use transactions** - For multi-table updates |
| 48 | - **Store checkpoints** - Resume after failures |
| 49 | - **Monitor lag** - Alert on projection delays |
| 50 | - **Plan for rebuilds** - Design for reconstruction |
| 51 | |
| 52 | ### Don'ts |
| 53 | |
| 54 | - **Don't couple projections** - Each is independent |
| 55 | - **Don't skip error handling** - Log and alert on failures |
| 56 | - **Don't ignore ordering** - Events must be processed in order |
| 57 | - **Don't over-normalize** - Denormalize for query patterns |