@saga-bus/store-inmemory 0.1.1
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/README.md +74 -0
- package/dist/index.cjs +131 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +47 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.js +104 -0
- package/dist/index.js.map +1 -0
- package/package.json +61 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Dean Foran
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# @saga-bus/store-inmemory
|
|
2
|
+
|
|
3
|
+
In-memory saga store for testing and development.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @saga-bus/store-inmemory
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { InMemorySagaStore } from "@saga-bus/store-inmemory";
|
|
15
|
+
import { createBus } from "@saga-bus/core";
|
|
16
|
+
|
|
17
|
+
const store = new InMemorySagaStore();
|
|
18
|
+
|
|
19
|
+
const bus = createBus({
|
|
20
|
+
sagas: [{ definition: mySaga, store }],
|
|
21
|
+
transport,
|
|
22
|
+
});
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Features
|
|
26
|
+
|
|
27
|
+
- Optimistic concurrency control
|
|
28
|
+
- Correlation ID indexing
|
|
29
|
+
- State versioning
|
|
30
|
+
- Zero configuration
|
|
31
|
+
- Instant operations
|
|
32
|
+
|
|
33
|
+
## When to Use
|
|
34
|
+
|
|
35
|
+
**Use for:**
|
|
36
|
+
- Unit and integration tests
|
|
37
|
+
- Local development
|
|
38
|
+
- Prototyping
|
|
39
|
+
|
|
40
|
+
**Do not use for:**
|
|
41
|
+
- Production deployments
|
|
42
|
+
- Multi-instance applications
|
|
43
|
+
- Data that needs to survive restarts
|
|
44
|
+
|
|
45
|
+
## Limitations
|
|
46
|
+
|
|
47
|
+
- **No persistence**: All data is lost when the process exits
|
|
48
|
+
- **Single process only**: State is not shared between Node.js processes
|
|
49
|
+
- **Memory bound**: Large numbers of sagas may consume significant memory
|
|
50
|
+
|
|
51
|
+
For production, use a persistent store like [@saga-bus/store-postgres](../store-postgres), [@saga-bus/store-mongo](../store-mongo), or [@saga-bus/store-dynamodb](../store-dynamodb).
|
|
52
|
+
|
|
53
|
+
## Sharing Across Sagas
|
|
54
|
+
|
|
55
|
+
A single store instance can be shared across multiple sagas within the same process:
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
const store = new InMemorySagaStore();
|
|
59
|
+
|
|
60
|
+
const bus = createBus({
|
|
61
|
+
transport,
|
|
62
|
+
store, // shared by all sagas
|
|
63
|
+
sagas: [
|
|
64
|
+
{ definition: orderSaga },
|
|
65
|
+
{ definition: paymentSaga },
|
|
66
|
+
],
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Data is isolated by `sagaName`, so different saga types won't conflict.
|
|
71
|
+
|
|
72
|
+
## License
|
|
73
|
+
|
|
74
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
InMemorySagaStore: () => InMemorySagaStore
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
|
|
27
|
+
// src/InMemorySagaStore.ts
|
|
28
|
+
var import_core = require("@saga-bus/core");
|
|
29
|
+
var InMemorySagaStore = class {
|
|
30
|
+
/** Primary store: sagaName:sagaId -> state */
|
|
31
|
+
store = /* @__PURE__ */ new Map();
|
|
32
|
+
/** Correlation index: "sagaName:correlationId" -> sagaId */
|
|
33
|
+
correlationIndex = /* @__PURE__ */ new Map();
|
|
34
|
+
/**
|
|
35
|
+
* Build the correlation index key.
|
|
36
|
+
*/
|
|
37
|
+
getCorrelationKey(sagaName, correlationId) {
|
|
38
|
+
return `${sagaName}:${correlationId}`;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Build the store key (sagaName:sagaId for isolation).
|
|
42
|
+
*/
|
|
43
|
+
getStoreKey(sagaName, sagaId) {
|
|
44
|
+
return `${sagaName}:${sagaId}`;
|
|
45
|
+
}
|
|
46
|
+
async getById(sagaName, sagaId) {
|
|
47
|
+
const key = this.getStoreKey(sagaName, sagaId);
|
|
48
|
+
const state = this.store.get(key);
|
|
49
|
+
return state ? this.clone(state) : null;
|
|
50
|
+
}
|
|
51
|
+
async getByCorrelationId(sagaName, correlationId) {
|
|
52
|
+
const correlationKey = this.getCorrelationKey(sagaName, correlationId);
|
|
53
|
+
const sagaId = this.correlationIndex.get(correlationKey);
|
|
54
|
+
if (!sagaId) {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
return this.getById(sagaName, sagaId);
|
|
58
|
+
}
|
|
59
|
+
async insert(sagaName, correlationId, state) {
|
|
60
|
+
const { sagaId } = state.metadata;
|
|
61
|
+
const storeKey = this.getStoreKey(sagaName, sagaId);
|
|
62
|
+
if (this.store.has(storeKey)) {
|
|
63
|
+
throw new Error(`Saga ${sagaId} already exists`);
|
|
64
|
+
}
|
|
65
|
+
this.store.set(storeKey, this.clone(state));
|
|
66
|
+
const correlationKey = this.getCorrelationKey(sagaName, correlationId);
|
|
67
|
+
this.correlationIndex.set(correlationKey, sagaId);
|
|
68
|
+
}
|
|
69
|
+
async update(sagaName, state, expectedVersion) {
|
|
70
|
+
const { sagaId, version } = state.metadata;
|
|
71
|
+
const storeKey = this.getStoreKey(sagaName, sagaId);
|
|
72
|
+
const existing = this.store.get(storeKey);
|
|
73
|
+
if (!existing) {
|
|
74
|
+
throw new Error(`Saga ${sagaId} not found`);
|
|
75
|
+
}
|
|
76
|
+
if (existing.metadata.version !== expectedVersion) {
|
|
77
|
+
throw new import_core.ConcurrencyError(
|
|
78
|
+
sagaId,
|
|
79
|
+
expectedVersion,
|
|
80
|
+
existing.metadata.version
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
if (version !== expectedVersion + 1) {
|
|
84
|
+
throw new Error(
|
|
85
|
+
`Invalid version: expected ${expectedVersion + 1}, got ${version}`
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
this.store.set(storeKey, this.clone(state));
|
|
89
|
+
}
|
|
90
|
+
async delete(sagaName, sagaId) {
|
|
91
|
+
const storeKey = this.getStoreKey(sagaName, sagaId);
|
|
92
|
+
this.store.delete(storeKey);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Deep clone a state object to prevent external mutations.
|
|
96
|
+
*/
|
|
97
|
+
clone(state) {
|
|
98
|
+
return JSON.parse(JSON.stringify(state));
|
|
99
|
+
}
|
|
100
|
+
// ============ Testing Utilities ============
|
|
101
|
+
/**
|
|
102
|
+
* Get the total number of stored sagas.
|
|
103
|
+
*/
|
|
104
|
+
get size() {
|
|
105
|
+
return this.store.size;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Clear all stored data.
|
|
109
|
+
*/
|
|
110
|
+
clear() {
|
|
111
|
+
this.store.clear();
|
|
112
|
+
this.correlationIndex.clear();
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Get all stored states (for testing/debugging).
|
|
116
|
+
*/
|
|
117
|
+
getAll() {
|
|
118
|
+
return Array.from(this.store.values()).map((s) => this.clone(s));
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Check if a saga exists by ID.
|
|
122
|
+
*/
|
|
123
|
+
has(sagaName, sagaId) {
|
|
124
|
+
return this.store.has(this.getStoreKey(sagaName, sagaId));
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
128
|
+
0 && (module.exports = {
|
|
129
|
+
InMemorySagaStore
|
|
130
|
+
});
|
|
131
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/InMemorySagaStore.ts"],"sourcesContent":["export { InMemorySagaStore } from \"./InMemorySagaStore.js\";\n","import type { SagaStore, SagaState } from \"@saga-bus/core\";\nimport { ConcurrencyError } from \"@saga-bus/core\";\n\n/**\n * In-memory saga store implementation for testing and development.\n * Uses Maps for O(1) lookups by both sagaId and correlationId.\n */\nexport class InMemorySagaStore<TState extends SagaState>\n implements SagaStore<TState>\n{\n /** Primary store: sagaName:sagaId -> state */\n private readonly store = new Map<string, TState>();\n\n /** Correlation index: \"sagaName:correlationId\" -> sagaId */\n private readonly correlationIndex = new Map<string, string>();\n\n /**\n * Build the correlation index key.\n */\n private getCorrelationKey(sagaName: string, correlationId: string): string {\n return `${sagaName}:${correlationId}`;\n }\n\n /**\n * Build the store key (sagaName:sagaId for isolation).\n */\n private getStoreKey(sagaName: string, sagaId: string): string {\n return `${sagaName}:${sagaId}`;\n }\n\n async getById(sagaName: string, sagaId: string): Promise<TState | null> {\n const key = this.getStoreKey(sagaName, sagaId);\n const state = this.store.get(key);\n return state ? this.clone(state) : null;\n }\n\n async getByCorrelationId(\n sagaName: string,\n correlationId: string\n ): Promise<TState | null> {\n const correlationKey = this.getCorrelationKey(sagaName, correlationId);\n const sagaId = this.correlationIndex.get(correlationKey);\n\n if (!sagaId) {\n return null;\n }\n\n return this.getById(sagaName, sagaId);\n }\n\n async insert(sagaName: string, correlationId: string, state: TState): Promise<void> {\n const { sagaId } = state.metadata;\n const storeKey = this.getStoreKey(sagaName, sagaId);\n\n if (this.store.has(storeKey)) {\n throw new Error(`Saga ${sagaId} already exists`);\n }\n\n // Store the state (deep clone to prevent mutation)\n this.store.set(storeKey, this.clone(state));\n\n // Index by correlation ID\n const correlationKey = this.getCorrelationKey(sagaName, correlationId);\n this.correlationIndex.set(correlationKey, sagaId);\n }\n\n async update(\n sagaName: string,\n state: TState,\n expectedVersion: number\n ): Promise<void> {\n const { sagaId, version } = state.metadata;\n const storeKey = this.getStoreKey(sagaName, sagaId);\n\n const existing = this.store.get(storeKey);\n\n if (!existing) {\n throw new Error(`Saga ${sagaId} not found`);\n }\n\n if (existing.metadata.version !== expectedVersion) {\n throw new ConcurrencyError(\n sagaId,\n expectedVersion,\n existing.metadata.version\n );\n }\n\n // Verify the new version is incremented\n if (version !== expectedVersion + 1) {\n throw new Error(\n `Invalid version: expected ${expectedVersion + 1}, got ${version}`\n );\n }\n\n // Store the updated state (deep clone to prevent mutation)\n this.store.set(storeKey, this.clone(state));\n }\n\n async delete(sagaName: string, sagaId: string): Promise<void> {\n const storeKey = this.getStoreKey(sagaName, sagaId);\n this.store.delete(storeKey);\n }\n\n /**\n * Deep clone a state object to prevent external mutations.\n */\n private clone(state: TState): TState {\n return JSON.parse(JSON.stringify(state)) as TState;\n }\n\n // ============ Testing Utilities ============\n\n /**\n * Get the total number of stored sagas.\n */\n get size(): number {\n return this.store.size;\n }\n\n /**\n * Clear all stored data.\n */\n clear(): void {\n this.store.clear();\n this.correlationIndex.clear();\n }\n\n /**\n * Get all stored states (for testing/debugging).\n */\n getAll(): TState[] {\n return Array.from(this.store.values()).map((s) => this.clone(s));\n }\n\n /**\n * Check if a saga exists by ID.\n */\n has(sagaName: string, sagaId: string): boolean {\n return this.store.has(this.getStoreKey(sagaName, sagaId));\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,kBAAiC;AAM1B,IAAM,oBAAN,MAEP;AAAA;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saga store implementation for testing and development.
|
|
5
|
+
* Uses Maps for O(1) lookups by both sagaId and correlationId.
|
|
6
|
+
*/
|
|
7
|
+
declare class InMemorySagaStore<TState extends SagaState> implements SagaStore<TState> {
|
|
8
|
+
/** Primary store: sagaName:sagaId -> state */
|
|
9
|
+
private readonly store;
|
|
10
|
+
/** Correlation index: "sagaName:correlationId" -> sagaId */
|
|
11
|
+
private readonly correlationIndex;
|
|
12
|
+
/**
|
|
13
|
+
* Build the correlation index key.
|
|
14
|
+
*/
|
|
15
|
+
private getCorrelationKey;
|
|
16
|
+
/**
|
|
17
|
+
* Build the store key (sagaName:sagaId for isolation).
|
|
18
|
+
*/
|
|
19
|
+
private getStoreKey;
|
|
20
|
+
getById(sagaName: string, sagaId: string): Promise<TState | null>;
|
|
21
|
+
getByCorrelationId(sagaName: string, correlationId: string): Promise<TState | null>;
|
|
22
|
+
insert(sagaName: string, correlationId: string, state: TState): Promise<void>;
|
|
23
|
+
update(sagaName: string, state: TState, expectedVersion: number): Promise<void>;
|
|
24
|
+
delete(sagaName: string, sagaId: string): Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* Deep clone a state object to prevent external mutations.
|
|
27
|
+
*/
|
|
28
|
+
private clone;
|
|
29
|
+
/**
|
|
30
|
+
* Get the total number of stored sagas.
|
|
31
|
+
*/
|
|
32
|
+
get size(): number;
|
|
33
|
+
/**
|
|
34
|
+
* Clear all stored data.
|
|
35
|
+
*/
|
|
36
|
+
clear(): void;
|
|
37
|
+
/**
|
|
38
|
+
* Get all stored states (for testing/debugging).
|
|
39
|
+
*/
|
|
40
|
+
getAll(): TState[];
|
|
41
|
+
/**
|
|
42
|
+
* Check if a saga exists by ID.
|
|
43
|
+
*/
|
|
44
|
+
has(sagaName: string, sagaId: string): boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export { InMemorySagaStore };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { SagaState, SagaStore } from '@saga-bus/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* In-memory saga store implementation for testing and development.
|
|
5
|
+
* Uses Maps for O(1) lookups by both sagaId and correlationId.
|
|
6
|
+
*/
|
|
7
|
+
declare class InMemorySagaStore<TState extends SagaState> implements SagaStore<TState> {
|
|
8
|
+
/** Primary store: sagaName:sagaId -> state */
|
|
9
|
+
private readonly store;
|
|
10
|
+
/** Correlation index: "sagaName:correlationId" -> sagaId */
|
|
11
|
+
private readonly correlationIndex;
|
|
12
|
+
/**
|
|
13
|
+
* Build the correlation index key.
|
|
14
|
+
*/
|
|
15
|
+
private getCorrelationKey;
|
|
16
|
+
/**
|
|
17
|
+
* Build the store key (sagaName:sagaId for isolation).
|
|
18
|
+
*/
|
|
19
|
+
private getStoreKey;
|
|
20
|
+
getById(sagaName: string, sagaId: string): Promise<TState | null>;
|
|
21
|
+
getByCorrelationId(sagaName: string, correlationId: string): Promise<TState | null>;
|
|
22
|
+
insert(sagaName: string, correlationId: string, state: TState): Promise<void>;
|
|
23
|
+
update(sagaName: string, state: TState, expectedVersion: number): Promise<void>;
|
|
24
|
+
delete(sagaName: string, sagaId: string): Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* Deep clone a state object to prevent external mutations.
|
|
27
|
+
*/
|
|
28
|
+
private clone;
|
|
29
|
+
/**
|
|
30
|
+
* Get the total number of stored sagas.
|
|
31
|
+
*/
|
|
32
|
+
get size(): number;
|
|
33
|
+
/**
|
|
34
|
+
* Clear all stored data.
|
|
35
|
+
*/
|
|
36
|
+
clear(): void;
|
|
37
|
+
/**
|
|
38
|
+
* Get all stored states (for testing/debugging).
|
|
39
|
+
*/
|
|
40
|
+
getAll(): TState[];
|
|
41
|
+
/**
|
|
42
|
+
* Check if a saga exists by ID.
|
|
43
|
+
*/
|
|
44
|
+
has(sagaName: string, sagaId: string): boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export { InMemorySagaStore };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// src/InMemorySagaStore.ts
|
|
2
|
+
import { ConcurrencyError } from "@saga-bus/core";
|
|
3
|
+
var InMemorySagaStore = class {
|
|
4
|
+
/** Primary store: sagaName:sagaId -> state */
|
|
5
|
+
store = /* @__PURE__ */ new Map();
|
|
6
|
+
/** Correlation index: "sagaName:correlationId" -> sagaId */
|
|
7
|
+
correlationIndex = /* @__PURE__ */ new Map();
|
|
8
|
+
/**
|
|
9
|
+
* Build the correlation index key.
|
|
10
|
+
*/
|
|
11
|
+
getCorrelationKey(sagaName, correlationId) {
|
|
12
|
+
return `${sagaName}:${correlationId}`;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Build the store key (sagaName:sagaId for isolation).
|
|
16
|
+
*/
|
|
17
|
+
getStoreKey(sagaName, sagaId) {
|
|
18
|
+
return `${sagaName}:${sagaId}`;
|
|
19
|
+
}
|
|
20
|
+
async getById(sagaName, sagaId) {
|
|
21
|
+
const key = this.getStoreKey(sagaName, sagaId);
|
|
22
|
+
const state = this.store.get(key);
|
|
23
|
+
return state ? this.clone(state) : null;
|
|
24
|
+
}
|
|
25
|
+
async getByCorrelationId(sagaName, correlationId) {
|
|
26
|
+
const correlationKey = this.getCorrelationKey(sagaName, correlationId);
|
|
27
|
+
const sagaId = this.correlationIndex.get(correlationKey);
|
|
28
|
+
if (!sagaId) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
return this.getById(sagaName, sagaId);
|
|
32
|
+
}
|
|
33
|
+
async insert(sagaName, correlationId, state) {
|
|
34
|
+
const { sagaId } = state.metadata;
|
|
35
|
+
const storeKey = this.getStoreKey(sagaName, sagaId);
|
|
36
|
+
if (this.store.has(storeKey)) {
|
|
37
|
+
throw new Error(`Saga ${sagaId} already exists`);
|
|
38
|
+
}
|
|
39
|
+
this.store.set(storeKey, this.clone(state));
|
|
40
|
+
const correlationKey = this.getCorrelationKey(sagaName, correlationId);
|
|
41
|
+
this.correlationIndex.set(correlationKey, sagaId);
|
|
42
|
+
}
|
|
43
|
+
async update(sagaName, state, expectedVersion) {
|
|
44
|
+
const { sagaId, version } = state.metadata;
|
|
45
|
+
const storeKey = this.getStoreKey(sagaName, sagaId);
|
|
46
|
+
const existing = this.store.get(storeKey);
|
|
47
|
+
if (!existing) {
|
|
48
|
+
throw new Error(`Saga ${sagaId} not found`);
|
|
49
|
+
}
|
|
50
|
+
if (existing.metadata.version !== expectedVersion) {
|
|
51
|
+
throw new ConcurrencyError(
|
|
52
|
+
sagaId,
|
|
53
|
+
expectedVersion,
|
|
54
|
+
existing.metadata.version
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
if (version !== expectedVersion + 1) {
|
|
58
|
+
throw new Error(
|
|
59
|
+
`Invalid version: expected ${expectedVersion + 1}, got ${version}`
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
this.store.set(storeKey, this.clone(state));
|
|
63
|
+
}
|
|
64
|
+
async delete(sagaName, sagaId) {
|
|
65
|
+
const storeKey = this.getStoreKey(sagaName, sagaId);
|
|
66
|
+
this.store.delete(storeKey);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Deep clone a state object to prevent external mutations.
|
|
70
|
+
*/
|
|
71
|
+
clone(state) {
|
|
72
|
+
return JSON.parse(JSON.stringify(state));
|
|
73
|
+
}
|
|
74
|
+
// ============ Testing Utilities ============
|
|
75
|
+
/**
|
|
76
|
+
* Get the total number of stored sagas.
|
|
77
|
+
*/
|
|
78
|
+
get size() {
|
|
79
|
+
return this.store.size;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Clear all stored data.
|
|
83
|
+
*/
|
|
84
|
+
clear() {
|
|
85
|
+
this.store.clear();
|
|
86
|
+
this.correlationIndex.clear();
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Get all stored states (for testing/debugging).
|
|
90
|
+
*/
|
|
91
|
+
getAll() {
|
|
92
|
+
return Array.from(this.store.values()).map((s) => this.clone(s));
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Check if a saga exists by ID.
|
|
96
|
+
*/
|
|
97
|
+
has(sagaName, sagaId) {
|
|
98
|
+
return this.store.has(this.getStoreKey(sagaName, sagaId));
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
export {
|
|
102
|
+
InMemorySagaStore
|
|
103
|
+
};
|
|
104
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/InMemorySagaStore.ts"],"sourcesContent":["import type { SagaStore, SagaState } from \"@saga-bus/core\";\nimport { ConcurrencyError } from \"@saga-bus/core\";\n\n/**\n * In-memory saga store implementation for testing and development.\n * Uses Maps for O(1) lookups by both sagaId and correlationId.\n */\nexport class InMemorySagaStore<TState extends SagaState>\n implements SagaStore<TState>\n{\n /** Primary store: sagaName:sagaId -> state */\n private readonly store = new Map<string, TState>();\n\n /** Correlation index: \"sagaName:correlationId\" -> sagaId */\n private readonly correlationIndex = new Map<string, string>();\n\n /**\n * Build the correlation index key.\n */\n private getCorrelationKey(sagaName: string, correlationId: string): string {\n return `${sagaName}:${correlationId}`;\n }\n\n /**\n * Build the store key (sagaName:sagaId for isolation).\n */\n private getStoreKey(sagaName: string, sagaId: string): string {\n return `${sagaName}:${sagaId}`;\n }\n\n async getById(sagaName: string, sagaId: string): Promise<TState | null> {\n const key = this.getStoreKey(sagaName, sagaId);\n const state = this.store.get(key);\n return state ? this.clone(state) : null;\n }\n\n async getByCorrelationId(\n sagaName: string,\n correlationId: string\n ): Promise<TState | null> {\n const correlationKey = this.getCorrelationKey(sagaName, correlationId);\n const sagaId = this.correlationIndex.get(correlationKey);\n\n if (!sagaId) {\n return null;\n }\n\n return this.getById(sagaName, sagaId);\n }\n\n async insert(sagaName: string, correlationId: string, state: TState): Promise<void> {\n const { sagaId } = state.metadata;\n const storeKey = this.getStoreKey(sagaName, sagaId);\n\n if (this.store.has(storeKey)) {\n throw new Error(`Saga ${sagaId} already exists`);\n }\n\n // Store the state (deep clone to prevent mutation)\n this.store.set(storeKey, this.clone(state));\n\n // Index by correlation ID\n const correlationKey = this.getCorrelationKey(sagaName, correlationId);\n this.correlationIndex.set(correlationKey, sagaId);\n }\n\n async update(\n sagaName: string,\n state: TState,\n expectedVersion: number\n ): Promise<void> {\n const { sagaId, version } = state.metadata;\n const storeKey = this.getStoreKey(sagaName, sagaId);\n\n const existing = this.store.get(storeKey);\n\n if (!existing) {\n throw new Error(`Saga ${sagaId} not found`);\n }\n\n if (existing.metadata.version !== expectedVersion) {\n throw new ConcurrencyError(\n sagaId,\n expectedVersion,\n existing.metadata.version\n );\n }\n\n // Verify the new version is incremented\n if (version !== expectedVersion + 1) {\n throw new Error(\n `Invalid version: expected ${expectedVersion + 1}, got ${version}`\n );\n }\n\n // Store the updated state (deep clone to prevent mutation)\n this.store.set(storeKey, this.clone(state));\n }\n\n async delete(sagaName: string, sagaId: string): Promise<void> {\n const storeKey = this.getStoreKey(sagaName, sagaId);\n this.store.delete(storeKey);\n }\n\n /**\n * Deep clone a state object to prevent external mutations.\n */\n private clone(state: TState): TState {\n return JSON.parse(JSON.stringify(state)) as TState;\n }\n\n // ============ Testing Utilities ============\n\n /**\n * Get the total number of stored sagas.\n */\n get size(): number {\n return this.store.size;\n }\n\n /**\n * Clear all stored data.\n */\n clear(): void {\n this.store.clear();\n this.correlationIndex.clear();\n }\n\n /**\n * Get all stored states (for testing/debugging).\n */\n getAll(): TState[] {\n return Array.from(this.store.values()).map((s) => this.clone(s));\n }\n\n /**\n * Check if a saga exists by ID.\n */\n has(sagaName: string, sagaId: string): boolean {\n return this.store.has(this.getStoreKey(sagaName, sagaId));\n }\n}\n"],"mappings":";AACA,SAAS,wBAAwB;AAM1B,IAAM,oBAAN,MAEP;AAAA;AAAA,EAEmB,QAAQ,oBAAI,IAAoB;AAAA;AAAA,EAGhC,mBAAmB,oBAAI,IAAoB;AAAA;AAAA;AAAA;AAAA,EA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S,SAAS,YAAY,iBAAiB;AACjD,YAAM,IAAI;AAAA,QACR;AAAA,QACA;AAAA,QACA,SAAS,SAAS;AAAA,MACpB;AAAA,IACF;AAGA,QAAI,YAAY,kBAAkB,GAAG;AACnC,YAAM,IAAI;AAAA,QACR,6BAA6B,kBAAkB,CAAC,SAAS,OAAO;AAAA,MAClE;AAAA,IACF;AAGA,SAAK,MAAM,IAAI,UAAU,KAAK,MAAM,KAAK,CAAC;AAAA,EAC5C;AAAA,EAEA,MAAM,OAAO,UAAkB,QAA+B;AAC5D,UAAM,WAAW,KAAK,YAAY,UAAU,MAAM;AAClD,SAAK,MAAM,OAAO,QAAQ;AAAA,EAC5B;AAAA;AAAA;AAAA;AAAA,EAKQ,MAAM,OAAuB;AACnC,WAAO,KAAK,MAAM,KAAK,UAAU,KAAK,CAAC;AAAA,EACzC;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,IAAI,OAAe;AACjB,WAAO,KAAK,MAAM;AAAA,EACpB;AAAA;AAAA;AAAA;AAAA,EAKA,QAAc;AACZ,SAAK,MAAM,MAAM;AACjB,SAAK,iBAAiB,MAAM;AAAA,EAC9B;AAAA;AAAA;AAAA;AAAA,EAKA,SAAmB;AACjB,WAAO,MAAM,KAAK,KAAK,MAAM,OAAO,CAAC,EAAE,IAAI,CAAC,MAAM,KAAK,MAAM,CAAC,CAAC;AAAA,EACjE;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,UAAkB,QAAyB;AAC7C,WAAO,KAAK,MAAM,IAAI,KAAK,YAAY,UAAU,MAAM,CAAC;AAAA,EAC1D;AACF;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@saga-bus/store-inmemory",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "In-memory saga store for saga-bus testing and development",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"README.md"
|
|
19
|
+
],
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "https://github.com/d-e-a-n-f/saga-bus.git",
|
|
26
|
+
"directory": "packages/store-inmemory"
|
|
27
|
+
},
|
|
28
|
+
"bugs": {
|
|
29
|
+
"url": "https://github.com/d-e-a-n-f/saga-bus/issues"
|
|
30
|
+
},
|
|
31
|
+
"homepage": "https://github.com/d-e-a-n-f/saga-bus#readme",
|
|
32
|
+
"keywords": [
|
|
33
|
+
"saga",
|
|
34
|
+
"message-bus",
|
|
35
|
+
"store",
|
|
36
|
+
"inmemory",
|
|
37
|
+
"testing"
|
|
38
|
+
],
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@saga-bus/core": "0.1.1"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@types/node": "^20.0.0",
|
|
44
|
+
"tsup": "^8.0.0",
|
|
45
|
+
"typescript": "^5.9.2",
|
|
46
|
+
"vitest": "^3.0.0",
|
|
47
|
+
"@repo/eslint-config": "0.0.0",
|
|
48
|
+
"@repo/typescript-config": "0.0.0"
|
|
49
|
+
},
|
|
50
|
+
"peerDependencies": {
|
|
51
|
+
"@saga-bus/core": ">=0.1.1"
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"build": "tsup",
|
|
55
|
+
"dev": "tsup --watch",
|
|
56
|
+
"lint": "eslint src/",
|
|
57
|
+
"check-types": "tsc --noEmit",
|
|
58
|
+
"test": "vitest run",
|
|
59
|
+
"test:watch": "vitest"
|
|
60
|
+
}
|
|
61
|
+
}
|