@requence/event-sourcing 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +81 -0
- package/package.json +5 -3
package/README.md
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# @requence/event-sourcing
|
|
2
|
+
|
|
3
|
+
A high-performance, type-safe event sourcing library for TypeScript.
|
|
4
|
+
|
|
5
|
+
Build resilient, auditable systems by storing every state change as an immutable event — then derive your current state by replaying them.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- **Aggregate Roots** — define your domain model with events, business rules, and commands using a fluent builder API.
|
|
10
|
+
- **Automatic Concurrency Control** — aggregate root streams are automatically locked to prevent simultaneous edits and event stream version collisions.
|
|
11
|
+
- **Distributed Locking** — use `redisLock` for multi-instance deployments to coordinate locks across processes via Redis.
|
|
12
|
+
- **Projections** — build query-optimized read models from events, with full replay support.
|
|
13
|
+
- **Process Managers** — coordinate workflows across multiple aggregates.
|
|
14
|
+
- **Event Listeners** — react to events with lightweight, stateless side effects.
|
|
15
|
+
- **Storage Adapters** — swap between an in-memory store (great for testing), PostgreSQL via Drizzle, or Redis.
|
|
16
|
+
- **Type-Safe Events** — leverage Zod schemas and TypeScript for compile-time and runtime guarantees on every event shape.
|
|
17
|
+
|
|
18
|
+
## Install
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install @requence/event-sourcing zod
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Quick Start
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { createAggregateRoot } from '@requence/event-sourcing'
|
|
28
|
+
import { createEventStore } from '@requence/event-sourcing/memory'
|
|
29
|
+
|
|
30
|
+
// 1. Define an aggregate root
|
|
31
|
+
const counter = createAggregateRoot('counter')
|
|
32
|
+
.withInitialState({ count: 0 })
|
|
33
|
+
.withEvents(({ z }) => ({
|
|
34
|
+
Incremented: z.object({ amount: z.number() }),
|
|
35
|
+
}))
|
|
36
|
+
.withEventHandlers((state) => ({
|
|
37
|
+
onIncremented({ payload }) {
|
|
38
|
+
state.count += payload.amount
|
|
39
|
+
},
|
|
40
|
+
}))
|
|
41
|
+
.withCommands((state, event) => ({
|
|
42
|
+
increment(amount: number) {
|
|
43
|
+
return event('Incremented', { amount })
|
|
44
|
+
},
|
|
45
|
+
}))
|
|
46
|
+
|
|
47
|
+
// 2. Create an event store with the aggregate root
|
|
48
|
+
const eventStore = createEventStore({
|
|
49
|
+
aggregateRoots: [counter],
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
// 3. Build a read model with a projection
|
|
53
|
+
const totals = new Map<string, number>()
|
|
54
|
+
|
|
55
|
+
eventStore.createProjection('counter-totals').withEventHandlers({
|
|
56
|
+
onIncremented({ streamId, payload }) {
|
|
57
|
+
totals.set(streamId, (totals.get(streamId) ?? 0) + payload.amount)
|
|
58
|
+
},
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
// 4. Execute commands on a new stream
|
|
62
|
+
const stream = await counter.newStream().increment(1).increment(5).settled()
|
|
63
|
+
console.log(totals.get(stream.streamId)) // 6
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Storage Adapters
|
|
67
|
+
|
|
68
|
+
| Adapter | Import Path | Use Case |
|
|
69
|
+
|---------|------------|----------|
|
|
70
|
+
| In-Memory | `@requence/event-sourcing/memory` | Testing & prototyping |
|
|
71
|
+
| PostgreSQL (Drizzle) | `@requence/event-sourcing/drizzle/postgres` | Production |
|
|
72
|
+
|
|
73
|
+
## Documentation
|
|
74
|
+
|
|
75
|
+
Full documentation — including concept guides, a step-by-step tutorial, and API reference — is available at:
|
|
76
|
+
|
|
77
|
+
**[https://event-sourcing.docs.requence.cloud](https://event-sourcing.docs.requence.cloud)**
|
|
78
|
+
|
|
79
|
+
## License
|
|
80
|
+
|
|
81
|
+
[MIT](./LICENSE)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@requence/event-sourcing",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Event sourcing primitives — aggregate roots, projections, process managers, and event listeners",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -42,13 +42,14 @@
|
|
|
42
42
|
"files": [
|
|
43
43
|
"dist",
|
|
44
44
|
"src",
|
|
45
|
-
"LICENSE"
|
|
45
|
+
"LICENSE",
|
|
46
|
+
"README.md"
|
|
46
47
|
],
|
|
47
48
|
"scripts": {
|
|
48
49
|
"build": "bun run build.ts",
|
|
49
50
|
"test": "bun test",
|
|
50
51
|
"check-types": "tsc --noEmit",
|
|
51
|
-
"
|
|
52
|
+
"cs": "changeset",
|
|
52
53
|
"version": "changeset version",
|
|
53
54
|
"release": "bun run build && changeset publish",
|
|
54
55
|
"prepublishOnly": "bun run build"
|
|
@@ -81,6 +82,7 @@
|
|
|
81
82
|
"zod": "^4.3.6"
|
|
82
83
|
},
|
|
83
84
|
"license": "MIT",
|
|
85
|
+
"homepage": "https://event-sourcing.docs.requence.cloud",
|
|
84
86
|
"repository": {
|
|
85
87
|
"type": "git",
|
|
86
88
|
"url": "git+https://github.com/requence/event-sourcing.git"
|