eventsourcingdb 0.10.3 → 0.10.5
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 +187 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -82,3 +82,190 @@ const writtenEvents = await client.writeEvents([
|
|
|
82
82
|
], [
|
|
83
83
|
isSubjectOnEventId('/books/42', '0')
|
|
84
84
|
]);
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
*Note that according to the CloudEvents standard, event IDs must be of type string.*
|
|
88
|
+
|
|
89
|
+
### Reading Events
|
|
90
|
+
|
|
91
|
+
To read all events of a subject, call the `readEvents` function with the subject as the first argument and an options object as the second argument. Set the `recursive` option to `false`. This ensures that only events of the given subject are returned, not events of nested subjects.
|
|
92
|
+
|
|
93
|
+
The function returns an asynchronous iterator, which you can use e.g. inside a `for await` loop:
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
for await (const event of client.readEvents('/books/42', {
|
|
97
|
+
recursive: false
|
|
98
|
+
})) {
|
|
99
|
+
// ...
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
#### Reading From Subjects Recursively
|
|
104
|
+
|
|
105
|
+
If you want to read not only all the events of a subject, but also the events of all nested subjects, set the `recursive` option to `true`:
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
for await (const event of client.readEvents('/books/42', {
|
|
109
|
+
recursive: true
|
|
110
|
+
})) {
|
|
111
|
+
// ...
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
This also allows you to read *all* events ever written. To do so, provide `/` as the subject and set `recursive` to `true`, since all subjects are nested under the root subject.
|
|
116
|
+
|
|
117
|
+
#### Reading in Anti-Chronological Order
|
|
118
|
+
|
|
119
|
+
By default, events are read in chronological order. To read in anti-chronological order, provide the `order` option and set it to `antichronological`:
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
for await (const event of client.readEvents('/books/42', {
|
|
123
|
+
recursive: false,
|
|
124
|
+
order: 'antichronological'
|
|
125
|
+
})) {
|
|
126
|
+
// ...
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
*Note that you can also specify `chronological` to explicitly enforce the default order.*
|
|
131
|
+
|
|
132
|
+
#### Specifying Bounds
|
|
133
|
+
|
|
134
|
+
Sometimes you do not want to read all events, but only a range of events. For that, you can specify the `lowerBound` and `upperBound` options – either one of them or even both at the same time.
|
|
135
|
+
|
|
136
|
+
Specify the ID and whether to include or exclude it, for both the lower and upper bound:
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
for await (const event of client.readEvents('/books/42', {
|
|
140
|
+
recursive: false,
|
|
141
|
+
lowerBound: { id: '100', type: 'inclusive' },
|
|
142
|
+
upperBound: { id: '200', type: 'exclusive' }
|
|
143
|
+
})) {
|
|
144
|
+
// ...
|
|
145
|
+
}
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
#### Starting From the Latest Event of a Given Type
|
|
149
|
+
|
|
150
|
+
To read starting from the latest event of a given type, provide the `fromLatestEvent` option and specify the subject, the type, and how to proceed if no such event exists.
|
|
151
|
+
|
|
152
|
+
Possible options are `read-nothing`, which skips reading entirely, or `read-everything`, which effectively behaves as if `fromLatestEvent` was not specified:
|
|
153
|
+
|
|
154
|
+
```typescript
|
|
155
|
+
for await (const event of client.readEvents('/books/42', {
|
|
156
|
+
recursive: false,
|
|
157
|
+
fromLatestEvent: {
|
|
158
|
+
subject: '/books/42',
|
|
159
|
+
type: 'io.eventsourcingdb.library.book-borrowed',
|
|
160
|
+
ifEventIsMissing: 'read-everything'
|
|
161
|
+
}
|
|
162
|
+
})) {
|
|
163
|
+
// ...
|
|
164
|
+
}
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
*Note that `fromLatestEvent` and `lowerBound` can not be provided at the same time.*
|
|
168
|
+
|
|
169
|
+
#### Aborting Reading
|
|
170
|
+
|
|
171
|
+
If you need to abort reading use `break` or `return` within the `await for` loop. However, this only works if there is currently an iteration going on.
|
|
172
|
+
|
|
173
|
+
To abort reading independently of that, hand over an abort signal as third parameter when calling `readEvents`, and abort the appropriate `AbortController`:
|
|
174
|
+
|
|
175
|
+
```typescript
|
|
176
|
+
const controller = new AbortController();
|
|
177
|
+
|
|
178
|
+
for await (const event of client.readEvents('/books/42', {
|
|
179
|
+
recursive: false
|
|
180
|
+
}, controller.signal)) {
|
|
181
|
+
// ...
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Somewhere else, abort the controller, which will cause
|
|
185
|
+
// reading to end.
|
|
186
|
+
controller.abort();
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### Observing Events
|
|
190
|
+
|
|
191
|
+
To observe all events of a subject, call the `observeEvents` function with the subject as the first argument and an options object as the second argument. Set the `recursive` option to `false`. This ensures that only events of the given subject are returned, not events of nested subjects.
|
|
192
|
+
|
|
193
|
+
The function returns an asynchronous iterator, which you can use e.g. inside a `for await` loop:
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
for await (const event of client.observeEvents('/books/42', {
|
|
197
|
+
recursive: false
|
|
198
|
+
})) {
|
|
199
|
+
// ...
|
|
200
|
+
}
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
#### Observing From Subjects Recursively
|
|
204
|
+
|
|
205
|
+
If you want to observe not only all the events of a subject, but also the events of all nested subjects, set the `recursive` option to `true`:
|
|
206
|
+
|
|
207
|
+
```typescript
|
|
208
|
+
for await (const event of client.observeEvents('/books/42', {
|
|
209
|
+
recursive: true
|
|
210
|
+
})) {
|
|
211
|
+
// ...
|
|
212
|
+
}
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
This also allows you to observe *all* events ever written. To do so, provide `/` as the subject and set `recursive` to `true`, since all subjects are nested under the root subject.
|
|
216
|
+
|
|
217
|
+
#### Specifying Bounds
|
|
218
|
+
|
|
219
|
+
Sometimes you do not want to observe all events, but only a range of events. For that, you can specify the `lowerBound` option.
|
|
220
|
+
|
|
221
|
+
Specify the ID and whether to include or exclude it:
|
|
222
|
+
|
|
223
|
+
```typescript
|
|
224
|
+
for await (const event of client.observeEvents('/books/42', {
|
|
225
|
+
recursive: false,
|
|
226
|
+
lowerBound: { id: '100', type: 'inclusive' }
|
|
227
|
+
})) {
|
|
228
|
+
// ...
|
|
229
|
+
}
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
#### Starting From the Latest Event of a Given Type
|
|
233
|
+
|
|
234
|
+
To observe starting from the latest event of a given type, provide the `fromLatestEvent` option and specify the subject, the type, and how to proceed if no such event exists.
|
|
235
|
+
|
|
236
|
+
Possible options are `wait-for-event`, which waits for an event of the given type to happen, or `read-everything`, which effectively behaves as if `fromLatestEvent` was not specified:
|
|
237
|
+
|
|
238
|
+
```typescript
|
|
239
|
+
for await (const event of client.observeEvents('/books/42', {
|
|
240
|
+
recursive: false,
|
|
241
|
+
fromLatestEvent: {
|
|
242
|
+
subject: '/books/42',
|
|
243
|
+
type: 'io.eventsourcingdb.library.book-borrowed',
|
|
244
|
+
ifEventIsMissing: 'read-everything'
|
|
245
|
+
}
|
|
246
|
+
})) {
|
|
247
|
+
// ...
|
|
248
|
+
}
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
*Note that `fromLatestEvent` and `lowerBound` can not be provided at the same time.*
|
|
252
|
+
|
|
253
|
+
#### Aborting Observing
|
|
254
|
+
|
|
255
|
+
If you need to abort observing use `break` or `return` within the `await for` loop. However, this only works if there is currently an iteration going on.
|
|
256
|
+
|
|
257
|
+
To abort observing independently of that, hand over an abort signal as third parameter when calling `observeEvents`, and abort the appropriate `AbortController`:
|
|
258
|
+
|
|
259
|
+
```typescript
|
|
260
|
+
const controller = new AbortController();
|
|
261
|
+
|
|
262
|
+
for await (const event of client.observeEvents('/books/42', {
|
|
263
|
+
recursive: false
|
|
264
|
+
}, controller.signal)) {
|
|
265
|
+
// ...
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// Somewhere else, abort the controller, which will cause
|
|
269
|
+
// observing to end.
|
|
270
|
+
controller.abort();
|
|
271
|
+
```
|