projection-patterns
它能做什么
这个工具可以帮你整理过去的事件信息,让你能快速查询。它会创建经过优化的“视图”来组织你的数据。
适用场景
- ✓当你需要实时查看事物的当前状态时。
- ✓当你需要根据事件历史重建信息时。
- ✓当你想让数据查询更快、更高效时。
技能链接
https://github.com/wshobson/agents/blob/main/plugins/backend-development/skills/projection-patterns/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我怎样才能根据交易事件,为我的销售数据创建一个经过优化的视图?”
每周 5 个新技能,1 分钟读完。
工作原理
它的工作方式是:获取一系列事件(也就是对已发生动作的记录),对它们进行处理,生成一个更简单、更直接的数据模型。之后就用这个模型快速回答各种问题。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Projection Patterns
Comprehensive guide to building projections and read models for event-sourced systems.
When to Use This Skill
- Building CQRS read models
- Creating materialized views from events
- Optimizing query performance
- Implementing real-time dashboards
- Building search indexes from events
- Aggregating data across streams
Core Concepts
1. Projection Architecture
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ Event Store │────►│ Projector │────►│ Read Model │
│ │ │ │ │ (Database) │
│ ┌─────────┐ │ │ ┌─────────┐ │ │ ┌─────────┐ │
│ │ Events │ │ │ │ Handler │ │ │ │ Tables │ │
│ └─────────┘ │ │ │ Logic │ │ │ │ Views │ │
│ │ │ └─────────┘ │ │ │ Cache │ │
└─────────────┘ └─────────────┘ └─────────────┘
2. Projection Types
| Type | Description | Use Case |
|---|---|---|
| Live | Real-time from subscription | Current state queries |
| Catchup | Process historical events | Rebuilding read models |
| Persistent | Stores checkpoint | Resume after restart |
| Inline | Same transaction as write | Strong consistency |
Templates and detailed worked examples
Full template library and detailed worked examples live in references/details.md. Read that file when you need the concrete templates.
Best Practices
Do's
- Make projections idempotent - Safe to replay
- Use transactions - For multi-table updates
- Store checkpoints - Resume after failures
- Monitor lag - Alert on projection delays
- Plan for rebuilds - Design for reconstruction
Don'ts
- Don't couple projections - Each is independent
- Don't skip error handling - Log and alert on failures
- Don't ignore ordering - Events must be processed in order
- Don't over-normalize - Denormalize for query patterns
常见问题
什么是“事件”?
事件是对过去发生之事的记录,比如“用户完成注册”或“订单已下单”。
什么是“读模型”?
这是一种专门为回答问题而设计的数据组织方式,与数据最初被记录的方式不同。
为什么要“优化性能”?
为了让系统在数据量很大时也能更快响应你的查询,避免变慢。
详情
作者的原始描述
Build read models and projections from event streams. Use when implementing CQRS read sides, building materialized views, or optimizing query performance in event-sourced systems.
repo: wshobson/agents
热度
- GitHub 星标
- 41k
- 排名
- #6640
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …