@schemeless/event-store-types 2.10.0 → 3.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.
@@ -1,4 +1,19 @@
1
- export interface BaseEventInput<Payload, META = undefined> {
1
+ /**
2
+ * Base metadata for all events.
3
+ * Framework automatically populates schemaVersion.
4
+ * User code can extend this interface for custom metadata.
5
+ */
6
+ export interface EventMeta {
7
+ /** Schema version of the event payload */
8
+ schemaVersion?: number;
9
+ /** Flag indicating this is a compensating event */
10
+ isCompensating?: boolean;
11
+ /** ID of the event being compensated */
12
+ compensatesEventId?: string;
13
+ /** Allow arbitrary extensions */
14
+ [key: string]: any;
15
+ }
16
+ export interface BaseEventInput<Payload, META extends EventMeta = EventMeta> {
2
17
  payload: Payload;
3
18
  meta?: META;
4
19
  identifier?: string;
@@ -11,7 +26,7 @@ export interface BaseEventInput<Payload, META = undefined> {
11
26
  causationId?: string;
12
27
  created?: Date;
13
28
  }
14
- export interface BaseEvent<Payload, META = undefined> extends BaseEventInput<Payload, META> {
29
+ export interface BaseEvent<Payload, META extends EventMeta = EventMeta> extends BaseEventInput<Payload, META> {
15
30
  id?: string;
16
31
  domain: string;
17
32
  type: string;
@@ -21,13 +36,13 @@ export interface BaseEvent<Payload, META = undefined> extends BaseEventInput<Pay
21
36
  causationId?: string;
22
37
  created?: Date;
23
38
  }
24
- export interface CreatedEvent<Payload, META = undefined> extends BaseEvent<Payload, META> {
39
+ export interface CreatedEvent<Payload, META extends EventMeta = EventMeta> extends BaseEvent<Payload, META> {
25
40
  id: string;
26
41
  readonly created: Date;
27
42
  }
28
- export interface StoredEvent<Payload, META = undefined> extends CreatedEvent<Payload, META> {
43
+ export interface StoredEvent<Payload, META extends EventMeta = EventMeta> extends CreatedEvent<Payload, META> {
29
44
  }
30
- export type Event<Payload, META = undefined> = StoredEvent<Payload, META>;
45
+ export type Event<Payload, META extends EventMeta = EventMeta> = StoredEvent<Payload, META>;
31
46
  export interface EventFlow<PartialPayload = any, Payload extends PartialPayload = PartialPayload> {
32
47
  readonly domain: string;
33
48
  readonly type: string;
@@ -38,6 +53,21 @@ export interface EventFlow<PartialPayload = any, Payload extends PartialPayload
38
53
  readonly eventType?: CreatedEvent<Payload>;
39
54
  readonly payloadType?: PartialPayload | Payload;
40
55
  readonly samplePayload?: PartialPayload | Payload;
56
+ /**
57
+ * Current schema version for this event type.
58
+ * Stored in event.meta.schemaVersion when event is created.
59
+ * @default 1
60
+ */
61
+ readonly schemaVersion?: number;
62
+ /**
63
+ * Upgrades event payload from older versions to the current schemaVersion.
64
+ * Called automatically during event processing and replay.
65
+ *
66
+ * @param event - The event with potentially outdated payload
67
+ * @param fromVersion - The version stored in event.meta.schemaVersion (defaults to 1)
68
+ * @returns Updated event with migrated payload, or void to use the original
69
+ */
70
+ readonly upcast?: (event: CreatedEvent<any>, fromVersion: number) => CreatedEvent<Payload> | Promise<CreatedEvent<Payload>> | void;
41
71
  /**
42
72
  * Extract the shard key for event routing.
43
73
  * Events with the same shard key will be processed sequentially in the same partition.
@@ -19,12 +19,32 @@ export interface StoreEventsOptions {
19
19
  */
20
20
  expectedSequence?: number;
21
21
  }
22
+ export interface ISnapshotEntity<STATE = any> {
23
+ domain: string;
24
+ identifier: string;
25
+ state: STATE;
26
+ sequence: number;
27
+ created: Date;
28
+ }
22
29
  export interface IEventStoreRepo<PAYLOAD = any, META = any> {
23
30
  init: () => Promise<void>;
24
31
  getAllEvents: (pageSize: number, startFromId?: string) => Promise<AsyncIterableIterator<Array<IEventStoreEntity<PAYLOAD, META>>>>;
25
32
  createEventEntity: (event: CreatedEvent<any>) => IEventStoreEntity<PAYLOAD, META>;
26
33
  storeEvents: (events: CreatedEvent<any>[], options?: StoreEventsOptions) => Promise<void>;
27
34
  resetStore: () => Promise<void>;
35
+ /**
36
+ * Get events for a specific stream (domain + identifier).
37
+ * MUST use efficient index-based query (e.g., DynamoDB Query, SQL WHERE).
38
+ * Required for getAggregate to work.
39
+ *
40
+ * @param domain - Event domain
41
+ * @param identifier - Stream identifier
42
+ * @param fromSequence - Start from this sequence (exclusive), 0 = from beginning
43
+ * @returns Events ordered by sequence ascending
44
+ */
45
+ getStreamEvents?: (domain: string, identifier: string, fromSequence?: number) => Promise<IEventStoreEntity<PAYLOAD, META>[]>;
46
+ getSnapshot?: <STATE>(domain: string, identifier: string) => Promise<ISnapshotEntity<STATE> | null>;
47
+ saveSnapshot?: <STATE>(snapshot: ISnapshotEntity<STATE>) => Promise<void>;
28
48
  /**
29
49
  * Get the current sequence number for a stream.
30
50
  * Returns 0 if no events exist for this stream.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@schemeless/event-store-types",
3
- "version": "2.10.0",
3
+ "version": "3.0.0",
4
4
  "typescript:main": "src/index.ts",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -34,5 +34,5 @@
34
34
  "publishConfig": {
35
35
  "access": "public"
36
36
  },
37
- "gitHead": "396c3bfbe61bb81b93f0d3d0686388ed3d80a594"
37
+ "gitHead": "a237dd78669897598770553da9508eedca5aaf50"
38
38
  }