eventsourcingdb 0.10.2 → 0.10.4
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 +110 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -55,3 +55,113 @@ const writtenEvents = await client.writeEvents([
|
|
|
55
55
|
}
|
|
56
56
|
]);
|
|
57
57
|
```
|
|
58
|
+
|
|
59
|
+
#### Using the `isSubjectPristine` precondition
|
|
60
|
+
|
|
61
|
+
If you only want to write events in case a subject (such as `/books/42`) does not yet have any events, import the `isSubjectPristine` function and pass it as the second argument as an array of preconditions:
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
import { isSubjectPristine } from 'eventsourcingdb';
|
|
65
|
+
|
|
66
|
+
const writtenEvents = await client.writeEvents([
|
|
67
|
+
// events
|
|
68
|
+
], [
|
|
69
|
+
isSubjectPristine('/books/42')
|
|
70
|
+
]);
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
#### Using the `isSubjectOnEventId` precondition
|
|
74
|
+
|
|
75
|
+
If you only want to write events in case the last event of a subject (such as `/books/42`) has a specific ID (e.g., `0`), import the `isSubjectOnEventId` function and pass it as an array of preconditions in the second argument:
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
import { isSubjectOnEventId } from 'eventsourcingdb';
|
|
79
|
+
|
|
80
|
+
const writtenEvents = await client.writeEvents([
|
|
81
|
+
// events
|
|
82
|
+
], [
|
|
83
|
+
isSubjectOnEventId('/books/42', '0')
|
|
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.*
|