Implement event sourcing and CQRS patterns using event stores, aggregates, and projections. Use when building audit trails, temporal queries, or systems requiring full history.
Store state changes as a sequence of events rather than the current state, enabling temporal queries, audit trails, and event replay.
Minimal working example:
interface DomainEvent {
id: string;
aggregateId: string;
aggregateType: string;
eventType: string;
data: any;
metadata: {
userId?: string;
timestamp: number;
version: number;
};
}
interface Aggregate {
id: string;
version: number;
}
class EventStore {
private events: DomainEvent[] = [];
async appendEvents(
aggregateId: string,
expectedVersion: number,
events: Omit<DomainEvent, "id" | "metadata">[],
// ... (see reference guides for full implementation)
Detailed implementations in the references/ directory:
| Guide | Contents | |---|---| | Event Store (TypeScript) | Event Store (TypeScript) | | Projections (Read Models) | Projections (Read Models) | | Event Store with PostgreSQL | Event Store with PostgreSQL | | Snapshots for Performance | Snapshots for Performance |
Copy a source-pinned command for your client. You run it yourself.
Destination: .claude/skills/event-sourcing · pinned to the source commit
git clone https://github.com/aj-geddes/useful-ai-prompts.git
cd useful-ai-prompts
git checkout 3f5182cfd739fc113f4af5244a1cf342ad7f7911
mkdir -p ".claude/skills/event-sourcing"
cp -r "skills/event-sourcing" ".claude/skills/event-sourcing"Review the source before running. This copies files into your project; it is not a one-click install and does not verify runtime safety.
Scanner static-checks@0.1.0 · commit 3f5182cfd739. Static checks cannot prove runtime safety – review the source and the exact diff before installing. How checks work.
No static rules matched. This is not a safety guarantee.