@schemeless/event-store-types 2.8.3 → 2.10.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.
@@ -0,0 +1,6 @@
1
+ export declare class ConcurrencyError extends Error {
2
+ readonly streamKey: string;
3
+ readonly expectedSequence: number;
4
+ readonly actualSequence: number;
5
+ constructor(streamKey: string, expectedSequence: number, actualSequence: number);
6
+ }
package/dist/Errors.js ADDED
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ConcurrencyError = void 0;
4
+ class ConcurrencyError extends Error {
5
+ constructor(streamKey, expectedSequence, actualSequence) {
6
+ super(`Concurrency conflict on stream "${streamKey}": expected sequence ${expectedSequence}, but found ${actualSequence}`);
7
+ this.streamKey = streamKey;
8
+ this.expectedSequence = expectedSequence;
9
+ this.actualSequence = actualSequence;
10
+ this.name = 'ConcurrencyError';
11
+ }
12
+ }
13
+ exports.ConcurrencyError = ConcurrencyError;
@@ -38,6 +38,17 @@ export interface EventFlow<PartialPayload = any, Payload extends PartialPayload
38
38
  readonly eventType?: CreatedEvent<Payload>;
39
39
  readonly payloadType?: PartialPayload | Payload;
40
40
  readonly samplePayload?: PartialPayload | Payload;
41
+ /**
42
+ * Extract the shard key for event routing.
43
+ * Events with the same shard key will be processed sequentially in the same partition.
44
+ * Events with different shard keys can be processed in parallel.
45
+ *
46
+ * @param event - The event to extract the shard key from
47
+ * @returns The shard key string, or undefined to use fallback (identifier)
48
+ * @example
49
+ * getShardKey: (event) => event.payload.userId
50
+ */
51
+ readonly getShardKey?: (event: BaseEvent<Payload>) => string | undefined;
41
52
  readonly receive: (eventStore: {
42
53
  receive: (eventFlow: EventFlow<PartialPayload, Payload>) => (eventInput: BaseEventInput<PartialPayload>) => Promise<[CreatedEvent<Payload>, ...Array<CreatedEvent<any>>]>;
43
54
  }) => (eventInputArgs: BaseEventInput<PartialPayload>) => Promise<[CreatedEvent<Payload>, ...Array<CreatedEvent<any>>]>;
@@ -8,14 +8,28 @@ export interface IEventStoreEntity<PAYLOAD = any, META = any> {
8
8
  identifier?: string;
9
9
  correlationId?: string;
10
10
  causationId?: string;
11
+ sequence?: number;
11
12
  readonly created: Date;
12
13
  }
14
+ export interface StoreEventsOptions {
15
+ /**
16
+ * Expected sequence number for the stream (domain + identifier).
17
+ * If provided, the store will verify the current sequence matches
18
+ * before writing. Throws ConcurrencyError on mismatch.
19
+ */
20
+ expectedSequence?: number;
21
+ }
13
22
  export interface IEventStoreRepo<PAYLOAD = any, META = any> {
14
23
  init: () => Promise<void>;
15
24
  getAllEvents: (pageSize: number, startFromId?: string) => Promise<AsyncIterableIterator<Array<IEventStoreEntity<PAYLOAD, META>>>>;
16
25
  createEventEntity: (event: CreatedEvent<any>) => IEventStoreEntity<PAYLOAD, META>;
17
- storeEvents: (events: CreatedEvent<any>[]) => Promise<void>;
26
+ storeEvents: (events: CreatedEvent<any>[], options?: StoreEventsOptions) => Promise<void>;
18
27
  resetStore: () => Promise<void>;
28
+ /**
29
+ * Get the current sequence number for a stream.
30
+ * Returns 0 if no events exist for this stream.
31
+ */
32
+ getStreamSequence?: (domain: string, identifier: string) => Promise<number>;
19
33
  /**
20
34
  * Retrieves a single event by its ID.
21
35
  * Required for revert operations.
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from './EventStore.types';
2
2
  export * from './Repo.types';
3
3
  export * from './Revert.types';
4
+ export * from './Errors';
package/dist/index.js CHANGED
@@ -17,3 +17,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./EventStore.types"), exports);
18
18
  __exportStar(require("./Repo.types"), exports);
19
19
  __exportStar(require("./Revert.types"), exports);
20
+ __exportStar(require("./Errors"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@schemeless/event-store-types",
3
- "version": "2.8.3",
3
+ "version": "2.10.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": "03e86735e57d21512ff96e3588f40f98e59e5d41"
37
+ "gitHead": "396c3bfbe61bb81b93f0d3d0686388ed3d80a594"
38
38
  }