@entifix/transactions 0.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/LICENSE +21 -0
- package/dist/contracts/command.d.ts +59 -0
- package/dist/contracts/command.d.ts.map +1 -0
- package/dist/contracts/command.js +42 -0
- package/dist/contracts/command.js.map +1 -0
- package/dist/contracts/event.d.ts +84 -0
- package/dist/contracts/event.d.ts.map +1 -0
- package/dist/contracts/event.js +67 -0
- package/dist/contracts/event.js.map +1 -0
- package/dist/contracts/inbox.d.ts +80 -0
- package/dist/contracts/inbox.d.ts.map +1 -0
- package/dist/contracts/inbox.js +5 -0
- package/dist/contracts/inbox.js.map +1 -0
- package/dist/contracts/outbox.d.ts +137 -0
- package/dist/contracts/outbox.d.ts.map +1 -0
- package/dist/contracts/outbox.js +5 -0
- package/dist/contracts/outbox.js.map +1 -0
- package/dist/contracts/record.d.ts +108 -0
- package/dist/contracts/record.d.ts.map +1 -0
- package/dist/contracts/record.js +27 -0
- package/dist/contracts/record.js.map +1 -0
- package/dist/contracts/saga-definition.d.ts +186 -0
- package/dist/contracts/saga-definition.d.ts.map +1 -0
- package/dist/contracts/saga-definition.js +95 -0
- package/dist/contracts/saga-definition.js.map +1 -0
- package/dist/engine/resolve-template.d.ts +49 -0
- package/dist/engine/resolve-template.d.ts.map +1 -0
- package/dist/engine/resolve-template.js +106 -0
- package/dist/engine/resolve-template.js.map +1 -0
- package/dist/engine/run-saga.d.ts +55 -0
- package/dist/engine/run-saga.d.ts.map +1 -0
- package/dist/engine/run-saga.js +467 -0
- package/dist/engine/run-saga.js.map +1 -0
- package/dist/engine/run-transaction.d.ts +55 -0
- package/dist/engine/run-transaction.d.ts.map +1 -0
- package/dist/engine/run-transaction.js +101 -0
- package/dist/engine/run-transaction.js.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +22 -0
- package/dist/index.js.map +1 -0
- package/dist/mixins/transaction-mixins.d.ts +20 -0
- package/dist/mixins/transaction-mixins.d.ts.map +1 -0
- package/dist/mixins/transaction-mixins.js +13 -0
- package/dist/mixins/transaction-mixins.js.map +1 -0
- package/dist/ports/event-bus.d.ts +98 -0
- package/dist/ports/event-bus.d.ts.map +1 -0
- package/dist/ports/event-bus.js +5 -0
- package/dist/ports/event-bus.js.map +1 -0
- package/dist/ports/event-source.d.ts +22 -0
- package/dist/ports/event-source.d.ts.map +1 -0
- package/dist/ports/event-source.js +20 -0
- package/dist/ports/event-source.js.map +1 -0
- package/dist/ports/lock-service.d.ts +24 -0
- package/dist/ports/lock-service.d.ts.map +1 -0
- package/dist/ports/lock-service.js +5 -0
- package/dist/ports/lock-service.js.map +1 -0
- package/dist/ports/saga-dispatcher.d.ts +47 -0
- package/dist/ports/saga-dispatcher.d.ts.map +1 -0
- package/dist/ports/saga-dispatcher.js +5 -0
- package/dist/ports/saga-dispatcher.js.map +1 -0
- package/dist/ports/saga-store.d.ts +98 -0
- package/dist/ports/saga-store.d.ts.map +1 -0
- package/dist/ports/saga-store.js +20 -0
- package/dist/ports/saga-store.js.map +1 -0
- package/dist/ports/sequence-service.d.ts +17 -0
- package/dist/ports/sequence-service.d.ts.map +1 -0
- package/dist/ports/sequence-service.js +5 -0
- package/dist/ports/sequence-service.js.map +1 -0
- package/dist/ports/transaction-handler.d.ts +68 -0
- package/dist/ports/transaction-handler.d.ts.map +1 -0
- package/dist/ports/transaction-handler.js +5 -0
- package/dist/ports/transaction-handler.js.map +1 -0
- package/dist/ports/transaction-sink.d.ts +119 -0
- package/dist/ports/transaction-sink.d.ts.map +1 -0
- package/dist/ports/transaction-sink.js +28 -0
- package/dist/ports/transaction-sink.js.map +1 -0
- package/dist/stream/transaction-stream-hub.d.ts +41 -0
- package/dist/stream/transaction-stream-hub.d.ts.map +1 -0
- package/dist/stream/transaction-stream-hub.js +53 -0
- package/dist/stream/transaction-stream-hub.js.map +1 -0
- package/dist/use-case/facade.uc.d.ts +22 -0
- package/dist/use-case/facade.uc.d.ts.map +1 -0
- package/dist/use-case/facade.uc.js +55 -0
- package/dist/use-case/facade.uc.js.map +1 -0
- package/package.json +65 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type { EntifixConnError } from '@entifix/core';
|
|
2
|
+
import { Context, type Effect } from 'effect';
|
|
3
|
+
import type { SagaInputs, SagaStepOutcome } from '../contracts/saga-definition.js';
|
|
4
|
+
/**
|
|
5
|
+
* Where a saga instance is.
|
|
6
|
+
*
|
|
7
|
+
* `COMPENSATED` and `STRANDED` are deliberately different terminal states:
|
|
8
|
+
* "failed and reversed" and "failed with something still held" are the two
|
|
9
|
+
* outcomes an operator has to be able to tell apart, and a single `FAILED`
|
|
10
|
+
* would make the second look like the first
|
|
11
|
+
* ([ADR 0039](../../../../../../docs/adr/0039-multi-step-sagas-are-orchestrated.md)).
|
|
12
|
+
*/
|
|
13
|
+
export declare const SAGA_STATES: readonly ["RUNNING", "COMPLETED", "COMPENSATING", "COMPENSATED", "STRANDED"];
|
|
14
|
+
export type SagaState = (typeof SAGA_STATES)[number];
|
|
15
|
+
/** One flow in progress, and what each of its steps returned. */
|
|
16
|
+
export interface SagaInstance {
|
|
17
|
+
readonly sagaId: string;
|
|
18
|
+
/** The {@link SagaDefinition}'s `name`. */
|
|
19
|
+
readonly definition: string;
|
|
20
|
+
readonly state: SagaState;
|
|
21
|
+
/** The step being dispatched, or the last one dispatched. */
|
|
22
|
+
readonly stepIndex: number;
|
|
23
|
+
readonly outcomes: readonly SagaStepOutcome[];
|
|
24
|
+
/**
|
|
25
|
+
* What the caller supplied, kept so a **different process** can finish this
|
|
26
|
+
* flow.
|
|
27
|
+
*
|
|
28
|
+
* ⚠️ This is the member that makes the instance a resumable record rather
|
|
29
|
+
* than a report on one. Without it a resumed walk knows which step is next
|
|
30
|
+
* and has nothing to dispatch it with: a fan-out step's cardinality is its
|
|
31
|
+
* input's length, and a body template resolves against `inputs` before it
|
|
32
|
+
* resolves against earlier outcomes
|
|
33
|
+
* ([ADR 0055](../../../../../../docs/adr/0055-a-coordinator-resumes-from-its-own-record.md)).
|
|
34
|
+
*/
|
|
35
|
+
readonly inputs: SagaInputs;
|
|
36
|
+
/**
|
|
37
|
+
* How many times a sweep has picked this instance up, which is the ceiling
|
|
38
|
+
* that stops a resume loop.
|
|
39
|
+
*
|
|
40
|
+
* It plays the part ADR 0030's `attempts` plays on an outbox entry, on the
|
|
41
|
+
* record that already exists rather than on a second one beside it. Past the
|
|
42
|
+
* ceiling the instance is settled `STRANDED` and logged — surfaced rather
|
|
43
|
+
* than retried forever, because a coordinator spinning on a permanent failure
|
|
44
|
+
* is how a customer's money stays captured with nobody told.
|
|
45
|
+
*/
|
|
46
|
+
readonly resumeAttempts: number;
|
|
47
|
+
readonly error?: string;
|
|
48
|
+
readonly createdAt: string;
|
|
49
|
+
readonly updatedAt: string;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The saga's own state, in the `saga` store.
|
|
53
|
+
*
|
|
54
|
+
* A saga instance is a transaction's state with steps in it, so it belongs in
|
|
55
|
+
* the store that already holds transaction state — no new store, and the
|
|
56
|
+
* `transaction` slice keeps `domains: []`, because orchestration is a mechanism
|
|
57
|
+
* rather than a business domain (ADR 0039).
|
|
58
|
+
*/
|
|
59
|
+
export interface SagaStore {
|
|
60
|
+
start(instance: Omit<SagaInstance, 'updatedAt'>): Effect.Effect<void, EntifixConnError>;
|
|
61
|
+
/**
|
|
62
|
+
* Persist that a step is about to be dispatched.
|
|
63
|
+
*
|
|
64
|
+
* ⚠️ **Before the dispatch, never after.** ADR 0028's rule extended from
|
|
65
|
+
* events to commands: a coordinator that marks a step in flight only after
|
|
66
|
+
* calling has the dual write that record exists to close, with the same
|
|
67
|
+
* consequence — a crash between the two leaves a step that either ran twice
|
|
68
|
+
* or never ran, and the record says the opposite of the truth.
|
|
69
|
+
*/
|
|
70
|
+
beginStep(sagaId: string, stepIndex: number): Effect.Effect<void, EntifixConnError>;
|
|
71
|
+
recordOutcome(sagaId: string, outcome: SagaStepOutcome): Effect.Effect<void, EntifixConnError>;
|
|
72
|
+
settle(sagaId: string, state: SagaState, error?: string): Effect.Effect<void, EntifixConnError>;
|
|
73
|
+
get(sagaId: string): Effect.Effect<SagaInstance | undefined, EntifixConnError>;
|
|
74
|
+
/** Instances left `RUNNING` or `COMPENSATING` past a deadline, for the sweep. */
|
|
75
|
+
findStale(olderThanMs: number): Effect.Effect<readonly SagaInstance[], EntifixConnError>;
|
|
76
|
+
/**
|
|
77
|
+
* Take ownership of a stale instance, or answer `undefined`.
|
|
78
|
+
*
|
|
79
|
+
* ⚠️ **A claim, not a read**, and it is what makes the sweep safe to run in
|
|
80
|
+
* more than one process: the same conditional write that increments
|
|
81
|
+
* `resumeAttempts` also re-stamps `updatedAt`, so an instance already picked
|
|
82
|
+
* up is no longer stale and the second sweeper gets nothing. Reading and then
|
|
83
|
+
* resuming would have two coordinators dispatching one flow — survivable,
|
|
84
|
+
* because every participant claims the command id, but it would double every
|
|
85
|
+
* call and make the attempt ceiling meaningless.
|
|
86
|
+
*
|
|
87
|
+
* Re-stamping is also what stops *this* sweep's next tick finding the
|
|
88
|
+
* instance it is still working on.
|
|
89
|
+
*/
|
|
90
|
+
claimForResume(sagaId: string, olderThanMs: number): Effect.Effect<SagaInstance | undefined, EntifixConnError>;
|
|
91
|
+
/** Mark one step's calls as given back, so a resumed unwind skips them. */
|
|
92
|
+
markCompensated(sagaId: string, stepId: string): Effect.Effect<void, EntifixConnError>;
|
|
93
|
+
}
|
|
94
|
+
declare const SagaStoreTag_base: Context.TagClass<SagaStoreTag, "SagaStoreTag", SagaStore>;
|
|
95
|
+
export declare class SagaStoreTag extends SagaStoreTag_base {
|
|
96
|
+
}
|
|
97
|
+
export {};
|
|
98
|
+
//# sourceMappingURL=saga-store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"saga-store.d.ts","sourceRoot":"","sources":["../../src/ports/saga-store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AACtD,OAAO,EAAE,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,QAAQ,CAAC;AAE9C,OAAO,KAAK,EACV,UAAU,EACV,eAAe,EAChB,MAAM,iCAAiC,CAAC;AAEzC;;;;;;;;GAQG;AACH,eAAO,MAAM,WAAW,8EAMd,CAAC;AAEX,MAAM,MAAM,SAAS,GAAG,CAAC,OAAO,WAAW,CAAC,CAAC,MAAM,CAAC,CAAC;AAErD,iEAAiE;AACjE,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,2CAA2C;IAC3C,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAC;IAC1B,6DAA6D;IAC7D,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,QAAQ,EAAE,SAAS,eAAe,EAAE,CAAC;IAC9C;;;;;;;;;;OAUG;IACH,QAAQ,CAAC,MAAM,EAAE,UAAU,CAAC;IAC5B;;;;;;;;;OASG;IACH,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;IAChC,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,SAAS;IACxB,KAAK,CACH,QAAQ,EAAE,IAAI,CAAC,YAAY,EAAE,WAAW,CAAC,GACxC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,gBAAgB,CAAC,CAAC;IACzC;;;;;;;;OAQG;IACH,SAAS,CACP,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,MAAM,GAChB,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,gBAAgB,CAAC,CAAC;IACzC,aAAa,CACX,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,eAAe,GACvB,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,gBAAgB,CAAC,CAAC;IACzC,MAAM,CACJ,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,SAAS,EAChB,KAAK,CAAC,EAAE,MAAM,GACb,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,gBAAgB,CAAC,CAAC;IACzC,GAAG,CACD,MAAM,EAAE,MAAM,GACb,MAAM,CAAC,MAAM,CAAC,YAAY,GAAG,SAAS,EAAE,gBAAgB,CAAC,CAAC;IAC7D,iFAAiF;IACjF,SAAS,CACP,WAAW,EAAE,MAAM,GAClB,MAAM,CAAC,MAAM,CAAC,SAAS,YAAY,EAAE,EAAE,gBAAgB,CAAC,CAAC;IAC5D;;;;;;;;;;;;;OAaG;IACH,cAAc,CACZ,MAAM,EAAE,MAAM,EACd,WAAW,EAAE,MAAM,GAClB,MAAM,CAAC,MAAM,CAAC,YAAY,GAAG,SAAS,EAAE,gBAAgB,CAAC,CAAC;IAC7D,2EAA2E;IAC3E,eAAe,CACb,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,GACb,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,gBAAgB,CAAC,CAAC;CAC1C;;AAED,qBAAa,YAAa,SAAQ,iBAG/B;CAAG"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Context } from 'effect';
|
|
2
|
+
/**
|
|
3
|
+
* Where a saga instance is.
|
|
4
|
+
*
|
|
5
|
+
* `COMPENSATED` and `STRANDED` are deliberately different terminal states:
|
|
6
|
+
* "failed and reversed" and "failed with something still held" are the two
|
|
7
|
+
* outcomes an operator has to be able to tell apart, and a single `FAILED`
|
|
8
|
+
* would make the second look like the first
|
|
9
|
+
* ([ADR 0039](../../../../../../docs/adr/0039-multi-step-sagas-are-orchestrated.md)).
|
|
10
|
+
*/ export const SAGA_STATES = [
|
|
11
|
+
'RUNNING',
|
|
12
|
+
'COMPLETED',
|
|
13
|
+
'COMPENSATING',
|
|
14
|
+
'COMPENSATED',
|
|
15
|
+
'STRANDED'
|
|
16
|
+
];
|
|
17
|
+
export class SagaStoreTag extends Context.Tag('SagaStoreTag')() {
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
//# sourceMappingURL=saga-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/ports/saga-store.ts"],"sourcesContent":["import type { EntifixConnError } from '@entifix/core';\nimport { Context, type Effect } from 'effect';\n\nimport type {\n SagaInputs,\n SagaStepOutcome,\n} from '../contracts/saga-definition.js';\n\n/**\n * Where a saga instance is.\n *\n * `COMPENSATED` and `STRANDED` are deliberately different terminal states:\n * \"failed and reversed\" and \"failed with something still held\" are the two\n * outcomes an operator has to be able to tell apart, and a single `FAILED`\n * would make the second look like the first\n * ([ADR 0039](../../../../../../docs/adr/0039-multi-step-sagas-are-orchestrated.md)).\n */\nexport const SAGA_STATES = [\n 'RUNNING',\n 'COMPLETED',\n 'COMPENSATING',\n 'COMPENSATED',\n 'STRANDED',\n] as const;\n\nexport type SagaState = (typeof SAGA_STATES)[number];\n\n/** One flow in progress, and what each of its steps returned. */\nexport interface SagaInstance {\n readonly sagaId: string;\n /** The {@link SagaDefinition}'s `name`. */\n readonly definition: string;\n readonly state: SagaState;\n /** The step being dispatched, or the last one dispatched. */\n readonly stepIndex: number;\n readonly outcomes: readonly SagaStepOutcome[];\n /**\n * What the caller supplied, kept so a **different process** can finish this\n * flow.\n *\n * ⚠️ This is the member that makes the instance a resumable record rather\n * than a report on one. Without it a resumed walk knows which step is next\n * and has nothing to dispatch it with: a fan-out step's cardinality is its\n * input's length, and a body template resolves against `inputs` before it\n * resolves against earlier outcomes\n * ([ADR 0055](../../../../../../docs/adr/0055-a-coordinator-resumes-from-its-own-record.md)).\n */\n readonly inputs: SagaInputs;\n /**\n * How many times a sweep has picked this instance up, which is the ceiling\n * that stops a resume loop.\n *\n * It plays the part ADR 0030's `attempts` plays on an outbox entry, on the\n * record that already exists rather than on a second one beside it. Past the\n * ceiling the instance is settled `STRANDED` and logged — surfaced rather\n * than retried forever, because a coordinator spinning on a permanent failure\n * is how a customer's money stays captured with nobody told.\n */\n readonly resumeAttempts: number;\n readonly error?: string;\n readonly createdAt: string;\n readonly updatedAt: string;\n}\n\n/**\n * The saga's own state, in the `saga` store.\n *\n * A saga instance is a transaction's state with steps in it, so it belongs in\n * the store that already holds transaction state — no new store, and the\n * `transaction` slice keeps `domains: []`, because orchestration is a mechanism\n * rather than a business domain (ADR 0039).\n */\nexport interface SagaStore {\n start(\n instance: Omit<SagaInstance, 'updatedAt'>,\n ): Effect.Effect<void, EntifixConnError>;\n /**\n * Persist that a step is about to be dispatched.\n *\n * ⚠️ **Before the dispatch, never after.** ADR 0028's rule extended from\n * events to commands: a coordinator that marks a step in flight only after\n * calling has the dual write that record exists to close, with the same\n * consequence — a crash between the two leaves a step that either ran twice\n * or never ran, and the record says the opposite of the truth.\n */\n beginStep(\n sagaId: string,\n stepIndex: number,\n ): Effect.Effect<void, EntifixConnError>;\n recordOutcome(\n sagaId: string,\n outcome: SagaStepOutcome,\n ): Effect.Effect<void, EntifixConnError>;\n settle(\n sagaId: string,\n state: SagaState,\n error?: string,\n ): Effect.Effect<void, EntifixConnError>;\n get(\n sagaId: string,\n ): Effect.Effect<SagaInstance | undefined, EntifixConnError>;\n /** Instances left `RUNNING` or `COMPENSATING` past a deadline, for the sweep. */\n findStale(\n olderThanMs: number,\n ): Effect.Effect<readonly SagaInstance[], EntifixConnError>;\n /**\n * Take ownership of a stale instance, or answer `undefined`.\n *\n * ⚠️ **A claim, not a read**, and it is what makes the sweep safe to run in\n * more than one process: the same conditional write that increments\n * `resumeAttempts` also re-stamps `updatedAt`, so an instance already picked\n * up is no longer stale and the second sweeper gets nothing. Reading and then\n * resuming would have two coordinators dispatching one flow — survivable,\n * because every participant claims the command id, but it would double every\n * call and make the attempt ceiling meaningless.\n *\n * Re-stamping is also what stops *this* sweep's next tick finding the\n * instance it is still working on.\n */\n claimForResume(\n sagaId: string,\n olderThanMs: number,\n ): Effect.Effect<SagaInstance | undefined, EntifixConnError>;\n /** Mark one step's calls as given back, so a resumed unwind skips them. */\n markCompensated(\n sagaId: string,\n stepId: string,\n ): Effect.Effect<void, EntifixConnError>;\n}\n\nexport class SagaStoreTag extends Context.Tag('SagaStoreTag')<\n SagaStoreTag,\n SagaStore\n>() {}\n"],"names":["Context","SAGA_STATES","SagaStoreTag","Tag"],"mappings":"AACA,SAASA,OAAO,QAAqB,SAAS;AAO9C;;;;;;;;CAQC,GACD,OAAO,MAAMC,cAAc;IACzB;IACA;IACA;IACA;IACA;CACD,CAAU;AA2GX,OAAO,MAAMC,qBAAqBF,QAAQG,GAAG,CAAC;AAGzC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { EntifixConnError } from '@entifix/core';
|
|
2
|
+
import { Context, type Effect } from 'effect';
|
|
3
|
+
/**
|
|
4
|
+
* A monotonic, gap-free-under-contention counter — the mechanism that makes
|
|
5
|
+
* codes unique across service instances. Implemented as Redis `INCR` in
|
|
6
|
+
* `@entifix/redis`; the atomicity of `INCR` is what guarantees no two
|
|
7
|
+
* concurrent commands ever draw the same number.
|
|
8
|
+
*/
|
|
9
|
+
export interface SequenceService {
|
|
10
|
+
/** Atomically increments the named sequence and returns the new value. */
|
|
11
|
+
next(name: string): Effect.Effect<number, EntifixConnError>;
|
|
12
|
+
}
|
|
13
|
+
declare const SequenceServiceTag_base: Context.TagClass<SequenceServiceTag, "SequenceServiceTag", SequenceService>;
|
|
14
|
+
export declare class SequenceServiceTag extends SequenceServiceTag_base {
|
|
15
|
+
}
|
|
16
|
+
export {};
|
|
17
|
+
//# sourceMappingURL=sequence-service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sequence-service.d.ts","sourceRoot":"","sources":["../../src/ports/sequence-service.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AACtD,OAAO,EAAE,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,QAAQ,CAAC;AAE9C;;;;;GAKG;AACH,MAAM,WAAW,eAAe;IAC9B,0EAA0E;IAC1E,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAC;CAC7D;;AAED,qBAAa,kBAAmB,SAAQ,uBAGrC;CAAG"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/ports/sequence-service.ts"],"sourcesContent":["import type { EntifixConnError } from '@entifix/core';\nimport { Context, type Effect } from 'effect';\n\n/**\n * A monotonic, gap-free-under-contention counter — the mechanism that makes\n * codes unique across service instances. Implemented as Redis `INCR` in\n * `@entifix/redis`; the atomicity of `INCR` is what guarantees no two\n * concurrent commands ever draw the same number.\n */\nexport interface SequenceService {\n /** Atomically increments the named sequence and returns the new value. */\n next(name: string): Effect.Effect<number, EntifixConnError>;\n}\n\nexport class SequenceServiceTag extends Context.Tag('SequenceServiceTag')<\n SequenceServiceTag,\n SequenceService\n>() {}\n"],"names":["Context","SequenceServiceTag","Tag"],"mappings":"AACA,SAASA,OAAO,QAAqB,SAAS;AAa9C,OAAO,MAAMC,2BAA2BD,QAAQE,GAAG,CAAC;AAG/C"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { EntifixError, EntityId } from '@entifix/core';
|
|
2
|
+
import { Context, type Effect } from 'effect';
|
|
3
|
+
import type { TransactionCommand } from '../contracts/command.js';
|
|
4
|
+
/** What a successful `execute` produced — echoed into the completed event. */
|
|
5
|
+
export interface TransactionOutcome {
|
|
6
|
+
code: string;
|
|
7
|
+
entityId: EntityId;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* The domain-specific half of the facade. `validate`/`lock`/`execute`/`free`/
|
|
11
|
+
* `rollback` are standardised by the engine, but *what* a command validates to,
|
|
12
|
+
* *which* resources it locks, and *how* it executes/rolls back are the service's
|
|
13
|
+
* concern — injected as this handler.
|
|
14
|
+
*
|
|
15
|
+
* Methods return `R = never`: the service closes over its own dependencies (db,
|
|
16
|
+
* sequence, repository) when constructing the handler, exactly as
|
|
17
|
+
* `makeMongoRepository` closes over its `Db`.
|
|
18
|
+
*/
|
|
19
|
+
export interface TransactionHandler {
|
|
20
|
+
/** Reject a malformed/illegal command (surfaced as `400`). */
|
|
21
|
+
validate(command: TransactionCommand): Effect.Effect<void, EntifixError>;
|
|
22
|
+
/** The resource keys to lock for this command (e.g. the code sequence). */
|
|
23
|
+
lockKeys(command: TransactionCommand): readonly string[];
|
|
24
|
+
/**
|
|
25
|
+
* Assign the code and persist — the actual write.
|
|
26
|
+
*
|
|
27
|
+
* **The implementation must write the `completed` outbox entry in the same
|
|
28
|
+
* storage transaction as its state change**, as a full `OutboxEntry` whose
|
|
29
|
+
* `event` is the `DomainEvent` `completedEvent` builds and whose `eventId` is
|
|
30
|
+
* that event's `id`. The engine deliberately records
|
|
31
|
+
* nothing on the success path, because only an implementation holds the
|
|
32
|
+
* driver session that can make those two writes one fact; an engine-level
|
|
33
|
+
* record would be a second dual write, announcing a state change that may
|
|
34
|
+
* still roll back.
|
|
35
|
+
*
|
|
36
|
+
* Two rules that only a replica set will teach you, and only in production:
|
|
37
|
+
* drive the transaction with the driver's retrying helper (Mongo's
|
|
38
|
+
* `session.withTransaction`), because an election aborts in-flight
|
|
39
|
+
* transactions with a `TransientTransactionError` the *application* is
|
|
40
|
+
* expected to retry; and keep any non-transactional side effect — drawing a
|
|
41
|
+
* code from the Redis sequence, say — **outside** that retried callback, or a
|
|
42
|
+
* retry consumes a second value and skips a code.
|
|
43
|
+
*/
|
|
44
|
+
execute(command: TransactionCommand): Effect.Effect<TransactionOutcome, EntifixError>;
|
|
45
|
+
/**
|
|
46
|
+
* Undo a failed execution. Must be idempotent (e.g. delete-if-exists), since
|
|
47
|
+
* the recovery sweep may reach a transaction whose rollback already ran.
|
|
48
|
+
*
|
|
49
|
+
* **`outcome` is always `undefined` on the single-step path, and cannot be
|
|
50
|
+
* anything else there.** `rollback` runs on exactly one arm — the one where
|
|
51
|
+
* `execute` failed — so there is no outcome to hand it.
|
|
52
|
+
*
|
|
53
|
+
* That is a property of this path rather than an unfinished signature, and it
|
|
54
|
+
* stays true now that the multi-step engine exists beside it. A saga's
|
|
55
|
+
* compensation is dispatched by `runSaga` with **its own call's** recorded
|
|
56
|
+
* outcome (`SagaStepOutcome`), because reversing a reservation needs the
|
|
57
|
+
* reservation's id and a delete-if-exists does not — see
|
|
58
|
+
* [ADR 0052](../../../../../../docs/adr/0052-the-checkout-saga.md). A
|
|
59
|
+
* single-step handler is untouched by that, which is what ADR 0039 meant by
|
|
60
|
+
* "grows a second shape and keeps the first".
|
|
61
|
+
*/
|
|
62
|
+
rollback(command: TransactionCommand, outcome: TransactionOutcome | undefined): Effect.Effect<void, EntifixError>;
|
|
63
|
+
}
|
|
64
|
+
declare const TransactionHandlerTag_base: Context.TagClass<TransactionHandlerTag, "TransactionHandlerTag", TransactionHandler>;
|
|
65
|
+
export declare class TransactionHandlerTag extends TransactionHandlerTag_base {
|
|
66
|
+
}
|
|
67
|
+
export {};
|
|
68
|
+
//# sourceMappingURL=transaction-handler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transaction-handler.d.ts","sourceRoot":"","sources":["../../src/ports/transaction-handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAC5D,OAAO,EAAE,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,QAAQ,CAAC;AAE9C,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAElE,8EAA8E;AAC9E,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,QAAQ,CAAC;CACpB;AAED;;;;;;;;;GASG;AACH,MAAM,WAAW,kBAAkB;IACjC,8DAA8D;IAC9D,QAAQ,CAAC,OAAO,EAAE,kBAAkB,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC;IACzE,2EAA2E;IAC3E,QAAQ,CAAC,OAAO,EAAE,kBAAkB,GAAG,SAAS,MAAM,EAAE,CAAC;IACzD;;;;;;;;;;;;;;;;;;;OAmBG;IACH,OAAO,CACL,OAAO,EAAE,kBAAkB,GAC1B,MAAM,CAAC,MAAM,CAAC,kBAAkB,EAAE,YAAY,CAAC,CAAC;IACnD;;;;;;;;;;;;;;;;OAgBG;IACH,QAAQ,CACN,OAAO,EAAE,kBAAkB,EAC3B,OAAO,EAAE,kBAAkB,GAAG,SAAS,GACtC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC;CACtC;;AAED,qBAAa,qBAAsB,SAAQ,0BAGxC;CAAG"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/ports/transaction-handler.ts"],"sourcesContent":["import type { EntifixError, EntityId } from '@entifix/core';\nimport { Context, type Effect } from 'effect';\n\nimport type { TransactionCommand } from '../contracts/command.js';\n\n/** What a successful `execute` produced — echoed into the completed event. */\nexport interface TransactionOutcome {\n code: string;\n entityId: EntityId;\n}\n\n/**\n * The domain-specific half of the facade. `validate`/`lock`/`execute`/`free`/\n * `rollback` are standardised by the engine, but *what* a command validates to,\n * *which* resources it locks, and *how* it executes/rolls back are the service's\n * concern — injected as this handler.\n *\n * Methods return `R = never`: the service closes over its own dependencies (db,\n * sequence, repository) when constructing the handler, exactly as\n * `makeMongoRepository` closes over its `Db`.\n */\nexport interface TransactionHandler {\n /** Reject a malformed/illegal command (surfaced as `400`). */\n validate(command: TransactionCommand): Effect.Effect<void, EntifixError>;\n /** The resource keys to lock for this command (e.g. the code sequence). */\n lockKeys(command: TransactionCommand): readonly string[];\n /**\n * Assign the code and persist — the actual write.\n *\n * **The implementation must write the `completed` outbox entry in the same\n * storage transaction as its state change**, as a full `OutboxEntry` whose\n * `event` is the `DomainEvent` `completedEvent` builds and whose `eventId` is\n * that event's `id`. The engine deliberately records\n * nothing on the success path, because only an implementation holds the\n * driver session that can make those two writes one fact; an engine-level\n * record would be a second dual write, announcing a state change that may\n * still roll back.\n *\n * Two rules that only a replica set will teach you, and only in production:\n * drive the transaction with the driver's retrying helper (Mongo's\n * `session.withTransaction`), because an election aborts in-flight\n * transactions with a `TransientTransactionError` the *application* is\n * expected to retry; and keep any non-transactional side effect — drawing a\n * code from the Redis sequence, say — **outside** that retried callback, or a\n * retry consumes a second value and skips a code.\n */\n execute(\n command: TransactionCommand,\n ): Effect.Effect<TransactionOutcome, EntifixError>;\n /**\n * Undo a failed execution. Must be idempotent (e.g. delete-if-exists), since\n * the recovery sweep may reach a transaction whose rollback already ran.\n *\n * **`outcome` is always `undefined` on the single-step path, and cannot be\n * anything else there.** `rollback` runs on exactly one arm — the one where\n * `execute` failed — so there is no outcome to hand it.\n *\n * That is a property of this path rather than an unfinished signature, and it\n * stays true now that the multi-step engine exists beside it. A saga's\n * compensation is dispatched by `runSaga` with **its own call's** recorded\n * outcome (`SagaStepOutcome`), because reversing a reservation needs the\n * reservation's id and a delete-if-exists does not — see\n * [ADR 0052](../../../../../../docs/adr/0052-the-checkout-saga.md). A\n * single-step handler is untouched by that, which is what ADR 0039 meant by\n * \"grows a second shape and keeps the first\".\n */\n rollback(\n command: TransactionCommand,\n outcome: TransactionOutcome | undefined,\n ): Effect.Effect<void, EntifixError>;\n}\n\nexport class TransactionHandlerTag extends Context.Tag('TransactionHandlerTag')<\n TransactionHandlerTag,\n TransactionHandler\n>() {}\n"],"names":["Context","TransactionHandlerTag","Tag"],"mappings":"AACA,SAASA,OAAO,QAAqB,SAAS;AAuE9C,OAAO,MAAMC,8BAA8BD,QAAQE,GAAG,CAAC;AAGlD"}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { Context } from 'effect';
|
|
2
|
+
/**
|
|
3
|
+
* A write the browser has started and the server has not finished.
|
|
4
|
+
*
|
|
5
|
+
* The `transactionId` is also the stored entity's id — ADR 0028 makes the
|
|
6
|
+
* client's minted id the primary key — so this is enough to find the record
|
|
7
|
+
* later without holding a copy of it.
|
|
8
|
+
*/
|
|
9
|
+
export interface PendingTransaction {
|
|
10
|
+
readonly transactionId: string;
|
|
11
|
+
/** The target entity's `key`, so a settle knows which query scope to refresh. */
|
|
12
|
+
readonly entity: string;
|
|
13
|
+
/** ISO-8601, when the `202` came back. Drives the oldest-first eviction. */
|
|
14
|
+
readonly at: string;
|
|
15
|
+
}
|
|
16
|
+
/** A pending write, plus what reconciliation has since learned about it. */
|
|
17
|
+
export type PendingEntry = PendingTransaction & {
|
|
18
|
+
readonly state: 'pending' | 'failed';
|
|
19
|
+
/**
|
|
20
|
+
* The record as the browser holds it, so a list can show it before the server
|
|
21
|
+
* has one.
|
|
22
|
+
*
|
|
23
|
+
* ⚠️ **In memory only — never persisted.** The store's `partialize` strips it,
|
|
24
|
+
* for ADR 0032's reason: a class instance does not survive a JSON round trip,
|
|
25
|
+
* it comes back as something else. So a refresh keeps the *watch* and loses
|
|
26
|
+
* the row, which is the documented behaviour — past a refresh, server truth
|
|
27
|
+
* plus an honest notice beats a record rebuilt from a blob.
|
|
28
|
+
*
|
|
29
|
+
* Typed `unknown` because this package must not care what an entity is; the
|
|
30
|
+
* shell that put it here is the only thing that reads it back.
|
|
31
|
+
*/
|
|
32
|
+
readonly record?: unknown;
|
|
33
|
+
/**
|
|
34
|
+
* Free text from whatever threw, in whatever language it was written in.
|
|
35
|
+
*
|
|
36
|
+
* ⚠️ Diagnostic only — never the headline of anything a user reads. A
|
|
37
|
+
* transaction's failure carries no error *code*, so the message shown has to
|
|
38
|
+
* be a catalog key and this is the detail beside it (ADR 0043's residual).
|
|
39
|
+
*/
|
|
40
|
+
readonly reason?: string;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* How a transactional write announces itself to whoever is watching.
|
|
44
|
+
*
|
|
45
|
+
* The save adapter is the only place that knows a create went out as a command
|
|
46
|
+
* rather than as a plain `POST`, and it learns nothing back — the `202`
|
|
47
|
+
* describes a transaction, not an entity. So it tells this port, and the browser
|
|
48
|
+
* takes it from there.
|
|
49
|
+
*
|
|
50
|
+
* Framework-free and synchronous on purpose: the implementation is a React store
|
|
51
|
+
* action, and keeping that behind a port is what stops `entifix:transactions`
|
|
52
|
+
* knowing anything about React or about where the pending set is persisted.
|
|
53
|
+
*/
|
|
54
|
+
export interface TransactionSink {
|
|
55
|
+
began(pending: PendingTransaction): void;
|
|
56
|
+
}
|
|
57
|
+
declare const TransactionSinkTag_base: Context.TagClass<TransactionSinkTag, "TransactionSinkTag", TransactionSink>;
|
|
58
|
+
/**
|
|
59
|
+
* ⚠️ Read with `Effect.serviceOption`, never `yield*`.
|
|
60
|
+
*
|
|
61
|
+
* That keeps the tag out of the save adapter's `R`, so the storefront, the plain
|
|
62
|
+
* REST adapters and every existing spec compile and run with no layer to
|
|
63
|
+
* provide — an optional capability must not become a build break for callers
|
|
64
|
+
* that will never want it.
|
|
65
|
+
*
|
|
66
|
+
* The cost of that erasure is that **nothing forces a composition root to
|
|
67
|
+
* provide it**: forget to, and the read returns `None`, everything compiles, and
|
|
68
|
+
* the feature is silently dead. A spec pins the one place that must
|
|
69
|
+
* (`mergeContext`), because an invariant the compiler cannot hold is one a test
|
|
70
|
+
* has to.
|
|
71
|
+
*/
|
|
72
|
+
export declare class TransactionSinkTag extends TransactionSinkTag_base {
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The sink where nothing is watching — outside a mounted provider, and in every
|
|
76
|
+
* caller that never opted into command creates.
|
|
77
|
+
*
|
|
78
|
+
* A create on a plain route still works exactly as it did; it simply gets no
|
|
79
|
+
* optimistic treatment, which is an honest boundary rather than a write into an
|
|
80
|
+
* unscoped store.
|
|
81
|
+
*/
|
|
82
|
+
export declare const NoopTransactionSink: TransactionSink;
|
|
83
|
+
/**
|
|
84
|
+
* The full view of the pending set, for the side that reconciles it.
|
|
85
|
+
*
|
|
86
|
+
* It extends {@link TransactionSink} because one store is behind both: the
|
|
87
|
+
* adapter needs only `began`, and the settlement side needs to read entries and
|
|
88
|
+
* retire them. Two interfaces rather than one because they are consumed from
|
|
89
|
+
* opposite sides of a layer boundary — `entifix:react` may not import a shell,
|
|
90
|
+
* so the store is handed in as a prop, the way `EntityDraftStore` already is for
|
|
91
|
+
* autosave (ADR 0032).
|
|
92
|
+
*/
|
|
93
|
+
export interface PendingTransactionStore extends TransactionSink {
|
|
94
|
+
readonly entries: readonly PendingEntry[];
|
|
95
|
+
/** The write landed. Drop it; the server's copy is the truth now. */
|
|
96
|
+
settle(transactionId: string): void;
|
|
97
|
+
/**
|
|
98
|
+
* Holds the record the browser already has, so the list can render it while
|
|
99
|
+
* the write is still in flight — and answers whether this id was being
|
|
100
|
+
* watched at all.
|
|
101
|
+
*
|
|
102
|
+
* ⚠️ **The return value is why this is one call and not two.** A caller that
|
|
103
|
+
* asked `entries.some(...)` first would be reading a React render closure
|
|
104
|
+
* captured *before* its own `await`, so the announcement the save adapter made
|
|
105
|
+
* during that await is invisible to it and every transactional write reads as
|
|
106
|
+
* a plain one. Answering from the store's current state is the only version
|
|
107
|
+
* without that race.
|
|
108
|
+
*
|
|
109
|
+
* Separate from `began` because the save adapter announces before it returns
|
|
110
|
+
* and has no reason to know about lists.
|
|
111
|
+
*/
|
|
112
|
+
attach(transactionId: string, record: unknown): boolean;
|
|
113
|
+
/** The write failed terminally. Keep it, marked, until it is dismissed. */
|
|
114
|
+
fail(transactionId: string, reason?: string): void;
|
|
115
|
+
/** The operator has read the failure. */
|
|
116
|
+
dismiss(transactionId: string): void;
|
|
117
|
+
}
|
|
118
|
+
export {};
|
|
119
|
+
//# sourceMappingURL=transaction-sink.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transaction-sink.d.ts","sourceRoot":"","sources":["../../src/ports/transaction-sink.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAC;AAEjC;;;;;;GAMG;AACH,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,iFAAiF;IACjF,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,4EAA4E;IAC5E,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;CACrB;AAED,4EAA4E;AAC5E,MAAM,MAAM,YAAY,GAAG,kBAAkB,GAAG;IAC9C,QAAQ,CAAC,KAAK,EAAE,SAAS,GAAG,QAAQ,CAAC;IACrC;;;;;;;;;;;;OAYG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC;IAC1B;;;;;;OAMG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;CAC1B,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,MAAM,WAAW,eAAe;IAC9B,KAAK,CAAC,OAAO,EAAE,kBAAkB,GAAG,IAAI,CAAC;CAC1C;;AAED;;;;;;;;;;;;;GAaG;AACH,qBAAa,kBAAmB,SAAQ,uBAGrC;CAAG;AAEN;;;;;;;GAOG;AACH,eAAO,MAAM,mBAAmB,EAAE,eAEjC,CAAC;AAEF;;;;;;;;;GASG;AACH,MAAM,WAAW,uBAAwB,SAAQ,eAAe;IAC9D,QAAQ,CAAC,OAAO,EAAE,SAAS,YAAY,EAAE,CAAC;IAC1C,qEAAqE;IACrE,MAAM,CAAC,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;IACpC;;;;;;;;;;;;;;OAcG;IACH,MAAM,CAAC,aAAa,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,GAAG,OAAO,CAAC;IACxD,2EAA2E;IAC3E,IAAI,CAAC,aAAa,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACnD,yCAAyC;IACzC,OAAO,CAAC,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;CACtC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Context } from 'effect';
|
|
2
|
+
/**
|
|
3
|
+
* ⚠️ Read with `Effect.serviceOption`, never `yield*`.
|
|
4
|
+
*
|
|
5
|
+
* That keeps the tag out of the save adapter's `R`, so the storefront, the plain
|
|
6
|
+
* REST adapters and every existing spec compile and run with no layer to
|
|
7
|
+
* provide — an optional capability must not become a build break for callers
|
|
8
|
+
* that will never want it.
|
|
9
|
+
*
|
|
10
|
+
* The cost of that erasure is that **nothing forces a composition root to
|
|
11
|
+
* provide it**: forget to, and the read returns `None`, everything compiles, and
|
|
12
|
+
* the feature is silently dead. A spec pins the one place that must
|
|
13
|
+
* (`mergeContext`), because an invariant the compiler cannot hold is one a test
|
|
14
|
+
* has to.
|
|
15
|
+
*/ export class TransactionSinkTag extends Context.Tag('TransactionSinkTag')() {
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The sink where nothing is watching — outside a mounted provider, and in every
|
|
19
|
+
* caller that never opted into command creates.
|
|
20
|
+
*
|
|
21
|
+
* A create on a plain route still works exactly as it did; it simply gets no
|
|
22
|
+
* optimistic treatment, which is an honest boundary rather than a write into an
|
|
23
|
+
* unscoped store.
|
|
24
|
+
*/ export const NoopTransactionSink = {
|
|
25
|
+
began: ()=>undefined
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
//# sourceMappingURL=transaction-sink.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/ports/transaction-sink.ts"],"sourcesContent":["import { Context } from 'effect';\n\n/**\n * A write the browser has started and the server has not finished.\n *\n * The `transactionId` is also the stored entity's id — ADR 0028 makes the\n * client's minted id the primary key — so this is enough to find the record\n * later without holding a copy of it.\n */\nexport interface PendingTransaction {\n readonly transactionId: string;\n /** The target entity's `key`, so a settle knows which query scope to refresh. */\n readonly entity: string;\n /** ISO-8601, when the `202` came back. Drives the oldest-first eviction. */\n readonly at: string;\n}\n\n/** A pending write, plus what reconciliation has since learned about it. */\nexport type PendingEntry = PendingTransaction & {\n readonly state: 'pending' | 'failed';\n /**\n * The record as the browser holds it, so a list can show it before the server\n * has one.\n *\n * ⚠️ **In memory only — never persisted.** The store's `partialize` strips it,\n * for ADR 0032's reason: a class instance does not survive a JSON round trip,\n * it comes back as something else. So a refresh keeps the *watch* and loses\n * the row, which is the documented behaviour — past a refresh, server truth\n * plus an honest notice beats a record rebuilt from a blob.\n *\n * Typed `unknown` because this package must not care what an entity is; the\n * shell that put it here is the only thing that reads it back.\n */\n readonly record?: unknown;\n /**\n * Free text from whatever threw, in whatever language it was written in.\n *\n * ⚠️ Diagnostic only — never the headline of anything a user reads. A\n * transaction's failure carries no error *code*, so the message shown has to\n * be a catalog key and this is the detail beside it (ADR 0043's residual).\n */\n readonly reason?: string;\n};\n\n/**\n * How a transactional write announces itself to whoever is watching.\n *\n * The save adapter is the only place that knows a create went out as a command\n * rather than as a plain `POST`, and it learns nothing back — the `202`\n * describes a transaction, not an entity. So it tells this port, and the browser\n * takes it from there.\n *\n * Framework-free and synchronous on purpose: the implementation is a React store\n * action, and keeping that behind a port is what stops `entifix:transactions`\n * knowing anything about React or about where the pending set is persisted.\n */\nexport interface TransactionSink {\n began(pending: PendingTransaction): void;\n}\n\n/**\n * ⚠️ Read with `Effect.serviceOption`, never `yield*`.\n *\n * That keeps the tag out of the save adapter's `R`, so the storefront, the plain\n * REST adapters and every existing spec compile and run with no layer to\n * provide — an optional capability must not become a build break for callers\n * that will never want it.\n *\n * The cost of that erasure is that **nothing forces a composition root to\n * provide it**: forget to, and the read returns `None`, everything compiles, and\n * the feature is silently dead. A spec pins the one place that must\n * (`mergeContext`), because an invariant the compiler cannot hold is one a test\n * has to.\n */\nexport class TransactionSinkTag extends Context.Tag('TransactionSinkTag')<\n TransactionSinkTag,\n TransactionSink\n>() {}\n\n/**\n * The sink where nothing is watching — outside a mounted provider, and in every\n * caller that never opted into command creates.\n *\n * A create on a plain route still works exactly as it did; it simply gets no\n * optimistic treatment, which is an honest boundary rather than a write into an\n * unscoped store.\n */\nexport const NoopTransactionSink: TransactionSink = {\n began: () => undefined,\n};\n\n/**\n * The full view of the pending set, for the side that reconciles it.\n *\n * It extends {@link TransactionSink} because one store is behind both: the\n * adapter needs only `began`, and the settlement side needs to read entries and\n * retire them. Two interfaces rather than one because they are consumed from\n * opposite sides of a layer boundary — `entifix:react` may not import a shell,\n * so the store is handed in as a prop, the way `EntityDraftStore` already is for\n * autosave (ADR 0032).\n */\nexport interface PendingTransactionStore extends TransactionSink {\n readonly entries: readonly PendingEntry[];\n /** The write landed. Drop it; the server's copy is the truth now. */\n settle(transactionId: string): void;\n /**\n * Holds the record the browser already has, so the list can render it while\n * the write is still in flight — and answers whether this id was being\n * watched at all.\n *\n * ⚠️ **The return value is why this is one call and not two.** A caller that\n * asked `entries.some(...)` first would be reading a React render closure\n * captured *before* its own `await`, so the announcement the save adapter made\n * during that await is invisible to it and every transactional write reads as\n * a plain one. Answering from the store's current state is the only version\n * without that race.\n *\n * Separate from `began` because the save adapter announces before it returns\n * and has no reason to know about lists.\n */\n attach(transactionId: string, record: unknown): boolean;\n /** The write failed terminally. Keep it, marked, until it is dismissed. */\n fail(transactionId: string, reason?: string): void;\n /** The operator has read the failure. */\n dismiss(transactionId: string): void;\n}\n"],"names":["Context","TransactionSinkTag","Tag","NoopTransactionSink","began","undefined"],"mappings":"AAAA,SAASA,OAAO,QAAQ,SAAS;AA4DjC;;;;;;;;;;;;;CAaC,GACD,OAAO,MAAMC,2BAA2BD,QAAQE,GAAG,CAAC;AAG/C;AAEL;;;;;;;CAOC,GACD,OAAO,MAAMC,sBAAuC;IAClDC,OAAO,IAAMC;AACf,EAAE"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { DomainEvent, EntityChangeEvent } from '@entifix/core';
|
|
2
|
+
import { Context, Effect, PubSub, Stream } from 'effect';
|
|
3
|
+
import type { TransactionEvent } from '../contracts/event.js';
|
|
4
|
+
/**
|
|
5
|
+
* Rewrites a transaction event as the entity change it announces, keeping the
|
|
6
|
+
* message's own metadata untouched.
|
|
7
|
+
*
|
|
8
|
+
* No fourth framing and no duplicated fields: the transaction id, the timestamp
|
|
9
|
+
* and the sequence a consumer needs are already `meta.event.correlationId`,
|
|
10
|
+
* `.at` and `.id` on the message this returns (ADR 0036).
|
|
11
|
+
*/
|
|
12
|
+
export declare const entityChangeFor: (event: DomainEvent<TransactionEvent>) => DomainEvent<EntityChangeEvent>;
|
|
13
|
+
/**
|
|
14
|
+
* The in-process fan-out behind `GET /api/transaction/events`.
|
|
15
|
+
*
|
|
16
|
+
* One subscription to the bus feeds it; each connected browser takes its own
|
|
17
|
+
* filtered view. It is a hub rather than a bus subscription per connection
|
|
18
|
+
* because a queue per open tab is a broker resource a client controls.
|
|
19
|
+
*/
|
|
20
|
+
export interface TransactionStreamHub {
|
|
21
|
+
/** Offer one bus event to every connection scoped to its organization. */
|
|
22
|
+
publish(event: DomainEvent<TransactionEvent>): Effect.Effect<void>;
|
|
23
|
+
/**
|
|
24
|
+
* One connection's view, already scoped and already mapped.
|
|
25
|
+
*
|
|
26
|
+
* ⚠️ **Fails closed.** An event whose `organizationId` does not match is
|
|
27
|
+
* dropped, and so is one carrying none at all — including every event emitted
|
|
28
|
+
* before that member existed. Defaulting an absent organization to "everyone"
|
|
29
|
+
* is a cross-tenant delivery that raises nothing.
|
|
30
|
+
*/
|
|
31
|
+
subscribe(organizationId: string): Stream.Stream<DomainEvent<EntityChangeEvent>>;
|
|
32
|
+
}
|
|
33
|
+
declare const TransactionStreamHubTag_base: Context.TagClass<TransactionStreamHubTag, "TransactionStreamHubTag", TransactionStreamHub>;
|
|
34
|
+
export declare class TransactionStreamHubTag extends TransactionStreamHubTag_base {
|
|
35
|
+
}
|
|
36
|
+
/** Builds a hub over a sliding {@link PubSub}. */
|
|
37
|
+
export declare const makeTransactionStreamHub: (pubsub: PubSub.PubSub<DomainEvent<TransactionEvent>>) => TransactionStreamHub;
|
|
38
|
+
/** Opens a hub. Scoped, so its subscribers are released with the scope. */
|
|
39
|
+
export declare const makeTransactionStreamHubEffect: Effect.Effect<TransactionStreamHub, never, never>;
|
|
40
|
+
export {};
|
|
41
|
+
//# sourceMappingURL=transaction-stream-hub.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transaction-stream-hub.d.ts","sourceRoot":"","sources":["../../src/stream/transaction-stream-hub.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AACpE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEzD,OAAO,KAAK,EAAE,gBAAgB,EAAoB,MAAM,uBAAuB,CAAC;AA4BhF;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,GAC1B,OAAO,WAAW,CAAC,gBAAgB,CAAC,KACnC,WAAW,CAAC,iBAAiB,CAW9B,CAAC;AAEH;;;;;;GAMG;AACH,MAAM,WAAW,oBAAoB;IACnC,0EAA0E;IAC1E,OAAO,CAAC,KAAK,EAAE,WAAW,CAAC,gBAAgB,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACnE;;;;;;;OAOG;IACH,SAAS,CACP,cAAc,EAAE,MAAM,GACrB,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,iBAAiB,CAAC,CAAC,CAAC;CAClD;;AAED,qBAAa,uBAAwB,SAAQ,4BAEK;CAAG;AAErD,kDAAkD;AAClD,eAAO,MAAM,wBAAwB,GACnC,QAAQ,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,gBAAgB,CAAC,CAAC,KACnD,oBAOD,CAAC;AAEH,2EAA2E;AAC3E,eAAO,MAAM,8BAA8B,mDAG1C,CAAC"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { Context, Effect, PubSub, Stream } from 'effect';
|
|
2
|
+
/**
|
|
3
|
+
* How many undelivered events one hub holds before the oldest are dropped.
|
|
4
|
+
*
|
|
5
|
+
* Bounded with a **sliding** strategy rather than unbounded: a browser that
|
|
6
|
+
* stops reading its connection must not be able to grow the service's heap, and
|
|
7
|
+
* a reactive hint is worth less the older it is — the record behind it is the
|
|
8
|
+
* truth, and re-reading it is cheap (ADR 0036 declines replay for the same
|
|
9
|
+
* reason).
|
|
10
|
+
*/ const CAPACITY = 256;
|
|
11
|
+
/**
|
|
12
|
+
* What a transaction's state means for the entity it is about.
|
|
13
|
+
*
|
|
14
|
+
* `type: 'create'` is the only command that exists, so `PENDING` and
|
|
15
|
+
* `COMPLETED` both announce an entity coming into existence — the id is already
|
|
16
|
+
* known at `accepted`, because ADR 0028 makes the client-minted transaction id
|
|
17
|
+
* the stored entity's id. A terminal failure announces the opposite: nothing was
|
|
18
|
+
* written, so a consumer holding an optimistic row must drop it.
|
|
19
|
+
*
|
|
20
|
+
* When `update`/`delete` commands land this keys off `command.type` instead;
|
|
21
|
+
* the state alone cannot distinguish them.
|
|
22
|
+
*/ const changeFor = (state)=>state === 'PENDING' || state === 'COMPLETED' ? 'created' : 'deleted';
|
|
23
|
+
/**
|
|
24
|
+
* Rewrites a transaction event as the entity change it announces, keeping the
|
|
25
|
+
* message's own metadata untouched.
|
|
26
|
+
*
|
|
27
|
+
* No fourth framing and no duplicated fields: the transaction id, the timestamp
|
|
28
|
+
* and the sequence a consumer needs are already `meta.event.correlationId`,
|
|
29
|
+
* `.at` and `.id` on the message this returns (ADR 0036).
|
|
30
|
+
*/ export const entityChangeFor = (event)=>{
|
|
31
|
+
var _event_data_entityId;
|
|
32
|
+
return {
|
|
33
|
+
name: event.name,
|
|
34
|
+
id: event.id,
|
|
35
|
+
source: event.source,
|
|
36
|
+
at: event.at,
|
|
37
|
+
correlationId: event.correlationId,
|
|
38
|
+
data: {
|
|
39
|
+
entity: event.data.entity,
|
|
40
|
+
change: changeFor(event.data.state),
|
|
41
|
+
id: (_event_data_entityId = event.data.entityId) != null ? _event_data_entityId : event.data.transactionId
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
export class TransactionStreamHubTag extends Context.Tag('TransactionStreamHubTag')() {
|
|
46
|
+
}
|
|
47
|
+
/** Builds a hub over a sliding {@link PubSub}. */ export const makeTransactionStreamHub = (pubsub)=>({
|
|
48
|
+
publish: (event)=>Effect.asVoid(PubSub.publish(pubsub, event)),
|
|
49
|
+
subscribe: (organizationId)=>Stream.fromPubSub(pubsub).pipe(Stream.filter((event)=>event.data.organizationId === organizationId), Stream.map(entityChangeFor))
|
|
50
|
+
});
|
|
51
|
+
/** Opens a hub. Scoped, so its subscribers are released with the scope. */ export const makeTransactionStreamHubEffect = Effect.map(PubSub.sliding(CAPACITY), makeTransactionStreamHub);
|
|
52
|
+
|
|
53
|
+
//# sourceMappingURL=transaction-stream-hub.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/stream/transaction-stream-hub.ts"],"sourcesContent":["import type { DomainEvent, EntityChangeEvent } from '@entifix/core';\nimport { Context, Effect, PubSub, Stream } from 'effect';\n\nimport type { TransactionEvent, TransactionState } from '../contracts/event.js';\n\n/**\n * How many undelivered events one hub holds before the oldest are dropped.\n *\n * Bounded with a **sliding** strategy rather than unbounded: a browser that\n * stops reading its connection must not be able to grow the service's heap, and\n * a reactive hint is worth less the older it is — the record behind it is the\n * truth, and re-reading it is cheap (ADR 0036 declines replay for the same\n * reason).\n */\nconst CAPACITY = 256;\n\n/**\n * What a transaction's state means for the entity it is about.\n *\n * `type: 'create'` is the only command that exists, so `PENDING` and\n * `COMPLETED` both announce an entity coming into existence — the id is already\n * known at `accepted`, because ADR 0028 makes the client-minted transaction id\n * the stored entity's id. A terminal failure announces the opposite: nothing was\n * written, so a consumer holding an optimistic row must drop it.\n *\n * When `update`/`delete` commands land this keys off `command.type` instead;\n * the state alone cannot distinguish them.\n */\nconst changeFor = (state: TransactionState): EntityChangeEvent['change'] =>\n state === 'PENDING' || state === 'COMPLETED' ? 'created' : 'deleted';\n\n/**\n * Rewrites a transaction event as the entity change it announces, keeping the\n * message's own metadata untouched.\n *\n * No fourth framing and no duplicated fields: the transaction id, the timestamp\n * and the sequence a consumer needs are already `meta.event.correlationId`,\n * `.at` and `.id` on the message this returns (ADR 0036).\n */\nexport const entityChangeFor = (\n event: DomainEvent<TransactionEvent>,\n): DomainEvent<EntityChangeEvent> => ({\n name: event.name,\n id: event.id,\n source: event.source,\n at: event.at,\n correlationId: event.correlationId,\n data: {\n entity: event.data.entity,\n change: changeFor(event.data.state),\n id: event.data.entityId ?? event.data.transactionId,\n },\n});\n\n/**\n * The in-process fan-out behind `GET /api/transaction/events`.\n *\n * One subscription to the bus feeds it; each connected browser takes its own\n * filtered view. It is a hub rather than a bus subscription per connection\n * because a queue per open tab is a broker resource a client controls.\n */\nexport interface TransactionStreamHub {\n /** Offer one bus event to every connection scoped to its organization. */\n publish(event: DomainEvent<TransactionEvent>): Effect.Effect<void>;\n /**\n * One connection's view, already scoped and already mapped.\n *\n * ⚠️ **Fails closed.** An event whose `organizationId` does not match is\n * dropped, and so is one carrying none at all — including every event emitted\n * before that member existed. Defaulting an absent organization to \"everyone\"\n * is a cross-tenant delivery that raises nothing.\n */\n subscribe(\n organizationId: string,\n ): Stream.Stream<DomainEvent<EntityChangeEvent>>;\n}\n\nexport class TransactionStreamHubTag extends Context.Tag(\n 'TransactionStreamHubTag',\n)<TransactionStreamHubTag, TransactionStreamHub>() {}\n\n/** Builds a hub over a sliding {@link PubSub}. */\nexport const makeTransactionStreamHub = (\n pubsub: PubSub.PubSub<DomainEvent<TransactionEvent>>,\n): TransactionStreamHub => ({\n publish: event => Effect.asVoid(PubSub.publish(pubsub, event)),\n subscribe: organizationId =>\n Stream.fromPubSub(pubsub).pipe(\n Stream.filter(event => event.data.organizationId === organizationId),\n Stream.map(entityChangeFor),\n ),\n});\n\n/** Opens a hub. Scoped, so its subscribers are released with the scope. */\nexport const makeTransactionStreamHubEffect = Effect.map(\n PubSub.sliding<DomainEvent<TransactionEvent>>(CAPACITY),\n makeTransactionStreamHub,\n);\n"],"names":["Context","Effect","PubSub","Stream","CAPACITY","changeFor","state","entityChangeFor","event","name","id","source","at","correlationId","data","entity","change","entityId","transactionId","TransactionStreamHubTag","Tag","makeTransactionStreamHub","pubsub","publish","asVoid","subscribe","organizationId","fromPubSub","pipe","filter","map","makeTransactionStreamHubEffect","sliding"],"mappings":"AACA,SAASA,OAAO,EAAEC,MAAM,EAAEC,MAAM,EAAEC,MAAM,QAAQ,SAAS;AAIzD;;;;;;;;CAQC,GACD,MAAMC,WAAW;AAEjB;;;;;;;;;;;CAWC,GACD,MAAMC,YAAY,CAACC,QACjBA,UAAU,aAAaA,UAAU,cAAc,YAAY;AAE7D;;;;;;;CAOC,GACD,OAAO,MAAMC,kBAAkB,CAC7BC;QAUMA;WAT8B;QACpCC,MAAMD,MAAMC,IAAI;QAChBC,IAAIF,MAAME,EAAE;QACZC,QAAQH,MAAMG,MAAM;QACpBC,IAAIJ,MAAMI,EAAE;QACZC,eAAeL,MAAMK,aAAa;QAClCC,MAAM;YACJC,QAAQP,MAAMM,IAAI,CAACC,MAAM;YACzBC,QAAQX,UAAUG,MAAMM,IAAI,CAACR,KAAK;YAClCI,EAAE,GAAEF,uBAAAA,MAAMM,IAAI,CAACG,QAAQ,YAAnBT,uBAAuBA,MAAMM,IAAI,CAACI,aAAa;QACrD;IACF;EAAG;AAyBH,OAAO,MAAMC,gCAAgCnB,QAAQoB,GAAG,CACtD;AACkD;AAEpD,gDAAgD,GAChD,OAAO,MAAMC,2BAA2B,CACtCC,SAC0B,CAAA;QAC1BC,SAASf,CAAAA,QAASP,OAAOuB,MAAM,CAACtB,OAAOqB,OAAO,CAACD,QAAQd;QACvDiB,WAAWC,CAAAA,iBACTvB,OAAOwB,UAAU,CAACL,QAAQM,IAAI,CAC5BzB,OAAO0B,MAAM,CAACrB,CAAAA,QAASA,MAAMM,IAAI,CAACY,cAAc,KAAKA,iBACrDvB,OAAO2B,GAAG,CAACvB;IAEjB,CAAA,EAAG;AAEH,yEAAyE,GACzE,OAAO,MAAMwB,iCAAiC9B,OAAO6B,GAAG,CACtD5B,OAAO8B,OAAO,CAAgC5B,WAC9CiB,0BACA"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Effect } from 'effect';
|
|
2
|
+
import { CommandTag, LockHandlesTag, OutcomeTag } from '../mixins/transaction-mixins.js';
|
|
3
|
+
import { type LockHandle, LockServiceTag } from '../ports/lock-service.js';
|
|
4
|
+
import { TransactionHandlerTag } from '../ports/transaction-handler.js';
|
|
5
|
+
/**
|
|
6
|
+
* The five-step transaction facade, each step a use-case in the same
|
|
7
|
+
* `*UCFactory -> Effect.gen -> yield Context.Tag` style as the entity
|
|
8
|
+
* use-cases. Inputs arrive through Context so a step is agnostic to how the
|
|
9
|
+
* command/handles/outcome were produced. The {@link runTransaction} engine
|
|
10
|
+
* sequences them.
|
|
11
|
+
*/
|
|
12
|
+
/** 1. Validate — delegates to the injected domain handler. */
|
|
13
|
+
export declare function validateUCFactory(): Effect.Effect<void, import("@entifix/core").EntifixError, TransactionHandlerTag | CommandTag>;
|
|
14
|
+
/** 2. Lock — acquire every resource key the handler declares for the command. */
|
|
15
|
+
export declare function lockUCFactory(): Effect.Effect<readonly LockHandle[], import("@entifix/core").EntifixLockError, TransactionHandlerTag | LockServiceTag | CommandTag>;
|
|
16
|
+
/** 3. Execute — the write; returns the assigned code + stored id. */
|
|
17
|
+
export declare function executeUCFactory(): Effect.Effect<import("../index.js").TransactionOutcome, import("@entifix/core").EntifixError, TransactionHandlerTag | CommandTag>;
|
|
18
|
+
/** 4. Rollback — undo a failed execution (idempotent). */
|
|
19
|
+
export declare function rollbackUCFactory(): Effect.Effect<void, import("@entifix/core").EntifixError, TransactionHandlerTag | CommandTag | OutcomeTag>;
|
|
20
|
+
/** 5. Free — release every held lock, best-effort per handle. */
|
|
21
|
+
export declare function freeUCFactory(): Effect.Effect<void, import("@entifix/core").EntifixConnError, LockServiceTag | LockHandlesTag>;
|
|
22
|
+
//# sourceMappingURL=facade.uc.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"facade.uc.d.ts","sourceRoot":"","sources":["../../src/use-case/facade.uc.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,OAAO,EACL,UAAU,EACV,cAAc,EACd,UAAU,EACX,MAAM,iCAAiC,CAAC;AACzC,OAAO,EAAE,KAAK,UAAU,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAC3E,OAAO,EAAE,qBAAqB,EAAE,MAAM,iCAAiC,CAAC;AAExE;;;;;;GAMG;AAEH,8DAA8D;AAC9D,wBAAgB,iBAAiB,kGAMhC;AAED,iFAAiF;AACjF,wBAAgB,aAAa,wIAY5B;AAED,qEAAqE;AACrE,wBAAgB,gBAAgB,sIAM/B;AAED,0DAA0D;AAC1D,wBAAgB,iBAAiB,+GAOhC;AAED,iEAAiE;AACjE,wBAAgB,aAAa,mGAQ5B"}
|