@semiont/event-sourcing 0.4.13 → 0.4.15
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 +141 -339
- package/dist/index.d.ts +62 -157
- package/dist/index.js +113 -261
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ResourceId, components, ResourceAnnotations, Logger,
|
|
1
|
+
import { ResourceId, components, ResourceAnnotations, Logger, EventInput, StoredEvent, EventQuery as EventQuery$1, PersistedEvent, EventBus } from '@semiont/core';
|
|
2
2
|
import { SemiontProject } from '@semiont/core/node';
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -82,8 +82,14 @@ declare class EventStorage {
|
|
|
82
82
|
/**
|
|
83
83
|
* Append an event - handles EVERYTHING for event creation
|
|
84
84
|
* Creates ID, timestamp, metadata, checksum, sequence tracking, and writes to disk
|
|
85
|
+
*
|
|
86
|
+
* @param options.correlationId - Optional id propagated from a command. Stored
|
|
87
|
+
* on the event's metadata so subscribers (notably the events-stream → frontend
|
|
88
|
+
* path) can match command-result events back to the POST that initiated them.
|
|
85
89
|
*/
|
|
86
|
-
appendEvent(event:
|
|
90
|
+
appendEvent(event: EventInput, resourceId: ResourceId, options?: {
|
|
91
|
+
correlationId?: string;
|
|
92
|
+
}): Promise<StoredEvent>;
|
|
87
93
|
/**
|
|
88
94
|
* Write an event to storage (append to JSONL)
|
|
89
95
|
* Internal method - use appendEvent() instead
|
|
@@ -167,9 +173,12 @@ declare class EventLog {
|
|
|
167
173
|
* Append event to log
|
|
168
174
|
* @param event - Resource event (from @semiont/core)
|
|
169
175
|
* @param resourceId - Branded ResourceId (from @semiont/core)
|
|
176
|
+
* @param options.correlationId - Optional command correlation id (stored on metadata)
|
|
170
177
|
* @returns Stored event with metadata (sequence number, timestamp, checksum)
|
|
171
178
|
*/
|
|
172
|
-
append(event:
|
|
179
|
+
append(event: EventInput, resourceId: ResourceId, options?: {
|
|
180
|
+
correlationId?: string;
|
|
181
|
+
}): Promise<StoredEvent>;
|
|
173
182
|
/**
|
|
174
183
|
* Get all events for a resource
|
|
175
184
|
* @param resourceId - Branded ResourceId (from @semiont/core)
|
|
@@ -188,141 +197,6 @@ declare class EventLog {
|
|
|
188
197
|
queryEvents(resourceId: ResourceId, filter?: EventQuery$1): Promise<StoredEvent[]>;
|
|
189
198
|
}
|
|
190
199
|
|
|
191
|
-
/**
|
|
192
|
-
* Event Subscriptions - Real-time Event Pub/Sub
|
|
193
|
-
*
|
|
194
|
-
* Manages subscriptions for both resource-scoped and system-level events:
|
|
195
|
-
* - Resource subscriptions: notifications for a specific resource's events
|
|
196
|
-
* - Global subscriptions: notifications for all system-level events
|
|
197
|
-
* - Fire-and-forget notification pattern (non-blocking)
|
|
198
|
-
* - Automatic cleanup of empty subscription sets
|
|
199
|
-
*
|
|
200
|
-
* SINGLETON PATTERN: All EventStore instances share the same EventSubscriptions
|
|
201
|
-
* to ensure SSE connections receive events from any EventStore instance.
|
|
202
|
-
*
|
|
203
|
-
* @see docs/EVENT-STORE.md#eventsubscriptions for architecture details
|
|
204
|
-
*/
|
|
205
|
-
|
|
206
|
-
type EventCallback = (event: StoredEvent) => void | Promise<void>;
|
|
207
|
-
interface EventSubscription {
|
|
208
|
-
resourceId: ResourceId;
|
|
209
|
-
callback: EventCallback;
|
|
210
|
-
unsubscribe: () => void;
|
|
211
|
-
}
|
|
212
|
-
/**
|
|
213
|
-
* EventSubscriptions manages real-time event pub/sub
|
|
214
|
-
* Supports both resource-scoped and global subscriptions
|
|
215
|
-
*/
|
|
216
|
-
declare class EventSubscriptions {
|
|
217
|
-
private subscriptions;
|
|
218
|
-
private globalSubscriptions;
|
|
219
|
-
private logger?;
|
|
220
|
-
constructor(logger?: Logger);
|
|
221
|
-
/**
|
|
222
|
-
* Subscribe to events for a specific resource
|
|
223
|
-
* Returns an EventSubscription with unsubscribe function
|
|
224
|
-
*/
|
|
225
|
-
subscribe(resourceId: ResourceId, callback: EventCallback): EventSubscription;
|
|
226
|
-
/**
|
|
227
|
-
* Subscribe to all system-level events (no resourceId)
|
|
228
|
-
* Returns an EventSubscription with unsubscribe function
|
|
229
|
-
*
|
|
230
|
-
* Use this for consumers that need to react to global events like:
|
|
231
|
-
* - entitytype.added (global entity type collection changes)
|
|
232
|
-
* - Future system-level events (user.created, workspace.created, etc.)
|
|
233
|
-
*/
|
|
234
|
-
subscribeGlobal(callback: EventCallback): EventSubscription;
|
|
235
|
-
/**
|
|
236
|
-
* Notify all subscribers for a resource when a new event is appended
|
|
237
|
-
* @param resourceId - Bare resource ID
|
|
238
|
-
*/
|
|
239
|
-
notifySubscribers(resourceId: ResourceId, event: StoredEvent): Promise<void>;
|
|
240
|
-
/**
|
|
241
|
-
* Notify all global subscribers when a system-level event is appended
|
|
242
|
-
*/
|
|
243
|
-
notifyGlobalSubscribers(event: StoredEvent): Promise<void>;
|
|
244
|
-
/**
|
|
245
|
-
* Get subscription count for a resource (useful for debugging)
|
|
246
|
-
*/
|
|
247
|
-
getSubscriptionCount(resourceId: ResourceId): number;
|
|
248
|
-
/**
|
|
249
|
-
* Get total number of active subscriptions across all resources
|
|
250
|
-
*/
|
|
251
|
-
getTotalSubscriptions(): number;
|
|
252
|
-
/**
|
|
253
|
-
* Get total number of global subscriptions
|
|
254
|
-
*/
|
|
255
|
-
getGlobalSubscriptionCount(): number;
|
|
256
|
-
}
|
|
257
|
-
declare function getEventSubscriptions(logger?: Logger): EventSubscriptions;
|
|
258
|
-
|
|
259
|
-
/**
|
|
260
|
-
* EventBus - Event Pub/Sub Layer
|
|
261
|
-
*
|
|
262
|
-
* Single Responsibility: Event pub/sub only
|
|
263
|
-
* - Publishes events to subscribers
|
|
264
|
-
* - Manages subscriptions (resource-scoped and global)
|
|
265
|
-
*
|
|
266
|
-
* Does NOT handle:
|
|
267
|
-
* - Event persistence (see EventLog)
|
|
268
|
-
* - View updates (see ViewManager)
|
|
269
|
-
*/
|
|
270
|
-
|
|
271
|
-
/**
|
|
272
|
-
* EventBus wraps EventSubscriptions with a clean API
|
|
273
|
-
* Uses bare ResourceId for subscriptions
|
|
274
|
-
*/
|
|
275
|
-
declare class EventBus {
|
|
276
|
-
readonly subscriptions: EventSubscriptions;
|
|
277
|
-
private logger?;
|
|
278
|
-
constructor(logger?: Logger);
|
|
279
|
-
/**
|
|
280
|
-
* Publish event to subscribers
|
|
281
|
-
* - Resource events: notifies BOTH resource-scoped AND global subscribers
|
|
282
|
-
* - System events: notifies global subscribers only
|
|
283
|
-
* @param event - Stored event (from @semiont/core)
|
|
284
|
-
*/
|
|
285
|
-
publish(event: StoredEvent): Promise<void>;
|
|
286
|
-
/**
|
|
287
|
-
* Subscribe to events for a specific resource
|
|
288
|
-
* @param resourceId - Branded ResourceId (from @semiont/core)
|
|
289
|
-
* @param callback - Event callback function
|
|
290
|
-
* @returns EventSubscription with unsubscribe function
|
|
291
|
-
*/
|
|
292
|
-
subscribe(resourceId: ResourceId, callback: EventCallback): EventSubscription;
|
|
293
|
-
/**
|
|
294
|
-
* Subscribe to all system-level events
|
|
295
|
-
* @param callback - Event callback function
|
|
296
|
-
* @returns EventSubscription with unsubscribe function
|
|
297
|
-
*/
|
|
298
|
-
subscribeGlobal(callback: EventCallback): EventSubscription;
|
|
299
|
-
/**
|
|
300
|
-
* Unsubscribe from resource events
|
|
301
|
-
* @param resourceId - Branded ResourceId (from @semiont/core)
|
|
302
|
-
* @param callback - Event callback function to remove
|
|
303
|
-
*/
|
|
304
|
-
unsubscribe(resourceId: ResourceId, callback: EventCallback): void;
|
|
305
|
-
/**
|
|
306
|
-
* Unsubscribe from global events
|
|
307
|
-
* @param callback - Event callback function to remove
|
|
308
|
-
*/
|
|
309
|
-
unsubscribeGlobal(callback: EventCallback): void;
|
|
310
|
-
/**
|
|
311
|
-
* Get subscriber count for a resource
|
|
312
|
-
* @param resourceId - Branded ResourceId (from @semiont/core)
|
|
313
|
-
* @returns Number of active subscribers
|
|
314
|
-
*/
|
|
315
|
-
getSubscriberCount(resourceId: ResourceId): number;
|
|
316
|
-
/**
|
|
317
|
-
* Get total number of active subscriptions across all resources
|
|
318
|
-
*/
|
|
319
|
-
getTotalSubscriptions(): number;
|
|
320
|
-
/**
|
|
321
|
-
* Get total number of global subscriptions
|
|
322
|
-
*/
|
|
323
|
-
getGlobalSubscriptionCount(): number;
|
|
324
|
-
}
|
|
325
|
-
|
|
326
200
|
/**
|
|
327
201
|
* View Materializer - Materialized View Management
|
|
328
202
|
*
|
|
@@ -334,6 +208,15 @@ declare class EventBus {
|
|
|
334
208
|
* @see docs/EVENT-STORE.md#viewmaterializer for architecture details
|
|
335
209
|
*/
|
|
336
210
|
|
|
211
|
+
/**
|
|
212
|
+
* Minimal structural type for the event log dependency of `rebuildAll`.
|
|
213
|
+
* Avoids importing the concrete EventLog class from a sibling directory and
|
|
214
|
+
* keeps the materializer independent of the event-log implementation.
|
|
215
|
+
*/
|
|
216
|
+
interface RebuildEventSource {
|
|
217
|
+
getEvents(resourceId: ResourceId): Promise<StoredEvent[]>;
|
|
218
|
+
getAllResourceIds(): Promise<ResourceId[]>;
|
|
219
|
+
}
|
|
337
220
|
interface ViewMaterializerConfig {
|
|
338
221
|
basePath: string;
|
|
339
222
|
}
|
|
@@ -354,11 +237,11 @@ declare class ViewMaterializer {
|
|
|
354
237
|
* Materialize view incrementally with a single event
|
|
355
238
|
* Falls back to full rebuild if view doesn't exist
|
|
356
239
|
*/
|
|
357
|
-
materializeIncremental(resourceId: ResourceId, event:
|
|
240
|
+
materializeIncremental(resourceId: ResourceId, event: PersistedEvent, getAllEvents: () => Promise<StoredEvent[]>): Promise<void>;
|
|
358
241
|
/**
|
|
359
242
|
* Update the storage-uri index in response to an event.
|
|
360
243
|
*
|
|
361
|
-
* Only
|
|
244
|
+
* Only yield:created (with storageUri), yield:moved, need index changes.
|
|
362
245
|
* resource.archived / resource.unarchived do NOT modify the index.
|
|
363
246
|
*/
|
|
364
247
|
private materializeStorageUriIndex;
|
|
@@ -374,6 +257,16 @@ declare class ViewMaterializer {
|
|
|
374
257
|
* Apply an event to ResourceAnnotations (annotation collections only)
|
|
375
258
|
*/
|
|
376
259
|
private applyEventToAnnotations;
|
|
260
|
+
/**
|
|
261
|
+
* Walk every event stream in the event log and materialize the corresponding
|
|
262
|
+
* view from scratch. Idempotent: existing view files are overwritten.
|
|
263
|
+
*
|
|
264
|
+
* Mirrors GraphDBConsumer.rebuildAll() and Smelter.rebuildAll() — this is the
|
|
265
|
+
* recovery path that makes the ephemeral stateDir safe to wipe. The live
|
|
266
|
+
* append path (EventStore.appendEvent → materializeIncremental /
|
|
267
|
+
* materializeEntityTypes) is unchanged and runs in addition.
|
|
268
|
+
*/
|
|
269
|
+
rebuildAll(eventLog: RebuildEventSource): Promise<void>;
|
|
377
270
|
/**
|
|
378
271
|
* Materialize entity types view - System-level view
|
|
379
272
|
*/
|
|
@@ -410,13 +303,20 @@ declare class ViewManager {
|
|
|
410
303
|
* @param event - Resource event (from @semiont/core)
|
|
411
304
|
* @param getAllEvents - Function to retrieve all events for rebuild if needed
|
|
412
305
|
*/
|
|
413
|
-
materializeResource(resourceId: ResourceId, event:
|
|
306
|
+
materializeResource(resourceId: ResourceId, event: PersistedEvent, getAllEvents: () => Promise<StoredEvent[]>): Promise<void>;
|
|
414
307
|
/**
|
|
415
308
|
* Update system-level view (currently only entity types)
|
|
416
309
|
* @param eventType - Type of system event
|
|
417
310
|
* @param payload - Event payload
|
|
418
311
|
*/
|
|
419
312
|
materializeSystem(eventType: string, payload: any): Promise<void>;
|
|
313
|
+
/**
|
|
314
|
+
* Rebuild all materialized views from the event log on startup.
|
|
315
|
+
* Mirrors GraphDBConsumer.rebuildAll() — call this once during
|
|
316
|
+
* createKnowledgeBase before the HTTP server begins accepting requests.
|
|
317
|
+
* Idempotent: existing view files are overwritten.
|
|
318
|
+
*/
|
|
319
|
+
rebuildAll(eventLog: RebuildEventSource): Promise<void>;
|
|
420
320
|
/**
|
|
421
321
|
* Get resource view (builds from events if needed)
|
|
422
322
|
* @param resourceId - Branded ResourceId (from @semiont/core)
|
|
@@ -429,14 +329,15 @@ declare class ViewManager {
|
|
|
429
329
|
/**
|
|
430
330
|
* EventStore - Orchestration Layer
|
|
431
331
|
*
|
|
432
|
-
* Coordinates event sourcing operations
|
|
332
|
+
* Coordinates event sourcing operations:
|
|
433
333
|
* - EventLog: Event persistence (append, retrieve, query)
|
|
434
|
-
* -
|
|
435
|
-
* -
|
|
334
|
+
* - ViewManager: View materialization (resource and system)
|
|
335
|
+
* - Core EventBus: Publishes StoredEvent to typed channels after persistence
|
|
436
336
|
*
|
|
437
|
-
*
|
|
438
|
-
*
|
|
439
|
-
*
|
|
337
|
+
* appendEvent() is the single write path:
|
|
338
|
+
* 1. Persist to EventLog
|
|
339
|
+
* 2. Materialize views
|
|
340
|
+
* 3. Publish StoredEvent to global and resource-scoped typed channels
|
|
440
341
|
*/
|
|
441
342
|
|
|
442
343
|
/**
|
|
@@ -446,16 +347,22 @@ declare class ViewManager {
|
|
|
446
347
|
*/
|
|
447
348
|
declare class EventStore {
|
|
448
349
|
readonly log: EventLog;
|
|
449
|
-
readonly bus: EventBus;
|
|
450
350
|
readonly views: ViewManager;
|
|
451
351
|
readonly viewStorage: ViewStorage;
|
|
452
|
-
readonly coreEventBus
|
|
453
|
-
constructor(project: SemiontProject, stateDir: string, viewStorage: ViewStorage, coreEventBus
|
|
352
|
+
readonly coreEventBus: EventBus;
|
|
353
|
+
constructor(project: SemiontProject, stateDir: string, viewStorage: ViewStorage, coreEventBus: EventBus, logger?: Logger);
|
|
454
354
|
/**
|
|
455
355
|
* Append an event to the store
|
|
456
356
|
* Coordinates: persistence → view → notification
|
|
457
|
-
|
|
458
|
-
|
|
357
|
+
*
|
|
358
|
+
* @param options.correlationId - Optional id propagated from a command. Stored
|
|
359
|
+
* on the event's metadata so that subscribers (notably the events-stream
|
|
360
|
+
* route) can match command-result events back to the POST that initiated
|
|
361
|
+
* them. Pass through from your route handler when handling commands.
|
|
362
|
+
*/
|
|
363
|
+
appendEvent(event: EventInput, options?: {
|
|
364
|
+
correlationId?: string;
|
|
365
|
+
}): Promise<StoredEvent>;
|
|
459
366
|
}
|
|
460
367
|
|
|
461
368
|
/**
|
|
@@ -469,11 +376,11 @@ declare class EventStore {
|
|
|
469
376
|
* Create and initialize an EventStore instance
|
|
470
377
|
*
|
|
471
378
|
* @param project - SemiontProject instance
|
|
472
|
-
* @param eventBus -
|
|
379
|
+
* @param eventBus - @semiont/core EventBus for publishing domain events
|
|
473
380
|
* @param logger - Optional logger for structured logging
|
|
474
381
|
* @returns Configured EventStore instance ready for use
|
|
475
382
|
*/
|
|
476
|
-
declare function createEventStore(project: SemiontProject, eventBus
|
|
383
|
+
declare function createEventStore(project: SemiontProject, eventBus: EventBus, logger?: Logger): EventStore;
|
|
477
384
|
|
|
478
385
|
/**
|
|
479
386
|
* Sharding Utilities
|
|
@@ -638,8 +545,6 @@ declare class EventQuery {
|
|
|
638
545
|
* - prevEventHash links to previous event's checksum
|
|
639
546
|
* - Each event's checksum is verified against its payload
|
|
640
547
|
* - Detects broken chains and tampered events
|
|
641
|
-
*
|
|
642
|
-
* @see docs/EVENT-STORE.md#eventvalidator for architecture details
|
|
643
548
|
*/
|
|
644
549
|
|
|
645
550
|
interface ValidationResult {
|
|
@@ -678,4 +583,4 @@ declare class EventValidator {
|
|
|
678
583
|
*/
|
|
679
584
|
declare function generateAnnotationId(): string;
|
|
680
585
|
|
|
681
|
-
export {
|
|
586
|
+
export { EventLog, type EventLogConfig, EventQuery, EventStorage, type EventStorageConfig, EventStore, EventValidator, FilesystemViewStorage, ResourceNotFoundError, type ResourceView, type StorageUriEntry, ViewManager, type ViewManagerConfig, ViewMaterializer, type ViewStorage, createEventStore, generateAnnotationId, getShardPath, jumpConsistentHash, removeStorageUriEntry, resolveStorageUri, sha256, writeStorageUriEntry };
|