@entifix/transactions 0.5.0 → 0.7.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/dist/contracts/command.d.ts +2 -3
- package/dist/contracts/command.d.ts.map +1 -1
- package/dist/contracts/command.js.map +1 -1
- package/dist/contracts/event.d.ts +3 -3
- package/dist/contracts/event.js.map +1 -1
- package/dist/contracts/inbox.d.ts +1 -1
- package/dist/contracts/inbox.js.map +1 -1
- package/dist/contracts/outbox.d.ts +4 -5
- package/dist/contracts/outbox.d.ts.map +1 -1
- package/dist/contracts/outbox.js.map +1 -1
- package/dist/contracts/record.d.ts +4 -5
- package/dist/contracts/record.d.ts.map +1 -1
- package/dist/contracts/record.js.map +1 -1
- package/dist/contracts/saga-definition.d.ts +15 -20
- package/dist/contracts/saga-definition.d.ts.map +1 -1
- package/dist/contracts/saga-definition.js +6 -6
- package/dist/contracts/saga-definition.js.map +1 -1
- package/dist/engine/resolve-template.d.ts +2 -3
- package/dist/engine/resolve-template.d.ts.map +1 -1
- package/dist/engine/resolve-template.js +2 -3
- package/dist/engine/resolve-template.js.map +1 -1
- package/dist/engine/run-saga.d.ts +1 -2
- package/dist/engine/run-saga.d.ts.map +1 -1
- package/dist/engine/run-saga.js +13 -17
- package/dist/engine/run-saga.js.map +1 -1
- package/dist/ports/event-bus.d.ts +6 -7
- package/dist/ports/event-bus.d.ts.map +1 -1
- package/dist/ports/event-bus.js.map +1 -1
- package/dist/ports/event-source.d.ts +3 -3
- package/dist/ports/event-source.js +3 -3
- package/dist/ports/event-source.js.map +1 -1
- package/dist/ports/saga-dispatcher.d.ts +3 -4
- package/dist/ports/saga-dispatcher.d.ts.map +1 -1
- package/dist/ports/saga-dispatcher.js.map +1 -1
- package/dist/ports/saga-store.d.ts +5 -7
- package/dist/ports/saga-store.d.ts.map +1 -1
- package/dist/ports/saga-store.js +1 -2
- package/dist/ports/saga-store.js.map +1 -1
- package/dist/ports/transaction-handler.d.ts +2 -4
- package/dist/ports/transaction-handler.d.ts.map +1 -1
- package/dist/ports/transaction-handler.js.map +1 -1
- package/dist/ports/transaction-sink.d.ts +6 -5
- package/dist/ports/transaction-sink.d.ts.map +1 -1
- package/dist/ports/transaction-sink.js.map +1 -1
- package/dist/stream/transaction-stream-hub.d.ts +1 -1
- package/dist/stream/transaction-stream-hub.d.ts.map +1 -1
- package/dist/stream/transaction-stream-hub.js +4 -5
- package/dist/stream/transaction-stream-hub.js.map +1 -1
- package/package.json +3 -3
|
@@ -1 +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
|
|
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 */\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 */\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 `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.\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.** The outbox'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.Service<SagaStoreTag, SagaStore>()(\n 'SagaStoreTag',\n) {}\n"],"names":["Context","SAGA_STATES","SagaStoreTag","Service"],"mappings":"AACA,SAASA,OAAO,QAAqB,SAAS;AAO9C;;;;;;;CAOC,GACD,OAAO,MAAMC,cAAc;IACzB;IACA;IACA;IACA;IACA;CACD,CAAU;AA0GX,OAAO,MAAMC,qBAAqBF,QAAQG,OAAO,GAC/C;AACC"}
|
|
@@ -54,10 +54,8 @@ export interface TransactionHandler {
|
|
|
54
54
|
* stays true now that the multi-step engine exists beside it. A saga's
|
|
55
55
|
* compensation is dispatched by `runSaga` with **its own call's** recorded
|
|
56
56
|
* outcome (`SagaStepOutcome`), because reversing a reservation needs the
|
|
57
|
-
* reservation's id and a delete-if-exists does not
|
|
58
|
-
*
|
|
59
|
-
* single-step handler is untouched by that, which is what ADR 0039 meant by
|
|
60
|
-
* "grows a second shape and keeps the first".
|
|
57
|
+
* reservation's id and a delete-if-exists does not. A single-step handler is
|
|
58
|
+
* untouched by that: the contract grows a second shape and keeps the first.
|
|
61
59
|
*/
|
|
62
60
|
rollback(command: TransactionCommand, outcome: TransactionOutcome | undefined): Effect.Effect<void, EntifixError>;
|
|
63
61
|
}
|
|
@@ -1 +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
|
|
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;;;;;;;;;;;;;;OAcG;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,0BAGf;CAAG"}
|
|
@@ -1 +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
|
|
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. A single-step handler is\n * untouched by that: the contract 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.Service<\n TransactionHandlerTag,\n TransactionHandler\n>()('TransactionHandlerTag') {}\n"],"names":["Context","TransactionHandlerTag","Service"],"mappings":"AACA,SAASA,OAAO,QAAqB,SAAS;AAqE9C,OAAO,MAAMC,8BAA8BD,QAAQE,OAAO,GAGtD;AAA0B"}
|
|
@@ -2,8 +2,8 @@ import { Context } from 'effect';
|
|
|
2
2
|
/**
|
|
3
3
|
* A write the browser has started and the server has not finished.
|
|
4
4
|
*
|
|
5
|
-
* The `transactionId` is also the stored entity's id —
|
|
6
|
-
*
|
|
5
|
+
* The `transactionId` is also the stored entity's id — the client's minted id
|
|
6
|
+
* is the primary key — so this is enough to find the record
|
|
7
7
|
* later without holding a copy of it.
|
|
8
8
|
*/
|
|
9
9
|
export interface PendingTransaction {
|
|
@@ -21,7 +21,8 @@ export type PendingEntry = PendingTransaction & {
|
|
|
21
21
|
* has one.
|
|
22
22
|
*
|
|
23
23
|
* ⚠️ **In memory only — never persisted.** The store's `partialize` strips it,
|
|
24
|
-
* for
|
|
24
|
+
* for the reason an autosaved draft gives: a class instance does not survive
|
|
25
|
+
* a JSON round trip,
|
|
25
26
|
* it comes back as something else. So a refresh keeps the *watch* and loses
|
|
26
27
|
* the row, which is the documented behaviour — past a refresh, server truth
|
|
27
28
|
* plus an honest notice beats a record rebuilt from a blob.
|
|
@@ -35,7 +36,7 @@ export type PendingEntry = PendingTransaction & {
|
|
|
35
36
|
*
|
|
36
37
|
* ⚠️ Diagnostic only — never the headline of anything a user reads. A
|
|
37
38
|
* 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
|
|
39
|
+
* be a catalog key and this is the detail beside it.
|
|
39
40
|
*/
|
|
40
41
|
readonly reason?: string;
|
|
41
42
|
};
|
|
@@ -88,7 +89,7 @@ export declare const NoopTransactionSink: TransactionSink;
|
|
|
88
89
|
* retire them. Two interfaces rather than one because they are consumed from
|
|
89
90
|
* opposite sides of a tier boundary — a T3 package may not import a T4 shell,
|
|
90
91
|
* so the store is handed in as a prop, the way `EntityDraftStore` already is for
|
|
91
|
-
* autosave
|
|
92
|
+
* autosave.
|
|
92
93
|
*/
|
|
93
94
|
export interface PendingTransactionStore extends TransactionSink {
|
|
94
95
|
readonly entries: readonly PendingEntry[];
|
|
@@ -1 +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
|
|
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;;;;;;;;;;;;;OAaG;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,uBAGf;CAAG;AAE5B;;;;;;;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"}
|
|
@@ -1 +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 —
|
|
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 — the client's minted id\n * is 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 the reason an autosaved draft gives: a class instance does not survive\n * 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.\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.Service<\n TransactionSinkTag,\n TransactionSink\n>()('TransactionSinkTag') {}\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 tier boundary — a T3 package may not import a T4 shell,\n * so the store is handed in as a prop, the way `EntityDraftStore` already is for\n * autosave.\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","Service","NoopTransactionSink","began","undefined"],"mappings":"AAAA,SAASA,OAAO,QAAQ,SAAS;AA6DjC;;;;;;;;;;;;;CAaC,GACD,OAAO,MAAMC,2BAA2BD,QAAQE,OAAO,GAGnD;AAAuB;AAE3B;;;;;;;CAOC,GACD,OAAO,MAAMC,sBAAuC;IAClDC,OAAO,IAAMC;AACf,EAAE"}
|
|
@@ -8,7 +8,7 @@ import type { TransactionStreamHub } from '../ports/transaction-stream-hub.js';
|
|
|
8
8
|
*
|
|
9
9
|
* No fourth framing and no duplicated fields: the transaction id, the timestamp
|
|
10
10
|
* and the sequence a consumer needs are already `meta.event.correlationId`,
|
|
11
|
-
* `.at` and `.id` on the message this returns
|
|
11
|
+
* `.at` and `.id` on the message this returns.
|
|
12
12
|
*/
|
|
13
13
|
export declare const entityChangeFor: (event: DomainEvent<TransactionEvent>) => DomainEvent<EntityChangeEvent>;
|
|
14
14
|
/** Builds a hub over a sliding {@link PubSub}. */
|
|
@@ -1 +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,MAAM,EAAE,MAAM,EAAU,MAAM,QAAQ,CAAC;AAEhD,OAAO,KAAK,EAAE,gBAAgB,EAAoB,MAAM,uBAAuB,CAAC;AAChF,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,oCAAoC,CAAC;
|
|
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,MAAM,EAAE,MAAM,EAAU,MAAM,QAAQ,CAAC;AAEhD,OAAO,KAAK,EAAE,gBAAgB,EAAoB,MAAM,uBAAuB,CAAC;AAChF,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,oCAAoC,CAAC;AA2B/E;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,GAC1B,OAAO,WAAW,CAAC,gBAAgB,CAAC,KACnC,WAAW,CAAC,iBAAiB,CAW9B,CAAC;AAEH,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"}
|
|
@@ -5,16 +5,15 @@ import { Effect, PubSub, Stream } from 'effect';
|
|
|
5
5
|
* Bounded with a **sliding** strategy rather than unbounded: a browser that
|
|
6
6
|
* stops reading its connection must not be able to grow the service's heap, and
|
|
7
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
|
|
9
|
-
*
|
|
8
|
+
* truth, and re-reading it is cheap, which is also why the stream offers no
|
|
9
|
+
* replay.
|
|
10
10
|
*/ const CAPACITY = 256;
|
|
11
11
|
/**
|
|
12
12
|
* What a transaction's state means for the entity it is about.
|
|
13
13
|
*
|
|
14
14
|
* `type: 'create'` is the only command that exists, so `PENDING` and
|
|
15
15
|
* `COMPLETED` both announce an entity coming into existence — the id is already
|
|
16
|
-
* known at `accepted`, because
|
|
17
|
-
* the stored entity's id. A terminal failure announces the opposite: nothing was
|
|
16
|
+
* known at `accepted`, because the client-minted transaction id is the stored entity's id. A terminal failure announces the opposite: nothing was
|
|
18
17
|
* written, so a consumer holding an optimistic row must drop it.
|
|
19
18
|
*
|
|
20
19
|
* When `update`/`delete` commands land this keys off `command.type` instead;
|
|
@@ -26,7 +25,7 @@ import { Effect, PubSub, Stream } from 'effect';
|
|
|
26
25
|
*
|
|
27
26
|
* No fourth framing and no duplicated fields: the transaction id, the timestamp
|
|
28
27
|
* and the sequence a consumer needs are already `meta.event.correlationId`,
|
|
29
|
-
* `.at` and `.id` on the message this returns
|
|
28
|
+
* `.at` and `.id` on the message this returns.
|
|
30
29
|
*/ export const entityChangeFor = (event)=>{
|
|
31
30
|
var _event_data_entityId;
|
|
32
31
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/stream/transaction-stream-hub.ts"],"sourcesContent":["import type { DomainEvent, EntityChangeEvent } from '@entifix/core';\nimport { Effect, PubSub, Stream } from 'effect';\n\nimport type { TransactionEvent, TransactionState } from '../contracts/event.js';\nimport type { TransactionStreamHub } from '../ports/transaction-stream-hub.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
|
|
1
|
+
{"version":3,"sources":["../../src/stream/transaction-stream-hub.ts"],"sourcesContent":["import type { DomainEvent, EntityChangeEvent } from '@entifix/core';\nimport { Effect, PubSub, Stream } from 'effect';\n\nimport type { TransactionEvent, TransactionState } from '../contracts/event.js';\nimport type { TransactionStreamHub } from '../ports/transaction-stream-hub.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, which is also why the stream offers no\n * replay.\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 the client-minted transaction id is 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.\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/** 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":["Effect","PubSub","Stream","CAPACITY","changeFor","state","entityChangeFor","event","name","id","source","at","correlationId","data","entity","change","entityId","transactionId","makeTransactionStreamHub","pubsub","publish","asVoid","subscribe","organizationId","fromPubSub","pipe","filter","map","makeTransactionStreamHubEffect","sliding"],"mappings":"AACA,SAASA,MAAM,EAAEC,MAAM,EAAEC,MAAM,QAAQ,SAAS;AAKhD;;;;;;;;CAQC,GACD,MAAMC,WAAW;AAEjB;;;;;;;;;;CAUC,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;AAEH,gDAAgD,GAChD,OAAO,MAAMC,2BAA2B,CACtCC,SAC0B,CAAA;QAC1BC,SAASb,CAAAA,QAASP,OAAOqB,MAAM,CAACpB,OAAOmB,OAAO,CAACD,QAAQZ;QACvDe,WAAWC,CAAAA,iBACTrB,OAAOsB,UAAU,CAACL,QAAQM,IAAI,CAC5BvB,OAAOwB,MAAM,CAACnB,CAAAA,QAASA,MAAMM,IAAI,CAACU,cAAc,KAAKA,iBACrDrB,OAAOyB,GAAG,CAACrB;IAEjB,CAAA,EAAG;AAEH,yEAAyE,GACzE,OAAO,MAAMsB,iCAAiC5B,OAAO2B,GAAG,CACtD1B,OAAO4B,OAAO,CAAgC1B,WAC9Ce,0BACA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@entifix/transactions",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Commands, transactions and sagas: inbox and outbox ports, an event bus, locks, and a declarative saga runner.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -56,8 +56,8 @@
|
|
|
56
56
|
},
|
|
57
57
|
"dependencies": {
|
|
58
58
|
"@swc/helpers": "0.5.23",
|
|
59
|
-
"@entifix/business": "0.
|
|
60
|
-
"@entifix/core": "0.
|
|
59
|
+
"@entifix/business": "0.7.0",
|
|
60
|
+
"@entifix/core": "0.7.0"
|
|
61
61
|
},
|
|
62
62
|
"peerDependencies": {
|
|
63
63
|
"effect": "^4.0.5"
|