Skip to content

Commit 732316f

Browse files
committed
fix: Add documentation on reading events.
1 parent b36d3ac commit 732316f

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,86 @@ const writtenEvents = await client.writeEvents([
8282
], [
8383
isSubjectOnEventId('/books/42', '0')
8484
]);
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.*

0 commit comments

Comments
 (0)