eventsourcingdb 0.10.1 → 0.10.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 +50 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -32,3 +32,53 @@ If you want to verify the API token, call `verifyApiToken`. If the token is inva
|
|
|
32
32
|
```typescript
|
|
33
33
|
await client.verifyApiToken();
|
|
34
34
|
```
|
|
35
|
+
|
|
36
|
+
### Writing Events
|
|
37
|
+
|
|
38
|
+
Call the `writeEvents` function and hand over an array with one or more events. You do not have to provide all event fields – some are automatically added by the server.
|
|
39
|
+
|
|
40
|
+
Specify `source`, `subject`, `type`, and `data` according to the [CloudEvents](https://docs.eventsourcingdb.io/fundamentals/cloud-events/) format.
|
|
41
|
+
|
|
42
|
+
The function returns the written events, including the fields added by the server:
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
const writtenEvents = await client.writeEvents([
|
|
46
|
+
{
|
|
47
|
+
source: 'https://library.eventsourcingdb.io',
|
|
48
|
+
subject: '/books/42',
|
|
49
|
+
type: 'io.eventsourcingdb.library.book-acquired',
|
|
50
|
+
data: {
|
|
51
|
+
title: '2001 – A Space Odyssey',
|
|
52
|
+
author: 'Arthur C. Clarke',
|
|
53
|
+
isbn: '978-0756906788'
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
]);
|
|
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
|
+
]);
|