@anabranch/eventlog 0.1.2 → 0.1.3
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 +38 -104
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,130 +1,64 @@
|
|
|
1
1
|
# @anabranch/eventlog
|
|
2
2
|
|
|
3
|
-
Event log with
|
|
4
|
-
|
|
3
|
+
Event log with Task/Stream semantics for event-sourced systems with cursor-based
|
|
4
|
+
consumption.
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
A high-level event log abstraction that integrates with anabranch's Task and
|
|
7
|
+
Stream types for composable error handling, concurrent processing, and reliable
|
|
8
|
+
consumer resumption.
|
|
7
9
|
|
|
8
|
-
|
|
9
|
-
representing log operations as **Tasks**, you get first-class support for
|
|
10
|
-
retries, timeouts, and `AbortSignal` propagation out of the box.
|
|
11
|
-
|
|
12
|
-
This library provides a unified interface for appending and consuming events
|
|
13
|
-
across different storage backends while maintaining strict type safety and lazy
|
|
14
|
-
execution patterns.
|
|
15
|
-
|
|
16
|
-
## Features
|
|
17
|
-
|
|
18
|
-
- **Lazy Execution**: Operations return a `Task`. Nothing happens until you
|
|
19
|
-
`.run()`.
|
|
20
|
-
- **First-Class Cancellation**: Built-in `AbortSignal` merging across all
|
|
21
|
-
operations.
|
|
22
|
-
- **Cursor-Based**: Resume processing from any position with consumer groups.
|
|
23
|
-
- **At-Least-Once Delivery**: Manual acknowledgement gives you full control.
|
|
24
|
-
- **Pluggable Architecture**: Standardized adapter interface for any storage
|
|
25
|
-
backend.
|
|
26
|
-
|
|
27
|
-
## Installation
|
|
28
|
-
|
|
29
|
-
```bash
|
|
30
|
-
# JSR
|
|
31
|
-
jsr add @anabranch/eventlog
|
|
32
|
-
|
|
33
|
-
# Deno
|
|
34
|
-
deno add @anabranch/eventlog
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
## Quick Start
|
|
10
|
+
## Usage
|
|
38
11
|
|
|
39
12
|
```ts
|
|
40
13
|
import { createInMemory, EventLog } from "@anabranch/eventlog";
|
|
41
14
|
|
|
42
15
|
const connector = createInMemory();
|
|
43
|
-
|
|
44
|
-
// Compose your logic as a Task chain
|
|
45
|
-
const program = EventLog.connect(connector).flatMap((log) => {
|
|
46
|
-
return log
|
|
47
|
-
.append("users", { type: "signup", email: "alice@example.com" })
|
|
48
|
-
.tap(() => console.log("Event appended!"))
|
|
49
|
-
.flatMap(() => log.get("users", 0));
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
// Execute the task at the edge of your application
|
|
53
|
-
const result = await program.result();
|
|
54
|
-
|
|
55
|
-
if (result.type === "success") {
|
|
56
|
-
console.log("User at sequence 0:", result.value);
|
|
57
|
-
}
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
## Reliable Consumption
|
|
61
|
-
|
|
62
|
-
The `.consume()` method returns a Stream of batches. To guarantee at-least-once
|
|
63
|
-
delivery, you manually commit the cursor after successful processing.
|
|
64
|
-
|
|
65
|
-
```ts
|
|
66
16
|
const log = await EventLog.connect(connector).run();
|
|
67
17
|
|
|
68
|
-
|
|
69
|
-
|
|
18
|
+
// Append an event
|
|
19
|
+
await log.append("users", { type: "created", userId: 123 }).run();
|
|
20
|
+
|
|
21
|
+
// Consume events with cursor-based resumption
|
|
22
|
+
const { successes, errors } = await log
|
|
23
|
+
.consume("users", "my-processor", { batchSize: 10 })
|
|
24
|
+
.withConcurrency(5)
|
|
70
25
|
.map(async (batch) => {
|
|
71
|
-
// 1. Process your events
|
|
72
26
|
for (const event of batch.events) {
|
|
73
|
-
await
|
|
27
|
+
await handleEvent(event.data);
|
|
74
28
|
}
|
|
75
|
-
//
|
|
29
|
+
// Manual commit for at-least-once delivery
|
|
76
30
|
await log.commit(batch.topic, batch.consumerGroup, batch.cursor).run();
|
|
77
|
-
})
|
|
78
|
-
|
|
79
|
-
// Run the consumer
|
|
80
|
-
await consumer.run();
|
|
31
|
+
})
|
|
32
|
+
.partition();
|
|
81
33
|
```
|
|
82
34
|
|
|
83
|
-
##
|
|
84
|
-
|
|
85
|
-
### `EventLog.connect(adapter)`
|
|
86
|
-
|
|
87
|
-
Initializes the connection. Returns a
|
|
88
|
-
`Task<EventLog, EventLogConnectionFailed>`.
|
|
89
|
-
|
|
90
|
-
### `log.append(topic, data, options?)`
|
|
91
|
-
|
|
92
|
-
Appends an event. Options include `partitionKey` and `metadata`.\
|
|
93
|
-
Returns `Task<string, EventLogAppendFailed>`.
|
|
94
|
-
|
|
95
|
-
### `log.consume(topic, consumerGroup, options?)`
|
|
96
|
-
|
|
97
|
-
Returns a Stream of event batches. If a cursor is found for the group, it
|
|
98
|
-
resumes automatically.
|
|
99
|
-
|
|
100
|
-
### `log.commit(topic, consumerGroup, cursor)`
|
|
35
|
+
## Installation
|
|
101
36
|
|
|
102
|
-
|
|
37
|
+
```bash
|
|
38
|
+
# JSR
|
|
39
|
+
jsr add @anabranch/eventlog
|
|
103
40
|
|
|
104
|
-
|
|
41
|
+
# Deno
|
|
42
|
+
deno add @anabranch/eventlog
|
|
43
|
+
```
|
|
105
44
|
|
|
106
|
-
##
|
|
45
|
+
## Features
|
|
107
46
|
|
|
108
|
-
|
|
47
|
+
- **Cursor-Based Consumption**: Resume processing from any position without data
|
|
48
|
+
loss
|
|
49
|
+
- **At-Least-Once Delivery**: Manual commit gives you control over processing
|
|
50
|
+
guarantees
|
|
51
|
+
- **Task/Stream Integration**: Leverage Task's retry/timeout and Stream's error
|
|
52
|
+
collection
|
|
53
|
+
- **Multiple Adapters**: In-memory implementation included
|
|
109
54
|
|
|
110
|
-
|
|
111
|
-
const task = log.append("orders", data)
|
|
112
|
-
.retry({
|
|
113
|
-
attempts: 3,
|
|
114
|
-
delay: (n) => Math.pow(2, n) * 1000, // Exponential backoff
|
|
115
|
-
when: (err) => err instanceof EventLogAppendFailed,
|
|
116
|
-
})
|
|
117
|
-
.timeout(5000)
|
|
118
|
-
.recover((err) => {
|
|
119
|
-
console.error("Critical failure:", err);
|
|
120
|
-
return "FALLBACK_ID";
|
|
121
|
-
});
|
|
55
|
+
## API Reference
|
|
122
56
|
|
|
123
|
-
|
|
124
|
-
|
|
57
|
+
See
|
|
58
|
+
[generated documentation](https://frodi-karlsson.github.io/anabranch/eventlog)
|
|
59
|
+
for full API details.
|
|
125
60
|
|
|
126
61
|
## Related
|
|
127
62
|
|
|
128
|
-
- [@anabranch/
|
|
129
|
-
|
|
130
|
-
processing
|
|
63
|
+
- [@anabranch/anabranch](https://jsr.io/@anabranch/anabranch) - Core Task/Stream
|
|
64
|
+
primitives
|
package/package.json
CHANGED