tapeworm 0.4.1 → 0.6.0-beta.0
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/.turbo/turbo-build.log +6 -0
- package/.turbo/turbo-test.log +33 -0
- package/CHANGELOG.md +13 -0
- package/LICENSE +22 -22
- package/README.md +332 -4
- package/biome.json +58 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/src/dispatch/dispatch_service.d.ts +8 -0
- package/dist/src/dispatch/dispatch_service.d.ts.map +1 -0
- package/dist/src/dispatch/dispatch_service.js +9 -0
- package/dist/src/dispatch/dispatch_service.js.map +1 -0
- package/dist/src/event.d.ts +10 -0
- package/dist/src/event.d.ts.map +1 -0
- package/dist/src/event.js +12 -0
- package/dist/src/event.js.map +1 -0
- package/dist/src/event_store.d.ts +29 -0
- package/dist/src/event_store.d.ts.map +1 -0
- package/dist/src/event_store.js +71 -0
- package/dist/src/event_store.js.map +1 -0
- package/dist/src/event_store_partition.d.ts +28 -0
- package/dist/src/event_store_partition.d.ts.map +1 -0
- package/dist/src/event_store_partition.js +130 -0
- package/dist/src/event_store_partition.js.map +1 -0
- package/dist/src/event_stream.d.ts +32 -0
- package/dist/src/event_stream.d.ts.map +1 -0
- package/dist/src/event_stream.js +142 -0
- package/dist/src/event_stream.js.map +1 -0
- package/dist/src/persistence/commit.d.ts +4 -0
- package/dist/src/persistence/commit.d.ts.map +1 -0
- package/dist/src/persistence/commit.js +11 -0
- package/dist/src/persistence/commit.js.map +1 -0
- package/dist/src/persistence/concurrency_error.d.ts +6 -0
- package/dist/src/persistence/concurrency_error.d.ts.map +1 -0
- package/dist/src/persistence/concurrency_error.js +13 -0
- package/dist/src/persistence/concurrency_error.js.map +1 -0
- package/dist/src/persistence/duplicate_commit_error.d.ts +6 -0
- package/dist/src/persistence/duplicate_commit_error.d.ts.map +1 -0
- package/dist/src/persistence/duplicate_commit_error.js +13 -0
- package/dist/src/persistence/duplicate_commit_error.js.map +1 -0
- package/dist/src/persistence/inmemory/inmemory_partition.d.ts +23 -0
- package/dist/src/persistence/inmemory/inmemory_partition.d.ts.map +1 -0
- package/dist/src/persistence/inmemory/inmemory_partition.js +138 -0
- package/dist/src/persistence/inmemory/inmemory_partition.js.map +1 -0
- package/dist/src/persistence/inmemory/inmemory_persistence.d.ts +13 -0
- package/dist/src/persistence/inmemory/inmemory_persistence.d.ts.map +1 -0
- package/dist/src/persistence/inmemory/inmemory_persistence.js +26 -0
- package/dist/src/persistence/inmemory/inmemory_persistence.js.map +1 -0
- package/dist/src/persistence/persistence_partition.d.ts +12 -0
- package/dist/src/persistence/persistence_partition.d.ts.map +1 -0
- package/dist/src/persistence/persistence_partition.js +21 -0
- package/dist/src/persistence/persistence_partition.js.map +1 -0
- package/dist/src/persistence/persistence_provider_template.d.ts +10 -0
- package/dist/src/persistence/persistence_provider_template.d.ts.map +1 -0
- package/dist/src/persistence/persistence_provider_template.js +11 -0
- package/dist/src/persistence/persistence_provider_template.js.map +1 -0
- package/dist/src/replication/master_replication_service.d.ts +7 -0
- package/dist/src/replication/master_replication_service.d.ts.map +1 -0
- package/dist/src/replication/master_replication_service.js +9 -0
- package/dist/src/replication/master_replication_service.js.map +1 -0
- package/dist/src/types.d.ts +68 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +3 -0
- package/dist/src/types.js.map +1 -0
- package/index.ts +21 -0
- package/package.json +44 -46
- package/scripts/postbuild.js +27 -0
- package/src/dispatch/dispatch_service.ts +14 -0
- package/src/event.ts +24 -0
- package/src/event_store.ts +82 -0
- package/src/event_store_partition.ts +270 -0
- package/src/event_stream.ts +184 -0
- package/src/persistence/commit.ts +24 -0
- package/src/persistence/concurrency_error.ts +16 -0
- package/src/persistence/duplicate_commit_error.ts +16 -0
- package/src/persistence/inmemory/inmemory_partition.ts +194 -0
- package/src/persistence/inmemory/inmemory_persistence.ts +35 -0
- package/src/persistence/persistence_partition.ts +34 -0
- package/src/persistence/persistence_provider_template.ts +20 -0
- package/src/replication/master_replication_service.ts +12 -0
- package/src/types.ts +128 -0
- package/test/event_partition.ts +151 -0
- package/test/event_store.ts +25 -0
- package/test/event_stream.ts +160 -0
- package/test/persistence/inmemory.ts +139 -0
- package/tsconfig.json +13 -0
- package/vitest.config.ts +13 -0
- package/.eslintrc +0 -4
- package/.travis.yml +0 -4
- package/checkstyle-result.xml +0 -1
- package/index.js +0 -1
- package/lib/dispatch/dispatch_service.js +0 -7
- package/lib/event.js +0 -10
- package/lib/event_store.js +0 -53
- package/lib/event_store_partition.js +0 -148
- package/lib/event_stream.js +0 -147
- package/lib/persistence/commit.js +0 -12
- package/lib/persistence/concurrency_error.js +0 -10
- package/lib/persistence/duplicate_commit_error.js +0 -10
- package/lib/persistence/inmemory/inmemory_partition.js +0 -146
- package/lib/persistence/inmemory/inmemory_persistence.js +0 -25
- package/lib/persistence/persistence_partition.js +0 -23
- package/lib/persistence/persistence_provider_template.js +0 -17
- package/lib/replication/master_replication_service.js +0 -7
- package/test/event_partition.js +0 -232
- package/test/event_store.js +0 -36
- package/test/event_stream.js +0 -214
- package/test/persistence/inmemory.js +0 -162
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
> tapeworm@0.6.0-beta.0 test
|
|
4
|
+
> vitest run
|
|
5
|
+
|
|
6
|
+
[?25l
|
|
7
|
+
[1m[46m RUN [49m[22m [36mv3.2.4 [39m[90m/home/kennyek/dev/surikaterna/tapeworm/packages/tapeworm[39m
|
|
8
|
+
|
|
9
|
+
[?2026h
|
|
10
|
+
[1m[33m ❯ [39m[22mtest/persistence/inmemory.ts[2m [queued][22m
|
|
11
|
+
|
|
12
|
+
[2m Test Files [22m[1m[32m0 passed[39m[22m[90m (4)[39m
|
|
13
|
+
[2m Tests [22m[1m[32m0 passed[39m[22m[90m (0)[39m
|
|
14
|
+
[2m Start at [22m13:12:03
|
|
15
|
+
[2m Duration [22m100ms
|
|
16
|
+
[?2026l[?2026h[K[1A[K[1A[K[1A[K[1A[K[1A[K[1A[K[1A[K [32m✓[39m test/event_partition.ts [2m([22m[2m10 tests[22m[2m)[22m[32m 7[2mms[22m[39m
|
|
17
|
+
[32m✓[39m test/event_store.ts [2m([22m[2m3 tests[22m[2m)[22m[32m 8[2mms[22m[39m
|
|
18
|
+
[32m✓[39m test/event_stream.ts [2m([22m[2m11 tests[22m[2m)[22m[32m 5[2mms[22m[39m
|
|
19
|
+
|
|
20
|
+
[1m[33m ❯ [39m[22mtest/persistence/inmemory.ts[2m 0/10[22m
|
|
21
|
+
|
|
22
|
+
[2m Test Files [22m[1m[32m3 passed[39m[22m[90m (4)[39m
|
|
23
|
+
[2m Tests [22m[1m[32m24 passed[39m[22m[90m (34)[39m
|
|
24
|
+
[2m Start at [22m13:12:03
|
|
25
|
+
[2m Duration [22m201ms
|
|
26
|
+
[?2026l[K[1A[K[1A[K[1A[K[1A[K[1A[K[1A[K[1A[K [32m✓[39m test/persistence/inmemory.ts [2m([22m[2m10 tests[22m[2m)[22m[32m 6[2mms[22m[39m
|
|
27
|
+
|
|
28
|
+
[2m Test Files [22m [1m[32m4 passed[39m[22m[90m (4)[39m
|
|
29
|
+
[2m Tests [22m [1m[32m34 passed[39m[22m[90m (34)[39m
|
|
30
|
+
[2m Start at [22m 13:12:03
|
|
31
|
+
[2m Duration [22m 270ms[2m (transform 112ms, setup 0ms, collect 324ms, tests 26ms, environment 0ms, prepare 161ms)[22m
|
|
32
|
+
|
|
33
|
+
[?25h⠙[1G[0K
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# tapeworm
|
|
2
|
+
|
|
3
|
+
## 0.6.0-beta.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 855c605: Convert monorepo to TypeScript with full type definitions
|
|
8
|
+
|
|
9
|
+
- Convert all packages from JavaScript to TypeScript with complete type annotations
|
|
10
|
+
- Add shared type system (IBaseEvent, ICommit, ISnapshot, IPersistencePartition)
|
|
11
|
+
- Migrate all test suites to vitest
|
|
12
|
+
- Add CJS backward-compatibility shims to preserve existing require() API
|
|
13
|
+
- Fix \_partitions initialization bug and indexeddb loadSnapshot error handler
|
package/LICENSE
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
The MIT License (MIT)
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2015 spralle
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
22
|
-
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2015 spralle
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
package/README.md
CHANGED
|
@@ -1,4 +1,332 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
Tapeworm
|
|
2
|
+
========
|
|
3
|
+
|
|
4
|
+
[Tape Write Once, Read Many](https://github.com/surikaterna/tapeworm) is a library for event sourcing for in Node.
|
|
5
|
+
|
|
6
|
+
* [Purpose](#purpose)
|
|
7
|
+
* [Installation](#installation)
|
|
8
|
+
* [Usage](#usage)
|
|
9
|
+
* [In Memory](#in-memory)
|
|
10
|
+
* [Custom Partition](#custom-partition)
|
|
11
|
+
* [Components](#components)
|
|
12
|
+
* [EventStore](#eventstore)
|
|
13
|
+
* [Methods](#methods)
|
|
14
|
+
* [openPartition](#openpartition)
|
|
15
|
+
* [EventStorePartition](#eventstorepartition)
|
|
16
|
+
* [Methods](#methods-1)
|
|
17
|
+
* [openStream](#openstream)
|
|
18
|
+
* [append](#append)
|
|
19
|
+
* [delete](#delete)
|
|
20
|
+
* [Persistence Partition Wrapper Methods](#persistence-partition-wrapper-methods)
|
|
21
|
+
* [queryStreamWithSnapshot](#querystreamwithsnapshot)
|
|
22
|
+
* [storeSnapshot](#storesnapshot)
|
|
23
|
+
* [loadSnapshot](#loadsnapshot)
|
|
24
|
+
* [queryStream](#querystream)
|
|
25
|
+
* [removeSnapshot](#removesnapshot)
|
|
26
|
+
* [getLatestCommit](#getlatestcommit)
|
|
27
|
+
* [querySnapshotsByMaxDateTime](#querysnapshotsbymaxdatetime)
|
|
28
|
+
* [EventStream](#eventstream)
|
|
29
|
+
* [Methods](#methods-2)
|
|
30
|
+
* [getVersion](#getversion)
|
|
31
|
+
* [append](#append-1)
|
|
32
|
+
* [hasChanges](#haschanges)
|
|
33
|
+
* [commit](#commit)
|
|
34
|
+
* [revertChanges](#revertchanges)
|
|
35
|
+
* [getCommittedEvents](#getcommittedevents)
|
|
36
|
+
* [getUncommittedEvents](#getuncommittedevents)
|
|
37
|
+
* [Commit](#commit-1)
|
|
38
|
+
* [Event](#event)
|
|
39
|
+
|
|
40
|
+
# Purpose
|
|
41
|
+
|
|
42
|
+
Tapeworm is an event store, configurable with an external persistence partition. By default it provides its own in
|
|
43
|
+
memory persistence partition, but it can be exchanged by e.g.
|
|
44
|
+
a [MongoDB persistence partition](https://github.com/surikaterna/tapeworm_persistence_store_mongodb) or
|
|
45
|
+
an [IndexedDB persistence partition](https://github.com/surikaterna/tapeworm_persistence_store_indexdb).
|
|
46
|
+
|
|
47
|
+
# Installation
|
|
48
|
+
|
|
49
|
+
```shell
|
|
50
|
+
npm install tapeworm
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
# Usage
|
|
54
|
+
|
|
55
|
+
## In Memory
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
import EventStore from 'tapeworm';
|
|
59
|
+
|
|
60
|
+
// A singleton Tapeworm EventStore should be instantiated somewhere
|
|
61
|
+
const eventStore = new EventStore();
|
|
62
|
+
|
|
63
|
+
// Opening the same partition multiple times returns the same Partition instance
|
|
64
|
+
const partition = await eventStore.openPartition('location');
|
|
65
|
+
|
|
66
|
+
const firstCommit = new Commit('1', 'location', '1', 0, []);
|
|
67
|
+
const secondCommit = new Commit('2', 'location', '1', 1, []);
|
|
68
|
+
const thirdCommit = new Commit('3', 'location', '1', 2, []);
|
|
69
|
+
|
|
70
|
+
// When all commits have ben persisted, the commits are returned
|
|
71
|
+
const commits = await partition.append([firstCommit, secondCommit]);
|
|
72
|
+
// [firstCommit, secondCommit]
|
|
73
|
+
|
|
74
|
+
// Single commits are returned as an array of one item
|
|
75
|
+
const addedCommits = await partition.append(thirdCommit);
|
|
76
|
+
// [thirdCommit]
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Custom Partition
|
|
80
|
+
|
|
81
|
+
This example uses
|
|
82
|
+
the [MongoDB persistence partition](https://github.com/surikaterna/tapeworm_persistence_store_mongodb), but a custom
|
|
83
|
+
persistence partition can be implemented and used as needed.
|
|
84
|
+
|
|
85
|
+
```js
|
|
86
|
+
const dispatchService = (commit) => {
|
|
87
|
+
// Commit dispatched from the custom store, handle it as necessary
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const eventStore = new EventStore(tapewormMdbStore, dispatchService);
|
|
91
|
+
const partition = await eventStore.openPartition('location');
|
|
92
|
+
|
|
93
|
+
const commits = [
|
|
94
|
+
new Commit('1', 'location', '1', 0, []),
|
|
95
|
+
new Commit('2', 'location', '1', 1, [])
|
|
96
|
+
];
|
|
97
|
+
|
|
98
|
+
// Will call dispatchService twice, once for each commit when handled by the custom partition
|
|
99
|
+
await partition.append(commits);
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
# Components
|
|
103
|
+
|
|
104
|
+
## EventStore
|
|
105
|
+
|
|
106
|
+
The TapeWORM EventStore is exported as default. It takes an optional custom partition and an optional dispatch service
|
|
107
|
+
and holds a collection of [event store partitions](#eventstorepartition) opened.
|
|
108
|
+
|
|
109
|
+
```js
|
|
110
|
+
import EventStore from 'tapeworm';
|
|
111
|
+
|
|
112
|
+
// A singleton TapeWORM EventStore should be instantiated somewhere
|
|
113
|
+
const eventStore = new EventStore();
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### Methods
|
|
117
|
+
|
|
118
|
+
#### openPartition
|
|
119
|
+
|
|
120
|
+
Resolves an [EventStorePartition](#eventstorepartition) when the persistence store has successfully opened its
|
|
121
|
+
persistence partition.
|
|
122
|
+
|
|
123
|
+
```js
|
|
124
|
+
const partition = await eventStore.openPartition();
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### EventStorePartition
|
|
128
|
+
|
|
129
|
+
A wrapper around the persistence partition.
|
|
130
|
+
|
|
131
|
+
### Methods
|
|
132
|
+
|
|
133
|
+
#### openStream
|
|
134
|
+
|
|
135
|
+
Resolves an [EventStream](#eventstream) when it has properly loaded all commits.
|
|
136
|
+
|
|
137
|
+
```js
|
|
138
|
+
const eventStream = await partition.openStream(streamId, writeOnly, callback);
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
#### append
|
|
142
|
+
|
|
143
|
+
Takes a commit or a list of commits and appends them on the persistence partition. Will call the dispatch service, if
|
|
144
|
+
provided, once per appended commit.
|
|
145
|
+
|
|
146
|
+
```js
|
|
147
|
+
const appendedCommits = await partition.append(commits, callback);
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
#### delete
|
|
151
|
+
|
|
152
|
+
Commit a stream deleted event to indicate that all related data for the stream shall be removed.
|
|
153
|
+
|
|
154
|
+
```js
|
|
155
|
+
await partition.delete(streamId, deleteEvent);
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### Persistence Partition Wrapper Methods
|
|
159
|
+
|
|
160
|
+
The EventStorePartition will provide methods for calling the following methods directly on the persistence partition,
|
|
161
|
+
providing the provided arguments.
|
|
162
|
+
|
|
163
|
+
#### queryStreamWithSnapshot
|
|
164
|
+
|
|
165
|
+
Has a fallback implementation assuming that the persistence partition used has the [loadSnapshot](#loadsnapshot)
|
|
166
|
+
and [queryStream](#querystream) methods implemented.
|
|
167
|
+
|
|
168
|
+
#### storeSnapshot
|
|
169
|
+
|
|
170
|
+
Store a snapshot for a stream and resolve data for the stored snapshot.
|
|
171
|
+
|
|
172
|
+
```js
|
|
173
|
+
const snapshotData = await partition.storeSnapshot(streamId, snapshot, version, callback);
|
|
174
|
+
// Result: { id, version, snapshot }
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
#### loadSnapshot
|
|
178
|
+
|
|
179
|
+
Retrieve the stored snapshot of a stream.
|
|
180
|
+
|
|
181
|
+
```js
|
|
182
|
+
const snapshotData = await partition.loadSnapshot(streamId, callback);
|
|
183
|
+
// Result: { id, version, snapshot }
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
#### queryStream
|
|
187
|
+
|
|
188
|
+
Retrieve the commits for a stream.
|
|
189
|
+
|
|
190
|
+
```js
|
|
191
|
+
const snapshotData = await partition.queryStream(streamId, fromEventSequence, callback);
|
|
192
|
+
// Result: [{ id, streamId, commitSequence, events }, { id, streamId, commitSequence, events }]
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
#### removeSnapshot
|
|
196
|
+
|
|
197
|
+
Remove the stored snapshot of a stream.
|
|
198
|
+
|
|
199
|
+
```js
|
|
200
|
+
await partition.removeSnapshot(streamId, callback);
|
|
201
|
+
// Result: { id, version, snapshot }
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
#### getLatestCommit
|
|
205
|
+
|
|
206
|
+
Retrieve the last stored commit for a stream.
|
|
207
|
+
|
|
208
|
+
```js
|
|
209
|
+
const commit = await partition.getLatestCommit(streamId, callback);
|
|
210
|
+
// Result: { id, streamId, commitSequence, events }
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
#### querySnapshotsByMaxDateTime
|
|
214
|
+
|
|
215
|
+
Retrieve the stored snapshots older than the provided date string.
|
|
216
|
+
|
|
217
|
+
```js
|
|
218
|
+
const snapshots = await partition.querySnapshotsByMaxDateTime(dateTime, callback);
|
|
219
|
+
// Result: [{ id }, { id }]
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## EventStream
|
|
223
|
+
|
|
224
|
+
### Methods
|
|
225
|
+
|
|
226
|
+
#### getVersion
|
|
227
|
+
|
|
228
|
+
Retrieves the current version of the stream.
|
|
229
|
+
|
|
230
|
+
```js
|
|
231
|
+
const version = eventStream.getVersion();
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
#### append
|
|
235
|
+
|
|
236
|
+
Add an [Event](#event) to the streams list of uncommitted (planned) events.
|
|
237
|
+
|
|
238
|
+
```js
|
|
239
|
+
eventStream.append(event);
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
#### hasChanges
|
|
243
|
+
|
|
244
|
+
Returns whether there are uncommitted events on the stream.
|
|
245
|
+
|
|
246
|
+
```js
|
|
247
|
+
const hasChanges = eventStream.hasChanges();
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
#### commit
|
|
251
|
+
|
|
252
|
+
Build a [commit](#commit) from the uncommitted [events](#event) and append it to
|
|
253
|
+
the [event store partition](#eventstorepartition).
|
|
254
|
+
|
|
255
|
+
```js
|
|
256
|
+
await eventStream.commit(commitId, callback);
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
#### revertChanges
|
|
260
|
+
|
|
261
|
+
Remove the uncommitted events from the stream to prevent the changes from being committed.
|
|
262
|
+
|
|
263
|
+
```js
|
|
264
|
+
eventStream.revertChanges();
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
#### getCommittedEvents
|
|
268
|
+
|
|
269
|
+
Returns a copy of the committed events for the stream.
|
|
270
|
+
|
|
271
|
+
Throws an error if the stream is created as _write only_.
|
|
272
|
+
|
|
273
|
+
```js
|
|
274
|
+
const events = eventStream.getCommittedEvents();
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
#### getUncommittedEvents
|
|
278
|
+
|
|
279
|
+
Returns a copy of the uncommitted events appended to the stream.
|
|
280
|
+
|
|
281
|
+
```js
|
|
282
|
+
const events = eventStream.getUncommittedEvents();
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
## Commit
|
|
286
|
+
|
|
287
|
+
Creates a new Commit instance.
|
|
288
|
+
|
|
289
|
+
```js
|
|
290
|
+
const commit = new Commit(id, partitionId, streamId, commitSequence, events);
|
|
291
|
+
|
|
292
|
+
// commit.id: id
|
|
293
|
+
// commit.partitionId: partitionId
|
|
294
|
+
// commit.streamId: streamId
|
|
295
|
+
// commit.commitSequence: commitSequence
|
|
296
|
+
// commit.events: events
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
## Event
|
|
300
|
+
|
|
301
|
+
Creates a new Event instance.
|
|
302
|
+
|
|
303
|
+
```js
|
|
304
|
+
const event = new Event(id, type, data, metadata);
|
|
305
|
+
|
|
306
|
+
// event.id: id
|
|
307
|
+
// event.type: type
|
|
308
|
+
// event.data: data
|
|
309
|
+
// event.metadata: metadata (defaults to {})
|
|
310
|
+
// event.timestamps: Date (date of creation)
|
|
311
|
+
// event.revision: null (to be updated by the event store when appended)
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
## Dispatch Service
|
|
315
|
+
|
|
316
|
+
The Tapeworm event store takes a dispatch service as an optional second argument. If provided, it will be called with
|
|
317
|
+
the commit as payload once it has been processed by the persistence partition.
|
|
318
|
+
|
|
319
|
+
```js
|
|
320
|
+
function dispatchService(commit, markAsDispatched) {
|
|
321
|
+
// Distribute information about the processed commit
|
|
322
|
+
// ...
|
|
323
|
+
|
|
324
|
+
markAsDispatched();
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
const eventStore = new EventStore(null, dispatchService);
|
|
328
|
+
const partition = await eventStore.openPartition('location');
|
|
329
|
+
|
|
330
|
+
// Will call dispatchService when the commit is processed
|
|
331
|
+
partition.append(new Commit('4', 'location', '1', 3, []));
|
|
332
|
+
```
|
package/biome.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://biomejs.dev/schemas/2.2.4/schema.json",
|
|
3
|
+
"vcs": {
|
|
4
|
+
"enabled": true,
|
|
5
|
+
"clientKind": "git",
|
|
6
|
+
"useIgnoreFile": true
|
|
7
|
+
},
|
|
8
|
+
"files": {
|
|
9
|
+
"ignoreUnknown": false
|
|
10
|
+
},
|
|
11
|
+
"formatter": {
|
|
12
|
+
"enabled": true,
|
|
13
|
+
"indentStyle": "space",
|
|
14
|
+
"indentWidth": 2,
|
|
15
|
+
"lineWidth": 160
|
|
16
|
+
},
|
|
17
|
+
"linter": {
|
|
18
|
+
"enabled": true,
|
|
19
|
+
"rules": {
|
|
20
|
+
"recommended": true,
|
|
21
|
+
"complexity": {
|
|
22
|
+
"noArguments": "off",
|
|
23
|
+
"noUselessThisAlias": "off",
|
|
24
|
+
"useArrowFunction": "off",
|
|
25
|
+
"useOptionalChain": "off"
|
|
26
|
+
},
|
|
27
|
+
"correctness": {
|
|
28
|
+
"noInnerDeclarations": "off",
|
|
29
|
+
"noInvalidUseBeforeDeclaration": "off",
|
|
30
|
+
"noUnusedFunctionParameters": "off",
|
|
31
|
+
"noUnusedVariables": "off"
|
|
32
|
+
},
|
|
33
|
+
"style": {
|
|
34
|
+
"useNodejsImportProtocol": "off",
|
|
35
|
+
"useTemplate": "off"
|
|
36
|
+
},
|
|
37
|
+
"suspicious": {
|
|
38
|
+
"noAssignInExpressions": "off",
|
|
39
|
+
"noDoubleEquals": "off",
|
|
40
|
+
"noRedeclare": "off",
|
|
41
|
+
"noShadowRestrictedNames": "off"
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"javascript": {
|
|
46
|
+
"formatter": {
|
|
47
|
+
"trailingCommas": "es5"
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"assist": {
|
|
51
|
+
"enabled": true,
|
|
52
|
+
"actions": {
|
|
53
|
+
"source": {
|
|
54
|
+
"organizeImports": "on"
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import EventStore from "./src/event_store";
|
|
2
|
+
export { Commit, ConcurrencyError, DuplicateCommitError, Event, EventStorePartition, EventStream, } from "./src/event_store";
|
|
3
|
+
export type { DispatchHandler, IBaseEvent, ICommit, IPersistencePartition, IPersistenceProvider, ISnapshot, NodeCallback, } from "./src/types";
|
|
4
|
+
export default EventStore;
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,mBAAmB,CAAC;AAE3C,OAAO,EACL,MAAM,EACN,gBAAgB,EAChB,oBAAoB,EACpB,KAAK,EACL,mBAAmB,EACnB,WAAW,GACZ,MAAM,mBAAmB,CAAC;AAE3B,YAAY,EACV,eAAe,EACf,UAAU,EACV,OAAO,EACP,qBAAqB,EACrB,oBAAoB,EACpB,SAAS,EACT,YAAY,GACb,MAAM,aAAa,CAAC;AACrB,eAAe,UAAU,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.EventStream = exports.EventStorePartition = exports.Event = exports.DuplicateCommitError = exports.ConcurrencyError = exports.Commit = void 0;
|
|
7
|
+
const event_store_1 = __importDefault(require("./src/event_store"));
|
|
8
|
+
var event_store_2 = require("./src/event_store");
|
|
9
|
+
Object.defineProperty(exports, "Commit", { enumerable: true, get: function () { return event_store_2.Commit; } });
|
|
10
|
+
Object.defineProperty(exports, "ConcurrencyError", { enumerable: true, get: function () { return event_store_2.ConcurrencyError; } });
|
|
11
|
+
Object.defineProperty(exports, "DuplicateCommitError", { enumerable: true, get: function () { return event_store_2.DuplicateCommitError; } });
|
|
12
|
+
Object.defineProperty(exports, "Event", { enumerable: true, get: function () { return event_store_2.Event; } });
|
|
13
|
+
Object.defineProperty(exports, "EventStorePartition", { enumerable: true, get: function () { return event_store_2.EventStorePartition; } });
|
|
14
|
+
Object.defineProperty(exports, "EventStream", { enumerable: true, get: function () { return event_store_2.EventStream; } });
|
|
15
|
+
exports.default = event_store_1.default;
|
|
16
|
+
//# sourceMappingURL=index.js.map
|
|
17
|
+
// CJS backward compat — added by scripts/postbuild.js
|
|
18
|
+
module.exports = exports.default;
|
|
19
|
+
Object.assign(module.exports, exports);
|
|
20
|
+
module.exports.default = exports.default;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;;AAAA,oEAA2C;AAE3C,iDAO2B;AANzB,qGAAA,MAAM,OAAA;AACN,+GAAA,gBAAgB,OAAA;AAChB,mHAAA,oBAAoB,OAAA;AACpB,oGAAA,KAAK,OAAA;AACL,kHAAA,mBAAmB,OAAA;AACnB,0GAAA,WAAW,OAAA;AAYb,kBAAe,qBAAU,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ICommit, NodeCallback } from "../types";
|
|
2
|
+
interface IDispatchService {
|
|
3
|
+
dispatch(_commit: ICommit, _callback?: NodeCallback<void>): void;
|
|
4
|
+
}
|
|
5
|
+
/** Abstract template — subclasses must override dispatch. */
|
|
6
|
+
declare var DispatchService: new () => IDispatchService;
|
|
7
|
+
export default DispatchService;
|
|
8
|
+
//# sourceMappingURL=dispatch_service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dispatch_service.d.ts","sourceRoot":"","sources":["../../../src/dispatch/dispatch_service.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAEtD,UAAU,gBAAgB;IACxB,QAAQ,CAAC,OAAO,EAAE,OAAO,EAAE,SAAS,CAAC,EAAE,YAAY,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;CAClE;AAED,6DAA6D;AAC7D,QAAA,IAAI,eAAe,EAAgC,UAAU,gBAAgB,CAAC;AAM9E,eAAe,eAAe,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/** Abstract template — subclasses must override dispatch. */
|
|
4
|
+
var DispatchService = function () { };
|
|
5
|
+
DispatchService.prototype.dispatch = function (_commit, _callback) {
|
|
6
|
+
// body...
|
|
7
|
+
};
|
|
8
|
+
exports.default = DispatchService;
|
|
9
|
+
//# sourceMappingURL=dispatch_service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dispatch_service.js","sourceRoot":"","sources":["../../../src/dispatch/dispatch_service.ts"],"names":[],"mappings":";;AAMA,6DAA6D;AAC7D,IAAI,eAAe,GAAG,cAAa,CAA0C,CAAC;AAE9E,eAAe,CAAC,SAAS,CAAC,QAAQ,GAAG,UAAU,OAAgB,EAAE,SAA8B;IAC7F,UAAU;AACZ,CAAC,CAAC;AAEF,kBAAe,eAAe,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { IBaseEvent } from "./types";
|
|
2
|
+
interface IEvent extends IBaseEvent {
|
|
3
|
+
payload: Record<string, unknown>;
|
|
4
|
+
timestamp: Date;
|
|
5
|
+
metadata: Record<string, unknown>;
|
|
6
|
+
revision: number | null;
|
|
7
|
+
}
|
|
8
|
+
declare var Event: new (id: string, type: string, payload: Record<string, unknown>, metadata?: Record<string, unknown>) => IEvent;
|
|
9
|
+
export default Event;
|
|
10
|
+
//# sourceMappingURL=event.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"event.d.ts","sourceRoot":"","sources":["../../src/event.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAE1C,UAAU,MAAO,SAAQ,UAAU;IACjC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACjC,SAAS,EAAE,IAAI,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAClC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;CACzB;AAED,QAAA,IAAI,KAAK,EAOO,KACd,EAAE,EAAE,MAAM,EACV,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAChC,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAC/B,MAAM,CAAC;AAEZ,eAAe,KAAK,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var Event = function (id, type, payload, metadata) {
|
|
4
|
+
this.id = id;
|
|
5
|
+
this.type = type;
|
|
6
|
+
this.payload = payload;
|
|
7
|
+
this.timestamp = new Date();
|
|
8
|
+
this.metadata = metadata || {};
|
|
9
|
+
this.revision = null; //filled by eventstore on append
|
|
10
|
+
};
|
|
11
|
+
exports.default = Event;
|
|
12
|
+
//# sourceMappingURL=event.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"event.js","sourceRoot":"","sources":["../../src/event.ts"],"names":[],"mappings":";;AASA,IAAI,KAAK,GAAG,UAAwB,EAAU,EAAE,IAAY,EAAE,OAAgC,EAAE,QAAkC;IAChI,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;IACb,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACjB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACvB,IAAI,CAAC,SAAS,GAAG,IAAI,IAAI,EAAE,CAAC;IAC5B,IAAI,CAAC,QAAQ,GAAG,QAAQ,IAAI,EAAE,CAAC;IAC/B,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,CAAC,gCAAgC;AACxD,CAKW,CAAC;AAEZ,kBAAe,KAAK,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import Promise from "bluebird";
|
|
2
|
+
import Event from "./event";
|
|
3
|
+
import Partition from "./event_store_partition";
|
|
4
|
+
import EventStream from "./event_stream";
|
|
5
|
+
import Commit from "./persistence/commit";
|
|
6
|
+
import ConcurrencyError from "./persistence/concurrency_error";
|
|
7
|
+
import DuplicateCommitError from "./persistence/duplicate_commit_error";
|
|
8
|
+
import type { DispatchHandler, IPersistenceProvider, NodeCallback } from "./types";
|
|
9
|
+
type PartitionInstance = InstanceType<typeof Partition>;
|
|
10
|
+
declare var _PENDING: Record<string, never>;
|
|
11
|
+
interface IEventStore {
|
|
12
|
+
_store: IPersistenceProvider;
|
|
13
|
+
_dispatchService?: DispatchHandler;
|
|
14
|
+
_partitions: Record<string, PartitionInstance | typeof _PENDING>;
|
|
15
|
+
openPartition(partitionId?: string, callback?: NodeCallback<PartitionInstance>): Promise<PartitionInstance>;
|
|
16
|
+
}
|
|
17
|
+
declare var EventStore: {
|
|
18
|
+
new (persistenceStore?: IPersistenceProvider, dispatchService?: DispatchHandler): IEventStore;
|
|
19
|
+
Event: typeof Event;
|
|
20
|
+
EventStream: typeof EventStream;
|
|
21
|
+
EventStorePartition: typeof Partition;
|
|
22
|
+
Commit: typeof Commit;
|
|
23
|
+
ConcurrencyError: typeof ConcurrencyError;
|
|
24
|
+
DuplicateCommitError: typeof DuplicateCommitError;
|
|
25
|
+
};
|
|
26
|
+
export default EventStore;
|
|
27
|
+
export { Event, EventStream, Commit, ConcurrencyError, DuplicateCommitError };
|
|
28
|
+
export { Partition as EventStorePartition };
|
|
29
|
+
//# sourceMappingURL=event_store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"event_store.d.ts","sourceRoot":"","sources":["../../src/event_store.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,UAAU,CAAC;AAC/B,OAAO,KAAK,MAAM,SAAS,CAAC;AAC5B,OAAO,SAAS,MAAM,yBAAyB,CAAC;AAChD,OAAO,WAAW,MAAM,gBAAgB,CAAC;AACzC,OAAO,MAAM,MAAM,sBAAsB,CAAC;AAC1C,OAAO,gBAAgB,MAAM,iCAAiC,CAAC;AAC/D,OAAO,oBAAoB,MAAM,sCAAsC,CAAC;AAExE,OAAO,KAAK,EAAE,eAAe,EAAyB,oBAAoB,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAE1G,KAAK,iBAAiB,GAAG,YAAY,CAAC,OAAO,SAAS,CAAC,CAAC;AAExD,QAAA,IAAI,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,CAAM,CAAC;AAEzC,UAAU,WAAW;IACnB,MAAM,EAAE,oBAAoB,CAAC;IAC7B,gBAAgB,CAAC,EAAE,eAAe,CAAC;IACnC,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,iBAAiB,GAAG,OAAO,QAAQ,CAAC,CAAC;IACjE,aAAa,CAAC,WAAW,CAAC,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,YAAY,CAAC,iBAAiB,CAAC,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAC;CAC7G;AAED,QAAA,IAAI,UAAU,EAIE;IACd,KAAK,gBAAgB,CAAC,EAAE,oBAAoB,EAAE,eAAe,CAAC,EAAE,eAAe,GAAG,WAAW,CAAC;IAC9F,KAAK,EAAE,OAAO,KAAK,CAAC;IACpB,WAAW,EAAE,OAAO,WAAW,CAAC;IAChC,mBAAmB,EAAE,OAAO,SAAS,CAAC;IACtC,MAAM,EAAE,OAAO,MAAM,CAAC;IACtB,gBAAgB,EAAE,OAAO,gBAAgB,CAAC;IAC1C,oBAAoB,EAAE,OAAO,oBAAoB,CAAC;CACnD,CAAC;AA4CF,eAAe,UAAU,CAAC;AAG1B,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,EAAE,gBAAgB,EAAE,oBAAoB,EAAE,CAAC;AAC9E,OAAO,EAAE,SAAS,IAAI,mBAAmB,EAAE,CAAC"}
|