@eventualize/core 1.0.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/dist/EvDbEventStore.d.ts +123 -0
- package/dist/EvDbEventStore.js +182 -0
- package/dist/EvDbEventStore.js.map +1 -0
- package/dist/EvDbStream.d.ts +49 -0
- package/dist/EvDbStream.js +115 -0
- package/dist/EvDbStream.js.map +1 -0
- package/dist/EvDbStreamFactory.d.ts +97 -0
- package/dist/EvDbStreamFactory.js +155 -0
- package/dist/EvDbStreamFactory.js.map +1 -0
- package/dist/EvDbView.d.ts +34 -0
- package/dist/EvDbView.js +64 -0
- package/dist/EvDbView.js.map +1 -0
- package/dist/EvDbViewFactory.d.ts +45 -0
- package/dist/EvDbViewFactory.js +58 -0
- package/dist/EvDbViewFactory.js.map +1 -0
- package/package.json +25 -0
- package/src/EvDbEventStore.ts +302 -0
- package/src/EvDbStream.ts +184 -0
- package/src/EvDbStreamFactory.ts +290 -0
- package/src/EvDbView.ts +100 -0
- package/src/EvDbViewFactory.ts +123 -0
- package/tsconfig.json +18 -0
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import EvDbStream from './EvDbStream.js';
|
|
2
|
+
import IEvDbStorageSnapshotAdapter from '@eventualize/types/IEvDbStorageSnapshotAdapter';
|
|
3
|
+
import IEvDbStorageStreamAdapter from '@eventualize/types/IEvDbStorageStreamAdapter';
|
|
4
|
+
import IEvDbEventPayload from "@eventualize/types/IEvDbEventPayload";
|
|
5
|
+
import { EvDbStreamFactory } from './EvDbStreamFactory.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Combined storage adapter interface
|
|
9
|
+
*/
|
|
10
|
+
export interface IEvDbStorageAdapter extends IEvDbStorageStreamAdapter, IEvDbStorageSnapshotAdapter { }
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Storage configuration - either separate adapters or combined
|
|
14
|
+
*/
|
|
15
|
+
interface StorageConfig {
|
|
16
|
+
streamAdapter: IEvDbStorageStreamAdapter;
|
|
17
|
+
snapshotAdapter: IEvDbStorageSnapshotAdapter;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Registry of stream factories by stream type
|
|
22
|
+
*/
|
|
23
|
+
interface StreamFactoryRegistry {
|
|
24
|
+
[streamType: string]: EvDbStreamFactory<any, any>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Store class - manages stream creation with configured adapters and factories
|
|
29
|
+
*/
|
|
30
|
+
class BaseStore {
|
|
31
|
+
private readonly storage: StorageConfig;
|
|
32
|
+
private readonly streamFactories: StreamFactoryRegistry = {};
|
|
33
|
+
|
|
34
|
+
public constructor(storage: StorageConfig, streamFactories: StreamFactoryRegistry) {
|
|
35
|
+
this.storage = storage;
|
|
36
|
+
this.streamFactories = streamFactories;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Create a stream instance by stream type and ID
|
|
41
|
+
*/
|
|
42
|
+
public createStream(streamType: string, streamId: string): EvDbStream {
|
|
43
|
+
const factory = this.streamFactories[streamType];
|
|
44
|
+
|
|
45
|
+
if (!factory) {
|
|
46
|
+
throw new Error(
|
|
47
|
+
`No stream factory registered for stream type: ${streamType}. ` +
|
|
48
|
+
`Available types: ${Object.keys(this.streamFactories).join(', ')}`
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const stream = factory.create(
|
|
53
|
+
streamId,
|
|
54
|
+
this.storage.streamAdapter,
|
|
55
|
+
this.storage.snapshotAdapter
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
return stream;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Get a stream instance from the event store by stream type and ID
|
|
63
|
+
*/
|
|
64
|
+
public async getStream(streamType: string, streamId: string): Promise<EvDbStream> {
|
|
65
|
+
const factory = this.streamFactories[streamType];
|
|
66
|
+
|
|
67
|
+
if (!factory) {
|
|
68
|
+
throw new Error(
|
|
69
|
+
`No stream factory registered for stream type: ${streamType}. ` +
|
|
70
|
+
`Available types: ${Object.keys(this.streamFactories).join(', ')}`
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return factory.get(
|
|
75
|
+
streamId,
|
|
76
|
+
this.storage.streamAdapter,
|
|
77
|
+
this.storage.snapshotAdapter
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Check if a stream type is registered
|
|
83
|
+
*/
|
|
84
|
+
public hasStreamType(streamType: string): boolean {
|
|
85
|
+
return streamType in this.streamFactories;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Get all registered stream types
|
|
90
|
+
*/
|
|
91
|
+
public getStreamTypes(): string[] {
|
|
92
|
+
return Object.keys(this.streamFactories);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Create a builder for configuring the store
|
|
97
|
+
*/
|
|
98
|
+
public static builder(): BaseStoreBuilder {
|
|
99
|
+
return new BaseStoreBuilder();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
public async close(): Promise<void> {
|
|
103
|
+
await Promise.all([this.storage.snapshotAdapter.close(), this.storage.streamAdapter.close()]);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Builder for creating Store instances
|
|
110
|
+
*/
|
|
111
|
+
class BaseStoreBuilder {
|
|
112
|
+
private streamAdapter?: IEvDbStorageStreamAdapter;
|
|
113
|
+
private snapshotAdapter?: IEvDbStorageSnapshotAdapter;
|
|
114
|
+
private streamFactories: StreamFactoryRegistry = {};
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Configure with separate stream and snapshot adapters
|
|
118
|
+
*/
|
|
119
|
+
public withAdapters(
|
|
120
|
+
streamAdapter: IEvDbStorageStreamAdapter,
|
|
121
|
+
snapshotAdapter: IEvDbStorageSnapshotAdapter
|
|
122
|
+
): this {
|
|
123
|
+
this.streamAdapter = streamAdapter;
|
|
124
|
+
this.snapshotAdapter = snapshotAdapter;
|
|
125
|
+
return this;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Configure with a combined adapter that implements both interfaces
|
|
130
|
+
*/
|
|
131
|
+
public withAdapter(adapter: IEvDbStorageAdapter): this {
|
|
132
|
+
this.streamAdapter = adapter;
|
|
133
|
+
this.snapshotAdapter = adapter;
|
|
134
|
+
return this;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Register a stream factory
|
|
139
|
+
*/
|
|
140
|
+
public withStreamFactory<TEvents extends IEvDbEventPayload, TStreamType extends string>(
|
|
141
|
+
factory: EvDbStreamFactory<TEvents, TStreamType>
|
|
142
|
+
): this {
|
|
143
|
+
const streamType = factory.getStreamType();
|
|
144
|
+
if (this.streamFactories[streamType]) {
|
|
145
|
+
throw new Error(`Stream factory for type '${streamType}' is already registered`);
|
|
146
|
+
}
|
|
147
|
+
this.streamFactories[streamType] = factory;
|
|
148
|
+
return this;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Register multiple stream factories at once
|
|
153
|
+
*/
|
|
154
|
+
public withStreamFactories(
|
|
155
|
+
factories: ReadonlyArray<EvDbStreamFactory<any, string>>
|
|
156
|
+
): this {
|
|
157
|
+
factories.forEach(factory => {
|
|
158
|
+
this.withStreamFactory(factory);
|
|
159
|
+
});
|
|
160
|
+
return this;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Build the store instance
|
|
165
|
+
*/
|
|
166
|
+
public build(): BaseStore {
|
|
167
|
+
if (!this.streamAdapter || !this.snapshotAdapter) {
|
|
168
|
+
throw new Error(
|
|
169
|
+
'Storage adapters must be configured. Use withAdapter() or withAdapters()'
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (Object.keys(this.streamFactories).length === 0) {
|
|
174
|
+
console.warn('Store created with no stream factories registered');
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return new BaseStore(
|
|
178
|
+
{
|
|
179
|
+
streamAdapter: this.streamAdapter,
|
|
180
|
+
snapshotAdapter: this.snapshotAdapter
|
|
181
|
+
},
|
|
182
|
+
this.streamFactories
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// ============================================================================
|
|
188
|
+
// TYPED STORE - Type-safe stream creation
|
|
189
|
+
// ============================================================================
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Helper type to create method signatures for each stream factory
|
|
193
|
+
*/
|
|
194
|
+
export type StreamCreatorMethods<TStreamTypes extends Record<string, EvDbStreamFactory<any, any>>> = {
|
|
195
|
+
[K in keyof TStreamTypes as `create${K & string}`]: (streamId: string) => EvDbStream;
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
export type StreamMap = Record<string, any>;
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Type-safe store that knows about specific stream types
|
|
202
|
+
*/
|
|
203
|
+
export class EvDbEventStore<TStreamTypes extends Record<string, EvDbStreamFactory<any, any>>> {
|
|
204
|
+
constructor(private readonly store: BaseStore) {
|
|
205
|
+
// Create dynamic methods for each stream type
|
|
206
|
+
this.createDynamicMethods();
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Create a stream with type-safe stream type parameter (fallback method)
|
|
211
|
+
*/
|
|
212
|
+
public createStream<K extends keyof TStreamTypes & string>(
|
|
213
|
+
streamType: K,
|
|
214
|
+
streamId: string,
|
|
215
|
+
): EvDbStream {
|
|
216
|
+
return this.store.createStream(streamType, streamId);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Fetch a stream from the event store with type-safe stream type parameter (fallback method)
|
|
221
|
+
*/
|
|
222
|
+
public getStream<K extends keyof TStreamTypes & string>(
|
|
223
|
+
streamType: K,
|
|
224
|
+
streamId: string
|
|
225
|
+
): Promise<EvDbStream> {
|
|
226
|
+
|
|
227
|
+
return this.store.getStream(streamType, streamId);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Create dynamic methods for each registered stream factory
|
|
232
|
+
*/
|
|
233
|
+
private createDynamicMethods(): void {
|
|
234
|
+
const streamTypes = this.store.getStreamTypes();
|
|
235
|
+
|
|
236
|
+
for (const streamType of streamTypes) {
|
|
237
|
+
const methodName = `create${streamType}` as keyof this;
|
|
238
|
+
|
|
239
|
+
// Create the dynamic method
|
|
240
|
+
(this as any)[methodName] = (streamId: string) => {
|
|
241
|
+
return this.store.createStream(streamType, streamId);
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Get the underlying store
|
|
248
|
+
*/
|
|
249
|
+
public getStore(): BaseStore {
|
|
250
|
+
return this.store;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Make TypedStore callable with dynamic methods
|
|
255
|
+
// export interface TypedStore<TStreamTypes extends Record<string, StreamFactory<any>>>
|
|
256
|
+
// extends StreamCreatorMethods<TStreamTypes> { }
|
|
257
|
+
|
|
258
|
+
export type TypedStoreType<
|
|
259
|
+
TStreamTypes extends Record<string, EvDbStreamFactory<any, any>>
|
|
260
|
+
> = StreamCreatorMethods<TStreamTypes>;
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Type-safe store builder
|
|
264
|
+
*/
|
|
265
|
+
export class EvDbEventStoreBuilder<
|
|
266
|
+
TStreamTypes extends Record<string, EvDbStreamFactory<any, string>> = {}
|
|
267
|
+
> {
|
|
268
|
+
private builder = BaseStore.builder();
|
|
269
|
+
|
|
270
|
+
public withAdapters(
|
|
271
|
+
streamAdapter: IEvDbStorageStreamAdapter,
|
|
272
|
+
snapshotAdapter: IEvDbStorageSnapshotAdapter
|
|
273
|
+
): this {
|
|
274
|
+
this.builder.withAdapters(streamAdapter, snapshotAdapter);
|
|
275
|
+
return this;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
public withAdapter(adapter: IEvDbStorageAdapter): this {
|
|
279
|
+
this.builder.withAdapter(adapter);
|
|
280
|
+
return this;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// Track stream type in generics
|
|
284
|
+
public withStreamFactory<
|
|
285
|
+
F extends EvDbStreamFactory<any, string>,
|
|
286
|
+
K extends F extends EvDbStreamFactory<any, infer ST> ? ST : never
|
|
287
|
+
>(
|
|
288
|
+
factory: F
|
|
289
|
+
): EvDbEventStoreBuilder<TStreamTypes & Record<K, F>> {
|
|
290
|
+
this.builder.withStreamFactory(factory);
|
|
291
|
+
// return as the new type that tracks K
|
|
292
|
+
return this as unknown as EvDbEventStoreBuilder<TStreamTypes & Record<K, F>>;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
public build(): EvDbEventStore<TStreamTypes> & StreamCreatorMethods<TStreamTypes> {
|
|
296
|
+
const store = new EvDbEventStore(this.builder.build());
|
|
297
|
+
return store as EvDbEventStore<TStreamTypes> & StreamCreatorMethods<TStreamTypes>;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
export type EvDbEventStoreType<TStreams extends StreamMap = StreamMap> =
|
|
302
|
+
EvDbEventStore<TStreams> & StreamCreatorMethods<TStreams>;
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import EvDbEvent from "@eventualize/types/EvDbEvent";
|
|
2
|
+
import EvDbMessage from "@eventualize/types/EvDbMessage";
|
|
3
|
+
import IEvDbStorageStreamAdapter from "@eventualize/types/IEvDbStorageStreamAdapter";
|
|
4
|
+
import IEvDbView from "@eventualize/types/IEvDbView";
|
|
5
|
+
import EvDbStreamAddress from "@eventualize/types/EvDbStreamAddress";
|
|
6
|
+
import IEvDbViewStore from "@eventualize/types/IEvDbViewStore";
|
|
7
|
+
import IEvDbStreamStore from "@eventualize/types/IEvDbStreamStore";
|
|
8
|
+
import IEvDbStreamStoreData from "@eventualize/types/IEvDbStreamStoreData";
|
|
9
|
+
import IEvDbEventPayload from "@eventualize/types/IEvDbEventPayload";
|
|
10
|
+
import StreamStoreAffected from "@eventualize/types/StreamStoreAffected";
|
|
11
|
+
import IEvDbEventMetadata from "@eventualize/types/IEvDbEventMetadata";
|
|
12
|
+
import EvDbStreamCursor from "@eventualize/types/EvDbStreamCursor";
|
|
13
|
+
import OCCException from "@eventualize/types/OCCException";
|
|
14
|
+
import { EvDbStreamType } from "@eventualize/types/primitiveTypes";
|
|
15
|
+
import EVDbMessagesProducer from "@eventualize/types/EvDbMessagesProducer";
|
|
16
|
+
import { EvDbView } from "./EvDbView.js"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
type ImmutableIEvDbView = Readonly<EvDbView<unknown>>;
|
|
20
|
+
export type ImmutableIEvDbViewMap = Readonly<Record<string, ImmutableIEvDbView>>;
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
export default class EvDbStream implements IEvDbStreamStore, IEvDbStreamStoreData {
|
|
25
|
+
|
|
26
|
+
protected _pendingEvents: ReadonlyArray<EvDbEvent> = [];
|
|
27
|
+
protected _pendingMessages: ReadonlyArray<EvDbMessage> = [];
|
|
28
|
+
|
|
29
|
+
private static readonly ASSEMBLY_NAME = {
|
|
30
|
+
name: 'evdb-core',
|
|
31
|
+
version: '1.0.0'
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
private static readonly DEFAULT_CAPTURE_BY =
|
|
35
|
+
`${EvDbStream.ASSEMBLY_NAME.name}-${EvDbStream.ASSEMBLY_NAME.version}`;
|
|
36
|
+
|
|
37
|
+
private readonly _storageAdapter: IEvDbStorageStreamAdapter;
|
|
38
|
+
|
|
39
|
+
// Views
|
|
40
|
+
protected readonly _views: ImmutableIEvDbViewMap;
|
|
41
|
+
|
|
42
|
+
getViews(): ImmutableIEvDbViewMap {
|
|
43
|
+
return this._views;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
getView(viewName: string): Readonly<IEvDbView> | undefined {
|
|
47
|
+
return this._views[viewName];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Events
|
|
51
|
+
/**
|
|
52
|
+
* Unspecialized events
|
|
53
|
+
*/
|
|
54
|
+
getEvents(): ReadonlyArray<EvDbEvent> {
|
|
55
|
+
return this._pendingEvents;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// StreamAddress
|
|
59
|
+
public streamAddress: EvDbStreamAddress;
|
|
60
|
+
|
|
61
|
+
// StoredOffset
|
|
62
|
+
public storedOffset: number;
|
|
63
|
+
|
|
64
|
+
// Constructor
|
|
65
|
+
public constructor(
|
|
66
|
+
streamType: EvDbStreamType,
|
|
67
|
+
views: ImmutableIEvDbView[],
|
|
68
|
+
storageAdapter: IEvDbStorageStreamAdapter,
|
|
69
|
+
streamId: string,
|
|
70
|
+
lastStoredOffset: number,
|
|
71
|
+
protected messagesProducer: EVDbMessagesProducer
|
|
72
|
+
) {
|
|
73
|
+
this._views = views.reduce((acc, view) => {
|
|
74
|
+
const viewName = view.address.viewName;
|
|
75
|
+
acc[viewName] = view;
|
|
76
|
+
return acc;
|
|
77
|
+
}, {} as Record<string, IEvDbViewStore>) as ImmutableIEvDbViewMap;
|
|
78
|
+
this._storageAdapter = storageAdapter;
|
|
79
|
+
this.streamAddress = new EvDbStreamAddress(streamType, streamId);
|
|
80
|
+
this.storedOffset = lastStoredOffset;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
protected appendEvent(
|
|
84
|
+
payload: IEvDbEventPayload,
|
|
85
|
+
capturedBy?: string | null
|
|
86
|
+
): IEvDbEventMetadata {
|
|
87
|
+
capturedBy = capturedBy ?? EvDbStream.DEFAULT_CAPTURE_BY;
|
|
88
|
+
// const json = JSON.stringify(payload); // Or use custom serializer
|
|
89
|
+
|
|
90
|
+
const cursor = this.getNextCursor(this._pendingEvents);
|
|
91
|
+
const e = new EvDbEvent(
|
|
92
|
+
payload.payloadType,
|
|
93
|
+
cursor,
|
|
94
|
+
payload,
|
|
95
|
+
new Date(),
|
|
96
|
+
capturedBy,
|
|
97
|
+
);
|
|
98
|
+
this._pendingEvents = [...this._pendingEvents, e];
|
|
99
|
+
|
|
100
|
+
// Apply to views
|
|
101
|
+
for (const view of Object.values(this._views)) {
|
|
102
|
+
view.applyEvent(e);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Outbox producer
|
|
106
|
+
const viewsStates = Object.fromEntries(Object.entries(this._views)
|
|
107
|
+
.map(([k, v]) => {
|
|
108
|
+
return [k, (v as EvDbView<unknown>).state]
|
|
109
|
+
}));
|
|
110
|
+
const producedMessages = this.messagesProducer(e, viewsStates);
|
|
111
|
+
this._pendingMessages = [...this._pendingMessages, ...producedMessages]
|
|
112
|
+
|
|
113
|
+
return e;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
private getNextCursor(events: ReadonlyArray<EvDbEvent>): EvDbStreamCursor {
|
|
117
|
+
if (events.length === 0) {
|
|
118
|
+
return new EvDbStreamCursor(this.streamAddress, this.storedOffset + 1);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const lastEvent = events[events.length - 1];
|
|
122
|
+
const offset = lastEvent.streamCursor.offset;
|
|
123
|
+
return new EvDbStreamCursor(this.streamAddress, offset + 1);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// AppendToOutbox
|
|
127
|
+
/**
|
|
128
|
+
* Put a row into the publication (out-box pattern).
|
|
129
|
+
*/
|
|
130
|
+
public appendToOutbox(e: EvDbMessage): void {
|
|
131
|
+
this._pendingMessages = [...this._pendingMessages, e];
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// StoreAsync
|
|
135
|
+
public async store(): Promise<StreamStoreAffected> {
|
|
136
|
+
// Telemetry
|
|
137
|
+
// const tags = this.streamAddress.toOtelTags();
|
|
138
|
+
// const duration = EvDbStream._sysMeters.measureStoreEventsDuration(tags);
|
|
139
|
+
// const activity = EvDbStream._trace.startActivity(tags, 'EvDb.Store');
|
|
140
|
+
|
|
141
|
+
try {
|
|
142
|
+
if (this._pendingEvents.length === 0) {
|
|
143
|
+
return StreamStoreAffected.Empty;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const affected = await this._storageAdapter.storeStreamAsync(
|
|
147
|
+
this._pendingEvents,
|
|
148
|
+
this._pendingMessages,
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
const lastEvent = this._pendingEvents[this._pendingEvents.length - 1];
|
|
152
|
+
this.storedOffset = lastEvent.streamCursor.offset;
|
|
153
|
+
this._pendingEvents = [];
|
|
154
|
+
this._pendingMessages = [];
|
|
155
|
+
|
|
156
|
+
const viewSaveTasks = Object.values(this._views).map(v => v.store());
|
|
157
|
+
await Promise.all(viewSaveTasks);
|
|
158
|
+
|
|
159
|
+
return affected;
|
|
160
|
+
} catch (error) {
|
|
161
|
+
if (error instanceof OCCException) {
|
|
162
|
+
throw error;
|
|
163
|
+
}
|
|
164
|
+
throw error;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// CountOfPendingEvents
|
|
170
|
+
/**
|
|
171
|
+
* number of events that were not stored yet.
|
|
172
|
+
*/
|
|
173
|
+
public get countOfPendingEvents(): number {
|
|
174
|
+
return this._pendingEvents.length;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Notifications
|
|
178
|
+
/**
|
|
179
|
+
* Unspecialized messages
|
|
180
|
+
*/
|
|
181
|
+
public getMessages(): ReadonlyArray<EvDbMessage> {
|
|
182
|
+
return this._pendingMessages;
|
|
183
|
+
}
|
|
184
|
+
}
|