@requence/event-sourcing 1.2.0 → 1.4.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/README.md +1 -0
- package/dist/{chunk-z2yp4rve.js → chunk-57b88rt5.js} +7 -7
- package/dist/chunk-57b88rt5.js.map +10 -0
- package/dist/chunk-jd4q8fcf.js.map +2 -2
- package/dist/{chunk-gq80tgvf.js → chunk-pc1q4se9.js} +14 -3
- package/dist/chunk-pc1q4se9.js.map +10 -0
- package/dist/{chunk-t7t9mfgn.js → chunk-vep2byx4.js} +46 -14
- package/dist/chunk-vep2byx4.js.map +10 -0
- package/dist/{chunk-fkzd741s.js → chunk-vvv14qxy.js} +4 -3
- package/dist/chunk-vvv14qxy.js.map +10 -0
- package/dist/createAggregateRoot.d.ts +14 -2
- package/dist/createAggregateRoot.d.ts.map +1 -1
- package/dist/createAggregateRoot.js +2 -2
- package/dist/createEventStore.d.ts +4 -3
- package/dist/createEventStore.d.ts.map +1 -1
- package/dist/createEventStore.js +3 -3
- package/dist/createProcessManager.d.ts +13 -1
- package/dist/createProcessManager.d.ts.map +1 -1
- package/dist/createProcessManager.js +1 -1
- package/dist/drizzle/index.js +4 -4
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -4
- package/dist/memory/index.js +4 -4
- package/dist/transaction.d.ts +19 -1
- package/dist/transaction.d.ts.map +1 -1
- package/dist/transaction.js +1 -1
- package/package.json +1 -1
- package/src/createAggregateRoot.ts +125 -15
- package/src/createEventStore.ts +14 -7
- package/src/createProcessManager.ts +15 -1
- package/src/index.ts +3 -0
- package/src/tests/aggregateRoot.spec.ts +42 -0
- package/src/tests/processManager.spec.ts +70 -0
- package/src/tests/transaction.spec.ts +53 -0
- package/src/transaction.ts +34 -2
- package/dist/chunk-fkzd741s.js.map +0 -10
- package/dist/chunk-gq80tgvf.js.map +0 -10
- package/dist/chunk-t7t9mfgn.js.map +0 -10
- package/dist/chunk-z2yp4rve.js.map +0 -10
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import type { AggregateRootApi, AggregateRootSnapshotMethods, AnyAggregateRoot } from './createAggregateRoot.ts';
|
|
2
2
|
import type { CheckpointMethods } from './createCheckpointApi.ts';
|
|
3
3
|
import { type EventListenerApi } from './createEventListener.ts';
|
|
4
|
-
import { type ProcessManagerApi } from './createProcessManager.ts';
|
|
4
|
+
import { type ProcessManagerApi, type ProcessManagerOptions } from './createProcessManager.ts';
|
|
5
5
|
import { type ProjectionApi, type ProjectionApiWithSnapshots, type ProjectionSnapshotMethods } from './createProjection.ts';
|
|
6
6
|
import { type LockCreator } from './lock.ts';
|
|
7
|
+
import { type TransactionOptions } from './transaction.ts';
|
|
7
8
|
import type { BaseInputEvent, BaseOutputEvent, EventTemplate, MaybePromise, Stream, StreamEvents, StreamId, StreamIdEvents } from './utilityTypes.ts';
|
|
8
9
|
import { type WrappedEvent } from './wrappedEventBuilder.ts';
|
|
9
10
|
export type LoadEvents = (select: Stream | StreamId | Stream[] | StreamId[] | StreamEvents | StreamEvents[] | StreamIdEvents | StreamIdEvents[] | null, range?: {
|
|
@@ -41,7 +42,7 @@ export interface EventStoreParamsWithoutAggregateRootSnapshots<Root extends AnyA
|
|
|
41
42
|
}
|
|
42
43
|
export type EventStore<Event extends EventTemplate, Root extends AnyAggregateRoot, SupportsProjectionSnapshots = boolean> = {
|
|
43
44
|
createProjection(name: string): SupportsProjectionSnapshots extends true ? ProjectionApiWithSnapshots<Event> : ProjectionApi<Event>;
|
|
44
|
-
createProcessManager(name: string): ProcessManagerApi<never, Event>;
|
|
45
|
+
createProcessManager(name: string, options?: ProcessManagerOptions): ProcessManagerApi<never, Event>;
|
|
45
46
|
createEventListener(name: string): EventListenerApi<Event>;
|
|
46
47
|
rebuild: () => Promise<void>;
|
|
47
48
|
isReady: () => Promise<void>;
|
|
@@ -49,7 +50,7 @@ export type EventStore<Event extends EventTemplate, Root extends AnyAggregateRoo
|
|
|
49
50
|
getAggregateRoot<S extends Root['type']>(type: S): Root extends {
|
|
50
51
|
type: S;
|
|
51
52
|
} ? Root : never;
|
|
52
|
-
transaction<T extends () => any>(handler: T): Promise<Awaited<ReturnType<T>>>;
|
|
53
|
+
transaction<T extends () => any>(handler: T, options?: TransactionOptions): Promise<Awaited<ReturnType<T>>>;
|
|
53
54
|
};
|
|
54
55
|
export declare function createEventStore<const Root extends AnyAggregateRoot>(params: EventStoreParamsWithoutAggregateRootSnapshots<Root>): EventStore<EventsFromRoot<Root>, Root, false>;
|
|
55
56
|
export declare function createEventStore<const Root extends AnyAggregateRoot>(params: EventStoreParamsWithAggregateRootSnapshots<Root>): EventStore<EventsFromRoot<Root>, Root, true>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createEventStore.d.ts","sourceRoot":"","sources":["../src/createEventStore.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,gBAAgB,EAChB,4BAA4B,EAC5B,gBAAgB,EAEjB,MAAM,0BAA0B,CAAA;AACjC,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAA;AACjE,OAAO,EACL,KAAK,gBAAgB,EAGtB,MAAM,0BAA0B,CAAA;AACjC,OAAO,EAEL,KAAK,iBAAiB,
|
|
1
|
+
{"version":3,"file":"createEventStore.d.ts","sourceRoot":"","sources":["../src/createEventStore.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,gBAAgB,EAChB,4BAA4B,EAC5B,gBAAgB,EAEjB,MAAM,0BAA0B,CAAA;AACjC,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAA;AACjE,OAAO,EACL,KAAK,gBAAgB,EAGtB,MAAM,0BAA0B,CAAA;AACjC,OAAO,EAEL,KAAK,iBAAiB,EACtB,KAAK,qBAAqB,EAE3B,MAAM,2BAA2B,CAAA;AAClC,OAAO,EAEL,KAAK,aAAa,EAClB,KAAK,0BAA0B,EAC/B,KAAK,yBAAyB,EA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EAAE,0CAA0C,CAAC,IAAI,CAAC,GACvD,UAAU,CAAC,cAAc,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,CAAA"}
|
package/dist/createEventStore.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import {
|
|
2
2
|
createEventStore
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-57b88rt5.js";
|
|
4
4
|
import"./chunk-24xjphpk.js";
|
|
5
5
|
import"./chunk-ty9bhdvx.js";
|
|
6
|
-
import"./chunk-
|
|
6
|
+
import"./chunk-vvv14qxy.js";
|
|
7
7
|
import"./chunk-6q8mr1sg.js";
|
|
8
|
-
import"./chunk-
|
|
8
|
+
import"./chunk-pc1q4se9.js";
|
|
9
9
|
import"./chunk-z388cd25.js";
|
|
10
10
|
import"./chunk-3ejt4h7x.js";
|
|
11
11
|
import"./chunk-8qn1nqnh.js";
|
|
@@ -1,9 +1,21 @@
|
|
|
1
|
+
import type { SettledOptions } from './createAggregateRoot.ts';
|
|
1
2
|
import { type CheckpointMethods } from './createCheckpointApi.ts';
|
|
2
3
|
import type { LoadEvents, OnProgress } from './createEventStore.ts';
|
|
3
4
|
import type { AfterEffectHandlers, BaseOutputEvent, EventHandlers, EventTemplate, Keys, Merge, StreamEvents } from './utilityTypes.js';
|
|
5
|
+
/**
|
|
6
|
+
* Options for `createProcessManager`.
|
|
7
|
+
*
|
|
8
|
+
* `settled` — default {@link SettledOptions} applied when the process manager
|
|
9
|
+
* auto-settles the streams its handlers dispatched commands on (explicit
|
|
10
|
+
* `settled()` calls are not allowed inside a process manager).
|
|
11
|
+
*/
|
|
12
|
+
export type ProcessManagerOptions = {
|
|
13
|
+
settled?: SettledOptions;
|
|
14
|
+
};
|
|
4
15
|
type ProcessManagerInfo = {
|
|
5
16
|
name: string;
|
|
6
17
|
event: BaseOutputEvent;
|
|
18
|
+
settledDefaults?: SettledOptions;
|
|
7
19
|
};
|
|
8
20
|
export declare function getProcessManagerInfo(): ProcessManagerInfo | undefined;
|
|
9
21
|
export declare function isInsideProcessManager(): boolean;
|
|
@@ -67,6 +79,6 @@ export declare function buildProcessManagerCreator(params: {
|
|
|
67
79
|
loadEvents: LoadEvents;
|
|
68
80
|
checkpoint: CheckpointMethods;
|
|
69
81
|
onRefresh?: OnProgress;
|
|
70
|
-
}): (name: string) => ProcessManagerApi;
|
|
82
|
+
}): (name: string, options?: ProcessManagerOptions) => ProcessManagerApi;
|
|
71
83
|
export {};
|
|
72
84
|
//# sourceMappingURL=createProcessManager.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createProcessManager.d.ts","sourceRoot":"","sources":["../src/createProcessManager.ts"],"names":[],"mappings":"AAEA,OAAO,EAEL,KAAK,iBAAiB,EAEvB,MAAM,0BAA0B,CAAA;AACjC,OAAO,KAAK,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAA;AAQnE,OAAO,KAAK,EACV,mBAAmB,EACnB,eAAe,EACf,aAAa,EACb,aAAa,EACb,IAAI,EACJ,KAAK,EACL,YAAY,EACb,MAAM,mBAAmB,CAAA;AAE1B,KAAK,kBAAkB,GAAG;IACxB,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,EAAE,eAAe,CAAA;
|
|
1
|
+
{"version":3,"file":"createProcessManager.d.ts","sourceRoot":"","sources":["../src/createProcessManager.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAA;AAC9D,OAAO,EAEL,KAAK,iBAAiB,EAEvB,MAAM,0BAA0B,CAAA;AACjC,OAAO,KAAK,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAA;AAQnE,OAAO,KAAK,EACV,mBAAmB,EACnB,eAAe,EACf,aAAa,EACb,aAAa,EACb,IAAI,EACJ,KAAK,EACL,YAAY,EACb,MAAM,mBAAmB,CAAA;AAE1B;;;;;;GAMG;AACH,MAAM,MAAM,qBAAqB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AAK,KAAK,iCAAiC,CAAC,MAAM,CAAC,KAC5D,IAAI,CACP,iBAAiB,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,CAAC,KAAK,EAAE;QAAE,gBAAgB,EAAE,IAAI,CAAA;KAAE,CAAC,CAAC,EAC1E,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE;QAAE,gBAAgB,EAAE,IAAI,CAAA;KAAE,CAAC,CAAC,CAC/C,CAAA;IACD,UAAU,EAAE,MAAM,IAAI,CACpB,iBAAiB,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,CAAC,KAAK,EAAE;QAAE,UAAU,EAAE,IAAI,CAAA;KAAE,CAAC,CAAC,EACpE,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE;QAAE,UAAU,EAAE,IAAI,CAAA;KAAE,CAAC,CAAC,CACzC,CAAA;IACD,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;IACxB,WAAW,EAAE,KAAK,CAAA;CACnB,GAAG,CAAC,KAAK,CAAC,WAAW,CAAC,SAAS,IAAI,GAChC;IACE,YAAY,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;IACjC,KAAK,EAAE,MAAM,OAAO,CAAC,KAAK,CAAC,CAAA;CAC5B,GACD,EAAE,CAAC,CAAA;AAEP,wBAAgB,0BAA0B,CAAC,MAAM,EAAE;IACjD,UAAU,EAAE,UAAU,CAAA;IACtB,UAAU,EAAE,iBAAiB,CAAA;IAC7B,SAAS,CAAC,EAAE,UAAU,CAAA;CACvB,IACS,MAAM,MAAM,EAAE,UAAU,qBAAqB,KAAG,iBAAiB,CAiT1E"}
|
package/dist/drizzle/index.js
CHANGED
|
@@ -11,18 +11,18 @@ import {
|
|
|
11
11
|
} from "../chunk-jd4q8fcf.js";
|
|
12
12
|
import {
|
|
13
13
|
createEventStore
|
|
14
|
-
} from "../chunk-
|
|
14
|
+
} from "../chunk-57b88rt5.js";
|
|
15
15
|
import"../chunk-24xjphpk.js";
|
|
16
16
|
import {
|
|
17
17
|
createAggregateRoot
|
|
18
|
-
} from "../chunk-
|
|
18
|
+
} from "../chunk-vep2byx4.js";
|
|
19
19
|
import"../chunk-ty9bhdvx.js";
|
|
20
|
-
import"../chunk-
|
|
20
|
+
import"../chunk-vvv14qxy.js";
|
|
21
21
|
import {
|
|
22
22
|
extendTypes,
|
|
23
23
|
superjson
|
|
24
24
|
} from "../chunk-6q8mr1sg.js";
|
|
25
|
-
import"../chunk-
|
|
25
|
+
import"../chunk-pc1q4se9.js";
|
|
26
26
|
import"../chunk-z388cd25.js";
|
|
27
27
|
import"../chunk-3ejt4h7x.js";
|
|
28
28
|
import"../chunk-8qn1nqnh.js";
|
package/dist/index.d.ts
CHANGED
|
@@ -3,10 +3,11 @@ export { createEventStore } from './createEventStore.ts';
|
|
|
3
3
|
export type { BaseOutputEvent as Event } from './utilityTypes.d.ts';
|
|
4
4
|
export { type Snapshot as ProjectionSnapshot, isInsideProjection, getProjectionInfo, } from './createProjection.ts';
|
|
5
5
|
export type { Checkpoint } from './createCheckpointApi.ts';
|
|
6
|
-
export { isInsideProcessManager, getProcessManagerInfo, } from './createProcessManager.ts';
|
|
6
|
+
export { type ProcessManagerOptions, isInsideProcessManager, getProcessManagerInfo, } from './createProcessManager.ts';
|
|
7
7
|
export { isInsideEventListener, getEventListenerInfo, } from './createEventListener.ts';
|
|
8
8
|
export * from './errors.ts';
|
|
9
|
-
export { type Snapshot as AggregateRootSnapshot, type AnyAggregateRoot, type AnyStream, createAggregateRoot, } from './createAggregateRoot.ts';
|
|
9
|
+
export { type Snapshot as AggregateRootSnapshot, type AnyAggregateRoot, type AnyStream, type SettledOptions, createAggregateRoot, } from './createAggregateRoot.ts';
|
|
10
|
+
export type { TransactionOptions } from './transaction.ts';
|
|
10
11
|
export { skipRefreshing } from './refresh.ts';
|
|
11
12
|
export { skipReplay } from './replay.ts';
|
|
12
13
|
export declare function isStreamId(stream: Stream): stream is StreamId | StreamIdEvents;
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,MAAM,EACN,YAAY,EACZ,QAAQ,EACR,cAAc,EACf,MAAM,mBAAmB,CAAA;AAE1B,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AACxD,YAAY,EAAE,eAAe,IAAI,KAAK,EAAE,MAAM,qBAAqB,CAAA;AACnE,OAAO,EACL,KAAK,QAAQ,IAAI,kBAAkB,EACnC,kBAAkB,EAClB,iBAAiB,GAClB,MAAM,uBAAuB,CAAA;AAC9B,YAAY,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAA;AAC1D,OAAO,EACL,sBAAsB,EACtB,qBAAqB,GACtB,MAAM,2BAA2B,CAAA;AAClC,OAAO,EACL,qBAAqB,EACrB,oBAAoB,GACrB,MAAM,0BAA0B,CAAA;AACjC,cAAc,aAAa,CAAA;AAC3B,OAAO,EACL,KAAK,QAAQ,IAAI,qBAAqB,EACtC,KAAK,gBAAgB,EACrB,KAAK,SAAS,EACd,mBAAmB,GACpB,MAAM,0BAA0B,CAAA;AAEjC,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAC7C,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAExC,wBAAgB,UAAU,CACxB,MAAM,EAAE,MAAM,GACb,MAAM,IAAI,QAAQ,GAAG,cAAc,CAErC;AACD,wBAAgB,cAAc,CAC5B,MAAM,EAAE,MAAM,GACb,MAAM,IAAI,YAAY,GAAG,cAAc,CAEzC;AACD,YAAY,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,MAAM,EACN,YAAY,EACZ,QAAQ,EACR,cAAc,EACf,MAAM,mBAAmB,CAAA;AAE1B,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AACxD,YAAY,EAAE,eAAe,IAAI,KAAK,EAAE,MAAM,qBAAqB,CAAA;AACnE,OAAO,EACL,KAAK,QAAQ,IAAI,kBAAkB,EACnC,kBAAkB,EAClB,iBAAiB,GAClB,MAAM,uBAAuB,CAAA;AAC9B,YAAY,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAA;AAC1D,OAAO,EACL,KAAK,qBAAqB,EAC1B,sBAAsB,EACtB,qBAAqB,GACtB,MAAM,2BAA2B,CAAA;AAClC,OAAO,EACL,qBAAqB,EACrB,oBAAoB,GACrB,MAAM,0BAA0B,CAAA;AACjC,cAAc,aAAa,CAAA;AAC3B,OAAO,EACL,KAAK,QAAQ,IAAI,qBAAqB,EACtC,KAAK,gBAAgB,EACrB,KAAK,SAAS,EACd,KAAK,cAAc,EACnB,mBAAmB,GACpB,MAAM,0BAA0B,CAAA;AAEjC,YAAY,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAA;AAC1D,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAC7C,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAExC,wBAAgB,UAAU,CACxB,MAAM,EAAE,MAAM,GACb,MAAM,IAAI,QAAQ,GAAG,cAAc,CAErC;AACD,wBAAgB,cAAc,CAC5B,MAAM,EAAE,MAAM,GACb,MAAM,IAAI,YAAY,GAAG,cAAc,CAEzC;AACD,YAAY,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -4,21 +4,21 @@ import {
|
|
|
4
4
|
} from "./chunk-jd4q8fcf.js";
|
|
5
5
|
import {
|
|
6
6
|
createEventStore
|
|
7
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-57b88rt5.js";
|
|
8
8
|
import {
|
|
9
9
|
getEventListenerInfo,
|
|
10
10
|
isInsideEventListener
|
|
11
11
|
} from "./chunk-24xjphpk.js";
|
|
12
12
|
import {
|
|
13
13
|
createAggregateRoot
|
|
14
|
-
} from "./chunk-
|
|
14
|
+
} from "./chunk-vep2byx4.js";
|
|
15
15
|
import"./chunk-ty9bhdvx.js";
|
|
16
16
|
import {
|
|
17
17
|
getProcessManagerInfo,
|
|
18
18
|
isInsideProcessManager
|
|
19
|
-
} from "./chunk-
|
|
19
|
+
} from "./chunk-vvv14qxy.js";
|
|
20
20
|
import"./chunk-6q8mr1sg.js";
|
|
21
|
-
import"./chunk-
|
|
21
|
+
import"./chunk-pc1q4se9.js";
|
|
22
22
|
import"./chunk-z388cd25.js";
|
|
23
23
|
import {
|
|
24
24
|
getProjectionInfo,
|
package/dist/memory/index.js
CHANGED
|
@@ -4,15 +4,15 @@ import {
|
|
|
4
4
|
} from "../chunk-jd4q8fcf.js";
|
|
5
5
|
import {
|
|
6
6
|
createEventStore
|
|
7
|
-
} from "../chunk-
|
|
7
|
+
} from "../chunk-57b88rt5.js";
|
|
8
8
|
import"../chunk-24xjphpk.js";
|
|
9
9
|
import {
|
|
10
10
|
createAggregateRoot
|
|
11
|
-
} from "../chunk-
|
|
11
|
+
} from "../chunk-vep2byx4.js";
|
|
12
12
|
import"../chunk-ty9bhdvx.js";
|
|
13
|
-
import"../chunk-
|
|
13
|
+
import"../chunk-vvv14qxy.js";
|
|
14
14
|
import"../chunk-6q8mr1sg.js";
|
|
15
|
-
import"../chunk-
|
|
15
|
+
import"../chunk-pc1q4se9.js";
|
|
16
16
|
import"../chunk-z388cd25.js";
|
|
17
17
|
import"../chunk-3ejt4h7x.js";
|
|
18
18
|
import"../chunk-8qn1nqnh.js";
|
package/dist/transaction.d.ts
CHANGED
|
@@ -1,13 +1,31 @@
|
|
|
1
|
+
import type { SettledOptions } from './createAggregateRoot.ts';
|
|
1
2
|
export type TransactionDelay = {
|
|
2
3
|
resolve: () => void;
|
|
3
4
|
reject: (error: Error) => void;
|
|
4
5
|
commit: () => Promise<void>;
|
|
5
6
|
};
|
|
7
|
+
/**
|
|
8
|
+
* Options for a transaction run.
|
|
9
|
+
*
|
|
10
|
+
* `settled` — default {@link SettledOptions} applied when the transaction
|
|
11
|
+
* auto-settles the streams whose commands were dispatched inside the handler
|
|
12
|
+
* (explicit `settled()` calls are not allowed there).
|
|
13
|
+
*/
|
|
14
|
+
export type TransactionOptions = {
|
|
15
|
+
settled?: SettledOptions;
|
|
16
|
+
};
|
|
6
17
|
export default function createTransaction(): {
|
|
7
18
|
readonly active: boolean;
|
|
19
|
+
readonly settledDefaults: SettledOptions | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* Runs the handler outside the transaction scope. Used by the settle
|
|
22
|
+
* retry: a retried append already passed the transaction gate on its
|
|
23
|
+
* first attempt and must not re-enter the (committed) transaction.
|
|
24
|
+
*/
|
|
25
|
+
exit<T>(handler: () => T): T;
|
|
8
26
|
delay(): TransactionDelay;
|
|
9
27
|
after(onAfter: () => Promise<any>): void;
|
|
10
|
-
run<T extends () => any>(handler: T): Promise<any>;
|
|
28
|
+
run<T extends () => any>(handler: T, options?: TransactionOptions): Promise<any>;
|
|
11
29
|
};
|
|
12
30
|
export type Transaction = ReturnType<typeof createTransaction>;
|
|
13
31
|
//# sourceMappingURL=transaction.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transaction.d.ts","sourceRoot":"","sources":["../src/transaction.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"transaction.d.ts","sourceRoot":"","sources":["../src/transaction.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAA;AAgB9D,MAAM,MAAM,gBAAgB,GAAG;IAC7B,OAAO,EAAE,MAAM,IAAI,CAAA;IACnB,MAAM,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAA;IAC9B,MAAM,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;CAC5B,CAAA;AAED;;;;;;GAMG;AACH,MAAM,MAAM,kBAAkB,GAAG;IAC/B,OAAO,CAAC,EAAE,cAAc,CAAA;CACzB,CAAA;AAED,MAAM,CAAC,OAAO,UAAU,iBAAiB;;;IAiBrC;;;;OAIG;SACE,CAAC,WAAW,MAAM,CAAC,GAAG,CAAC;aAInB,gBAAgB;mBAgCV,MAAM,OAAO,CAAC,GAAG,CAAC;QAIvB,CAAC,SAAS,MAAM,GAAG,WAAW,CAAC,YAAY,kBAAkB;EAyB1E;AAED,MAAM,MAAM,WAAW,GAAG,UAAU,CAAC,OAAO,iBAAiB,CAAC,CAAA"}
|
package/dist/transaction.js
CHANGED
package/package.json
CHANGED
|
@@ -12,6 +12,7 @@ import { getProcessManagerInfo } from './createProcessManager.ts'
|
|
|
12
12
|
import { getProjectionInfo } from './createProjection.ts'
|
|
13
13
|
import {
|
|
14
14
|
CommandError,
|
|
15
|
+
ConcurrencyError,
|
|
15
16
|
EventValidationError,
|
|
16
17
|
InfiniteLoopError,
|
|
17
18
|
ValidationError,
|
|
@@ -135,12 +136,25 @@ type VersionBuilder = <V extends number, S extends z.ZodType>(
|
|
|
135
136
|
schema: S,
|
|
136
137
|
) => { schema: S; version: V }
|
|
137
138
|
|
|
139
|
+
/**
|
|
140
|
+
* Options for {@link Stream.settled}.
|
|
141
|
+
*
|
|
142
|
+
* `maxRetries` — how many times to reload the stream and re-apply the dispatched
|
|
143
|
+
* commands when the append loses the optimistic-concurrency race
|
|
144
|
+
* ({@link ConcurrencyError}). Defaults to `0` (no retry — surface the error, the
|
|
145
|
+
* historical behaviour). Only the append is retried; command handlers must be
|
|
146
|
+
* pure functions of loaded state and their arguments for retries to be safe.
|
|
147
|
+
*/
|
|
148
|
+
export type SettledOptions = {
|
|
149
|
+
maxRetries?: number
|
|
150
|
+
}
|
|
151
|
+
|
|
138
152
|
type Stream<Commands extends Record<string, Command>, State> = {
|
|
139
153
|
[A in keyof Commands]: (
|
|
140
154
|
...args: Parameters<Commands[A]>
|
|
141
155
|
) => Stream<Commands, State>
|
|
142
156
|
} & {
|
|
143
|
-
settled: () => Promise<Stream<Commands, State>>
|
|
157
|
+
settled: (options?: SettledOptions) => Promise<Stream<Commands, State>>
|
|
144
158
|
state: () => Promise<State>
|
|
145
159
|
transformError: <T extends Error>(
|
|
146
160
|
error: Constructor<T>,
|
|
@@ -150,7 +164,7 @@ type Stream<Commands extends Record<string, Command>, State> = {
|
|
|
150
164
|
}
|
|
151
165
|
|
|
152
166
|
export type AnyStream = {
|
|
153
|
-
settled: () => Promise<AnyStream>
|
|
167
|
+
settled: (options?: SettledOptions) => Promise<AnyStream>
|
|
154
168
|
state: () => Promise<unknown>
|
|
155
169
|
transformError: <T extends Error>(
|
|
156
170
|
error: Constructor<T>,
|
|
@@ -475,7 +489,33 @@ export function createAggregateRoot<Name extends string>(type: Name) {
|
|
|
475
489
|
return version
|
|
476
490
|
}
|
|
477
491
|
|
|
478
|
-
|
|
492
|
+
// A command as dispatched by the caller, recorded so it can be re-applied
|
|
493
|
+
// against a freshly-loaded stream when a settle() retries a ConcurrencyError.
|
|
494
|
+
type RecordedCommand = { name: string; args: any[] }
|
|
495
|
+
|
|
496
|
+
type CreateStreamConfig = {
|
|
497
|
+
// Whether to load the existing stream before commands run. `false` for
|
|
498
|
+
// newStream (expects version 0); `true` for loadStream and every retry.
|
|
499
|
+
load?: boolean
|
|
500
|
+
filter?: (event: BaseOutputEvent) => MaybePromise<boolean>
|
|
501
|
+
// Seeded on a retry so replayed commands re-apply automatically, and
|
|
502
|
+
// transformError handlers registered before settle() carry over.
|
|
503
|
+
replayCommands?: RecordedCommand[]
|
|
504
|
+
errorHandlerEntries?: [
|
|
505
|
+
Constructor<Error>,
|
|
506
|
+
(error: CommandError<Error>) => Error,
|
|
507
|
+
][]
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
const createStream = (
|
|
511
|
+
state: any,
|
|
512
|
+
streamId: string,
|
|
513
|
+
config: CreateStreamConfig = {},
|
|
514
|
+
) => {
|
|
515
|
+
// Captured at the dispatch site so a settle() retry can re-apply the
|
|
516
|
+
// commands within the same AsyncLocalStorage state (user metadata scopes,
|
|
517
|
+
// process-manager info, …) that the original attempt observed.
|
|
518
|
+
const dispatchContext = AsyncLocalStorage.snapshot()
|
|
479
519
|
let version = 0
|
|
480
520
|
const snapshotApi = createSnapshotApi?.(
|
|
481
521
|
state,
|
|
@@ -485,7 +525,8 @@ export function createAggregateRoot<Name extends string>(type: Name) {
|
|
|
485
525
|
const errorHandlers = new Map<
|
|
486
526
|
Constructor<Error>,
|
|
487
527
|
(error: CommandError<Error>) => Error
|
|
488
|
-
>()
|
|
528
|
+
>(config.errorHandlerEntries)
|
|
529
|
+
const dispatchedCommands: RecordedCommand[] = []
|
|
489
530
|
let downstreamSettledPromise = () => Promise.resolve()
|
|
490
531
|
let releaseLock!: () => Promise<void>
|
|
491
532
|
let extendLock!: EventHandlerOptions['extendLock']
|
|
@@ -563,6 +604,9 @@ export function createAggregateRoot<Name extends string>(type: Name) {
|
|
|
563
604
|
return api.commands
|
|
564
605
|
}
|
|
565
606
|
|
|
607
|
+
// Record for a potential settle() retry (reload + re-apply).
|
|
608
|
+
dispatchedCommands.push({ name: commandName, args })
|
|
609
|
+
|
|
566
610
|
const commandId = crypto.randomUUID()
|
|
567
611
|
transactionDelay ??= transaction.delay()
|
|
568
612
|
commandChainTouched = true
|
|
@@ -747,11 +791,11 @@ export function createAggregateRoot<Name extends string>(type: Name) {
|
|
|
747
791
|
|
|
748
792
|
return state
|
|
749
793
|
},
|
|
750
|
-
async settled() {
|
|
794
|
+
async settled(options?: SettledOptions): Promise<any> {
|
|
751
795
|
const parentProcessManager = getProcessManagerInfo()
|
|
752
796
|
if (parentProcessManager) {
|
|
753
797
|
const error = new Error(
|
|
754
|
-
`aggregateRoot.settled() cannot be called inside a process manager. Attempted on aggregate stream "${type}" within Process Manager ${parentProcessManager.name}.`,
|
|
798
|
+
`aggregateRoot.settled() cannot be called inside a process manager. Attempted on aggregate stream "${type}" within Process Manager ${parentProcessManager.name}. To configure how dispatched streams settle, pass options to createProcessManager(name, { settled }).`,
|
|
755
799
|
)
|
|
756
800
|
commandChain = commandChain.then(() => error)
|
|
757
801
|
throw error
|
|
@@ -759,13 +803,57 @@ export function createAggregateRoot<Name extends string>(type: Name) {
|
|
|
759
803
|
|
|
760
804
|
if (transaction.active) {
|
|
761
805
|
const error = new Error(
|
|
762
|
-
`aggregateRoot.settled() cannot be called inside a transaction. Attempted on aggregate stream "${type}".`,
|
|
806
|
+
`aggregateRoot.settled() cannot be called inside a transaction. Attempted on aggregate stream "${type}". To configure how dispatched streams settle, pass options to transaction(handler, { settled }).`,
|
|
763
807
|
)
|
|
764
808
|
commandChain = commandChain.then(() => error)
|
|
765
809
|
throw error
|
|
766
810
|
}
|
|
767
811
|
|
|
768
|
-
|
|
812
|
+
const maxRetries = options?.maxRetries ?? 0
|
|
813
|
+
|
|
814
|
+
try {
|
|
815
|
+
await awaitAll(() => commandChain)
|
|
816
|
+
} catch (error) {
|
|
817
|
+
// Optimistic-concurrency recovery: another writer advanced the
|
|
818
|
+
// stream between load and append. Reload and re-apply the same
|
|
819
|
+
// commands against the fresh version. A failed append leaves this
|
|
820
|
+
// attempt's lock held (it never reached releaseLock), so release it
|
|
821
|
+
// before the retry re-acquires — otherwise the retry blocks until
|
|
822
|
+
// the lock's TTL lapses.
|
|
823
|
+
if (maxRetries > 0 && error instanceof ConcurrencyError) {
|
|
824
|
+
await releaseLock?.()
|
|
825
|
+
|
|
826
|
+
// Re-create the stream within the async context captured at the
|
|
827
|
+
// dispatch site, so the replayed commands and event
|
|
828
|
+
// post-processing observe the same AsyncLocalStorage state as
|
|
829
|
+
// the original attempt. Two scopes are explicitly exited: the
|
|
830
|
+
// transaction (the first attempt already passed its commit gate;
|
|
831
|
+
// re-entering the committed store would swallow errors and
|
|
832
|
+
// register dead delays) and the upstream collection (this retry
|
|
833
|
+
// is settled right here, not through another auto-settle
|
|
834
|
+
// registration).
|
|
835
|
+
const retryStream = dispatchContext(() =>
|
|
836
|
+
transaction.exit(() =>
|
|
837
|
+
downstreamRootsPromiseStore.exit(() =>
|
|
838
|
+
createStream(clone(initialState), streamId, {
|
|
839
|
+
load: true,
|
|
840
|
+
filter: config.filter,
|
|
841
|
+
replayCommands: dispatchedCommands,
|
|
842
|
+
errorHandlerEntries: Array.from(errorHandlers),
|
|
843
|
+
}),
|
|
844
|
+
),
|
|
845
|
+
),
|
|
846
|
+
)
|
|
847
|
+
|
|
848
|
+
return retryStream.commands.settled({
|
|
849
|
+
...options,
|
|
850
|
+
maxRetries: maxRetries - 1,
|
|
851
|
+
})
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
throw error
|
|
855
|
+
}
|
|
856
|
+
|
|
769
857
|
await downstreamSettledPromise()
|
|
770
858
|
return api.commands
|
|
771
859
|
},
|
|
@@ -780,8 +868,28 @@ export function createAggregateRoot<Name extends string>(type: Name) {
|
|
|
780
868
|
},
|
|
781
869
|
}
|
|
782
870
|
|
|
783
|
-
|
|
784
|
-
|
|
871
|
+
// Streams dispatched inside a process manager or a transaction cannot
|
|
872
|
+
// call settled() themselves — the surrounding scope settles them when it
|
|
873
|
+
// completes. Capture the defaults declared on that scope
|
|
874
|
+
// (createProcessManager / transaction options) so the auto-settle
|
|
875
|
+
// inherits them.
|
|
876
|
+
const autoSettleOptions =
|
|
877
|
+
getProcessManagerInfo()?.settledDefaults ?? transaction.settledDefaults
|
|
878
|
+
|
|
879
|
+
registerRootPromiseToUpstream(() => api.commands.settled(autoSettleOptions))
|
|
880
|
+
transaction.after(() => api.commands.settled(autoSettleOptions))
|
|
881
|
+
|
|
882
|
+
// Fold existing events before commands run (loadStream, and every retry).
|
|
883
|
+
if (config.load) {
|
|
884
|
+
api.load(config.filter)
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
// Re-apply the caller's commands on a retry so the fresh, reloaded stream
|
|
888
|
+
// reproduces the original intent against the current version.
|
|
889
|
+
for (const { name, args } of config.replayCommands ?? []) {
|
|
890
|
+
;(api.commands as any)[name](...args)
|
|
891
|
+
}
|
|
892
|
+
|
|
785
893
|
return api
|
|
786
894
|
}
|
|
787
895
|
|
|
@@ -861,8 +969,9 @@ export function createAggregateRoot<Name extends string>(type: Name) {
|
|
|
861
969
|
`aggregateRoot.newStream() cannot be called inside a projection. Attempted on aggregate stream ${type} within Projection ${parentProjectionName}.`,
|
|
862
970
|
)
|
|
863
971
|
}
|
|
864
|
-
return createStream(clone(initialState), id ?? crypto.randomUUID()
|
|
865
|
-
|
|
972
|
+
return createStream(clone(initialState), id ?? crypto.randomUUID(), {
|
|
973
|
+
load: false,
|
|
974
|
+
}).commands as any
|
|
866
975
|
},
|
|
867
976
|
|
|
868
977
|
loadStream(id, filter) {
|
|
@@ -878,9 +987,10 @@ export function createAggregateRoot<Name extends string>(type: Name) {
|
|
|
878
987
|
`aggregateRoot.loadStream("${id}") cannot be called inside a projection. Attempted on aggregate stream ${type} within Projection ${parentProjectionName}.`,
|
|
879
988
|
)
|
|
880
989
|
}
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
990
|
+
return createStream(clone(initialState), id, {
|
|
991
|
+
load: true,
|
|
992
|
+
filter: filter as any,
|
|
993
|
+
}).commands as any
|
|
884
994
|
},
|
|
885
995
|
|
|
886
996
|
linkEventStore(load, append, emit, trans, lock, snap) {
|
package/src/createEventStore.ts
CHANGED
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
import {
|
|
14
14
|
type InternalProcessManager,
|
|
15
15
|
type ProcessManagerApi,
|
|
16
|
+
type ProcessManagerOptions,
|
|
16
17
|
buildProcessManagerCreator,
|
|
17
18
|
} from './createProcessManager.ts'
|
|
18
19
|
import {
|
|
@@ -24,7 +25,7 @@ import {
|
|
|
24
25
|
} from './createProjection.ts'
|
|
25
26
|
import lock, { type LockCreator } from './lock.ts'
|
|
26
27
|
import { withRefreshing } from './refresh.ts'
|
|
27
|
-
import createTransaction from './transaction.ts'
|
|
28
|
+
import createTransaction, { type TransactionOptions } from './transaction.ts'
|
|
28
29
|
import type {
|
|
29
30
|
BaseInputEvent,
|
|
30
31
|
BaseOutputEvent,
|
|
@@ -126,7 +127,10 @@ export type EventStore<
|
|
|
126
127
|
): SupportsProjectionSnapshots extends true
|
|
127
128
|
? ProjectionApiWithSnapshots<Event>
|
|
128
129
|
: ProjectionApi<Event>
|
|
129
|
-
createProcessManager(
|
|
130
|
+
createProcessManager(
|
|
131
|
+
name: string,
|
|
132
|
+
options?: ProcessManagerOptions,
|
|
133
|
+
): ProcessManagerApi<never, Event>
|
|
130
134
|
createEventListener(name: string): EventListenerApi<Event>
|
|
131
135
|
rebuild: () => Promise<void>
|
|
132
136
|
isReady: () => Promise<void>
|
|
@@ -134,7 +138,10 @@ export type EventStore<
|
|
|
134
138
|
getAggregateRoot<S extends Root['type']>(
|
|
135
139
|
type: S,
|
|
136
140
|
): Root extends { type: S } ? Root : never
|
|
137
|
-
transaction<T extends () => any>(
|
|
141
|
+
transaction<T extends () => any>(
|
|
142
|
+
handler: T,
|
|
143
|
+
options?: TransactionOptions,
|
|
144
|
+
): Promise<Awaited<ReturnType<T>>>
|
|
138
145
|
}
|
|
139
146
|
|
|
140
147
|
export function createEventStore<const Root extends AnyAggregateRoot>(
|
|
@@ -357,11 +364,11 @@ export function createEventStore<const Root extends AnyAggregateRoot>(
|
|
|
357
364
|
}
|
|
358
365
|
return api
|
|
359
366
|
},
|
|
360
|
-
createProcessManager(name) {
|
|
367
|
+
createProcessManager(name, options) {
|
|
361
368
|
if (processManagers.has(name)) {
|
|
362
369
|
throw new Error(`Process Manager ${name} is already registered`)
|
|
363
370
|
}
|
|
364
|
-
const api = baseCreateProcessManager(name)
|
|
371
|
+
const api = baseCreateProcessManager(name, options)
|
|
365
372
|
|
|
366
373
|
const internalApi = api as unknown as InternalProcessManager
|
|
367
374
|
internalApi.setRelatedStreamEvents((handlers) =>
|
|
@@ -464,8 +471,8 @@ export function createEventStore<const Root extends AnyAggregateRoot>(
|
|
|
464
471
|
(aggregateRoot) => aggregateRoot.type === type,
|
|
465
472
|
) as any
|
|
466
473
|
},
|
|
467
|
-
async transaction(handler) {
|
|
468
|
-
return transaction.run(handler)
|
|
474
|
+
async transaction(handler, options) {
|
|
475
|
+
return transaction.run(handler, options)
|
|
469
476
|
},
|
|
470
477
|
} as EventStore<any, Root>
|
|
471
478
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from 'node:async_hooks'
|
|
2
2
|
|
|
3
|
+
import type { SettledOptions } from './createAggregateRoot.ts'
|
|
3
4
|
import {
|
|
4
5
|
type CheckpointApi,
|
|
5
6
|
type CheckpointMethods,
|
|
@@ -23,9 +24,21 @@ import type {
|
|
|
23
24
|
StreamEvents,
|
|
24
25
|
} from './utilityTypes.js'
|
|
25
26
|
|
|
27
|
+
/**
|
|
28
|
+
* Options for `createProcessManager`.
|
|
29
|
+
*
|
|
30
|
+
* `settled` — default {@link SettledOptions} applied when the process manager
|
|
31
|
+
* auto-settles the streams its handlers dispatched commands on (explicit
|
|
32
|
+
* `settled()` calls are not allowed inside a process manager).
|
|
33
|
+
*/
|
|
34
|
+
export type ProcessManagerOptions = {
|
|
35
|
+
settled?: SettledOptions
|
|
36
|
+
}
|
|
37
|
+
|
|
26
38
|
type ProcessManagerInfo = {
|
|
27
39
|
name: string
|
|
28
40
|
event: BaseOutputEvent
|
|
41
|
+
settledDefaults?: SettledOptions
|
|
29
42
|
}
|
|
30
43
|
const processManagerInfoStore = new AsyncLocalStorage<ProcessManagerInfo>()
|
|
31
44
|
|
|
@@ -129,7 +142,7 @@ export function buildProcessManagerCreator(params: {
|
|
|
129
142
|
checkpoint: CheckpointMethods
|
|
130
143
|
onRefresh?: OnProgress
|
|
131
144
|
}) {
|
|
132
|
-
return (name: string): ProcessManagerApi => {
|
|
145
|
+
return (name: string, options?: ProcessManagerOptions): ProcessManagerApi => {
|
|
133
146
|
let idle = true
|
|
134
147
|
let createEventHandlers: (
|
|
135
148
|
state: any,
|
|
@@ -196,6 +209,7 @@ export function buildProcessManagerCreator(params: {
|
|
|
196
209
|
{
|
|
197
210
|
name,
|
|
198
211
|
event,
|
|
212
|
+
settledDefaults: options?.settled,
|
|
199
213
|
},
|
|
200
214
|
async () => {
|
|
201
215
|
try {
|
package/src/index.ts
CHANGED
|
@@ -14,6 +14,7 @@ export {
|
|
|
14
14
|
} from './createProjection.ts'
|
|
15
15
|
export type { Checkpoint } from './createCheckpointApi.ts'
|
|
16
16
|
export {
|
|
17
|
+
type ProcessManagerOptions,
|
|
17
18
|
isInsideProcessManager,
|
|
18
19
|
getProcessManagerInfo,
|
|
19
20
|
} from './createProcessManager.ts'
|
|
@@ -26,9 +27,11 @@ export {
|
|
|
26
27
|
type Snapshot as AggregateRootSnapshot,
|
|
27
28
|
type AnyAggregateRoot,
|
|
28
29
|
type AnyStream,
|
|
30
|
+
type SettledOptions,
|
|
29
31
|
createAggregateRoot,
|
|
30
32
|
} from './createAggregateRoot.ts'
|
|
31
33
|
|
|
34
|
+
export type { TransactionOptions } from './transaction.ts'
|
|
32
35
|
export { skipRefreshing } from './refresh.ts'
|
|
33
36
|
export { skipReplay } from './replay.ts'
|
|
34
37
|
|