@spine-event-engine/storage 2.0.0-snapshot.24 → 2.0.0-snapshot.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -3
- package/REFERENCE.md +76 -68
- package/dist/entity/agent-history-record-spec.d.ts +8 -0
- package/dist/entity/agent-history-record-spec.d.ts.map +1 -1
- package/dist/entity/agent-history-record-spec.js +12 -1
- package/dist/entity/agent-history-record-spec.js.map +1 -1
- package/dist/entity/agent-history.d.ts +25 -0
- package/dist/entity/agent-history.d.ts.map +1 -1
- package/dist/entity/agent-history.js.map +1 -1
- package/dist/entity/history-conformance.d.ts +38 -8
- package/dist/entity/history-conformance.d.ts.map +1 -1
- package/dist/entity/history-conformance.js +103 -58
- package/dist/entity/history-conformance.js.map +1 -1
- package/dist/event/event-store.d.ts +51 -3
- package/dist/event/event-store.d.ts.map +1 -1
- package/dist/event/event-store.js +171 -27
- package/dist/event/event-store.js.map +1 -1
- package/dist/internal/agent-model-preferences.d.ts +76 -0
- package/dist/internal/agent-model-preferences.d.ts.map +1 -0
- package/dist/internal/agent-model-preferences.js +133 -0
- package/dist/internal/agent-model-preferences.js.map +1 -0
- package/dist/internal/entity-commit.d.ts +64 -8
- package/dist/internal/entity-commit.d.ts.map +1 -1
- package/dist/internal/entity-commit.js.map +1 -1
- package/dist/internal/entity-history.d.ts +1 -7
- package/dist/internal/entity-history.d.ts.map +1 -1
- package/dist/internal/entity-history.js +0 -5
- package/dist/internal/entity-history.js.map +1 -1
- package/dist/internal/event-store.d.ts +1 -1
- package/dist/internal/event-store.d.ts.map +1 -1
- package/dist/internal/event-store.js +1 -1
- package/dist/internal/event-store.js.map +1 -1
- package/dist/internal/storage-preparation.d.ts +88 -0
- package/dist/internal/storage-preparation.d.ts.map +1 -0
- package/dist/internal/storage-preparation.js +41 -0
- package/dist/internal/storage-preparation.js.map +1 -0
- package/dist/internal/tenancy.d.ts +7 -0
- package/dist/internal/tenancy.d.ts.map +1 -1
- package/dist/memory/in-memory-agent-history.d.ts.map +1 -1
- package/dist/memory/in-memory-agent-history.js +22 -7
- package/dist/memory/in-memory-agent-history.js.map +1 -1
- package/dist/memory/in-memory-entity-commit.d.ts +154 -21
- package/dist/memory/in-memory-entity-commit.d.ts.map +1 -1
- package/dist/memory/in-memory-entity-commit.js +396 -60
- package/dist/memory/in-memory-entity-commit.js.map +1 -1
- package/dist/memory/in-memory-entity-history.d.ts.map +1 -1
- package/dist/memory/in-memory-entity-history.js +4 -3
- package/dist/memory/in-memory-entity-history.js.map +1 -1
- package/dist/memory/in-memory-storage-backend.d.ts +33 -1
- package/dist/memory/in-memory-storage-backend.d.ts.map +1 -1
- package/dist/memory/in-memory-storage-backend.js +118 -1
- package/dist/memory/in-memory-storage-backend.js.map +1 -1
- package/dist/memory/in-memory-storage-factory.d.ts +71 -12
- package/dist/memory/in-memory-storage-factory.d.ts.map +1 -1
- package/dist/memory/in-memory-storage-factory.js +199 -25
- package/dist/memory/in-memory-storage-factory.js.map +1 -1
- package/dist/provider.d.ts +2 -0
- package/dist/provider.d.ts.map +1 -1
- package/dist/provider.js +2 -0
- package/dist/provider.js.map +1 -1
- package/dist/storage/storage-factory.d.ts +27 -4
- package/dist/storage/storage-factory.d.ts.map +1 -1
- package/dist/storage/storage-factory.js +30 -1
- package/dist/storage/storage-factory.js.map +1 -1
- package/dist/storage/storage.d.ts +2 -1
- package/dist/storage/storage.d.ts.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
- package/dist/entity/agent-execution-record-spec.d.ts +0 -87
- package/dist/entity/agent-execution-record-spec.d.ts.map +0 -1
- package/dist/entity/agent-execution-record-spec.js +0 -230
- package/dist/entity/agent-execution-record-spec.js.map +0 -1
- package/dist/entity/agent-execution-sizes.d.ts +0 -30
- package/dist/entity/agent-execution-sizes.d.ts.map +0 -1
- package/dist/entity/agent-execution-sizes.js +0 -51
- package/dist/entity/agent-execution-sizes.js.map +0 -1
- package/dist/entity/agent-execution-transitions.d.ts +0 -72
- package/dist/entity/agent-execution-transitions.d.ts.map +0 -1
- package/dist/entity/agent-execution-transitions.js +0 -165
- package/dist/entity/agent-execution-transitions.js.map +0 -1
- package/dist/entity/agent-execution-values.d.ts +0 -143
- package/dist/entity/agent-execution-values.d.ts.map +0 -1
- package/dist/entity/agent-execution-values.js +0 -251
- package/dist/entity/agent-execution-values.js.map +0 -1
- package/dist/entity/agent-execution.d.ts +0 -235
- package/dist/entity/agent-execution.d.ts.map +0 -1
- package/dist/entity/agent-execution.js +0 -15
- package/dist/entity/agent-execution.js.map +0 -1
- package/dist/internal/agent-execution.d.ts +0 -51
- package/dist/internal/agent-execution.d.ts.map +0 -1
- package/dist/internal/agent-execution.js +0 -53
- package/dist/internal/agent-execution.js.map +0 -1
- package/dist/memory/agent-execution-head-index.d.ts +0 -52
- package/dist/memory/agent-execution-head-index.d.ts.map +0 -1
- package/dist/memory/agent-execution-head-index.js +0 -91
- package/dist/memory/agent-execution-head-index.js.map +0 -1
- package/dist/memory/in-memory-agent-execution.d.ts +0 -21
- package/dist/memory/in-memory-agent-execution.d.ts.map +0 -1
- package/dist/memory/in-memory-agent-execution.js +0 -587
- package/dist/memory/in-memory-agent-execution.js.map +0 -1
|
@@ -11,8 +11,9 @@
|
|
|
11
11
|
* or implied. See the License for the specific language governing permissions and limitations under
|
|
12
12
|
* the License.
|
|
13
13
|
*/
|
|
14
|
-
import { create,
|
|
15
|
-
import {
|
|
14
|
+
import { create, ScalarType } from "@bufbuild/protobuf";
|
|
15
|
+
import { AnyPacker } from "@spine-event-engine/core";
|
|
16
|
+
import { StringValueSchema, TimestampSchema } from "@bufbuild/protobuf/wkt";
|
|
16
17
|
import { EntityRecordSchema, } from "@spine-event-engine/proto/generated/spine/server/entity/entity_pb.js";
|
|
17
18
|
import { EventIdSchema, EventSchema, VersionSchema } from "@spine-event-engine/proto";
|
|
18
19
|
import { RecordColumn } from "../record/record-column.js";
|
|
@@ -25,30 +26,30 @@ export const EntityHistoryConformance = Object.freeze({
|
|
|
25
26
|
// prettier-ignore
|
|
26
27
|
/**
|
|
27
28
|
* Checks the shared current-record behavior.
|
|
28
|
-
*
|
|
29
|
+
* @typeParam S Generated Entity state type.
|
|
29
30
|
* @param adapter Supplies the provider under test.
|
|
30
31
|
* @returns Completes when the behavior is verified.
|
|
31
32
|
*/
|
|
32
33
|
async checkCurrentQueries(adapter) {
|
|
33
|
-
const storage = adapter.create(EntityHistoryFixture.input());
|
|
34
|
+
const storage = adapter.create(EntityHistoryFixture.input(adapter));
|
|
34
35
|
try {
|
|
35
|
-
await storage.current.write(EntityHistoryFixture.record("task", "task", 3));
|
|
36
|
-
await storage.current.write(EntityHistoryFixture.record("later", "later", 4));
|
|
36
|
+
await storage.current.write(EntityHistoryFixture.record(adapter, "task", "task", 3));
|
|
37
|
+
await storage.current.write(EntityHistoryFixture.record(adapter, "later", "later", 4));
|
|
37
38
|
const queried = await storage.current.query({
|
|
38
|
-
predicate: { kind: "comparison", column: "
|
|
39
|
-
order: [{ column: "
|
|
39
|
+
predicate: { kind: "comparison", column: "title", operator: "equal", value: "task" },
|
|
40
|
+
order: [{ column: "title", direction: "asc" }],
|
|
40
41
|
limit: 1,
|
|
41
42
|
candidateLimit: 2,
|
|
42
43
|
});
|
|
43
44
|
const current = await storage.current.read("task");
|
|
44
45
|
EntityHistoryFixture.assert(current !== undefined, "current state must round-trip");
|
|
45
|
-
EntityHistoryFixture.assert(EntityHistoryFixture.state(current)
|
|
46
|
-
EntityHistoryFixture.assert(queried.length === 1 && queried[0]?.id === "
|
|
46
|
+
EntityHistoryFixture.assert(adapter.value(EntityHistoryFixture.state(adapter, current)) === "task", "current state must round-trip");
|
|
47
|
+
EntityHistoryFixture.assert(queried.length === 1 && queried[0]?.id === "task", "current queries must filter, order, and limit generated records");
|
|
47
48
|
}
|
|
48
49
|
finally {
|
|
49
50
|
storage.close?.();
|
|
50
51
|
}
|
|
51
|
-
const reopened = adapter.reopen(EntityHistoryFixture.input());
|
|
52
|
+
const reopened = adapter.reopen(EntityHistoryFixture.input(adapter));
|
|
52
53
|
try {
|
|
53
54
|
EntityHistoryFixture.assert((await reopened.current.read("task")) !== undefined, "current record must survive reopen");
|
|
54
55
|
}
|
|
@@ -58,31 +59,27 @@ export const EntityHistoryConformance = Object.freeze({
|
|
|
58
59
|
},
|
|
59
60
|
/**
|
|
60
61
|
* Checks the generated state-history behavior.
|
|
61
|
-
*
|
|
62
|
+
* @typeParam S Generated Entity state type.
|
|
62
63
|
* @param adapter Supplies the provider under test.
|
|
63
64
|
* @returns Completes when the behavior is verified.
|
|
64
65
|
*/
|
|
65
66
|
async check(adapter) {
|
|
66
67
|
await EntityHistoryConformance.checkCurrentQueries(adapter);
|
|
67
|
-
const input = EntityHistoryFixture.input();
|
|
68
|
+
const input = EntityHistoryFixture.input(adapter);
|
|
68
69
|
const storage = adapter.create(input);
|
|
69
70
|
try {
|
|
70
|
-
await storage.states.append(EntityHistoryFixture.record("task", "first", 1));
|
|
71
|
-
await storage.states.append(EntityHistoryFixture.record("task", "second", 2));
|
|
71
|
+
await storage.states.append(EntityHistoryFixture.record(adapter, "task", "first", 1));
|
|
72
|
+
await storage.states.append(EntityHistoryFixture.record(adapter, "task", "second", 2));
|
|
72
73
|
const backward = await storage.states.backward("task", 2);
|
|
73
74
|
const newest = backward[0];
|
|
74
75
|
const older = backward[1];
|
|
75
76
|
EntityHistoryFixture.assert(newest !== undefined &&
|
|
76
77
|
older !== undefined &&
|
|
77
|
-
EntityHistoryFixture.state(newest)
|
|
78
|
-
EntityHistoryFixture.state(older)
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
await storage.events.append(event);
|
|
83
|
-
EntityHistoryFixture.assert((await storage.events.backward("task", 1))[0]?.id?.value === "event", "event history must retain generated events");
|
|
84
|
-
await storage.events.truncate(create(TimestampSchema, { seconds: 3n }));
|
|
85
|
-
EntityHistoryFixture.assert((await storage.events.backward("task", 1)).length === 0, "event history truncation must remove older generated events");
|
|
78
|
+
adapter.value(EntityHistoryFixture.state(adapter, newest)) === "second" &&
|
|
79
|
+
adapter.value(EntityHistoryFixture.state(adapter, older)) === "first", "state history must return newest records first");
|
|
80
|
+
const stateAt = await storage.states.stateAt("task", create(TimestampSchema, { seconds: 1n }));
|
|
81
|
+
EntityHistoryFixture.assert(stateAt !== undefined && adapter.value(stateAt) === "first", "state history must return the state at an inclusive timestamp");
|
|
82
|
+
await EntityHistoryFixture.checkEvents(storage.events);
|
|
86
83
|
}
|
|
87
84
|
finally {
|
|
88
85
|
storage.close?.();
|
|
@@ -90,73 +87,121 @@ export const EntityHistoryConformance = Object.freeze({
|
|
|
90
87
|
},
|
|
91
88
|
});
|
|
92
89
|
const EntityHistoryFixture = Object.freeze({
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
90
|
+
/**
|
|
91
|
+
* Builds provider input using the caller's generated Entity state schema.
|
|
92
|
+
* @typeParam S Generated Entity state type.
|
|
93
|
+
* @param adapter Generated state fixture and provider port factory.
|
|
94
|
+
* @returns Entity storage input for conformance checks.
|
|
95
|
+
*/
|
|
96
|
+
input(adapter) {
|
|
97
|
+
const unpack = (id) => AnyPacker.tryUnpack(id, StringValueSchema)?.value;
|
|
97
98
|
const columns = [
|
|
98
|
-
new RecordColumn("
|
|
99
|
+
new RecordColumn("title", ColumnTypes.scalar(ScalarType.STRING), (record) => adapter.value(EntityHistoryFixture.state(adapter, record))),
|
|
99
100
|
];
|
|
100
101
|
return {
|
|
101
102
|
context: { name: "EntityHistoryConformance", multitenant: false },
|
|
102
103
|
id: {
|
|
103
104
|
clone: (id) => id,
|
|
104
105
|
key: (id) => id,
|
|
105
|
-
pack: (id) =>
|
|
106
|
+
pack: (id) => AnyPacker.pack(StringValueSchema, create(StringValueSchema, { value: id })),
|
|
106
107
|
unpack,
|
|
107
108
|
},
|
|
108
109
|
columns,
|
|
109
|
-
recordSpec:
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
idKind: "string",
|
|
113
|
-
extractId: (record) => {
|
|
114
|
-
if (record.entityId === undefined)
|
|
115
|
-
throw new Error("EntityRecord requires entityId.");
|
|
116
|
-
const id = unpack(record.entityId);
|
|
117
|
-
if (id === undefined)
|
|
118
|
-
throw new Error("EntityRecord has an incompatible ID.");
|
|
119
|
-
return id;
|
|
120
|
-
},
|
|
121
|
-
columns,
|
|
122
|
-
}),
|
|
123
|
-
sourceType: StringValueSchema,
|
|
124
|
-
stateSchema: StringValueSchema,
|
|
110
|
+
recordSpec: EntityHistoryFixture.spec(adapter, unpack, columns),
|
|
111
|
+
sourceType: adapter.stateSchema,
|
|
112
|
+
stateSchema: adapter.stateSchema,
|
|
125
113
|
stateHistory: true,
|
|
126
114
|
eventHistory: true,
|
|
127
115
|
};
|
|
128
116
|
},
|
|
129
|
-
|
|
117
|
+
/**
|
|
118
|
+
* Creates a generated Entity record for one state version.
|
|
119
|
+
* @typeParam S Generated Entity state type.
|
|
120
|
+
* @param adapter Generated state fixture and provider port factory.
|
|
121
|
+
* @param id Entity identifier.
|
|
122
|
+
* @param value State title.
|
|
123
|
+
* @param version Entity version.
|
|
124
|
+
* @returns Generated Entity record.
|
|
125
|
+
*/
|
|
126
|
+
record(adapter, id, value, version) {
|
|
130
127
|
return create(EntityRecordSchema, {
|
|
131
|
-
entityId:
|
|
132
|
-
state:
|
|
128
|
+
entityId: AnyPacker.pack(StringValueSchema, create(StringValueSchema, { value: id })),
|
|
129
|
+
state: AnyPacker.pack(adapter.stateSchema, adapter.state(id, value)),
|
|
133
130
|
version: {
|
|
134
131
|
number: version,
|
|
135
132
|
timestamp: create(TimestampSchema, { seconds: BigInt(version) }),
|
|
136
133
|
},
|
|
137
134
|
});
|
|
138
135
|
},
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
136
|
+
/**
|
|
137
|
+
* Decodes the caller's Entity state from a generated record.
|
|
138
|
+
* @typeParam S Generated Entity state type.
|
|
139
|
+
* @param adapter Generated state fixture and provider port factory.
|
|
140
|
+
* @param record Generated Entity record.
|
|
141
|
+
* @returns Decoded generated Entity state.
|
|
142
|
+
*/
|
|
143
|
+
state(adapter, record) {
|
|
146
144
|
if (record.state === undefined)
|
|
147
145
|
throw new Error("Entity record has no state.");
|
|
148
|
-
return
|
|
146
|
+
return AnyPacker.unpack(record.state, adapter.stateSchema);
|
|
149
147
|
},
|
|
148
|
+
/**
|
|
149
|
+
* Builds the current-record specification for the generated state.
|
|
150
|
+
* @typeParam S Generated Entity state type.
|
|
151
|
+
* @param adapter Generated state fixture.
|
|
152
|
+
* @param unpack Decodes scalar Entity identifiers.
|
|
153
|
+
* @param columns Declared query columns.
|
|
154
|
+
* @returns Record specification.
|
|
155
|
+
*/
|
|
156
|
+
spec(adapter, unpack, columns) {
|
|
157
|
+
return new RecordSpec({
|
|
158
|
+
sourceType: adapter.stateSchema,
|
|
159
|
+
recordType: EntityRecordSchema,
|
|
160
|
+
idKind: "string",
|
|
161
|
+
extractId: (record) => {
|
|
162
|
+
if (record.entityId === undefined)
|
|
163
|
+
throw new Error("EntityRecord requires entityId.");
|
|
164
|
+
const id = unpack(record.entityId);
|
|
165
|
+
if (id === undefined)
|
|
166
|
+
throw new Error("EntityRecord has an incompatible ID.");
|
|
167
|
+
return id;
|
|
168
|
+
},
|
|
169
|
+
columns,
|
|
170
|
+
});
|
|
171
|
+
},
|
|
172
|
+
/**
|
|
173
|
+
* Checks generated Event retention and truncation.
|
|
174
|
+
* @param events Event-history port.
|
|
175
|
+
* @returns Completion after Event assertions.
|
|
176
|
+
*/
|
|
177
|
+
async checkEvents(events) {
|
|
178
|
+
await events.append(EntityHistoryFixture.event("event", "task", 2));
|
|
179
|
+
EntityHistoryFixture.assert((await events.backward("task", 1))[0]?.id?.value === "event", "event history must retain generated events");
|
|
180
|
+
await events.truncate(create(TimestampSchema, { seconds: 3n }));
|
|
181
|
+
EntityHistoryFixture.assert((await events.backward("task", 1)).length === 0, "event history truncation must remove older generated events");
|
|
182
|
+
},
|
|
183
|
+
/**
|
|
184
|
+
* Creates a generated diagnostic Event.
|
|
185
|
+
* @param id Event identifier.
|
|
186
|
+
* @param producer Producer Entity identifier.
|
|
187
|
+
* @param version Event version.
|
|
188
|
+
* @returns Generated Event.
|
|
189
|
+
*/
|
|
150
190
|
event(id, producer, version) {
|
|
151
191
|
return create(EventSchema, {
|
|
152
192
|
id: create(EventIdSchema, { value: id }),
|
|
153
193
|
context: {
|
|
154
|
-
producerId:
|
|
194
|
+
producerId: AnyPacker.pack(StringValueSchema, create(StringValueSchema, { value: producer })),
|
|
155
195
|
timestamp: create(TimestampSchema, { seconds: BigInt(version) }),
|
|
156
196
|
version: create(VersionSchema, { number: version }),
|
|
157
197
|
},
|
|
158
198
|
});
|
|
159
199
|
},
|
|
200
|
+
/**
|
|
201
|
+
* Checks one conformance condition.
|
|
202
|
+
* @param condition Expected condition.
|
|
203
|
+
* @param message Failure explanation.
|
|
204
|
+
*/
|
|
160
205
|
assert(condition, message) {
|
|
161
206
|
if (!condition)
|
|
162
207
|
throw new Error(`Entity storage conformance failed: ${message}`);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"history-conformance.js","sourceRoot":"","sources":["../../src/entity/history-conformance.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,MAAM,EAAE,UAAU,
|
|
1
|
+
{"version":3,"file":"history-conformance.js","sourceRoot":"","sources":["../../src/entity/history-conformance.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,MAAM,EAAE,UAAU,EAAgB,MAAM,oBAAoB,CAAC;AAEtE,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AACrD,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAY,MAAM,wBAAwB,CAAC;AACtF,OAAO,EACL,kBAAkB,GAEnB,MAAM,sEAAsE,CAAC;AAC9E,OA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C,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,KAAK,KAAK,OAAO,EAC5D,4CAA4C,CAC7C,CAAC;QACF,MAAM,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,eAAe,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC;QAChE,oBAAoB,CAAC,MAAM,CACzB,CAAC,MAAM,MAAM,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC,MAAM,KAAK,CAAC,EAC/C,6DAA6D,CAC9D,CAAC;IACJ,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,EAAU,EAAE,QAAgB,EAAE,OAAe;QACjD,OAAO,MAAM,CAAC,WAAW,EAAE;YACzB,EAAE,EAAE,MAAM,CAAC,aAAa,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC;YACxC,OAAO,EAAE;gBACP,UAAU,EAAE,SAAS,CAAC,IAAI,CACxB,iBAAiB,EACjB,MAAM,CAAC,iBAAiB,EAAE,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAC,CAC/C;gBACD,SAAS,EAAE,MAAM,CAAC,eAAe,EAAE,EAAE,OAAO,EAAE,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC;gBAChE,OAAO,EAAE,MAAM,CAAC,aAAa,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC;aACpD;SACF,CAAC,CAAC;IACL,CAAC;IAED;;;;OAIG;IACH,MAAM,CAAC,SAAkB,EAAE,OAAe;QACxC,IAAI,CAAC,SAAS;YAAE,MAAM,IAAI,KAAK,CAAC,sCAAsC,OAAO,EAAE,CAAC,CAAC;IACnF,CAAC;CACF,CAAC,CAAC"}
|
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
import type { Event, EventId, TenantId } from "@spine-event-engine/proto";
|
|
2
2
|
import type { RecordQuery } from "../record/record-query.js";
|
|
3
3
|
import { RecordSpec } from "../record/record-spec.js";
|
|
4
|
-
import
|
|
4
|
+
import { StorageGroup } from "../record/storage-group.js";
|
|
5
5
|
import type { StorageFactory } from "../storage/storage-factory.js";
|
|
6
|
+
/**
|
|
7
|
+
* Stable physical Event record family for one logical context and store role.
|
|
8
|
+
*/
|
|
9
|
+
export declare const EventStoreGroups: Readonly<{
|
|
10
|
+
forContext(contextName: string, role?: "domain" | "system"): StorageGroup;
|
|
11
|
+
identity(group: StorageGroup): string | undefined;
|
|
12
|
+
}>;
|
|
6
13
|
/**
|
|
7
14
|
* Framework event store backed by record storage.
|
|
8
15
|
*
|
|
@@ -28,9 +35,22 @@ export declare class EventStore {
|
|
|
28
35
|
*/
|
|
29
36
|
isOpen(): boolean;
|
|
30
37
|
/**
|
|
31
|
-
*
|
|
38
|
+
* Rejects new work, drains admitted storage groups, and closes retained handles.
|
|
39
|
+
* Repeated calls return the same close result.
|
|
40
|
+
*
|
|
41
|
+
* @returns Completion after all admitted work and storage cleanup settle.
|
|
42
|
+
*/
|
|
43
|
+
close(): Promise<void>;
|
|
44
|
+
/**
|
|
45
|
+
* Waits for admitted physical work and then closes every retained handle.
|
|
46
|
+
*
|
|
47
|
+
* @returns Completion after admitted work and cleanup settle.
|
|
32
48
|
*/
|
|
33
|
-
|
|
49
|
+
private drainAndClose;
|
|
50
|
+
/**
|
|
51
|
+
* Closes every retained handle and reports all cleanup failures.
|
|
52
|
+
*/
|
|
53
|
+
private closeStorages;
|
|
34
54
|
/**
|
|
35
55
|
* Validates strict new admission without storing an Event. An identical
|
|
36
56
|
* existing Event still rejects; this precheck does not reserve its ID.
|
|
@@ -96,11 +116,39 @@ export declare class EventStore {
|
|
|
96
116
|
* @returns Completion when no ID is stored.
|
|
97
117
|
*/
|
|
98
118
|
private checkUnique;
|
|
119
|
+
/**
|
|
120
|
+
* Executes one complete physical storage operation through settlement.
|
|
121
|
+
*
|
|
122
|
+
* @typeParam T Operation result type.
|
|
123
|
+
* @param context Captured tenant scope.
|
|
124
|
+
* @param onStorage Work admitted against the retained handle.
|
|
125
|
+
* @returns The operation result or its original failure.
|
|
126
|
+
*/
|
|
127
|
+
private runStorage;
|
|
128
|
+
/**
|
|
129
|
+
* Returns retained record storage for the captured tenant boundary.
|
|
130
|
+
*
|
|
131
|
+
* @param context Complete tenant-scoped storage context.
|
|
132
|
+
* @returns The retained record storage.
|
|
133
|
+
*/
|
|
134
|
+
private storageFor;
|
|
99
135
|
/**
|
|
100
136
|
* Rejects operations after this Event Store closes.
|
|
101
137
|
*/
|
|
102
138
|
private requireOpen;
|
|
103
139
|
}
|
|
140
|
+
/**
|
|
141
|
+
* Gives server assembly access to synchronous idle cleanup through the provider entrypoint.
|
|
142
|
+
* @internal
|
|
143
|
+
*/
|
|
144
|
+
export declare const eventStoreAccess: {
|
|
145
|
+
/**
|
|
146
|
+
* Closes an idle Event Store during synchronous assembly rollback.
|
|
147
|
+
*
|
|
148
|
+
* @param store Framework Event Store to close.
|
|
149
|
+
*/
|
|
150
|
+
closeIdle(store: EventStore): void;
|
|
151
|
+
};
|
|
104
152
|
/**
|
|
105
153
|
* Selects Event Store tenancy before an event envelope supplies a tenant.
|
|
106
154
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"event-store.d.ts","sourceRoot":"","sources":["../../src/event/event-store.ts"],"names":[],"mappings":"AAgBA,OAAO,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AAK1E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAC7D,OAAO,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAEtD,OAAO,
|
|
1
|
+
{"version":3,"file":"event-store.d.ts","sourceRoot":"","sources":["../../src/event/event-store.ts"],"names":[],"mappings":"AAgBA,OAAO,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AAK1E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAC7D,O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B;;;;;;OAMG;YACW,eAAe;IAM7B;;;;;;OAMG;YACW,WAAW;IAOzB;;;;;;;OAOG;YACW,UAAU;IAcxB;;;;;OAKG;IACH,OAAO,CAAC,UAAU;IAUlB;;OAEG;IACH,OAAO,CAAC,WAAW;CAGpB;AAED;;;GAGG;AACH,eAAO,MAAM,gBAAgB;IAC3B;;;;OAIG;qBACc,UAAU,GAAG,IAAI;CAKnC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,MAAM,iBAAiB,GACzB;IAGE;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB;;OAEG;IACH,QAAQ,CAAC,WAAW,EAAE,KAAK,CAAC;CAC7B,GACD;IAGE;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB;;OAEG;IACH,QAAQ,CAAC,WAAW,EAAE,IAAI,CAAC;IAE3B;;OAEG;IACH,QAAQ,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAC;CAC9B,CAAC;AAEN;;;;;GAKG;AACH,MAAM,MAAM,eAAe,GAAG,CAAC,KAAK,EAAE,KAAK,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;AAmNrE;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,EAAE,UAAU,CAAC,OAAO,EAAE,KAAK,CAgB1D,CAAC"}
|
|
@@ -17,7 +17,33 @@ import { EventIdSchema, EventSchema, TenantIdSchema } from "@spine-event-engine/
|
|
|
17
17
|
import { RecordColumn } from "../record/record-column.js";
|
|
18
18
|
import { ColumnTypes } from "../record/column-type.js";
|
|
19
19
|
import { RecordSpec } from "../record/record-spec.js";
|
|
20
|
+
import { StorageGroup } from "../record/storage-group.js";
|
|
20
21
|
import { TenantBoundary } from "../internal/tenancy.js";
|
|
22
|
+
const idleClosers = new WeakMap();
|
|
23
|
+
const eventStoreSelections = new WeakMap();
|
|
24
|
+
/**
|
|
25
|
+
* Stable physical Event record family for one logical context and store role.
|
|
26
|
+
*/
|
|
27
|
+
export const EventStoreGroups = Object.freeze({
|
|
28
|
+
/**
|
|
29
|
+
* Selects the readable Bounded Context name and retains the Event Store role.
|
|
30
|
+
*
|
|
31
|
+
* @param contextName Framework-validated logical context name.
|
|
32
|
+
* @param role Domain or paired System Event store.
|
|
33
|
+
* @returns Stable physical Event family selector.
|
|
34
|
+
*/
|
|
35
|
+
forContext(contextName, role = "domain") {
|
|
36
|
+
const group = new StorageGroup(contextName);
|
|
37
|
+
eventStoreSelections.set(group, { name: contextName, role });
|
|
38
|
+
return group;
|
|
39
|
+
},
|
|
40
|
+
identity(group) {
|
|
41
|
+
const selection = eventStoreSelections.get(group);
|
|
42
|
+
return selection === undefined
|
|
43
|
+
? undefined
|
|
44
|
+
: JSON.stringify(["EventStore", selection.role, selection.name]);
|
|
45
|
+
},
|
|
46
|
+
});
|
|
21
47
|
/**
|
|
22
48
|
* Framework event store backed by record storage.
|
|
23
49
|
*
|
|
@@ -36,9 +62,25 @@ export class EventStore {
|
|
|
36
62
|
*/
|
|
37
63
|
#factory;
|
|
38
64
|
/**
|
|
39
|
-
*
|
|
65
|
+
* Record family selected by the logical context and store role.
|
|
40
66
|
*/
|
|
41
67
|
#group;
|
|
68
|
+
/**
|
|
69
|
+
* Retains one record storage per complete tenant boundary.
|
|
70
|
+
*/
|
|
71
|
+
#storages = new Map();
|
|
72
|
+
/**
|
|
73
|
+
* Tracks admitted physical storage groups until their complete operation settles.
|
|
74
|
+
*/
|
|
75
|
+
#active = new Set();
|
|
76
|
+
/**
|
|
77
|
+
* Reuses one asynchronous close result across repeated calls.
|
|
78
|
+
*/
|
|
79
|
+
#closing;
|
|
80
|
+
/**
|
|
81
|
+
* Retains the terminal result of synchronous assembly cleanup.
|
|
82
|
+
*/
|
|
83
|
+
#idleOutcome;
|
|
42
84
|
/**
|
|
43
85
|
* Whether this Event Store accepts operations.
|
|
44
86
|
*/
|
|
@@ -53,7 +95,10 @@ export class EventStore {
|
|
|
53
95
|
constructor(context, factory, group) {
|
|
54
96
|
this.#context = EventContexts.base(context);
|
|
55
97
|
this.#factory = factory;
|
|
56
|
-
this.#group = group;
|
|
98
|
+
this.#group = group ?? EventStoreGroups.forContext(this.#context.name);
|
|
99
|
+
idleClosers.set(this, () => {
|
|
100
|
+
this.#closeIdle();
|
|
101
|
+
});
|
|
57
102
|
}
|
|
58
103
|
/**
|
|
59
104
|
* Returns whether this Event Store still accepts operations.
|
|
@@ -64,10 +109,80 @@ export class EventStore {
|
|
|
64
109
|
return this.#open;
|
|
65
110
|
}
|
|
66
111
|
/**
|
|
67
|
-
*
|
|
112
|
+
* Rejects new work, drains admitted storage groups, and closes retained handles.
|
|
113
|
+
* Repeated calls return the same close result.
|
|
114
|
+
*
|
|
115
|
+
* @returns Completion after all admitted work and storage cleanup settle.
|
|
68
116
|
*/
|
|
69
117
|
close() {
|
|
118
|
+
if (this.#closing !== undefined)
|
|
119
|
+
return this.#closing;
|
|
120
|
+
if (this.#idleOutcome !== undefined) {
|
|
121
|
+
const error = this.#idleOutcome.error;
|
|
122
|
+
return error === undefined ? Promise.resolve() : Promise.reject(error);
|
|
123
|
+
}
|
|
70
124
|
this.#open = false;
|
|
125
|
+
this.#closing = this.drainAndClose();
|
|
126
|
+
return this.#closing;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Closes an idle store during synchronous assembly cleanup.
|
|
130
|
+
* Active storage work is a misuse and leaves all handles open.
|
|
131
|
+
*
|
|
132
|
+
*/
|
|
133
|
+
#closeIdle() {
|
|
134
|
+
if (this.#active.size > 0 || (this.#closing !== undefined && this.#idleOutcome === undefined))
|
|
135
|
+
throw new Error("EventStore closeIdle requires no active storage work.");
|
|
136
|
+
if (this.#idleOutcome !== undefined) {
|
|
137
|
+
if (this.#idleOutcome.error !== undefined)
|
|
138
|
+
throw this.#idleOutcome.error;
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
this.#open = false;
|
|
142
|
+
try {
|
|
143
|
+
this.closeStorages();
|
|
144
|
+
this.#idleOutcome = {};
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
this.#idleOutcome = { error: error };
|
|
148
|
+
throw error;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Waits for admitted physical work and then closes every retained handle.
|
|
153
|
+
*
|
|
154
|
+
* @returns Completion after admitted work and cleanup settle.
|
|
155
|
+
*/
|
|
156
|
+
async drainAndClose() {
|
|
157
|
+
await Promise.allSettled([...this.#active]);
|
|
158
|
+
try {
|
|
159
|
+
this.closeStorages();
|
|
160
|
+
this.#idleOutcome = {};
|
|
161
|
+
}
|
|
162
|
+
catch (error) {
|
|
163
|
+
this.#idleOutcome = { error: error };
|
|
164
|
+
throw error;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Closes every retained handle and reports all cleanup failures.
|
|
169
|
+
*/
|
|
170
|
+
closeStorages() {
|
|
171
|
+
const storages = [...this.#storages.values()];
|
|
172
|
+
this.#storages.clear();
|
|
173
|
+
const errors = [];
|
|
174
|
+
for (const storage of storages) {
|
|
175
|
+
try {
|
|
176
|
+
storage.close();
|
|
177
|
+
}
|
|
178
|
+
catch (error) {
|
|
179
|
+
errors.push(error);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
if (errors.length === 1)
|
|
183
|
+
throw errors[0];
|
|
184
|
+
if (errors.length > 1)
|
|
185
|
+
throw new AggregateError(errors, "EventStore close failed.");
|
|
71
186
|
}
|
|
72
187
|
/**
|
|
73
188
|
* Validates strict new admission without storing an Event. An identical
|
|
@@ -117,6 +232,7 @@ export class EventStore {
|
|
|
117
232
|
* @returns Completes when the events are appended.
|
|
118
233
|
*/
|
|
119
234
|
async appendAll(events) {
|
|
235
|
+
this.requireOpen();
|
|
120
236
|
const records = [...events].map((event) => clone(EventSchema, event));
|
|
121
237
|
if (records.length > 0) {
|
|
122
238
|
await this.appendImmutable(records, EventContexts.batch(this.#context, records));
|
|
@@ -130,13 +246,7 @@ export class EventStore {
|
|
|
130
246
|
*/
|
|
131
247
|
async read(query = {}) {
|
|
132
248
|
this.requireOpen();
|
|
133
|
-
|
|
134
|
-
try {
|
|
135
|
-
return await storage.query(query);
|
|
136
|
-
}
|
|
137
|
-
finally {
|
|
138
|
-
storage.close();
|
|
139
|
-
}
|
|
249
|
+
return this.runStorage(EventContexts.snapshot(this.#context), (storage) => storage.query(query));
|
|
140
250
|
}
|
|
141
251
|
/**
|
|
142
252
|
* Writes strictly new Events after caller acceptance.
|
|
@@ -149,13 +259,7 @@ export class EventStore {
|
|
|
149
259
|
this.requireOpen();
|
|
150
260
|
const ids = records.map((record) => EventIds.require(record));
|
|
151
261
|
EventIds.rejectDuplicates(ids);
|
|
152
|
-
|
|
153
|
-
try {
|
|
154
|
-
await EventIds.insertUnique(storage, records);
|
|
155
|
-
}
|
|
156
|
-
finally {
|
|
157
|
-
storage.close();
|
|
158
|
-
}
|
|
262
|
+
await this.runStorage(context, (storage) => EventIds.insertUnique(storage, records));
|
|
159
263
|
}
|
|
160
264
|
/**
|
|
161
265
|
* Writes captured immutable Events, accepting identical stored retries.
|
|
@@ -167,13 +271,7 @@ export class EventStore {
|
|
|
167
271
|
async appendImmutable(records, context) {
|
|
168
272
|
this.requireOpen();
|
|
169
273
|
EventIds.rejectDuplicates(records.map((record) => EventIds.require(record)));
|
|
170
|
-
|
|
171
|
-
try {
|
|
172
|
-
await EventIds.insertImmutable(storage, records);
|
|
173
|
-
}
|
|
174
|
-
finally {
|
|
175
|
-
storage.close();
|
|
176
|
-
}
|
|
274
|
+
await this.runStorage(context, (storage) => EventIds.insertImmutable(storage, records));
|
|
177
275
|
}
|
|
178
276
|
/**
|
|
179
277
|
* Checks strict admission without writing an Event.
|
|
@@ -185,14 +283,43 @@ export class EventStore {
|
|
|
185
283
|
async checkUnique(ids, context) {
|
|
186
284
|
this.requireOpen();
|
|
187
285
|
EventIds.rejectDuplicates(ids);
|
|
188
|
-
|
|
286
|
+
await this.runStorage(context, (storage) => EventIds.rejectStored(storage, ids));
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Executes one complete physical storage operation through settlement.
|
|
290
|
+
*
|
|
291
|
+
* @typeParam T Operation result type.
|
|
292
|
+
* @param context Captured tenant scope.
|
|
293
|
+
* @param onStorage Work admitted against the retained handle.
|
|
294
|
+
* @returns The operation result or its original failure.
|
|
295
|
+
*/
|
|
296
|
+
async runStorage(context, onStorage) {
|
|
297
|
+
const storage = this.storageFor(context);
|
|
298
|
+
const work = Promise.resolve().then(() => onStorage(storage));
|
|
299
|
+
this.#active.add(work);
|
|
189
300
|
try {
|
|
190
|
-
await
|
|
301
|
+
return await work;
|
|
191
302
|
}
|
|
192
303
|
finally {
|
|
193
|
-
|
|
304
|
+
this.#active.delete(work);
|
|
194
305
|
}
|
|
195
306
|
}
|
|
307
|
+
/**
|
|
308
|
+
* Returns retained record storage for the captured tenant boundary.
|
|
309
|
+
*
|
|
310
|
+
* @param context Complete tenant-scoped storage context.
|
|
311
|
+
* @returns The retained record storage.
|
|
312
|
+
*/
|
|
313
|
+
storageFor(context) {
|
|
314
|
+
this.requireOpen();
|
|
315
|
+
const key = TenantBoundary.of(context).key;
|
|
316
|
+
const existing = this.#storages.get(key);
|
|
317
|
+
if (existing !== undefined)
|
|
318
|
+
return existing;
|
|
319
|
+
const storage = this.#factory.createRecordStorage(context, eventStoreRecordSpec, this.#group);
|
|
320
|
+
this.#storages.set(key, storage);
|
|
321
|
+
return storage;
|
|
322
|
+
}
|
|
196
323
|
/**
|
|
197
324
|
* Rejects operations after this Event Store closes.
|
|
198
325
|
*/
|
|
@@ -201,6 +328,23 @@ export class EventStore {
|
|
|
201
328
|
throw new Error("EventStore is closed.");
|
|
202
329
|
}
|
|
203
330
|
}
|
|
331
|
+
/**
|
|
332
|
+
* Gives server assembly access to synchronous idle cleanup through the provider entrypoint.
|
|
333
|
+
* @internal
|
|
334
|
+
*/
|
|
335
|
+
export const eventStoreAccess = {
|
|
336
|
+
/**
|
|
337
|
+
* Closes an idle Event Store during synchronous assembly rollback.
|
|
338
|
+
*
|
|
339
|
+
* @param store Framework Event Store to close.
|
|
340
|
+
*/
|
|
341
|
+
closeIdle(store) {
|
|
342
|
+
const close = idleClosers.get(store);
|
|
343
|
+
if (close === undefined)
|
|
344
|
+
throw new TypeError("EventStore idle cleanup requires an EventStore.");
|
|
345
|
+
close();
|
|
346
|
+
},
|
|
347
|
+
};
|
|
204
348
|
/**
|
|
205
349
|
* Validates event IDs before record-store operations.
|
|
206
350
|
*/
|