@event-driven-io/emmett-sqlite 0.43.0-beta.13 → 0.43.0-beta.15
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/cli.cjs +0 -1
- package/dist/cli.d.cts +1 -2
- package/dist/cli.d.ts +1 -2
- package/dist/cli.js +1 -1
- package/dist/cloudflare.cjs +16 -16
- package/dist/cloudflare.cjs.map +1 -1
- package/dist/cloudflare.d.cts +9 -10
- package/dist/cloudflare.d.ts +9 -10
- package/dist/cloudflare.js +18 -19
- package/dist/cloudflare.js.map +1 -1
- package/dist/index-2i8q-ZKl.d.ts +415 -0
- package/dist/index-CqxXzmd1.d.cts +415 -0
- package/dist/index.cjs +1164 -2613
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -243
- package/dist/index.d.ts +2 -243
- package/dist/index.js +1090 -2586
- package/dist/index.js.map +1 -1
- package/dist/sqlite3.cjs +14 -14
- package/dist/sqlite3.cjs.map +1 -1
- package/dist/sqlite3.d.cts +8 -9
- package/dist/sqlite3.d.ts +8 -9
- package/dist/sqlite3.js +14 -15
- package/dist/sqlite3.js.map +1 -1
- package/package.json +10 -10
- package/dist/cli.cjs.map +0 -1
- package/dist/cli.js.map +0 -1
- package/dist/sqliteProjection-BrbKryzB.d.cts +0 -150
- package/dist/sqliteProjection-BrbKryzB.d.ts +0 -150
package/dist/index.js
CHANGED
|
@@ -1,1703 +1,427 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
return typeof expect === "function" && expect.prototype && // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
6
|
-
expect.prototype.constructor === expect;
|
|
7
|
-
};
|
|
8
|
-
var EmmettError = class _EmmettError extends Error {
|
|
9
|
-
static Codes = {
|
|
10
|
-
ValidationError: 400,
|
|
11
|
-
IllegalStateError: 403,
|
|
12
|
-
NotFoundError: 404,
|
|
13
|
-
ConcurrencyError: 412,
|
|
14
|
-
InternalServerError: 500
|
|
15
|
-
};
|
|
16
|
-
errorCode;
|
|
17
|
-
constructor(options) {
|
|
18
|
-
const errorCode = options && typeof options === "object" && "errorCode" in options ? options.errorCode : isNumber(options) ? options : _EmmettError.Codes.InternalServerError;
|
|
19
|
-
const message = options && typeof options === "object" && "message" in options ? options.message : isString(options) ? options : `Error with status code '${errorCode}' ocurred during Emmett processing`;
|
|
20
|
-
super(message);
|
|
21
|
-
this.errorCode = errorCode;
|
|
22
|
-
Object.setPrototypeOf(this, _EmmettError.prototype);
|
|
23
|
-
}
|
|
24
|
-
static mapFrom(error) {
|
|
25
|
-
if (_EmmettError.isInstanceOf(error)) {
|
|
26
|
-
return error;
|
|
27
|
-
}
|
|
28
|
-
return new _EmmettError({
|
|
29
|
-
errorCode: "errorCode" in error && error.errorCode !== void 0 && error.errorCode !== null ? error.errorCode : _EmmettError.Codes.InternalServerError,
|
|
30
|
-
message: error.message ?? "An unknown error occurred"
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
static isInstanceOf(error, errorCode) {
|
|
34
|
-
return typeof error === "object" && error !== null && "errorCode" in error && isNumber(error.errorCode) && (errorCode === void 0 || error.errorCode === errorCode);
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
var ConcurrencyError = class _ConcurrencyError extends EmmettError {
|
|
38
|
-
constructor(current, expected, message) {
|
|
39
|
-
super({
|
|
40
|
-
errorCode: EmmettError.Codes.ConcurrencyError,
|
|
41
|
-
message: message ?? `Expected version ${expected.toString()} does not match current ${current?.toString()}`
|
|
42
|
-
});
|
|
43
|
-
this.current = current;
|
|
44
|
-
this.expected = expected;
|
|
45
|
-
Object.setPrototypeOf(this, _ConcurrencyError.prototype);
|
|
46
|
-
}
|
|
47
|
-
};
|
|
1
|
+
import { AssertionError, EmmettError, ExpectedVersionConflictError, JSONSerializer, NO_CONCURRENCY_CHECK, STREAM_DOES_NOT_EXIST, STREAM_EXISTS, assertDeepEqual, assertEqual, assertExpectedVersionMatchesCurrent, assertFails, assertIsNotNull, assertIsNull, assertThatArray, assertTrue, asyncAwaiter, bigIntProcessorCheckpoint, defaultProcessorPartition, defaultProcessorVersion, downcastRecordedMessages, getCheckpoint, getProcessorInstanceId, getProjectorId, getWorkflowId, isErrorConstructor, isExpectedVersionConflictError, parseBigIntProcessorCheckpoint, projection, projector, reactor, reduceAsync, upcastRecordedMessage, workflowProcessor } from "@event-driven-io/emmett";
|
|
2
|
+
import { pongoClient } from "@event-driven-io/pongo";
|
|
3
|
+
import { BatchCommandNoChangesError, DumboError, SQL, UniqueConstraintError, dumbo, exists, mapRows, singleOrNull } from "@event-driven-io/dumbo";
|
|
4
|
+
import { v4, v7 } from "uuid";
|
|
48
5
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
6
|
+
//#region src/eventStore/projections/pongo/pongoProjections.ts
|
|
7
|
+
const pongoProjection = ({ name, kind, version, truncate, handle, canHandle, eventsOptions }) => sqliteProjection({
|
|
8
|
+
name,
|
|
9
|
+
version,
|
|
10
|
+
kind: kind ?? "emt:projections:postgresql:pongo:generic",
|
|
11
|
+
canHandle,
|
|
12
|
+
eventsOptions,
|
|
13
|
+
handle: async (events, context) => {
|
|
14
|
+
const { connection } = context;
|
|
15
|
+
const pongo = pongoClient({
|
|
16
|
+
driver: await pongoDriverRegistry.tryResolve(context.driverType),
|
|
17
|
+
connectionOptions: { connection }
|
|
18
|
+
});
|
|
19
|
+
try {
|
|
20
|
+
await handle(events, {
|
|
21
|
+
...context,
|
|
22
|
+
pongo
|
|
23
|
+
});
|
|
24
|
+
} finally {
|
|
25
|
+
await pongo.close();
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
truncate: truncate ? async (context) => {
|
|
29
|
+
const { connection } = context;
|
|
30
|
+
const pongo = pongoClient({
|
|
31
|
+
driver: await pongoDriverRegistry.tryResolve(context.driverType),
|
|
32
|
+
connectionOptions: { connection }
|
|
33
|
+
});
|
|
34
|
+
try {
|
|
35
|
+
await truncate({
|
|
36
|
+
...context,
|
|
37
|
+
pongo
|
|
38
|
+
});
|
|
39
|
+
} finally {
|
|
40
|
+
await pongo.close();
|
|
41
|
+
}
|
|
42
|
+
} : void 0
|
|
69
43
|
});
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
44
|
+
const pongoMultiStreamProjection = (options) => {
|
|
45
|
+
const { collectionName, getDocumentId, canHandle } = options;
|
|
46
|
+
const collectionNameWithVersion = options.version && options.version > 0 ? `${collectionName}_v${options.version}` : collectionName;
|
|
47
|
+
return pongoProjection({
|
|
48
|
+
name: collectionNameWithVersion,
|
|
49
|
+
version: options.version,
|
|
50
|
+
kind: options.kind ?? "emt:projections:postgresql:pongo:multi_stream",
|
|
51
|
+
eventsOptions: options.eventsOptions,
|
|
52
|
+
handle: async (events, { pongo }) => {
|
|
53
|
+
const collection = pongo.db().collection(collectionNameWithVersion, options.collectionOptions);
|
|
54
|
+
const eventsByDocumentId = events.map((event) => {
|
|
55
|
+
return {
|
|
56
|
+
documentId: getDocumentId(event),
|
|
57
|
+
event
|
|
58
|
+
};
|
|
59
|
+
}).reduce((acc, { documentId, event }) => {
|
|
60
|
+
if (!acc.has(documentId)) acc.set(documentId, []);
|
|
61
|
+
acc.get(documentId).push(event);
|
|
62
|
+
return acc;
|
|
63
|
+
}, /* @__PURE__ */ new Map());
|
|
64
|
+
await collection.handle([...eventsByDocumentId.keys()], (document, id) => {
|
|
65
|
+
return reduceAsync(eventsByDocumentId.get(id), async (acc, event) => await options.evolve(acc, event), document ?? ("initialState" in options ? options.initialState() : null));
|
|
66
|
+
});
|
|
67
|
+
},
|
|
68
|
+
canHandle,
|
|
69
|
+
truncate: async (context) => {
|
|
70
|
+
const { connection } = context;
|
|
71
|
+
const pongo = pongoClient({
|
|
72
|
+
driver: await pongoDriverRegistry.tryResolve(context.driverType),
|
|
73
|
+
connectionOptions: { connection }
|
|
74
|
+
});
|
|
75
|
+
try {
|
|
76
|
+
await pongo.db().collection(collectionNameWithVersion, options.collectionOptions).deleteMany();
|
|
77
|
+
} finally {
|
|
78
|
+
await pongo.close();
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
init: async (context) => {
|
|
82
|
+
const { connection } = context;
|
|
83
|
+
const driver = await pongoDriverRegistry.tryResolve(context.driverType);
|
|
84
|
+
const pongo = pongoClient({
|
|
85
|
+
connectionOptions: { connection },
|
|
86
|
+
driver
|
|
87
|
+
});
|
|
88
|
+
try {
|
|
89
|
+
await pongo.db().collection(collectionNameWithVersion, options.collectionOptions).schema.migrate();
|
|
90
|
+
} finally {
|
|
91
|
+
await pongo.close();
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
};
|
|
96
|
+
const pongoSingleStreamProjection = (options) => {
|
|
97
|
+
return pongoMultiStreamProjection({
|
|
98
|
+
...options,
|
|
99
|
+
kind: "emt:projections:postgresql:pongo:single_stream",
|
|
100
|
+
getDocumentId: options.getDocumentId ?? ((event) => event.metadata.streamName)
|
|
101
|
+
});
|
|
116
102
|
};
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
found = true;
|
|
149
|
-
break;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
if (!found) return false;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
return true;
|
|
156
|
-
};
|
|
157
|
-
var compareSets = (left, right) => {
|
|
158
|
-
if (left.size !== right.size) return false;
|
|
159
|
-
for (const leftItem of left) {
|
|
160
|
-
if (isPrimitive(leftItem)) {
|
|
161
|
-
if (!right.has(leftItem)) return false;
|
|
162
|
-
} else {
|
|
163
|
-
let found = false;
|
|
164
|
-
for (const rightItem of right) {
|
|
165
|
-
if (deepEquals(leftItem, rightItem)) {
|
|
166
|
-
found = true;
|
|
167
|
-
break;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
if (!found) return false;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
return true;
|
|
174
|
-
};
|
|
175
|
-
var compareArrayBuffers = (left, right) => {
|
|
176
|
-
if (left.byteLength !== right.byteLength) return false;
|
|
177
|
-
const leftView = new Uint8Array(left);
|
|
178
|
-
const rightView = new Uint8Array(right);
|
|
179
|
-
for (let i = 0; i < leftView.length; i++) {
|
|
180
|
-
if (leftView[i] !== rightView[i]) return false;
|
|
181
|
-
}
|
|
182
|
-
return true;
|
|
183
|
-
};
|
|
184
|
-
var compareTypedArrays = (left, right) => {
|
|
185
|
-
if (left.constructor !== right.constructor) return false;
|
|
186
|
-
if (left.byteLength !== right.byteLength) return false;
|
|
187
|
-
const leftArray = new Uint8Array(
|
|
188
|
-
left.buffer,
|
|
189
|
-
left.byteOffset,
|
|
190
|
-
left.byteLength
|
|
191
|
-
);
|
|
192
|
-
const rightArray = new Uint8Array(
|
|
193
|
-
right.buffer,
|
|
194
|
-
right.byteOffset,
|
|
195
|
-
right.byteLength
|
|
196
|
-
);
|
|
197
|
-
for (let i = 0; i < leftArray.length; i++) {
|
|
198
|
-
if (leftArray[i] !== rightArray[i]) return false;
|
|
199
|
-
}
|
|
200
|
-
return true;
|
|
201
|
-
};
|
|
202
|
-
var compareObjects = (left, right) => {
|
|
203
|
-
const keys1 = Object.keys(left);
|
|
204
|
-
const keys2 = Object.keys(right);
|
|
205
|
-
if (keys1.length !== keys2.length) {
|
|
206
|
-
return false;
|
|
207
|
-
}
|
|
208
|
-
for (const key of keys1) {
|
|
209
|
-
if (left[key] instanceof Function && right[key] instanceof Function) {
|
|
210
|
-
continue;
|
|
211
|
-
}
|
|
212
|
-
const isEqual = deepEquals(left[key], right[key]);
|
|
213
|
-
if (!isEqual) {
|
|
214
|
-
return false;
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
return true;
|
|
218
|
-
};
|
|
219
|
-
var getType = (value) => {
|
|
220
|
-
if (value === null) return "null";
|
|
221
|
-
if (value === void 0) return "undefined";
|
|
222
|
-
const primitiveType = typeof value;
|
|
223
|
-
if (primitiveType !== "object") return primitiveType;
|
|
224
|
-
if (Array.isArray(value)) return "array";
|
|
225
|
-
if (value instanceof Boolean) return "boxed-boolean";
|
|
226
|
-
if (value instanceof Number) return "boxed-number";
|
|
227
|
-
if (value instanceof String) return "boxed-string";
|
|
228
|
-
if (value instanceof Date) return "date";
|
|
229
|
-
if (value instanceof RegExp) return "regexp";
|
|
230
|
-
if (value instanceof Error) return "error";
|
|
231
|
-
if (value instanceof Map) return "map";
|
|
232
|
-
if (value instanceof Set) return "set";
|
|
233
|
-
if (value instanceof ArrayBuffer) return "arraybuffer";
|
|
234
|
-
if (value instanceof DataView) return "dataview";
|
|
235
|
-
if (value instanceof WeakMap) return "weakmap";
|
|
236
|
-
if (value instanceof WeakSet) return "weakset";
|
|
237
|
-
if (ArrayBuffer.isView(value)) return "typedarray";
|
|
238
|
-
return "object";
|
|
239
|
-
};
|
|
240
|
-
var deepEquals = (left, right) => {
|
|
241
|
-
if (left === right) return true;
|
|
242
|
-
if (isEquatable(left)) {
|
|
243
|
-
return left.equals(right);
|
|
244
|
-
}
|
|
245
|
-
const leftType = getType(left);
|
|
246
|
-
const rightType = getType(right);
|
|
247
|
-
if (leftType !== rightType) return false;
|
|
248
|
-
switch (leftType) {
|
|
249
|
-
case "null":
|
|
250
|
-
case "undefined":
|
|
251
|
-
case "boolean":
|
|
252
|
-
case "number":
|
|
253
|
-
case "bigint":
|
|
254
|
-
case "string":
|
|
255
|
-
case "symbol":
|
|
256
|
-
case "function":
|
|
257
|
-
return left === right;
|
|
258
|
-
case "array":
|
|
259
|
-
return compareArrays(left, right);
|
|
260
|
-
case "date":
|
|
261
|
-
return compareDates(left, right);
|
|
262
|
-
case "regexp":
|
|
263
|
-
return compareRegExps(left, right);
|
|
264
|
-
case "error":
|
|
265
|
-
return compareErrors(left, right);
|
|
266
|
-
case "map":
|
|
267
|
-
return compareMaps(
|
|
268
|
-
left,
|
|
269
|
-
right
|
|
270
|
-
);
|
|
271
|
-
case "set":
|
|
272
|
-
return compareSets(left, right);
|
|
273
|
-
case "arraybuffer":
|
|
274
|
-
return compareArrayBuffers(left, right);
|
|
275
|
-
case "dataview":
|
|
276
|
-
case "weakmap":
|
|
277
|
-
case "weakset":
|
|
278
|
-
return false;
|
|
279
|
-
case "typedarray":
|
|
280
|
-
return compareTypedArrays(
|
|
281
|
-
left,
|
|
282
|
-
right
|
|
283
|
-
);
|
|
284
|
-
case "boxed-boolean":
|
|
285
|
-
return left.valueOf() === right.valueOf();
|
|
286
|
-
case "boxed-number":
|
|
287
|
-
return left.valueOf() === right.valueOf();
|
|
288
|
-
case "boxed-string":
|
|
289
|
-
return left.valueOf() === right.valueOf();
|
|
290
|
-
case "object":
|
|
291
|
-
return compareObjects(
|
|
292
|
-
left,
|
|
293
|
-
right
|
|
294
|
-
);
|
|
295
|
-
default:
|
|
296
|
-
return false;
|
|
297
|
-
}
|
|
298
|
-
};
|
|
299
|
-
var isEquatable = (left) => {
|
|
300
|
-
return left !== null && left !== void 0 && typeof left === "object" && "equals" in left && typeof left["equals"] === "function";
|
|
301
|
-
};
|
|
302
|
-
var toNormalizedString = (value) => value.toString().padStart(19, "0");
|
|
303
|
-
var bigInt = {
|
|
304
|
-
toNormalizedString
|
|
305
|
-
};
|
|
306
|
-
var bigIntReplacer = (_key, value) => {
|
|
307
|
-
return typeof value === "bigint" ? value.toString() : value;
|
|
308
|
-
};
|
|
309
|
-
var dateReplacer = (_key, value) => {
|
|
310
|
-
return value instanceof Date ? value.toISOString() : value;
|
|
311
|
-
};
|
|
312
|
-
var isFirstLetterNumeric = (str) => {
|
|
313
|
-
const c = str.charCodeAt(0);
|
|
314
|
-
return c >= 48 && c <= 57;
|
|
315
|
-
};
|
|
316
|
-
var isFirstLetterNumericOrMinus = (str) => {
|
|
317
|
-
const c = str.charCodeAt(0);
|
|
318
|
-
return c >= 48 && c <= 57 || c === 45;
|
|
319
|
-
};
|
|
320
|
-
var bigIntReviver = (_key, value, context) => {
|
|
321
|
-
if (typeof value === "number" && Number.isInteger(value) && !Number.isSafeInteger(value)) {
|
|
322
|
-
try {
|
|
323
|
-
return BigInt(context?.source ?? value.toString());
|
|
324
|
-
} catch {
|
|
325
|
-
return value;
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
if (typeof value === "string" && value.length > 15) {
|
|
329
|
-
if (isFirstLetterNumericOrMinus(value)) {
|
|
330
|
-
const num = Number(value);
|
|
331
|
-
if (Number.isFinite(num) && !Number.isSafeInteger(num)) {
|
|
332
|
-
try {
|
|
333
|
-
return BigInt(value);
|
|
334
|
-
} catch {
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
return value;
|
|
340
|
-
};
|
|
341
|
-
var dateReviver = (_key, value) => {
|
|
342
|
-
if (typeof value === "string" && value.length === 24 && isFirstLetterNumeric(value) && value[10] === "T" && value[23] === "Z") {
|
|
343
|
-
const date = new Date(value);
|
|
344
|
-
if (!isNaN(date.getTime())) {
|
|
345
|
-
return date;
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
return value;
|
|
349
|
-
};
|
|
350
|
-
var composeJSONReplacers = (...replacers) => {
|
|
351
|
-
const filteredReplacers = replacers.filter((r) => r !== void 0);
|
|
352
|
-
if (filteredReplacers.length === 0) return void 0;
|
|
353
|
-
return (key, value) => (
|
|
354
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
355
|
-
filteredReplacers.reduce(
|
|
356
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
357
|
-
(accValue, replacer) => replacer(key, accValue),
|
|
358
|
-
value
|
|
359
|
-
)
|
|
360
|
-
);
|
|
361
|
-
};
|
|
362
|
-
var composeJSONRevivers = (...revivers) => {
|
|
363
|
-
const filteredRevivers = revivers.filter((r) => r !== void 0);
|
|
364
|
-
if (filteredRevivers.length === 0) return void 0;
|
|
365
|
-
return (key, value, context) => (
|
|
366
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
367
|
-
filteredRevivers.reduce(
|
|
368
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
369
|
-
(accValue, reviver) => reviver(key, accValue, context),
|
|
370
|
-
value
|
|
371
|
-
)
|
|
372
|
-
);
|
|
373
|
-
};
|
|
374
|
-
var JSONReplacer = (opts) => composeJSONReplacers(
|
|
375
|
-
opts?.replacer,
|
|
376
|
-
opts?.failOnBigIntSerialization !== true ? JSONReplacers.bigInt : void 0,
|
|
377
|
-
opts?.useDefaultDateSerialization !== true ? JSONReplacers.date : void 0
|
|
378
|
-
);
|
|
379
|
-
var JSONReviver = (opts) => composeJSONRevivers(
|
|
380
|
-
opts?.reviver,
|
|
381
|
-
opts?.parseBigInts === true ? JSONRevivers.bigInt : void 0,
|
|
382
|
-
opts?.parseDates === true ? JSONRevivers.date : void 0
|
|
383
|
-
);
|
|
384
|
-
var JSONReplacers = {
|
|
385
|
-
bigInt: bigIntReplacer,
|
|
386
|
-
date: dateReplacer
|
|
387
|
-
};
|
|
388
|
-
var JSONRevivers = {
|
|
389
|
-
bigInt: bigIntReviver,
|
|
390
|
-
date: dateReviver
|
|
391
|
-
};
|
|
392
|
-
var jsonSerializer = (options) => {
|
|
393
|
-
const defaultReplacer = JSONReplacer(options);
|
|
394
|
-
const defaultReviver = JSONReviver(options);
|
|
395
|
-
return {
|
|
396
|
-
serialize: (object, serializerOptions) => JSON.stringify(
|
|
397
|
-
object,
|
|
398
|
-
serializerOptions ? JSONReplacer(serializerOptions) : defaultReplacer
|
|
399
|
-
),
|
|
400
|
-
deserialize: (payload, deserializerOptions) => JSON.parse(
|
|
401
|
-
payload,
|
|
402
|
-
deserializerOptions ? JSONReviver(deserializerOptions) : defaultReviver
|
|
403
|
-
)
|
|
404
|
-
};
|
|
405
|
-
};
|
|
406
|
-
var JSONSerializer = Object.assign(jsonSerializer(), {
|
|
407
|
-
from: (options) => options?.serialization?.serializer ?? (options?.serialization?.options ? jsonSerializer(options?.serialization?.options) : JSONSerializer)
|
|
103
|
+
|
|
104
|
+
//#endregion
|
|
105
|
+
//#region src/eventStore/projections/pongo/pongoProjectionSpec.ts
|
|
106
|
+
const withCollection = async (handle, options) => {
|
|
107
|
+
const { connection, inDatabase, inCollection } = options;
|
|
108
|
+
const driver = await pongoDriverRegistry.tryResolve(connection.driverType);
|
|
109
|
+
const pongo = pongoClient({
|
|
110
|
+
connectionOptions: { connection },
|
|
111
|
+
driver
|
|
112
|
+
});
|
|
113
|
+
try {
|
|
114
|
+
return handle(pongo.db(inDatabase).collection(inCollection));
|
|
115
|
+
} finally {
|
|
116
|
+
await pongo.close();
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
const withoutIdAndVersion = (doc) => {
|
|
120
|
+
const { _id, _version, ...without } = doc;
|
|
121
|
+
return without;
|
|
122
|
+
};
|
|
123
|
+
const assertDocumentsEqual = (actual, expected) => {
|
|
124
|
+
if ("_id" in expected) assertEqual(expected._id, actual._id, `Document ids are not matching! Expected: ${expected._id}, Actual: ${actual._id}`);
|
|
125
|
+
return assertDeepEqual(withoutIdAndVersion(actual), withoutIdAndVersion(expected));
|
|
126
|
+
};
|
|
127
|
+
const documentExists = (document, options) => (assertOptions) => withCollection(async (collection) => {
|
|
128
|
+
const result = await collection.findOne("withId" in options ? { _id: options.withId } : options.matchingFilter);
|
|
129
|
+
assertIsNotNull(result);
|
|
130
|
+
assertDocumentsEqual(result, document);
|
|
131
|
+
}, {
|
|
132
|
+
...options,
|
|
133
|
+
...assertOptions
|
|
408
134
|
});
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
if (opts?.shouldRetryResult && opts.shouldRetryResult(result)) {
|
|
417
|
-
throw new EmmettError(
|
|
418
|
-
`Retrying because of result: ${JSONSerializer.serialize(result)}`
|
|
419
|
-
);
|
|
420
|
-
}
|
|
421
|
-
return result;
|
|
422
|
-
} catch (error) {
|
|
423
|
-
if (opts?.shouldRetryError && !opts.shouldRetryError(error)) {
|
|
424
|
-
bail(error);
|
|
425
|
-
return void 0;
|
|
426
|
-
}
|
|
427
|
-
throw error;
|
|
428
|
-
}
|
|
429
|
-
},
|
|
430
|
-
opts ?? { retries: 0 }
|
|
431
|
-
);
|
|
432
|
-
};
|
|
433
|
-
var onShutdown = (handler) => {
|
|
434
|
-
const signals = ["SIGTERM", "SIGINT"];
|
|
435
|
-
if (typeof process !== "undefined" && typeof process.on === "function") {
|
|
436
|
-
for (const signal of signals) {
|
|
437
|
-
process.on(signal, handler);
|
|
438
|
-
}
|
|
439
|
-
return () => {
|
|
440
|
-
for (const signal of signals) {
|
|
441
|
-
process.off(signal, handler);
|
|
442
|
-
}
|
|
443
|
-
};
|
|
444
|
-
}
|
|
445
|
-
const deno = globalThis.Deno;
|
|
446
|
-
if (deno && typeof deno.addSignalListener === "function") {
|
|
447
|
-
for (const signal of signals) {
|
|
448
|
-
deno.addSignalListener(signal, handler);
|
|
449
|
-
}
|
|
450
|
-
return () => {
|
|
451
|
-
for (const signal of signals) {
|
|
452
|
-
deno.removeSignalListener(signal, handler);
|
|
453
|
-
}
|
|
454
|
-
};
|
|
455
|
-
}
|
|
456
|
-
return () => {
|
|
457
|
-
};
|
|
458
|
-
};
|
|
459
|
-
var textEncoder = new TextEncoder();
|
|
460
|
-
var getCheckpoint = (message2) => {
|
|
461
|
-
return message2.metadata.checkpoint;
|
|
462
|
-
};
|
|
463
|
-
var wasMessageHandled = (message2, checkpoint) => {
|
|
464
|
-
const messageCheckpoint = getCheckpoint(message2);
|
|
465
|
-
return messageCheckpoint !== null && messageCheckpoint !== void 0 && checkpoint !== null && checkpoint !== void 0 && messageCheckpoint <= checkpoint;
|
|
466
|
-
};
|
|
467
|
-
var MessageProcessorType = {
|
|
468
|
-
PROJECTOR: "projector",
|
|
469
|
-
REACTOR: "reactor"
|
|
470
|
-
};
|
|
471
|
-
var defaultProcessingMessageProcessingScope = (handler, partialContext) => handler(partialContext);
|
|
472
|
-
var bigIntProcessorCheckpoint = (value) => bigInt.toNormalizedString(value);
|
|
473
|
-
var parseBigIntProcessorCheckpoint = (value) => BigInt(value);
|
|
474
|
-
var defaultProcessorVersion = 1;
|
|
475
|
-
var defaultProcessorPartition = defaultTag;
|
|
476
|
-
var getProcessorInstanceId = (processorId) => `${processorId}:${uuid3()}`;
|
|
477
|
-
var getProjectorId = (options) => `emt:processor:projector:${options.projectionName}`;
|
|
478
|
-
var reactor = (options) => {
|
|
479
|
-
const {
|
|
480
|
-
checkpoints,
|
|
481
|
-
processorId,
|
|
482
|
-
processorInstanceId: instanceId = getProcessorInstanceId(processorId),
|
|
483
|
-
type = MessageProcessorType.REACTOR,
|
|
484
|
-
version = defaultProcessorVersion,
|
|
485
|
-
partition = defaultProcessorPartition,
|
|
486
|
-
hooks = {},
|
|
487
|
-
processingScope = defaultProcessingMessageProcessingScope,
|
|
488
|
-
startFrom,
|
|
489
|
-
canHandle,
|
|
490
|
-
stopAfter
|
|
491
|
-
} = options;
|
|
492
|
-
const isCustomBatch = "eachBatch" in options && !!options.eachBatch;
|
|
493
|
-
const eachBatch = isCustomBatch ? options.eachBatch : async (messages, context) => {
|
|
494
|
-
let result = void 0;
|
|
495
|
-
for (let i = 0; i < messages.length; i++) {
|
|
496
|
-
const message2 = messages[i];
|
|
497
|
-
const messageProcessingResult = await options.eachMessage(
|
|
498
|
-
message2,
|
|
499
|
-
context
|
|
500
|
-
);
|
|
501
|
-
if (messageProcessingResult && messageProcessingResult.type === "STOP") {
|
|
502
|
-
result = {
|
|
503
|
-
...messageProcessingResult,
|
|
504
|
-
lastSuccessfulMessage: messageProcessingResult.error ? messages[i - 1] : message2
|
|
505
|
-
};
|
|
506
|
-
break;
|
|
507
|
-
}
|
|
508
|
-
if (stopAfter && stopAfter(message2)) {
|
|
509
|
-
result = {
|
|
510
|
-
type: "STOP",
|
|
511
|
-
reason: "Stop condition reached",
|
|
512
|
-
lastSuccessfulMessage: message2
|
|
513
|
-
};
|
|
514
|
-
break;
|
|
515
|
-
}
|
|
516
|
-
if (messageProcessingResult && messageProcessingResult.type === "SKIP") {
|
|
517
|
-
result = {
|
|
518
|
-
...messageProcessingResult,
|
|
519
|
-
lastSuccessfulMessage: message2
|
|
520
|
-
};
|
|
521
|
-
continue;
|
|
522
|
-
}
|
|
523
|
-
}
|
|
524
|
-
return result;
|
|
525
|
-
};
|
|
526
|
-
let isInitiated = false;
|
|
527
|
-
let isActive = false;
|
|
528
|
-
let lastCheckpoint = null;
|
|
529
|
-
let closeSignal = null;
|
|
530
|
-
const init = async (initOptions) => {
|
|
531
|
-
if (isInitiated) return;
|
|
532
|
-
if (hooks.onInit === void 0) {
|
|
533
|
-
isInitiated = true;
|
|
534
|
-
return;
|
|
535
|
-
}
|
|
536
|
-
return await processingScope(async (context) => {
|
|
537
|
-
await hooks.onInit(context);
|
|
538
|
-
isInitiated = true;
|
|
539
|
-
}, initOptions);
|
|
540
|
-
};
|
|
541
|
-
const close = async (closeOptions) => {
|
|
542
|
-
isActive = false;
|
|
543
|
-
if (closeSignal) {
|
|
544
|
-
closeSignal();
|
|
545
|
-
closeSignal = null;
|
|
546
|
-
}
|
|
547
|
-
if (hooks.onClose) {
|
|
548
|
-
await processingScope(hooks.onClose, closeOptions);
|
|
549
|
-
}
|
|
550
|
-
};
|
|
551
|
-
return {
|
|
552
|
-
// TODO: Consider whether not make it optional or add URN prefix
|
|
553
|
-
id: processorId,
|
|
554
|
-
instanceId,
|
|
555
|
-
type,
|
|
556
|
-
canHandle,
|
|
557
|
-
init,
|
|
558
|
-
start: async (startOptions) => {
|
|
559
|
-
if (isActive) return;
|
|
560
|
-
await init(startOptions);
|
|
561
|
-
isActive = true;
|
|
562
|
-
closeSignal = onShutdown(() => close(startOptions));
|
|
563
|
-
if (lastCheckpoint !== null) {
|
|
564
|
-
return {
|
|
565
|
-
lastCheckpoint
|
|
566
|
-
};
|
|
567
|
-
}
|
|
568
|
-
return await processingScope(async (context) => {
|
|
569
|
-
if (hooks.onStart) {
|
|
570
|
-
await hooks.onStart(context);
|
|
571
|
-
}
|
|
572
|
-
if (startFrom && startFrom !== "CURRENT") return startFrom;
|
|
573
|
-
if (checkpoints) {
|
|
574
|
-
const readResult = await checkpoints?.read(
|
|
575
|
-
{
|
|
576
|
-
processorId,
|
|
577
|
-
partition
|
|
578
|
-
},
|
|
579
|
-
{ ...startOptions, ...context }
|
|
580
|
-
);
|
|
581
|
-
lastCheckpoint = readResult.lastCheckpoint;
|
|
582
|
-
}
|
|
583
|
-
if (lastCheckpoint === null) return "BEGINNING";
|
|
584
|
-
return {
|
|
585
|
-
lastCheckpoint
|
|
586
|
-
};
|
|
587
|
-
}, startOptions);
|
|
588
|
-
},
|
|
589
|
-
close,
|
|
590
|
-
get isActive() {
|
|
591
|
-
return isActive;
|
|
592
|
-
},
|
|
593
|
-
handle: async (messages, partialContext) => {
|
|
594
|
-
if (!isActive) return Promise.resolve();
|
|
595
|
-
return await processingScope(async (context) => {
|
|
596
|
-
const messagesAboveCheckpoint = messages.filter(
|
|
597
|
-
(message2) => !wasMessageHandled(message2, lastCheckpoint)
|
|
598
|
-
);
|
|
599
|
-
const upcastedMessages = messagesAboveCheckpoint.map(
|
|
600
|
-
(message2) => upcastRecordedMessage(
|
|
601
|
-
// TODO: Make it smarter
|
|
602
|
-
message2,
|
|
603
|
-
options.messageOptions?.schema?.versioning
|
|
604
|
-
)
|
|
605
|
-
).filter(
|
|
606
|
-
(upcasted) => !canHandle || canHandle.includes(upcasted.type)
|
|
607
|
-
);
|
|
608
|
-
const stopMessageIndex = isCustomBatch && stopAfter ? upcastedMessages.findIndex(stopAfter) : -1;
|
|
609
|
-
const unhandledMessages = stopMessageIndex !== -1 ? upcastedMessages.slice(0, stopMessageIndex + 1) : upcastedMessages;
|
|
610
|
-
const batchResult = await eachBatch(unhandledMessages, context);
|
|
611
|
-
const messageProcessingResult = batchResult?.type === "STOP" ? batchResult : stopMessageIndex !== -1 ? {
|
|
612
|
-
type: "STOP",
|
|
613
|
-
reason: "Stop condition reached",
|
|
614
|
-
lastSuccessfulMessage: unhandledMessages[stopMessageIndex]
|
|
615
|
-
} : batchResult;
|
|
616
|
-
const isStop = messageProcessingResult && messageProcessingResult.type === "STOP";
|
|
617
|
-
const checkpointMessage = messageProcessingResult?.type === "STOP" ? messageProcessingResult.lastSuccessfulMessage : messagesAboveCheckpoint[messagesAboveCheckpoint.length - 1];
|
|
618
|
-
if (checkpointMessage && checkpoints) {
|
|
619
|
-
const storeCheckpointResult = await checkpoints.store(
|
|
620
|
-
{
|
|
621
|
-
processorId,
|
|
622
|
-
version,
|
|
623
|
-
message: checkpointMessage,
|
|
624
|
-
lastCheckpoint,
|
|
625
|
-
partition
|
|
626
|
-
},
|
|
627
|
-
context
|
|
628
|
-
);
|
|
629
|
-
if (storeCheckpointResult.success) {
|
|
630
|
-
lastCheckpoint = storeCheckpointResult.newCheckpoint;
|
|
631
|
-
}
|
|
632
|
-
}
|
|
633
|
-
if (isStop) {
|
|
634
|
-
isActive = false;
|
|
635
|
-
return messageProcessingResult;
|
|
636
|
-
}
|
|
637
|
-
return void 0;
|
|
638
|
-
}, partialContext);
|
|
639
|
-
}
|
|
640
|
-
};
|
|
641
|
-
};
|
|
642
|
-
var projector = (options) => {
|
|
643
|
-
const {
|
|
644
|
-
projection: projection2,
|
|
645
|
-
processorId = getProjectorId({
|
|
646
|
-
projectionName: projection2.name ?? "unknown"
|
|
647
|
-
}),
|
|
648
|
-
...rest
|
|
649
|
-
} = options;
|
|
650
|
-
return reactor({
|
|
651
|
-
...rest,
|
|
652
|
-
type: MessageProcessorType.PROJECTOR,
|
|
653
|
-
canHandle: projection2.canHandle,
|
|
654
|
-
processorId,
|
|
655
|
-
messageOptions: options.projection.eventsOptions,
|
|
656
|
-
hooks: {
|
|
657
|
-
onInit: options.hooks?.onInit,
|
|
658
|
-
onStart: options.truncateOnStart && options.projection.truncate || options.hooks?.onStart ? async (context) => {
|
|
659
|
-
if (options.truncateOnStart && options.projection.truncate)
|
|
660
|
-
await options.projection.truncate(context);
|
|
661
|
-
if (options.hooks?.onStart) await options.hooks?.onStart(context);
|
|
662
|
-
} : void 0,
|
|
663
|
-
onClose: options.hooks?.onClose
|
|
664
|
-
},
|
|
665
|
-
eachBatch: async (events, context) => projection2.handle(events, context)
|
|
666
|
-
});
|
|
667
|
-
};
|
|
668
|
-
var AssertionError = class extends Error {
|
|
669
|
-
constructor(message2) {
|
|
670
|
-
super(message2);
|
|
671
|
-
}
|
|
672
|
-
};
|
|
673
|
-
var isSubset = (superObj, subObj) => {
|
|
674
|
-
const sup = superObj;
|
|
675
|
-
const sub = subObj;
|
|
676
|
-
assertOk(sup);
|
|
677
|
-
assertOk(sub);
|
|
678
|
-
return Object.keys(sub).every((ele) => {
|
|
679
|
-
if (sub[ele] !== null && typeof sub[ele] == "object") {
|
|
680
|
-
return isSubset(sup[ele], sub[ele]);
|
|
681
|
-
}
|
|
682
|
-
return sub[ele] === sup[ele];
|
|
683
|
-
});
|
|
684
|
-
};
|
|
685
|
-
var assertFails = (message2) => {
|
|
686
|
-
throw new AssertionError(message2 ?? "That should not ever happened, right?");
|
|
687
|
-
};
|
|
688
|
-
var assertDeepEqual = (actual, expected, message2) => {
|
|
689
|
-
if (!deepEquals(actual, expected))
|
|
690
|
-
throw new AssertionError(
|
|
691
|
-
message2 ?? `subObj:
|
|
692
|
-
${JSONSerializer.serialize(expected)}
|
|
693
|
-
is not equal to
|
|
694
|
-
${JSONSerializer.serialize(actual)}`
|
|
695
|
-
);
|
|
696
|
-
};
|
|
697
|
-
function assertTrue(condition, message2) {
|
|
698
|
-
if (condition !== true)
|
|
699
|
-
throw new AssertionError(message2 ?? `Condition is false`);
|
|
700
|
-
}
|
|
701
|
-
function assertOk(obj, message2) {
|
|
702
|
-
if (!obj) throw new AssertionError(message2 ?? `Condition is not truthy`);
|
|
703
|
-
}
|
|
704
|
-
function assertEqual(expected, actual, message2) {
|
|
705
|
-
if (expected !== actual)
|
|
706
|
-
throw new AssertionError(
|
|
707
|
-
`${message2 ?? "Objects are not equal"}:
|
|
708
|
-
Expected: ${JSONSerializer.serialize(expected)}
|
|
709
|
-
Actual: ${JSONSerializer.serialize(actual)}`
|
|
710
|
-
);
|
|
711
|
-
}
|
|
712
|
-
function assertNotEqual(obj, other, message2) {
|
|
713
|
-
if (obj === other)
|
|
714
|
-
throw new AssertionError(
|
|
715
|
-
message2 ?? `Objects are equal: ${JSONSerializer.serialize(obj)}`
|
|
716
|
-
);
|
|
717
|
-
}
|
|
718
|
-
function assertIsNotNull(result) {
|
|
719
|
-
assertNotEqual(result, null);
|
|
720
|
-
assertOk(result);
|
|
721
|
-
}
|
|
722
|
-
function assertIsNull(result) {
|
|
723
|
-
assertEqual(result, null);
|
|
724
|
-
}
|
|
725
|
-
var assertThatArray = (array) => {
|
|
726
|
-
return {
|
|
727
|
-
isEmpty: () => assertEqual(
|
|
728
|
-
array.length,
|
|
729
|
-
0,
|
|
730
|
-
`Array is not empty ${JSONSerializer.serialize(array)}`
|
|
731
|
-
),
|
|
732
|
-
isNotEmpty: () => assertNotEqual(array.length, 0, `Array is empty`),
|
|
733
|
-
hasSize: (length) => assertEqual(array.length, length),
|
|
734
|
-
containsElements: (other) => {
|
|
735
|
-
assertTrue(other.every((ts) => array.some((o) => deepEquals(ts, o))));
|
|
736
|
-
},
|
|
737
|
-
containsElementsMatching: (other) => {
|
|
738
|
-
assertTrue(other.every((ts) => array.some((o) => isSubset(o, ts))));
|
|
739
|
-
},
|
|
740
|
-
containsOnlyElementsMatching: (other) => {
|
|
741
|
-
assertEqual(array.length, other.length, `Arrays lengths don't match`);
|
|
742
|
-
assertTrue(other.every((ts) => array.some((o) => isSubset(o, ts))));
|
|
743
|
-
},
|
|
744
|
-
containsExactlyInAnyOrder: (other) => {
|
|
745
|
-
assertEqual(array.length, other.length);
|
|
746
|
-
assertTrue(array.every((ts) => other.some((o) => deepEquals(ts, o))));
|
|
747
|
-
},
|
|
748
|
-
containsExactlyInAnyOrderElementsOf: (other) => {
|
|
749
|
-
assertEqual(array.length, other.length);
|
|
750
|
-
assertTrue(array.every((ts) => other.some((o) => deepEquals(ts, o))));
|
|
751
|
-
},
|
|
752
|
-
containsExactlyElementsOf: (other) => {
|
|
753
|
-
assertEqual(array.length, other.length);
|
|
754
|
-
for (let i = 0; i < array.length; i++) {
|
|
755
|
-
assertTrue(deepEquals(array[i], other[i]));
|
|
756
|
-
}
|
|
757
|
-
},
|
|
758
|
-
containsExactly: (elem) => {
|
|
759
|
-
assertEqual(array.length, 1);
|
|
760
|
-
assertTrue(deepEquals(array[0], elem));
|
|
761
|
-
},
|
|
762
|
-
contains: (elem) => {
|
|
763
|
-
assertTrue(array.some((a) => deepEquals(a, elem)));
|
|
764
|
-
},
|
|
765
|
-
containsOnlyOnceElementsOf: (other) => {
|
|
766
|
-
assertTrue(
|
|
767
|
-
other.map((o) => array.filter((a) => deepEquals(a, o)).length).filter((a) => a === 1).length === other.length
|
|
768
|
-
);
|
|
769
|
-
},
|
|
770
|
-
containsAnyOf: (other) => {
|
|
771
|
-
assertTrue(array.some((a) => other.some((o) => deepEquals(a, o))));
|
|
772
|
-
},
|
|
773
|
-
allMatch: (matches) => {
|
|
774
|
-
assertTrue(array.every(matches));
|
|
775
|
-
},
|
|
776
|
-
anyMatches: (matches) => {
|
|
777
|
-
assertTrue(array.some(matches));
|
|
778
|
-
},
|
|
779
|
-
allMatchAsync: async (matches) => {
|
|
780
|
-
for (const item of array) {
|
|
781
|
-
assertTrue(await matches(item));
|
|
782
|
-
}
|
|
783
|
-
}
|
|
784
|
-
};
|
|
785
|
-
};
|
|
786
|
-
var downcastRecordedMessage = (recordedMessage, options) => {
|
|
787
|
-
if (!options?.downcast)
|
|
788
|
-
return recordedMessage;
|
|
789
|
-
const downcasted = options.downcast(
|
|
790
|
-
recordedMessage
|
|
791
|
-
);
|
|
792
|
-
return {
|
|
793
|
-
...recordedMessage,
|
|
794
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
795
|
-
data: downcasted.data,
|
|
796
|
-
..."metadata" in recordedMessage || "metadata" in downcasted ? {
|
|
797
|
-
metadata: {
|
|
798
|
-
..."metadata" in recordedMessage ? recordedMessage.metadata : {},
|
|
799
|
-
..."metadata" in downcasted ? downcasted.metadata : {}
|
|
800
|
-
}
|
|
801
|
-
} : {}
|
|
802
|
-
};
|
|
803
|
-
};
|
|
804
|
-
var downcastRecordedMessages = (recordedMessages, options) => {
|
|
805
|
-
if (!options?.downcast)
|
|
806
|
-
return recordedMessages;
|
|
807
|
-
return recordedMessages.map(
|
|
808
|
-
(recordedMessage) => downcastRecordedMessage(recordedMessage, options)
|
|
809
|
-
);
|
|
810
|
-
};
|
|
811
|
-
var upcastRecordedMessage = (recordedMessage, options) => {
|
|
812
|
-
if (!options?.upcast)
|
|
813
|
-
return recordedMessage;
|
|
814
|
-
const upcasted = options.upcast(
|
|
815
|
-
recordedMessage
|
|
816
|
-
);
|
|
817
|
-
return {
|
|
818
|
-
...recordedMessage,
|
|
819
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
820
|
-
data: upcasted.data,
|
|
821
|
-
..."metadata" in recordedMessage || "metadata" in upcasted ? {
|
|
822
|
-
metadata: {
|
|
823
|
-
..."metadata" in recordedMessage ? recordedMessage.metadata : {},
|
|
824
|
-
..."metadata" in upcasted ? upcasted.metadata : {}
|
|
825
|
-
}
|
|
826
|
-
} : {}
|
|
827
|
-
};
|
|
828
|
-
};
|
|
829
|
-
var projection = (definition) => definition;
|
|
830
|
-
var WorkflowHandlerStreamVersionConflictRetryOptions = {
|
|
831
|
-
retries: 3,
|
|
832
|
-
minTimeout: 100,
|
|
833
|
-
factor: 1.5,
|
|
834
|
-
shouldRetryError: isExpectedVersionConflictError
|
|
835
|
-
};
|
|
836
|
-
var fromWorkflowHandlerRetryOptions = (retryOptions) => {
|
|
837
|
-
if (retryOptions === void 0) return NoRetries;
|
|
838
|
-
if ("onVersionConflict" in retryOptions) {
|
|
839
|
-
if (typeof retryOptions.onVersionConflict === "boolean")
|
|
840
|
-
return WorkflowHandlerStreamVersionConflictRetryOptions;
|
|
841
|
-
else if (typeof retryOptions.onVersionConflict === "number")
|
|
842
|
-
return {
|
|
843
|
-
...WorkflowHandlerStreamVersionConflictRetryOptions,
|
|
844
|
-
retries: retryOptions.onVersionConflict
|
|
845
|
-
};
|
|
846
|
-
else return retryOptions.onVersionConflict;
|
|
847
|
-
}
|
|
848
|
-
return retryOptions;
|
|
849
|
-
};
|
|
850
|
-
var emptyHandlerResult = (nextExpectedStreamVersion = 0n) => ({
|
|
851
|
-
newMessages: [],
|
|
852
|
-
createdNewStream: false,
|
|
853
|
-
nextExpectedStreamVersion
|
|
135
|
+
const documentsAreTheSame = (documents, options) => (assertOptions) => withCollection(async (collection) => {
|
|
136
|
+
const result = await collection.find("withId" in options ? { _id: options.withId } : options.matchingFilter);
|
|
137
|
+
assertEqual(documents.length, result.length, "Different Documents Count than expected");
|
|
138
|
+
for (let i = 0; i < documents.length; i++) assertThatArray(result).contains(documents[i]);
|
|
139
|
+
}, {
|
|
140
|
+
...options,
|
|
141
|
+
...assertOptions
|
|
854
142
|
});
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
143
|
+
const documentsMatchingHaveCount = (expectedCount, options) => (assertOptions) => withCollection(async (collection) => {
|
|
144
|
+
assertEqual(expectedCount, (await collection.find("withId" in options ? { _id: options.withId } : options.matchingFilter)).length, "Different Documents Count than expected");
|
|
145
|
+
}, {
|
|
146
|
+
...options,
|
|
147
|
+
...assertOptions
|
|
859
148
|
});
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
...existingMetadata,
|
|
866
|
-
action
|
|
867
|
-
}
|
|
868
|
-
};
|
|
869
|
-
};
|
|
870
|
-
var createWrappedInitialState = (initialState) => {
|
|
871
|
-
return () => ({
|
|
872
|
-
userState: initialState(),
|
|
873
|
-
processedInputIds: /* @__PURE__ */ new Set()
|
|
874
|
-
});
|
|
875
|
-
};
|
|
876
|
-
var createWrappedEvolve = (evolve, workflowName, separateInputInboxFromProcessing) => {
|
|
877
|
-
return (state, event2) => {
|
|
878
|
-
const metadata = event2.metadata;
|
|
879
|
-
let processedInputIds = state.processedInputIds;
|
|
880
|
-
if (metadata?.input === true && typeof metadata?.originalMessageId === "string") {
|
|
881
|
-
processedInputIds = new Set(state.processedInputIds);
|
|
882
|
-
processedInputIds.add(metadata.originalMessageId);
|
|
883
|
-
}
|
|
884
|
-
if (separateInputInboxFromProcessing && metadata?.input === true) {
|
|
885
|
-
return {
|
|
886
|
-
userState: state.userState,
|
|
887
|
-
processedInputIds
|
|
888
|
-
};
|
|
889
|
-
}
|
|
890
|
-
const eventType = event2.type;
|
|
891
|
-
const eventForEvolve = eventType.startsWith(`${workflowName}:`) ? {
|
|
892
|
-
...event2,
|
|
893
|
-
type: eventType.replace(`${workflowName}:`, "")
|
|
894
|
-
} : event2;
|
|
895
|
-
return {
|
|
896
|
-
userState: evolve(state.userState, eventForEvolve),
|
|
897
|
-
processedInputIds
|
|
898
|
-
};
|
|
899
|
-
};
|
|
900
|
-
};
|
|
901
|
-
var workflowStreamName = ({
|
|
902
|
-
workflowName,
|
|
903
|
-
workflowId
|
|
904
|
-
}) => `emt:workflow:${workflowName}:${workflowId}`;
|
|
905
|
-
var WorkflowHandler = (options) => async (store, message2, handleOptions) => asyncRetry(
|
|
906
|
-
async () => {
|
|
907
|
-
const result = await withSession2(store, async ({ eventStore }) => {
|
|
908
|
-
const {
|
|
909
|
-
workflow: { evolve, initialState, decide, name: workflowName },
|
|
910
|
-
getWorkflowId: getWorkflowId2
|
|
911
|
-
} = options;
|
|
912
|
-
const inputMessageId = (
|
|
913
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
914
|
-
("metadata" in message2 && message2.metadata?.messageId ? (
|
|
915
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
916
|
-
message2.metadata.messageId
|
|
917
|
-
) : void 0) ?? uuid6()
|
|
918
|
-
);
|
|
919
|
-
const messageWithMetadata = {
|
|
920
|
-
...message2,
|
|
921
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
922
|
-
metadata: {
|
|
923
|
-
messageId: inputMessageId,
|
|
924
|
-
...message2.metadata
|
|
925
|
-
}
|
|
926
|
-
};
|
|
927
|
-
const workflowId = getWorkflowId2(messageWithMetadata);
|
|
928
|
-
if (!workflowId) {
|
|
929
|
-
return emptyHandlerResult();
|
|
930
|
-
}
|
|
931
|
-
const streamName = options.mapWorkflowId ? options.mapWorkflowId(workflowId) : workflowStreamName({ workflowName, workflowId });
|
|
932
|
-
const messageType = messageWithMetadata.type;
|
|
933
|
-
const hasWorkflowPrefix = messageType.startsWith(`${workflowName}:`);
|
|
934
|
-
if (options.separateInputInboxFromProcessing && !hasWorkflowPrefix) {
|
|
935
|
-
const inputMetadata2 = createInputMetadata(
|
|
936
|
-
inputMessageId,
|
|
937
|
-
"InitiatedBy"
|
|
938
|
-
);
|
|
939
|
-
const inputToStore2 = {
|
|
940
|
-
type: `${workflowName}:${messageWithMetadata.type}`,
|
|
941
|
-
data: messageWithMetadata.data,
|
|
942
|
-
kind: messageWithMetadata.kind,
|
|
943
|
-
metadata: inputMetadata2
|
|
944
|
-
};
|
|
945
|
-
const appendResult2 = await eventStore.appendToStream(
|
|
946
|
-
streamName,
|
|
947
|
-
[inputToStore2],
|
|
948
|
-
{
|
|
949
|
-
...handleOptions,
|
|
950
|
-
expectedStreamVersion: handleOptions?.expectedStreamVersion ?? NO_CONCURRENCY_CHECK
|
|
951
|
-
}
|
|
952
|
-
);
|
|
953
|
-
return {
|
|
954
|
-
...appendResult2,
|
|
955
|
-
newMessages: []
|
|
956
|
-
};
|
|
957
|
-
}
|
|
958
|
-
const wrappedInitialState = createWrappedInitialState(initialState);
|
|
959
|
-
const wrappedEvolve = createWrappedEvolve(
|
|
960
|
-
evolve,
|
|
961
|
-
workflowName,
|
|
962
|
-
options.separateInputInboxFromProcessing ?? false
|
|
963
|
-
);
|
|
964
|
-
const aggregationResult = await eventStore.aggregateStream(streamName, {
|
|
965
|
-
evolve: wrappedEvolve,
|
|
966
|
-
initialState: wrappedInitialState,
|
|
967
|
-
read: {
|
|
968
|
-
...handleOptions,
|
|
969
|
-
// expected stream version is passed to fail fast
|
|
970
|
-
// if stream is in the wrong state
|
|
971
|
-
expectedStreamVersion: handleOptions?.expectedStreamVersion ?? NO_CONCURRENCY_CHECK
|
|
972
|
-
}
|
|
973
|
-
});
|
|
974
|
-
const { currentStreamVersion } = aggregationResult;
|
|
975
|
-
const { userState: state, processedInputIds } = aggregationResult.state;
|
|
976
|
-
if (processedInputIds.has(inputMessageId)) {
|
|
977
|
-
return emptyHandlerResult(currentStreamVersion);
|
|
978
|
-
}
|
|
979
|
-
const messageForDecide = hasWorkflowPrefix ? {
|
|
980
|
-
...messageWithMetadata,
|
|
981
|
-
type: messageType.replace(`${workflowName}:`, "")
|
|
982
|
-
} : messageWithMetadata;
|
|
983
|
-
const result2 = decide(messageForDecide, state);
|
|
984
|
-
const inputMetadata = createInputMetadata(
|
|
985
|
-
inputMessageId,
|
|
986
|
-
aggregationResult.streamExists ? "Received" : "InitiatedBy"
|
|
987
|
-
);
|
|
988
|
-
const inputToStore = {
|
|
989
|
-
type: `${workflowName}:${messageWithMetadata.type}`,
|
|
990
|
-
data: messageWithMetadata.data,
|
|
991
|
-
kind: messageWithMetadata.kind,
|
|
992
|
-
metadata: inputMetadata
|
|
993
|
-
};
|
|
994
|
-
const outputMessages = (Array.isArray(result2) ? result2 : [result2]).filter((msg) => msg !== void 0 && msg !== null);
|
|
995
|
-
const outputCommandTypes = options.outputs?.commands ?? [];
|
|
996
|
-
const taggedOutputMessages = outputMessages.map((msg) => {
|
|
997
|
-
const action = outputCommandTypes.includes(
|
|
998
|
-
msg.type
|
|
999
|
-
) ? "Sent" : "Published";
|
|
1000
|
-
return tagOutputMessage(msg, action);
|
|
1001
|
-
});
|
|
1002
|
-
const messagesToAppend = options.separateInputInboxFromProcessing && hasWorkflowPrefix ? [...taggedOutputMessages] : [inputToStore, ...taggedOutputMessages];
|
|
1003
|
-
if (messagesToAppend.length === 0) {
|
|
1004
|
-
return emptyHandlerResult(currentStreamVersion);
|
|
1005
|
-
}
|
|
1006
|
-
const expectedStreamVersion = handleOptions?.expectedStreamVersion ?? (aggregationResult.streamExists ? currentStreamVersion : STREAM_DOES_NOT_EXIST);
|
|
1007
|
-
const appendResult = await eventStore.appendToStream(
|
|
1008
|
-
streamName,
|
|
1009
|
-
// TODO: Fix this cast
|
|
1010
|
-
messagesToAppend,
|
|
1011
|
-
{
|
|
1012
|
-
...handleOptions,
|
|
1013
|
-
expectedStreamVersion
|
|
1014
|
-
}
|
|
1015
|
-
);
|
|
1016
|
-
return {
|
|
1017
|
-
...appendResult,
|
|
1018
|
-
newMessages: outputMessages
|
|
1019
|
-
};
|
|
1020
|
-
});
|
|
1021
|
-
return result;
|
|
1022
|
-
},
|
|
1023
|
-
fromWorkflowHandlerRetryOptions(
|
|
1024
|
-
handleOptions && "retry" in handleOptions ? handleOptions.retry : options.retry
|
|
1025
|
-
)
|
|
1026
|
-
);
|
|
1027
|
-
var withSession2 = (eventStore, callback) => {
|
|
1028
|
-
const sessionFactory = canCreateEventStoreSession(eventStore) ? eventStore : nulloSessionFactory(eventStore);
|
|
1029
|
-
return sessionFactory.withSession(callback);
|
|
1030
|
-
};
|
|
1031
|
-
var getWorkflowId = (options) => `emt:processor:workflow:${options.workflowName}`;
|
|
1032
|
-
var workflowProcessor = (options) => {
|
|
1033
|
-
const { workflow, ...rest } = options;
|
|
1034
|
-
const inputs = [...options.inputs.commands, ...options.inputs.events];
|
|
1035
|
-
let canHandle = inputs;
|
|
1036
|
-
if (options.separateInputInboxFromProcessing)
|
|
1037
|
-
canHandle = [
|
|
1038
|
-
...canHandle,
|
|
1039
|
-
...options.inputs.commands.map((t) => `${workflow.name}:${t}`),
|
|
1040
|
-
...options.inputs.events.map((t) => `${workflow.name}:${t}`)
|
|
1041
|
-
];
|
|
1042
|
-
if (options.outputHandler)
|
|
1043
|
-
canHandle = [...canHandle, ...options.outputHandler.canHandle];
|
|
1044
|
-
const handle = WorkflowHandler(options);
|
|
1045
|
-
return reactor({
|
|
1046
|
-
...rest,
|
|
1047
|
-
processorId: options.processorId ?? getWorkflowId({ workflowName: workflow.name }),
|
|
1048
|
-
canHandle,
|
|
1049
|
-
type: MessageProcessorType.PROJECTOR,
|
|
1050
|
-
eachMessage: async (message2, context) => {
|
|
1051
|
-
const messageType = message2.type;
|
|
1052
|
-
const metadata = message2.metadata;
|
|
1053
|
-
const isInput = metadata?.input === true;
|
|
1054
|
-
if (isInput || inputs.includes(messageType)) {
|
|
1055
|
-
const result = await handle(
|
|
1056
|
-
context.connection.messageStore,
|
|
1057
|
-
message2,
|
|
1058
|
-
context
|
|
1059
|
-
);
|
|
1060
|
-
if (options.stopAfter && result.newMessages.length > 0) {
|
|
1061
|
-
for (const outputMessage of result.newMessages) {
|
|
1062
|
-
if (options.stopAfter(
|
|
1063
|
-
outputMessage
|
|
1064
|
-
)) {
|
|
1065
|
-
return { type: "STOP", reason: "Stop condition reached" };
|
|
1066
|
-
}
|
|
1067
|
-
}
|
|
1068
|
-
}
|
|
1069
|
-
return;
|
|
1070
|
-
}
|
|
1071
|
-
if (options.outputHandler?.canHandle.includes(messageType) === true) {
|
|
1072
|
-
const recordedMessage = message2;
|
|
1073
|
-
const handledOutputMessages = options.outputHandler.eachBatch ? await options.outputHandler.eachBatch([recordedMessage], context) : await options.outputHandler.eachMessage(recordedMessage, context);
|
|
1074
|
-
if (handledOutputMessages instanceof EmmettError) {
|
|
1075
|
-
return {
|
|
1076
|
-
type: "STOP",
|
|
1077
|
-
reason: "Routing error",
|
|
1078
|
-
error: handledOutputMessages
|
|
1079
|
-
};
|
|
1080
|
-
}
|
|
1081
|
-
const messagesToAppend = Array.isArray(handledOutputMessages) ? handledOutputMessages : handledOutputMessages ? [handledOutputMessages] : [];
|
|
1082
|
-
if (messagesToAppend.length === 0) {
|
|
1083
|
-
return;
|
|
1084
|
-
}
|
|
1085
|
-
const workflowId = options.getWorkflowId(
|
|
1086
|
-
message2
|
|
1087
|
-
);
|
|
1088
|
-
if (!workflowId) return;
|
|
1089
|
-
const streamName = options.mapWorkflowId ? options.mapWorkflowId(workflowId) : workflowStreamName({
|
|
1090
|
-
workflowName: workflow.name,
|
|
1091
|
-
workflowId
|
|
1092
|
-
});
|
|
1093
|
-
await context.connection.messageStore.appendToStream(
|
|
1094
|
-
streamName,
|
|
1095
|
-
messagesToAppend
|
|
1096
|
-
);
|
|
1097
|
-
return;
|
|
1098
|
-
}
|
|
1099
|
-
return;
|
|
1100
|
-
}
|
|
1101
|
-
});
|
|
1102
|
-
};
|
|
1103
|
-
|
|
1104
|
-
// src/eventStore/projections/pongo/pongoProjections.ts
|
|
1105
|
-
import {
|
|
1106
|
-
pongoClient
|
|
1107
|
-
} from "@event-driven-io/pongo";
|
|
1108
|
-
var pongoProjection = ({
|
|
1109
|
-
name,
|
|
1110
|
-
kind,
|
|
1111
|
-
version,
|
|
1112
|
-
truncate,
|
|
1113
|
-
handle,
|
|
1114
|
-
canHandle,
|
|
1115
|
-
eventsOptions
|
|
1116
|
-
}) => sqliteProjection({
|
|
1117
|
-
name,
|
|
1118
|
-
version,
|
|
1119
|
-
kind: kind ?? "emt:projections:postgresql:pongo:generic",
|
|
1120
|
-
canHandle,
|
|
1121
|
-
eventsOptions,
|
|
1122
|
-
handle: async (events, context) => {
|
|
1123
|
-
const { connection } = context;
|
|
1124
|
-
const driver = await pongoDriverRegistry.tryResolve(
|
|
1125
|
-
context.driverType
|
|
1126
|
-
);
|
|
1127
|
-
const pongo = pongoClient({
|
|
1128
|
-
driver,
|
|
1129
|
-
connectionOptions: { connection }
|
|
1130
|
-
});
|
|
1131
|
-
try {
|
|
1132
|
-
await handle(events, {
|
|
1133
|
-
...context,
|
|
1134
|
-
pongo
|
|
1135
|
-
});
|
|
1136
|
-
} finally {
|
|
1137
|
-
await pongo.close();
|
|
1138
|
-
}
|
|
1139
|
-
},
|
|
1140
|
-
truncate: truncate ? async (context) => {
|
|
1141
|
-
const { connection } = context;
|
|
1142
|
-
const driver = await pongoDriverRegistry.tryResolve(
|
|
1143
|
-
context.driverType
|
|
1144
|
-
);
|
|
1145
|
-
const pongo = pongoClient({
|
|
1146
|
-
driver,
|
|
1147
|
-
connectionOptions: { connection }
|
|
1148
|
-
});
|
|
1149
|
-
try {
|
|
1150
|
-
await truncate({
|
|
1151
|
-
...context,
|
|
1152
|
-
pongo
|
|
1153
|
-
});
|
|
1154
|
-
} finally {
|
|
1155
|
-
await pongo.close();
|
|
1156
|
-
}
|
|
1157
|
-
} : void 0
|
|
149
|
+
const documentMatchingExists = (options) => (assertOptions) => withCollection(async (collection) => {
|
|
150
|
+
assertThatArray(await collection.find("withId" in options ? { _id: options.withId } : options.matchingFilter)).isNotEmpty();
|
|
151
|
+
}, {
|
|
152
|
+
...options,
|
|
153
|
+
...assertOptions
|
|
1158
154
|
});
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
version: options.version,
|
|
1165
|
-
kind: options.kind ?? "emt:projections:postgresql:pongo:multi_stream",
|
|
1166
|
-
eventsOptions: options.eventsOptions,
|
|
1167
|
-
handle: async (events, { pongo }) => {
|
|
1168
|
-
const collection = pongo.db().collection(
|
|
1169
|
-
collectionNameWithVersion,
|
|
1170
|
-
options.collectionOptions
|
|
1171
|
-
);
|
|
1172
|
-
const eventsByDocumentId = events.map((event) => {
|
|
1173
|
-
const documentId = getDocumentId(event);
|
|
1174
|
-
return {
|
|
1175
|
-
documentId,
|
|
1176
|
-
event
|
|
1177
|
-
};
|
|
1178
|
-
}).reduce((acc, { documentId, event }) => {
|
|
1179
|
-
if (!acc.has(documentId)) {
|
|
1180
|
-
acc.set(documentId, []);
|
|
1181
|
-
}
|
|
1182
|
-
acc.get(documentId).push(event);
|
|
1183
|
-
return acc;
|
|
1184
|
-
}, /* @__PURE__ */ new Map());
|
|
1185
|
-
await collection.handle(
|
|
1186
|
-
[...eventsByDocumentId.keys()],
|
|
1187
|
-
(document, id) => {
|
|
1188
|
-
const events2 = eventsByDocumentId.get(id);
|
|
1189
|
-
return reduceAsync(
|
|
1190
|
-
events2,
|
|
1191
|
-
async (acc, event) => await options.evolve(acc, event),
|
|
1192
|
-
document ?? ("initialState" in options ? options.initialState() : null)
|
|
1193
|
-
);
|
|
1194
|
-
}
|
|
1195
|
-
);
|
|
1196
|
-
},
|
|
1197
|
-
canHandle,
|
|
1198
|
-
truncate: async (context) => {
|
|
1199
|
-
const { connection } = context;
|
|
1200
|
-
const driver = await pongoDriverRegistry.tryResolve(
|
|
1201
|
-
context.driverType
|
|
1202
|
-
);
|
|
1203
|
-
const pongo = pongoClient({
|
|
1204
|
-
driver,
|
|
1205
|
-
connectionOptions: { connection }
|
|
1206
|
-
});
|
|
1207
|
-
try {
|
|
1208
|
-
await pongo.db().collection(
|
|
1209
|
-
collectionNameWithVersion,
|
|
1210
|
-
options.collectionOptions
|
|
1211
|
-
).deleteMany();
|
|
1212
|
-
} finally {
|
|
1213
|
-
await pongo.close();
|
|
1214
|
-
}
|
|
1215
|
-
},
|
|
1216
|
-
init: async (context) => {
|
|
1217
|
-
const { connection } = context;
|
|
1218
|
-
const driver = await pongoDriverRegistry.tryResolve(
|
|
1219
|
-
context.driverType
|
|
1220
|
-
);
|
|
1221
|
-
const pongo = pongoClient({
|
|
1222
|
-
connectionOptions: { connection },
|
|
1223
|
-
driver
|
|
1224
|
-
});
|
|
1225
|
-
try {
|
|
1226
|
-
await pongo.db().collection(
|
|
1227
|
-
collectionNameWithVersion,
|
|
1228
|
-
options.collectionOptions
|
|
1229
|
-
).schema.migrate();
|
|
1230
|
-
} finally {
|
|
1231
|
-
await pongo.close();
|
|
1232
|
-
}
|
|
1233
|
-
}
|
|
1234
|
-
});
|
|
1235
|
-
};
|
|
1236
|
-
var pongoSingleStreamProjection = (options) => {
|
|
1237
|
-
return pongoMultiStreamProjection({
|
|
1238
|
-
...options,
|
|
1239
|
-
kind: "emt:projections:postgresql:pongo:single_stream",
|
|
1240
|
-
getDocumentId: options.getDocumentId ?? ((event) => event.metadata.streamName)
|
|
1241
|
-
});
|
|
1242
|
-
};
|
|
1243
|
-
|
|
1244
|
-
// src/eventStore/projections/pongo/pongoProjectionSpec.ts
|
|
1245
|
-
import {
|
|
1246
|
-
pongoClient as pongoClient2
|
|
1247
|
-
} from "@event-driven-io/pongo";
|
|
1248
|
-
var withCollection = async (handle, options) => {
|
|
1249
|
-
const { connection, inDatabase, inCollection } = options;
|
|
1250
|
-
const driver = await pongoDriverRegistry.tryResolve(
|
|
1251
|
-
connection.driverType
|
|
1252
|
-
);
|
|
1253
|
-
const pongo = pongoClient2({
|
|
1254
|
-
connectionOptions: { connection },
|
|
1255
|
-
driver
|
|
1256
|
-
});
|
|
1257
|
-
try {
|
|
1258
|
-
const collection = pongo.db(inDatabase).collection(inCollection);
|
|
1259
|
-
return handle(collection);
|
|
1260
|
-
} finally {
|
|
1261
|
-
await pongo.close();
|
|
1262
|
-
}
|
|
1263
|
-
};
|
|
1264
|
-
var withoutIdAndVersion = (doc) => {
|
|
1265
|
-
const { _id, _version, ...without } = doc;
|
|
1266
|
-
return without;
|
|
1267
|
-
};
|
|
1268
|
-
var assertDocumentsEqual = (actual, expected) => {
|
|
1269
|
-
if ("_id" in expected)
|
|
1270
|
-
assertEqual(
|
|
1271
|
-
expected._id,
|
|
1272
|
-
actual._id,
|
|
1273
|
-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
1274
|
-
`Document ids are not matching! Expected: ${expected._id}, Actual: ${actual._id}`
|
|
1275
|
-
);
|
|
1276
|
-
return assertDeepEqual(
|
|
1277
|
-
withoutIdAndVersion(actual),
|
|
1278
|
-
withoutIdAndVersion(expected)
|
|
1279
|
-
);
|
|
1280
|
-
};
|
|
1281
|
-
var documentExists = (document, options) => (assertOptions) => withCollection(
|
|
1282
|
-
async (collection) => {
|
|
1283
|
-
const result = await collection.findOne(
|
|
1284
|
-
"withId" in options ? { _id: options.withId } : options.matchingFilter
|
|
1285
|
-
);
|
|
1286
|
-
assertIsNotNull(result);
|
|
1287
|
-
assertDocumentsEqual(result, document);
|
|
1288
|
-
},
|
|
1289
|
-
{ ...options, ...assertOptions }
|
|
1290
|
-
);
|
|
1291
|
-
var documentsAreTheSame = (documents, options) => (assertOptions) => withCollection(
|
|
1292
|
-
async (collection) => {
|
|
1293
|
-
const result = await collection.find(
|
|
1294
|
-
"withId" in options ? { _id: options.withId } : options.matchingFilter
|
|
1295
|
-
);
|
|
1296
|
-
assertEqual(
|
|
1297
|
-
documents.length,
|
|
1298
|
-
result.length,
|
|
1299
|
-
"Different Documents Count than expected"
|
|
1300
|
-
);
|
|
1301
|
-
for (let i = 0; i < documents.length; i++) {
|
|
1302
|
-
assertThatArray(result).contains(documents[i]);
|
|
1303
|
-
}
|
|
1304
|
-
},
|
|
1305
|
-
{ ...options, ...assertOptions }
|
|
1306
|
-
);
|
|
1307
|
-
var documentsMatchingHaveCount = (expectedCount, options) => (assertOptions) => withCollection(
|
|
1308
|
-
async (collection) => {
|
|
1309
|
-
const result = await collection.find(
|
|
1310
|
-
"withId" in options ? { _id: options.withId } : options.matchingFilter
|
|
1311
|
-
);
|
|
1312
|
-
assertEqual(
|
|
1313
|
-
expectedCount,
|
|
1314
|
-
result.length,
|
|
1315
|
-
"Different Documents Count than expected"
|
|
1316
|
-
);
|
|
1317
|
-
},
|
|
1318
|
-
{ ...options, ...assertOptions }
|
|
1319
|
-
);
|
|
1320
|
-
var documentMatchingExists = (options) => (assertOptions) => withCollection(
|
|
1321
|
-
async (collection) => {
|
|
1322
|
-
const result = await collection.find(
|
|
1323
|
-
"withId" in options ? { _id: options.withId } : options.matchingFilter
|
|
1324
|
-
);
|
|
1325
|
-
assertThatArray(result).isNotEmpty();
|
|
1326
|
-
},
|
|
1327
|
-
{ ...options, ...assertOptions }
|
|
1328
|
-
);
|
|
1329
|
-
var documentDoesNotExist = (options) => (assertOptions) => withCollection(
|
|
1330
|
-
async (collection) => {
|
|
1331
|
-
const result = await collection.findOne(
|
|
1332
|
-
"withId" in options ? { _id: options.withId } : options.matchingFilter
|
|
1333
|
-
);
|
|
1334
|
-
assertIsNull(result);
|
|
1335
|
-
},
|
|
1336
|
-
{ ...options, ...assertOptions }
|
|
1337
|
-
);
|
|
1338
|
-
var expectPongoDocuments = {
|
|
1339
|
-
fromCollection: (collectionName) => {
|
|
1340
|
-
return {
|
|
1341
|
-
withId: (id) => {
|
|
1342
|
-
return {
|
|
1343
|
-
toBeEqual: (document) => documentExists(document, {
|
|
1344
|
-
withId: id,
|
|
1345
|
-
inCollection: collectionName
|
|
1346
|
-
}),
|
|
1347
|
-
toExist: () => documentMatchingExists({
|
|
1348
|
-
withId: id,
|
|
1349
|
-
inCollection: collectionName
|
|
1350
|
-
}),
|
|
1351
|
-
notToExist: () => documentDoesNotExist({
|
|
1352
|
-
withId: id,
|
|
1353
|
-
inCollection: collectionName
|
|
1354
|
-
})
|
|
1355
|
-
};
|
|
1356
|
-
},
|
|
1357
|
-
matching: (filter) => {
|
|
1358
|
-
return {
|
|
1359
|
-
toBeTheSame: (documents) => documentsAreTheSame(documents, {
|
|
1360
|
-
matchingFilter: filter,
|
|
1361
|
-
inCollection: collectionName
|
|
1362
|
-
}),
|
|
1363
|
-
toHaveCount: (expectedCount) => documentsMatchingHaveCount(expectedCount, {
|
|
1364
|
-
matchingFilter: filter,
|
|
1365
|
-
inCollection: collectionName
|
|
1366
|
-
}),
|
|
1367
|
-
toExist: () => documentMatchingExists({
|
|
1368
|
-
matchingFilter: filter,
|
|
1369
|
-
inCollection: collectionName
|
|
1370
|
-
}),
|
|
1371
|
-
notToExist: () => documentDoesNotExist({
|
|
1372
|
-
matchingFilter: filter,
|
|
1373
|
-
inCollection: collectionName
|
|
1374
|
-
})
|
|
1375
|
-
};
|
|
1376
|
-
}
|
|
1377
|
-
};
|
|
1378
|
-
}
|
|
1379
|
-
};
|
|
1380
|
-
|
|
1381
|
-
// src/eventStore/projections/sqliteProjection.ts
|
|
1382
|
-
var handleProjections = async (options) => {
|
|
1383
|
-
const {
|
|
1384
|
-
projections: allProjections,
|
|
1385
|
-
events,
|
|
1386
|
-
connection,
|
|
1387
|
-
execute,
|
|
1388
|
-
driverType
|
|
1389
|
-
} = options;
|
|
1390
|
-
const eventTypes = events.map((e) => e.type);
|
|
1391
|
-
for (const projection2 of allProjections) {
|
|
1392
|
-
if (!projection2.canHandle.some((type) => eventTypes.includes(type))) {
|
|
1393
|
-
continue;
|
|
1394
|
-
}
|
|
1395
|
-
await projection2.handle(events, {
|
|
1396
|
-
connection,
|
|
1397
|
-
execute,
|
|
1398
|
-
driverType
|
|
1399
|
-
});
|
|
1400
|
-
}
|
|
1401
|
-
};
|
|
1402
|
-
var sqliteProjection = (definition) => projection(definition);
|
|
1403
|
-
var sqliteRawBatchSQLProjection = (options) => sqliteProjection({
|
|
1404
|
-
name: options.name,
|
|
1405
|
-
kind: options.kind ?? "emt:projections:sqlite:raw_sql:batch",
|
|
1406
|
-
version: options.version,
|
|
1407
|
-
canHandle: options.canHandle,
|
|
1408
|
-
eventsOptions: options.eventsOptions,
|
|
1409
|
-
handle: async (events, context) => {
|
|
1410
|
-
const sqls = await options.evolve(events, context);
|
|
1411
|
-
await context.execute.batchCommand(sqls);
|
|
1412
|
-
},
|
|
1413
|
-
init: async (initOptions) => {
|
|
1414
|
-
const initSQL = options.init ? await options.init(initOptions) : void 0;
|
|
1415
|
-
if (initSQL) {
|
|
1416
|
-
if (Array.isArray(initSQL)) {
|
|
1417
|
-
await initOptions.context.execute.batchCommand(initSQL);
|
|
1418
|
-
} else {
|
|
1419
|
-
await initOptions.context.execute.command(initSQL);
|
|
1420
|
-
}
|
|
1421
|
-
}
|
|
1422
|
-
}
|
|
155
|
+
const documentDoesNotExist = (options) => (assertOptions) => withCollection(async (collection) => {
|
|
156
|
+
assertIsNull(await collection.findOne("withId" in options ? { _id: options.withId } : options.matchingFilter));
|
|
157
|
+
}, {
|
|
158
|
+
...options,
|
|
159
|
+
...assertOptions
|
|
1423
160
|
});
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
161
|
+
const expectPongoDocuments = { fromCollection: (collectionName) => {
|
|
162
|
+
return {
|
|
163
|
+
withId: (id) => {
|
|
164
|
+
return {
|
|
165
|
+
toBeEqual: (document) => documentExists(document, {
|
|
166
|
+
withId: id,
|
|
167
|
+
inCollection: collectionName
|
|
168
|
+
}),
|
|
169
|
+
toExist: () => documentMatchingExists({
|
|
170
|
+
withId: id,
|
|
171
|
+
inCollection: collectionName
|
|
172
|
+
}),
|
|
173
|
+
notToExist: () => documentDoesNotExist({
|
|
174
|
+
withId: id,
|
|
175
|
+
inCollection: collectionName
|
|
176
|
+
})
|
|
177
|
+
};
|
|
178
|
+
},
|
|
179
|
+
matching: (filter) => {
|
|
180
|
+
return {
|
|
181
|
+
toBeTheSame: (documents) => documentsAreTheSame(documents, {
|
|
182
|
+
matchingFilter: filter,
|
|
183
|
+
inCollection: collectionName
|
|
184
|
+
}),
|
|
185
|
+
toHaveCount: (expectedCount) => documentsMatchingHaveCount(expectedCount, {
|
|
186
|
+
matchingFilter: filter,
|
|
187
|
+
inCollection: collectionName
|
|
188
|
+
}),
|
|
189
|
+
toExist: () => documentMatchingExists({
|
|
190
|
+
matchingFilter: filter,
|
|
191
|
+
inCollection: collectionName
|
|
192
|
+
}),
|
|
193
|
+
notToExist: () => documentDoesNotExist({
|
|
194
|
+
matchingFilter: filter,
|
|
195
|
+
inCollection: collectionName
|
|
196
|
+
})
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
} };
|
|
1443
201
|
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
registrationType: "async",
|
|
1491
|
-
status: "active",
|
|
1492
|
-
context: {
|
|
1493
|
-
execute: connection.execute,
|
|
1494
|
-
connection,
|
|
1495
|
-
driverType
|
|
1496
|
-
},
|
|
1497
|
-
version: projection2.version ?? 1
|
|
1498
|
-
});
|
|
1499
|
-
wasInitialized = true;
|
|
1500
|
-
}
|
|
1501
|
-
await connection.withTransaction(
|
|
1502
|
-
() => handleProjections({
|
|
1503
|
-
events: allEvents,
|
|
1504
|
-
projections: [projection2],
|
|
1505
|
-
execute: connection.execute,
|
|
1506
|
-
connection,
|
|
1507
|
-
driverType
|
|
1508
|
-
})
|
|
1509
|
-
);
|
|
1510
|
-
};
|
|
1511
|
-
return {
|
|
1512
|
-
then: (assert, message) => pool.withConnection(async (connection) => {
|
|
1513
|
-
await run(connection);
|
|
1514
|
-
const succeeded = await assert({
|
|
1515
|
-
connection
|
|
1516
|
-
});
|
|
1517
|
-
if (succeeded !== void 0 && succeeded === false)
|
|
1518
|
-
assertFails(
|
|
1519
|
-
message ?? "Projection specification didn't match the criteria"
|
|
1520
|
-
);
|
|
1521
|
-
}),
|
|
1522
|
-
thenThrows: (...args) => pool.withConnection(async (connection) => {
|
|
1523
|
-
try {
|
|
1524
|
-
await run(connection);
|
|
1525
|
-
throw new AssertionError(
|
|
1526
|
-
"Handler did not fail as expected"
|
|
1527
|
-
);
|
|
1528
|
-
} catch (error) {
|
|
1529
|
-
if (error instanceof AssertionError) throw error;
|
|
1530
|
-
if (args.length === 0) return;
|
|
1531
|
-
if (!isErrorConstructor(args[0])) {
|
|
1532
|
-
assertTrue(
|
|
1533
|
-
args[0](error),
|
|
1534
|
-
`Error didn't match the error condition: ${error?.toString()}`
|
|
1535
|
-
);
|
|
1536
|
-
return;
|
|
1537
|
-
}
|
|
1538
|
-
assertTrue(
|
|
1539
|
-
error instanceof args[0],
|
|
1540
|
-
`Caught error is not an instance of the expected type: ${error?.toString()}`
|
|
1541
|
-
);
|
|
1542
|
-
if (args[1]) {
|
|
1543
|
-
assertTrue(
|
|
1544
|
-
args[1](error),
|
|
1545
|
-
`Error didn't match the error condition: ${error?.toString()}`
|
|
1546
|
-
);
|
|
1547
|
-
}
|
|
1548
|
-
}
|
|
1549
|
-
})
|
|
1550
|
-
};
|
|
1551
|
-
}
|
|
1552
|
-
};
|
|
1553
|
-
};
|
|
1554
|
-
}
|
|
1555
|
-
}
|
|
1556
|
-
};
|
|
1557
|
-
var eventInStream = (streamName, event) => {
|
|
1558
|
-
return {
|
|
1559
|
-
...event,
|
|
1560
|
-
metadata: {
|
|
1561
|
-
...event.metadata ?? {},
|
|
1562
|
-
streamName: event.metadata?.streamName ?? streamName
|
|
1563
|
-
}
|
|
1564
|
-
};
|
|
1565
|
-
};
|
|
1566
|
-
var eventsInStream = (streamName, events) => {
|
|
1567
|
-
return events.map((e) => eventInStream(streamName, e));
|
|
1568
|
-
};
|
|
1569
|
-
var newEventsInStream = eventsInStream;
|
|
1570
|
-
var assertSQLQueryResultMatches = (sql, rows) => async ({
|
|
1571
|
-
connection
|
|
1572
|
-
}) => {
|
|
1573
|
-
const result = await connection.execute.query(sql);
|
|
1574
|
-
assertThatArray(rows).containsExactlyInAnyOrder(result.rows);
|
|
1575
|
-
};
|
|
1576
|
-
var expectSQL = {
|
|
1577
|
-
query: (sql) => ({
|
|
1578
|
-
resultRows: {
|
|
1579
|
-
toBeTheSame: (rows) => assertSQLQueryResultMatches(sql, rows)
|
|
1580
|
-
}
|
|
1581
|
-
})
|
|
202
|
+
//#endregion
|
|
203
|
+
//#region src/eventStore/projections/sqliteProjection.ts
|
|
204
|
+
const handleProjections = async (options) => {
|
|
205
|
+
const { projections: allProjections, events, connection, execute, driverType } = options;
|
|
206
|
+
const eventTypes = events.map((e) => e.type);
|
|
207
|
+
for (const projection of allProjections) {
|
|
208
|
+
if (!projection.canHandle.some((type) => eventTypes.includes(type))) continue;
|
|
209
|
+
await projection.handle(events, {
|
|
210
|
+
connection,
|
|
211
|
+
execute,
|
|
212
|
+
driverType
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
const sqliteProjection = (definition) => projection(definition);
|
|
217
|
+
const sqliteRawBatchSQLProjection = (options) => sqliteProjection({
|
|
218
|
+
name: options.name,
|
|
219
|
+
kind: options.kind ?? "emt:projections:sqlite:raw_sql:batch",
|
|
220
|
+
version: options.version,
|
|
221
|
+
canHandle: options.canHandle,
|
|
222
|
+
eventsOptions: options.eventsOptions,
|
|
223
|
+
handle: async (events, context) => {
|
|
224
|
+
const sqls = await options.evolve(events, context);
|
|
225
|
+
await context.execute.batchCommand(sqls);
|
|
226
|
+
},
|
|
227
|
+
init: async (initOptions) => {
|
|
228
|
+
const initSQL = options.init ? await options.init(initOptions) : void 0;
|
|
229
|
+
if (initSQL) if (Array.isArray(initSQL)) await initOptions.context.execute.batchCommand(initSQL);
|
|
230
|
+
else await initOptions.context.execute.command(initSQL);
|
|
231
|
+
}
|
|
232
|
+
});
|
|
233
|
+
const sqliteRawSQLProjection = (options) => {
|
|
234
|
+
const { evolve, kind, ...rest } = options;
|
|
235
|
+
return sqliteRawBatchSQLProjection({
|
|
236
|
+
kind: kind ?? "emt:projections:sqlite:raw:_sql:single",
|
|
237
|
+
...rest,
|
|
238
|
+
evolve: async (events, context) => {
|
|
239
|
+
const sqls = [];
|
|
240
|
+
for (const event of events) {
|
|
241
|
+
const pendingSqls = await evolve(event, context);
|
|
242
|
+
if (Array.isArray(pendingSqls)) sqls.push(...pendingSqls);
|
|
243
|
+
else sqls.push(pendingSqls);
|
|
244
|
+
}
|
|
245
|
+
return sqls;
|
|
246
|
+
}
|
|
247
|
+
});
|
|
1582
248
|
};
|
|
1583
249
|
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
250
|
+
//#endregion
|
|
251
|
+
//#region src/eventStore/projections/sqliteProjectionSpec.ts
|
|
252
|
+
const SQLiteProjectionSpec = { for: (options) => {
|
|
253
|
+
{
|
|
254
|
+
const driverType = options.driver.driverType;
|
|
255
|
+
const pool = options.pool ?? dumbo({
|
|
256
|
+
serialization: options.serialization,
|
|
257
|
+
transactionOptions: {
|
|
258
|
+
allowNestedTransactions: true,
|
|
259
|
+
mode: "session_based"
|
|
260
|
+
},
|
|
261
|
+
...options.driver.mapToDumboOptions(options)
|
|
262
|
+
});
|
|
263
|
+
const projection = options.projection;
|
|
264
|
+
let wasInitialized = false;
|
|
265
|
+
return (givenEvents) => {
|
|
266
|
+
return { when: (events, options) => {
|
|
267
|
+
const allEvents = [];
|
|
268
|
+
const run = async (connection) => {
|
|
269
|
+
let globalPosition = 0n;
|
|
270
|
+
const numberOfTimes = options?.numberOfTimes ?? 1;
|
|
271
|
+
for (const event of [...givenEvents, ...Array.from({ length: numberOfTimes }).flatMap(() => events)]) {
|
|
272
|
+
const metadata = {
|
|
273
|
+
checkpoint: bigIntProcessorCheckpoint(++globalPosition),
|
|
274
|
+
globalPosition,
|
|
275
|
+
streamPosition: globalPosition,
|
|
276
|
+
streamName: `test-${v4()}`,
|
|
277
|
+
messageId: v4()
|
|
278
|
+
};
|
|
279
|
+
allEvents.push({
|
|
280
|
+
...event,
|
|
281
|
+
kind: "Event",
|
|
282
|
+
metadata: {
|
|
283
|
+
...metadata,
|
|
284
|
+
..."metadata" in event ? event.metadata ?? {} : {}
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
if (!wasInitialized && projection.init) {
|
|
289
|
+
await projection.init({
|
|
290
|
+
registrationType: "async",
|
|
291
|
+
status: "active",
|
|
292
|
+
context: {
|
|
293
|
+
execute: connection.execute,
|
|
294
|
+
connection,
|
|
295
|
+
driverType
|
|
296
|
+
},
|
|
297
|
+
version: projection.version ?? 1
|
|
298
|
+
});
|
|
299
|
+
wasInitialized = true;
|
|
300
|
+
}
|
|
301
|
+
await connection.withTransaction(() => handleProjections({
|
|
302
|
+
events: allEvents,
|
|
303
|
+
projections: [projection],
|
|
304
|
+
execute: connection.execute,
|
|
305
|
+
connection,
|
|
306
|
+
driverType
|
|
307
|
+
}));
|
|
308
|
+
};
|
|
309
|
+
return {
|
|
310
|
+
then: (assert, message) => pool.withConnection(async (connection) => {
|
|
311
|
+
await run(connection);
|
|
312
|
+
const succeeded = await assert({ connection });
|
|
313
|
+
if (succeeded !== void 0 && succeeded === false) assertFails(message ?? "Projection specification didn't match the criteria");
|
|
314
|
+
}),
|
|
315
|
+
thenThrows: (...args) => pool.withConnection(async (connection) => {
|
|
316
|
+
try {
|
|
317
|
+
await run(connection);
|
|
318
|
+
throw new AssertionError("Handler did not fail as expected");
|
|
319
|
+
} catch (error) {
|
|
320
|
+
if (error instanceof AssertionError) throw error;
|
|
321
|
+
if (args.length === 0) return;
|
|
322
|
+
if (!isErrorConstructor(args[0])) {
|
|
323
|
+
assertTrue(args[0](error), `Error didn't match the error condition: ${error?.toString()}`);
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
assertTrue(error instanceof args[0], `Caught error is not an instance of the expected type: ${error?.toString()}`);
|
|
327
|
+
if (args[1]) assertTrue(args[1](error), `Error didn't match the error condition: ${error?.toString()}`);
|
|
328
|
+
}
|
|
329
|
+
})
|
|
330
|
+
};
|
|
331
|
+
} };
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
} };
|
|
335
|
+
const eventInStream = (streamName, event) => {
|
|
336
|
+
return {
|
|
337
|
+
...event,
|
|
338
|
+
metadata: {
|
|
339
|
+
...event.metadata ?? {},
|
|
340
|
+
streamName: event.metadata?.streamName ?? streamName
|
|
341
|
+
}
|
|
342
|
+
};
|
|
343
|
+
};
|
|
344
|
+
const eventsInStream = (streamName, events) => {
|
|
345
|
+
return events.map((e) => eventInStream(streamName, e));
|
|
346
|
+
};
|
|
347
|
+
const newEventsInStream = eventsInStream;
|
|
348
|
+
const assertSQLQueryResultMatches = (sql, rows) => async ({ connection }) => {
|
|
349
|
+
const result = await connection.execute.query(sql);
|
|
350
|
+
assertThatArray(rows).containsExactlyInAnyOrder(result.rows);
|
|
351
|
+
};
|
|
352
|
+
const expectSQL = { query: (sql) => ({ resultRows: { toBeTheSame: (rows) => assertSQLQueryResultMatches(sql, rows) } }) };
|
|
1593
353
|
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
};
|
|
1622
|
-
var processorsTable = {
|
|
1623
|
-
name: `${emmettPrefix2}_processors`
|
|
1624
|
-
};
|
|
1625
|
-
var projectionsTable = {
|
|
1626
|
-
name: `${emmettPrefix2}_projections`
|
|
1627
|
-
};
|
|
354
|
+
//#endregion
|
|
355
|
+
//#region src/eventStore/schema/typing.ts
|
|
356
|
+
const emmettPrefix = "emt";
|
|
357
|
+
const globalTag = "global";
|
|
358
|
+
const defaultTag = `${"emt"}:default`;
|
|
359
|
+
const unknownTag = `${"emt"}:unknown`;
|
|
360
|
+
const globalNames = { module: `${"emt"}:module:${globalTag}` };
|
|
361
|
+
const columns = {
|
|
362
|
+
partition: { name: "partition" },
|
|
363
|
+
isArchived: { name: "is_archived" }
|
|
364
|
+
};
|
|
365
|
+
const streamsTable = {
|
|
366
|
+
name: `${"emt"}_streams`,
|
|
367
|
+
columns: {
|
|
368
|
+
partition: columns.partition,
|
|
369
|
+
isArchived: columns.isArchived
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
const messagesTable = {
|
|
373
|
+
name: `${"emt"}_messages`,
|
|
374
|
+
columns: {
|
|
375
|
+
partition: columns.partition,
|
|
376
|
+
isArchived: columns.isArchived
|
|
377
|
+
}
|
|
378
|
+
};
|
|
379
|
+
const processorsTable = { name: `${"emt"}_processors` };
|
|
380
|
+
const projectionsTable = { name: `${"emt"}_projections` };
|
|
1628
381
|
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
errorType: BatchCommandNoChangesError.ErrorType
|
|
1673
|
-
})) {
|
|
1674
|
-
return { success: false };
|
|
1675
|
-
}
|
|
1676
|
-
throw err;
|
|
1677
|
-
}
|
|
1678
|
-
};
|
|
1679
|
-
var toExpectedVersion = (expected) => {
|
|
1680
|
-
if (expected === void 0) return null;
|
|
1681
|
-
if (expected === NO_CONCURRENCY_CHECK) return null;
|
|
1682
|
-
if (expected == STREAM_DOES_NOT_EXIST) return null;
|
|
1683
|
-
if (expected == STREAM_EXISTS) return null;
|
|
1684
|
-
return expected;
|
|
1685
|
-
};
|
|
1686
|
-
var appendToStreamRaw = async (execute, streamId, streamType, messages, options) => {
|
|
1687
|
-
let expectedStreamVersion = options?.expectedStreamVersion ?? null;
|
|
1688
|
-
const currentStreamVersion = await getLastStreamPosition(
|
|
1689
|
-
execute,
|
|
1690
|
-
streamId,
|
|
1691
|
-
expectedStreamVersion
|
|
1692
|
-
);
|
|
1693
|
-
expectedStreamVersion ??= currentStreamVersion ?? 0n;
|
|
1694
|
-
if (expectedStreamVersion !== currentStreamVersion) {
|
|
1695
|
-
throw new ExpectedVersionConflictError(
|
|
1696
|
-
currentStreamVersion,
|
|
1697
|
-
expectedStreamVersion
|
|
1698
|
-
);
|
|
1699
|
-
}
|
|
1700
|
-
const streamSQL = expectedStreamVersion === 0n ? SQL`INSERT INTO ${identifier(streamsTable.name)}
|
|
382
|
+
//#endregion
|
|
383
|
+
//#region src/eventStore/schema/appendToStream.ts
|
|
384
|
+
const { identifier: identifier$7, merge } = SQL;
|
|
385
|
+
const appendToStream = async (connection, streamName, streamType, messages, options) => {
|
|
386
|
+
if (messages.length === 0) return { success: false };
|
|
387
|
+
const expectedStreamVersion = toExpectedVersion(options?.expectedStreamVersion);
|
|
388
|
+
const messagesToAppend = messages.map((m, i) => ({
|
|
389
|
+
...m,
|
|
390
|
+
kind: m.kind ?? "Event",
|
|
391
|
+
metadata: {
|
|
392
|
+
streamName,
|
|
393
|
+
messageId: v4(),
|
|
394
|
+
streamPosition: BigInt(i + 1),
|
|
395
|
+
..."metadata" in m ? m.metadata ?? {} : {}
|
|
396
|
+
}
|
|
397
|
+
}));
|
|
398
|
+
try {
|
|
399
|
+
return await connection.withTransaction(async (transaction) => {
|
|
400
|
+
const result = await appendToStreamRaw(transaction.execute, streamName, streamType, downcastRecordedMessages(messagesToAppend, options?.schema?.versioning), { expectedStreamVersion });
|
|
401
|
+
if (options?.onBeforeCommit) await options.onBeforeCommit(messagesToAppend, { connection });
|
|
402
|
+
return {
|
|
403
|
+
success: true,
|
|
404
|
+
result
|
|
405
|
+
};
|
|
406
|
+
});
|
|
407
|
+
} catch (err) {
|
|
408
|
+
if (isExpectedVersionConflictError(err) || DumboError.isInstanceOf(err, { errorType: UniqueConstraintError.ErrorType }) || DumboError.isInstanceOf(err, { errorType: BatchCommandNoChangesError.ErrorType })) return { success: false };
|
|
409
|
+
throw err;
|
|
410
|
+
}
|
|
411
|
+
};
|
|
412
|
+
const toExpectedVersion = (expected) => {
|
|
413
|
+
if (expected === void 0) return null;
|
|
414
|
+
if (expected === NO_CONCURRENCY_CHECK) return null;
|
|
415
|
+
if (expected == STREAM_DOES_NOT_EXIST) return null;
|
|
416
|
+
if (expected == STREAM_EXISTS) return null;
|
|
417
|
+
return expected;
|
|
418
|
+
};
|
|
419
|
+
const appendToStreamRaw = async (execute, streamId, streamType, messages, options) => {
|
|
420
|
+
let expectedStreamVersion = options?.expectedStreamVersion ?? null;
|
|
421
|
+
const currentStreamVersion = await getLastStreamPosition(execute, streamId, expectedStreamVersion);
|
|
422
|
+
expectedStreamVersion ??= currentStreamVersion ?? 0n;
|
|
423
|
+
if (expectedStreamVersion !== currentStreamVersion) throw new ExpectedVersionConflictError(currentStreamVersion, expectedStreamVersion);
|
|
424
|
+
const streamSQL = expectedStreamVersion === 0n ? SQL`INSERT INTO ${identifier$7(streamsTable.name)}
|
|
1701
425
|
(stream_id, stream_position, partition, stream_type, stream_metadata, is_archived)
|
|
1702
426
|
VALUES (
|
|
1703
427
|
${streamId},
|
|
@@ -1708,7 +432,7 @@ var appendToStreamRaw = async (execute, streamId, streamType, messages, options)
|
|
|
1708
432
|
false
|
|
1709
433
|
)
|
|
1710
434
|
RETURNING stream_position;
|
|
1711
|
-
` : SQL`UPDATE ${identifier(streamsTable.name)}
|
|
435
|
+
` : SQL`UPDATE ${identifier$7(streamsTable.name)}
|
|
1712
436
|
SET stream_position = stream_position + ${messages.length}
|
|
1713
437
|
WHERE stream_id = ${streamId}
|
|
1714
438
|
AND stream_position = ${expectedStreamVersion}
|
|
@@ -1716,48 +440,31 @@ var appendToStreamRaw = async (execute, streamId, streamType, messages, options)
|
|
|
1716
440
|
AND is_archived = false
|
|
1717
441
|
RETURNING stream_position;
|
|
1718
442
|
`;
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
throw new ExpectedVersionConflictError(0n, expectedStreamVersion ?? 0n);
|
|
1731
|
-
if (!globalPosition) throw new Error("Could not find global position");
|
|
1732
|
-
return {
|
|
1733
|
-
success: true,
|
|
1734
|
-
nextStreamPosition: BigInt(streamPosition),
|
|
1735
|
-
lastGlobalPosition: BigInt(globalPosition)
|
|
1736
|
-
};
|
|
443
|
+
const insertSQL = buildMessageInsertQuery(messages, expectedStreamVersion, streamId, options?.partition?.toString() ?? defaultTag);
|
|
444
|
+
const [streamResult, messagesResult] = await execute.batchCommand([streamSQL, insertSQL], { assertChanges: true });
|
|
445
|
+
const streamPosition = streamResult?.rows[0]?.stream_position;
|
|
446
|
+
const globalPosition = messagesResult?.rows.at(-1)?.global_position;
|
|
447
|
+
if (!streamPosition) throw new ExpectedVersionConflictError(0n, expectedStreamVersion ?? 0n);
|
|
448
|
+
if (!globalPosition) throw new Error("Could not find global position");
|
|
449
|
+
return {
|
|
450
|
+
success: true,
|
|
451
|
+
nextStreamPosition: BigInt(streamPosition),
|
|
452
|
+
lastGlobalPosition: BigInt(globalPosition)
|
|
453
|
+
};
|
|
1737
454
|
};
|
|
1738
455
|
async function getLastStreamPosition(execute, streamId, expectedStreamVersion) {
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
);
|
|
1744
|
-
if (result?.stream_position == null) {
|
|
1745
|
-
expectedStreamVersion = 0n;
|
|
1746
|
-
} else {
|
|
1747
|
-
expectedStreamVersion = BigInt(result.stream_position);
|
|
1748
|
-
}
|
|
1749
|
-
return expectedStreamVersion;
|
|
456
|
+
const result = await singleOrNull(execute.query(SQL`SELECT CAST(stream_position AS VARCHAR) AS stream_position FROM ${identifier$7(streamsTable.name)} WHERE stream_id = ${streamId}`));
|
|
457
|
+
if (result?.stream_position == null) expectedStreamVersion = 0n;
|
|
458
|
+
else expectedStreamVersion = BigInt(result.stream_position);
|
|
459
|
+
return expectedStreamVersion;
|
|
1750
460
|
}
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
});
|
|
1759
|
-
return SQL`
|
|
1760
|
-
INSERT INTO ${identifier(messagesTable.name)} (
|
|
461
|
+
const buildMessageInsertQuery = (messages, expectedStreamVersion, streamId, partition) => {
|
|
462
|
+
const values = messages.map((message) => {
|
|
463
|
+
if (message.metadata?.streamPosition == null || typeof message.metadata.streamPosition !== "bigint") throw new Error("Stream position is required");
|
|
464
|
+
return SQL`(${streamId},${BigInt(message.metadata.streamPosition) + BigInt(expectedStreamVersion) ?? 0n},${partition ?? defaultTag},${message.kind === "Event" ? "E" : "C"},${message.data},${message.metadata},${expectedStreamVersion ?? 0n},${message.type},${message.metadata.messageId},${false})`;
|
|
465
|
+
});
|
|
466
|
+
return SQL`
|
|
467
|
+
INSERT INTO ${identifier$7(messagesTable.name)} (
|
|
1761
468
|
stream_id,
|
|
1762
469
|
stream_position,
|
|
1763
470
|
partition,
|
|
@@ -1775,10 +482,10 @@ var buildMessageInsertQuery = (messages, expectedStreamVersion, streamId, partit
|
|
|
1775
482
|
`;
|
|
1776
483
|
};
|
|
1777
484
|
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
485
|
+
//#endregion
|
|
486
|
+
//#region src/eventStore/schema/migrations/0_41_0/0_41_0.snapshot.ts
|
|
487
|
+
const schema_0_41_0 = [
|
|
488
|
+
SQL`CREATE TABLE IF NOT EXISTS emt_streams(
|
|
1782
489
|
stream_id TEXT NOT NULL,
|
|
1783
490
|
stream_position BIGINT NOT NULL DEFAULT 0,
|
|
1784
491
|
partition TEXT NOT NULL DEFAULT 'global',
|
|
@@ -1788,7 +495,7 @@ var schema_0_41_0 = [
|
|
|
1788
495
|
PRIMARY KEY (stream_id, partition, is_archived),
|
|
1789
496
|
UNIQUE (stream_id, partition, is_archived)
|
|
1790
497
|
)`,
|
|
1791
|
-
|
|
498
|
+
SQL`CREATE TABLE IF NOT EXISTS emt_messages(
|
|
1792
499
|
stream_id TEXT NOT NULL,
|
|
1793
500
|
stream_position BIGINT NOT NULL,
|
|
1794
501
|
partition TEXT NOT NULL DEFAULT 'global',
|
|
@@ -1803,7 +510,7 @@ var schema_0_41_0 = [
|
|
|
1803
510
|
created DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
1804
511
|
UNIQUE (stream_id, stream_position, partition, is_archived)
|
|
1805
512
|
)`,
|
|
1806
|
-
|
|
513
|
+
SQL`CREATE TABLE IF NOT EXISTS emt_subscriptions(
|
|
1807
514
|
subscription_id TEXT NOT NULL,
|
|
1808
515
|
version INTEGER NOT NULL DEFAULT 1,
|
|
1809
516
|
partition TEXT NOT NULL DEFAULT 'global',
|
|
@@ -1812,30 +519,30 @@ var schema_0_41_0 = [
|
|
|
1812
519
|
)`
|
|
1813
520
|
];
|
|
1814
521
|
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
522
|
+
//#endregion
|
|
523
|
+
//#region src/eventStore/schema/migrations/0_42_0/0_42_0.migration.ts
|
|
524
|
+
const { identifier: identifier$6, plain: plain$1 } = SQL;
|
|
525
|
+
const migration_0_42_0_SQLs = [
|
|
526
|
+
SQL`CREATE TABLE IF NOT EXISTS ${identifier$6(processorsTable.name)}(
|
|
1820
527
|
processor_id TEXT NOT NULL,
|
|
1821
528
|
version INTEGER NOT NULL DEFAULT 1,
|
|
1822
|
-
partition TEXT NOT NULL DEFAULT '${plain(globalTag)}',
|
|
529
|
+
partition TEXT NOT NULL DEFAULT '${plain$1(globalTag)}',
|
|
1823
530
|
status TEXT NOT NULL DEFAULT 'stopped',
|
|
1824
531
|
last_processed_checkpoint TEXT NOT NULL,
|
|
1825
532
|
processor_instance_id TEXT DEFAULT 'emt:unknown',
|
|
1826
533
|
PRIMARY KEY (processor_id, partition, version)
|
|
1827
534
|
)`,
|
|
1828
|
-
|
|
535
|
+
SQL`CREATE TABLE IF NOT EXISTS ${identifier$6(projectionsTable.name)}(
|
|
1829
536
|
name TEXT NOT NULL,
|
|
1830
537
|
version INTEGER NOT NULL DEFAULT 1,
|
|
1831
|
-
partition TEXT NOT NULL DEFAULT '${plain(globalTag)}',
|
|
538
|
+
partition TEXT NOT NULL DEFAULT '${plain$1(globalTag)}',
|
|
1832
539
|
type CHAR(1) NOT NULL,
|
|
1833
540
|
kind TEXT NOT NULL,
|
|
1834
541
|
status TEXT NOT NULL,
|
|
1835
542
|
definition JSONB NOT NULL DEFAULT '{}',
|
|
1836
543
|
PRIMARY KEY (name, partition, version)
|
|
1837
544
|
)`,
|
|
1838
|
-
|
|
545
|
+
SQL`INSERT INTO ${identifier$6(processorsTable.name)}
|
|
1839
546
|
(processor_id, version, partition, status, last_processed_checkpoint, processor_instance_id)
|
|
1840
547
|
SELECT
|
|
1841
548
|
subscription_id,
|
|
@@ -1845,24 +552,17 @@ var migration_0_42_0_SQLs = [
|
|
|
1845
552
|
printf('%019d', last_processed_position),
|
|
1846
553
|
'emt:unknown'
|
|
1847
554
|
FROM emt_subscriptions`,
|
|
1848
|
-
|
|
555
|
+
SQL`DROP TABLE emt_subscriptions`
|
|
1849
556
|
];
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
SQL3`SELECT name FROM sqlite_master WHERE type='table' AND name='emt_subscriptions'`
|
|
1854
|
-
)
|
|
1855
|
-
);
|
|
1856
|
-
if (!tableExists) {
|
|
1857
|
-
return;
|
|
1858
|
-
}
|
|
1859
|
-
await execute.batchCommand(migration_0_42_0_SQLs);
|
|
557
|
+
const migration_0_42_0_FromSubscriptionsToProcessors = async (execute) => {
|
|
558
|
+
if (!await singleOrNull(execute.query(SQL`SELECT name FROM sqlite_master WHERE type='table' AND name='emt_subscriptions'`))) return;
|
|
559
|
+
await execute.batchCommand(migration_0_42_0_SQLs);
|
|
1860
560
|
};
|
|
1861
561
|
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
562
|
+
//#endregion
|
|
563
|
+
//#region src/eventStore/schema/migrations/0_42_0/0_42_0.snapshot.ts
|
|
564
|
+
const schema_0_42_0 = [
|
|
565
|
+
SQL`CREATE TABLE IF NOT EXISTS emt_streams(
|
|
1866
566
|
stream_id TEXT NOT NULL,
|
|
1867
567
|
stream_position BIGINT NOT NULL DEFAULT 0,
|
|
1868
568
|
partition TEXT NOT NULL DEFAULT 'global',
|
|
@@ -1872,7 +572,7 @@ var schema_0_42_0 = [
|
|
|
1872
572
|
PRIMARY KEY (stream_id, partition, is_archived),
|
|
1873
573
|
UNIQUE (stream_id, partition, is_archived)
|
|
1874
574
|
)`,
|
|
1875
|
-
|
|
575
|
+
SQL`CREATE TABLE IF NOT EXISTS emt_messages(
|
|
1876
576
|
stream_id TEXT NOT NULL,
|
|
1877
577
|
stream_position BIGINT NOT NULL,
|
|
1878
578
|
partition TEXT NOT NULL DEFAULT 'global',
|
|
@@ -1887,7 +587,7 @@ var schema_0_42_0 = [
|
|
|
1887
587
|
created DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
1888
588
|
UNIQUE (stream_id, stream_position, partition, is_archived)
|
|
1889
589
|
)`,
|
|
1890
|
-
|
|
590
|
+
SQL`CREATE TABLE IF NOT EXISTS emt_processors(
|
|
1891
591
|
processor_id TEXT NOT NULL,
|
|
1892
592
|
version INTEGER NOT NULL DEFAULT 1,
|
|
1893
593
|
partition TEXT NOT NULL DEFAULT 'global',
|
|
@@ -1896,7 +596,7 @@ var schema_0_42_0 = [
|
|
|
1896
596
|
processor_instance_id TEXT DEFAULT 'emt:unknown',
|
|
1897
597
|
PRIMARY KEY (processor_id, partition, version)
|
|
1898
598
|
)`,
|
|
1899
|
-
|
|
599
|
+
SQL`CREATE TABLE IF NOT EXISTS emt_projections(
|
|
1900
600
|
name TEXT NOT NULL,
|
|
1901
601
|
version INTEGER NOT NULL DEFAULT 1,
|
|
1902
602
|
partition TEXT NOT NULL DEFAULT 'global',
|
|
@@ -1908,801 +608,657 @@ var schema_0_42_0 = [
|
|
|
1908
608
|
)`
|
|
1909
609
|
];
|
|
1910
610
|
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
execute.query(
|
|
1917
|
-
SQL5`
|
|
611
|
+
//#endregion
|
|
612
|
+
//#region src/eventStore/schema/readLastMessageGlobalPosition.ts
|
|
613
|
+
const { identifier: identifier$5 } = SQL;
|
|
614
|
+
const readLastMessageGlobalPosition = async (execute, options) => {
|
|
615
|
+
const result = await singleOrNull(execute.query(SQL`
|
|
1918
616
|
SELECT global_position
|
|
1919
|
-
FROM ${
|
|
1920
|
-
WHERE partition = ${options?.partition ??
|
|
1921
|
-
ORDER BY global_position
|
|
1922
|
-
LIMIT 1`
|
|
1923
|
-
|
|
1924
|
-
);
|
|
1925
|
-
return {
|
|
1926
|
-
currentGlobalPosition: result !== null ? BigInt(result.global_position) : null
|
|
1927
|
-
};
|
|
617
|
+
FROM ${identifier$5(messagesTable.name)}
|
|
618
|
+
WHERE partition = ${options?.partition ?? defaultTag} AND is_archived = FALSE
|
|
619
|
+
ORDER BY global_position DESC
|
|
620
|
+
LIMIT 1`));
|
|
621
|
+
return { currentGlobalPosition: result !== null ? BigInt(result.global_position) : null };
|
|
1928
622
|
};
|
|
1929
623
|
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
FROM ${identifier4(messagesTable.name)}
|
|
1945
|
-
WHERE partition = ${options?.partition ?? defaultTag2} AND is_archived = FALSE ${fromCondition} ${toCondition}
|
|
624
|
+
//#endregion
|
|
625
|
+
//#region src/eventStore/schema/readMessagesBatch.ts
|
|
626
|
+
const { identifier: identifier$4 } = SQL;
|
|
627
|
+
const readMessagesBatch = async (execute, options) => {
|
|
628
|
+
const { serializer } = options;
|
|
629
|
+
const from = "from" in options ? options.from : void 0;
|
|
630
|
+
const after = "after" in options ? options.after : void 0;
|
|
631
|
+
const batchSize = "batchSize" in options ? options.batchSize : options.to - options.from;
|
|
632
|
+
const fromCondition = from !== void 0 ? SQL`AND global_position >= ${from}` : after !== void 0 ? SQL`AND global_position > ${after}` : SQL.EMPTY;
|
|
633
|
+
const toCondition = "to" in options ? SQL`AND global_position <= ${options.to}` : SQL.EMPTY;
|
|
634
|
+
const limitCondition = "batchSize" in options ? SQL`LIMIT ${options.batchSize}` : SQL.EMPTY;
|
|
635
|
+
const messages = await mapRows(execute.query(SQL`SELECT stream_id, stream_position, global_position, message_data, message_metadata, message_schema_version, message_type, message_id
|
|
636
|
+
FROM ${identifier$4(messagesTable.name)}
|
|
637
|
+
WHERE partition = ${options?.partition ?? defaultTag} AND is_archived = FALSE ${fromCondition} ${toCondition}
|
|
1946
638
|
ORDER BY global_position
|
|
1947
|
-
${limitCondition}`
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
messages: [],
|
|
1977
|
-
areMessagesLeft: false
|
|
1978
|
-
};
|
|
639
|
+
${limitCondition}`), (row) => {
|
|
640
|
+
const rawEvent = {
|
|
641
|
+
type: row.message_type,
|
|
642
|
+
data: serializer.deserialize(row.message_data),
|
|
643
|
+
metadata: serializer.deserialize(row.message_metadata)
|
|
644
|
+
};
|
|
645
|
+
const metadata = {
|
|
646
|
+
..."metadata" in rawEvent ? rawEvent.metadata ?? {} : {},
|
|
647
|
+
messageId: row.message_id,
|
|
648
|
+
streamName: row.stream_id,
|
|
649
|
+
streamPosition: BigInt(row.stream_position),
|
|
650
|
+
globalPosition: BigInt(row.global_position),
|
|
651
|
+
checkpoint: bigIntProcessorCheckpoint(BigInt(row.global_position))
|
|
652
|
+
};
|
|
653
|
+
return {
|
|
654
|
+
...rawEvent,
|
|
655
|
+
kind: "Event",
|
|
656
|
+
metadata
|
|
657
|
+
};
|
|
658
|
+
});
|
|
659
|
+
return messages.length > 0 ? {
|
|
660
|
+
currentGlobalPosition: messages[messages.length - 1].metadata.globalPosition,
|
|
661
|
+
messages,
|
|
662
|
+
areMessagesLeft: messages.length === batchSize
|
|
663
|
+
} : {
|
|
664
|
+
currentGlobalPosition: "from" in options ? options.from : "after" in options ? options.after : 0n,
|
|
665
|
+
messages: [],
|
|
666
|
+
areMessagesLeft: false
|
|
667
|
+
};
|
|
1979
668
|
};
|
|
1980
669
|
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
LIMIT 1`
|
|
1991
|
-
)
|
|
1992
|
-
);
|
|
1993
|
-
return {
|
|
1994
|
-
lastProcessedCheckpoint: result !== null ? result.last_processed_checkpoint : null
|
|
1995
|
-
};
|
|
670
|
+
//#endregion
|
|
671
|
+
//#region src/eventStore/schema/readProcessorCheckpoint.ts
|
|
672
|
+
const { identifier: identifier$3 } = SQL;
|
|
673
|
+
const readProcessorCheckpoint = async (execute, options) => {
|
|
674
|
+
const result = await singleOrNull(execute.query(SQL`SELECT last_processed_checkpoint
|
|
675
|
+
FROM ${identifier$3(processorsTable.name)}
|
|
676
|
+
WHERE partition = ${options?.partition ?? defaultTag} AND processor_id = ${options.processorId}
|
|
677
|
+
LIMIT 1`));
|
|
678
|
+
return { lastProcessedCheckpoint: result !== null ? result.last_processed_checkpoint : null };
|
|
1996
679
|
};
|
|
1997
680
|
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
stop: async () => {
|
|
2062
|
-
if (!isRunning) return;
|
|
2063
|
-
isRunning = false;
|
|
2064
|
-
await start;
|
|
2065
|
-
}
|
|
2066
|
-
};
|
|
2067
|
-
};
|
|
2068
|
-
var zipSQLiteEventStoreMessageBatchPullerStartFrom = (options) => {
|
|
2069
|
-
if (options.length === 0 || options.some((o) => o === void 0 || o === "BEGINNING"))
|
|
2070
|
-
return "BEGINNING";
|
|
2071
|
-
if (options.every((o) => o === "END")) return "END";
|
|
2072
|
-
return options.filter((o) => o !== void 0 && o !== "BEGINNING" && o !== "END").sort((a, b) => a > b ? 1 : -1)[0];
|
|
681
|
+
//#endregion
|
|
682
|
+
//#region src/eventStore/consumers/messageBatchProcessing/index.ts
|
|
683
|
+
const sqliteEventStoreMessageBatchPuller = ({ executor, batchSize, eachBatch, pullingFrequencyInMs, stopWhen, signal, serialization }) => {
|
|
684
|
+
let isRunning = false;
|
|
685
|
+
let start;
|
|
686
|
+
const serializer = JSONSerializer.from({ serialization });
|
|
687
|
+
const pullMessages = async (options) => {
|
|
688
|
+
let after;
|
|
689
|
+
try {
|
|
690
|
+
after = options.startFrom === "BEGINNING" ? 0n : options.startFrom === "END" ? (await readLastMessageGlobalPosition(executor)).currentGlobalPosition ?? 0n : parseBigIntProcessorCheckpoint(options.startFrom.lastCheckpoint);
|
|
691
|
+
} catch (error) {
|
|
692
|
+
options.started?.reject(error);
|
|
693
|
+
throw error;
|
|
694
|
+
}
|
|
695
|
+
options.started?.resolve();
|
|
696
|
+
const readMessagesOptions = {
|
|
697
|
+
after,
|
|
698
|
+
batchSize,
|
|
699
|
+
serializer
|
|
700
|
+
};
|
|
701
|
+
let waitTime = 100;
|
|
702
|
+
while (isRunning && !signal?.aborted) {
|
|
703
|
+
const { messages, currentGlobalPosition, areMessagesLeft } = await readMessagesBatch(executor, readMessagesOptions);
|
|
704
|
+
if (messages.length > 0) {
|
|
705
|
+
const result = await eachBatch(messages);
|
|
706
|
+
if (result && result.type === "STOP") {
|
|
707
|
+
isRunning = false;
|
|
708
|
+
break;
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
readMessagesOptions.after = currentGlobalPosition;
|
|
712
|
+
await new Promise((resolve) => setTimeout(resolve, waitTime));
|
|
713
|
+
if (stopWhen?.noMessagesLeft === true && !areMessagesLeft) {
|
|
714
|
+
isRunning = false;
|
|
715
|
+
break;
|
|
716
|
+
}
|
|
717
|
+
if (!areMessagesLeft) waitTime = Math.min(waitTime * 2, 1e3);
|
|
718
|
+
else waitTime = pullingFrequencyInMs;
|
|
719
|
+
}
|
|
720
|
+
};
|
|
721
|
+
return {
|
|
722
|
+
get isRunning() {
|
|
723
|
+
return isRunning;
|
|
724
|
+
},
|
|
725
|
+
start: (options) => {
|
|
726
|
+
if (isRunning) return start;
|
|
727
|
+
isRunning = true;
|
|
728
|
+
start = (async () => {
|
|
729
|
+
return pullMessages(options);
|
|
730
|
+
})();
|
|
731
|
+
return start;
|
|
732
|
+
},
|
|
733
|
+
stop: async () => {
|
|
734
|
+
if (!isRunning) return;
|
|
735
|
+
isRunning = false;
|
|
736
|
+
await start;
|
|
737
|
+
}
|
|
738
|
+
};
|
|
739
|
+
};
|
|
740
|
+
const zipSQLiteEventStoreMessageBatchPullerStartFrom = (options) => {
|
|
741
|
+
if (options.length === 0 || options.some((o) => o === void 0 || o === "BEGINNING")) return "BEGINNING";
|
|
742
|
+
if (options.every((o) => o === "END")) return "END";
|
|
743
|
+
return options.filter((o) => o !== void 0 && o !== "BEGINNING" && o !== "END").sort((a, b) => a > b ? 1 : -1)[0];
|
|
2073
744
|
};
|
|
2074
745
|
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
}
|
|
746
|
+
//#endregion
|
|
747
|
+
//#region src/eventStore/consumers/sqliteCheckpointer.ts
|
|
748
|
+
const sqliteCheckpointer = () => ({
|
|
749
|
+
read: async (options, context) => {
|
|
750
|
+
return { lastCheckpoint: (await readProcessorCheckpoint(context.execute, options))?.lastProcessedCheckpoint };
|
|
751
|
+
},
|
|
752
|
+
store: async (options, context) => {
|
|
753
|
+
const newCheckpoint = getCheckpoint(options.message);
|
|
754
|
+
const result = await storeProcessorCheckpoint(context.execute, {
|
|
755
|
+
lastProcessedCheckpoint: options.lastCheckpoint,
|
|
756
|
+
newCheckpoint,
|
|
757
|
+
processorId: options.processorId,
|
|
758
|
+
partition: options.partition,
|
|
759
|
+
version: options.version
|
|
760
|
+
});
|
|
761
|
+
return result.success ? {
|
|
762
|
+
success: true,
|
|
763
|
+
newCheckpoint: result.newCheckpoint
|
|
764
|
+
} : result;
|
|
765
|
+
}
|
|
2096
766
|
});
|
|
2097
767
|
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
|
|
2123
|
-
|
|
2124
|
-
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
2146
|
-
|
|
2147
|
-
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2160
|
-
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
2178
|
-
|
|
2179
|
-
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
onInit: options.projection.init !== void 0 || options.hooks?.onInit ? async (context) => {
|
|
2190
|
-
if (options.projection.init)
|
|
2191
|
-
await options.projection.init({
|
|
2192
|
-
version: options.projection.version ?? version,
|
|
2193
|
-
status: "active",
|
|
2194
|
-
registrationType: "async",
|
|
2195
|
-
context: {
|
|
2196
|
-
...context,
|
|
2197
|
-
migrationOptions: options.migrationOptions
|
|
2198
|
-
}
|
|
2199
|
-
});
|
|
2200
|
-
if (options.hooks?.onInit)
|
|
2201
|
-
await options.hooks.onInit({
|
|
2202
|
-
...context,
|
|
2203
|
-
migrationOptions: options.migrationOptions
|
|
2204
|
-
});
|
|
2205
|
-
} : options.hooks?.onInit,
|
|
2206
|
-
onClose: options.hooks?.onClose
|
|
2207
|
-
};
|
|
2208
|
-
const processor = projector({
|
|
2209
|
-
...options,
|
|
2210
|
-
processorId,
|
|
2211
|
-
processorInstanceId,
|
|
2212
|
-
version,
|
|
2213
|
-
partition,
|
|
2214
|
-
hooks,
|
|
2215
|
-
processingScope: sqliteProcessingScope(),
|
|
2216
|
-
checkpoints: sqliteCheckpointer()
|
|
2217
|
-
});
|
|
2218
|
-
return processor;
|
|
768
|
+
//#endregion
|
|
769
|
+
//#region src/eventStore/consumers/sqliteProcessor.ts
|
|
770
|
+
const sqliteProcessingScope = () => {
|
|
771
|
+
const processingScope = async (handler, partialContext) => {
|
|
772
|
+
const connection = partialContext?.connection;
|
|
773
|
+
if (!connection) throw new EmmettError("Connection is required in context or options");
|
|
774
|
+
return connection.withTransaction(async (transaction) => {
|
|
775
|
+
return handler({
|
|
776
|
+
...partialContext,
|
|
777
|
+
connection,
|
|
778
|
+
execute: transaction.execute
|
|
779
|
+
});
|
|
780
|
+
});
|
|
781
|
+
};
|
|
782
|
+
return processingScope;
|
|
783
|
+
};
|
|
784
|
+
const sqliteWorkflowProcessingScope = (messageStore) => {
|
|
785
|
+
const processingScope = async (handler, partialContext) => {
|
|
786
|
+
const connection = partialContext?.connection;
|
|
787
|
+
if (!connection) throw new EmmettError("Connection is required in context or options");
|
|
788
|
+
return connection.withTransaction(async (transaction) => {
|
|
789
|
+
return handler({
|
|
790
|
+
...partialContext,
|
|
791
|
+
connection: Object.assign(connection, { messageStore }),
|
|
792
|
+
execute: transaction.execute
|
|
793
|
+
});
|
|
794
|
+
});
|
|
795
|
+
};
|
|
796
|
+
return processingScope;
|
|
797
|
+
};
|
|
798
|
+
const sqliteWorkflowProcessor = (options) => {
|
|
799
|
+
const { processorId = options.processorId ?? getWorkflowId({ workflowName: options.workflow.name ?? "unknown" }), processorInstanceId = getProcessorInstanceId(processorId), version = defaultProcessorVersion, partition = defaultProcessorPartition } = options;
|
|
800
|
+
const hooks = {
|
|
801
|
+
...options.hooks ?? {},
|
|
802
|
+
onClose: options.hooks?.onClose
|
|
803
|
+
};
|
|
804
|
+
return workflowProcessor({
|
|
805
|
+
...options,
|
|
806
|
+
processorId,
|
|
807
|
+
processorInstanceId,
|
|
808
|
+
version,
|
|
809
|
+
partition,
|
|
810
|
+
hooks,
|
|
811
|
+
processingScope: sqliteWorkflowProcessingScope(options.messageStore),
|
|
812
|
+
checkpoints: sqliteCheckpointer()
|
|
813
|
+
});
|
|
814
|
+
};
|
|
815
|
+
const sqliteReactor = (options) => {
|
|
816
|
+
const { processorId = options.processorId, processorInstanceId = getProcessorInstanceId(processorId), version = defaultProcessorVersion, partition = defaultProcessorPartition, hooks } = options;
|
|
817
|
+
return reactor({
|
|
818
|
+
...options,
|
|
819
|
+
processorId,
|
|
820
|
+
processorInstanceId,
|
|
821
|
+
version,
|
|
822
|
+
partition,
|
|
823
|
+
hooks,
|
|
824
|
+
processingScope: sqliteProcessingScope(),
|
|
825
|
+
checkpoints: sqliteCheckpointer()
|
|
826
|
+
});
|
|
827
|
+
};
|
|
828
|
+
const sqliteProjector = (options) => {
|
|
829
|
+
const { processorId = getProjectorId({ projectionName: options.projection.name ?? "unknown" }), processorInstanceId = getProcessorInstanceId(processorId), version = defaultProcessorVersion, partition = defaultProcessorPartition } = options;
|
|
830
|
+
const hooks = {
|
|
831
|
+
...options.hooks ?? {},
|
|
832
|
+
onInit: options.projection.init !== void 0 || options.hooks?.onInit ? async (context) => {
|
|
833
|
+
if (options.projection.init) await options.projection.init({
|
|
834
|
+
version: options.projection.version ?? version,
|
|
835
|
+
status: "active",
|
|
836
|
+
registrationType: "async",
|
|
837
|
+
context: {
|
|
838
|
+
...context,
|
|
839
|
+
migrationOptions: options.migrationOptions
|
|
840
|
+
}
|
|
841
|
+
});
|
|
842
|
+
if (options.hooks?.onInit) await options.hooks.onInit({
|
|
843
|
+
...context,
|
|
844
|
+
migrationOptions: options.migrationOptions
|
|
845
|
+
});
|
|
846
|
+
} : options.hooks?.onInit,
|
|
847
|
+
onClose: options.hooks?.onClose
|
|
848
|
+
};
|
|
849
|
+
return projector({
|
|
850
|
+
...options,
|
|
851
|
+
processorId,
|
|
852
|
+
processorInstanceId,
|
|
853
|
+
version,
|
|
854
|
+
partition,
|
|
855
|
+
hooks,
|
|
856
|
+
processingScope: sqliteProcessingScope(),
|
|
857
|
+
checkpoints: sqliteCheckpointer()
|
|
858
|
+
});
|
|
2219
859
|
};
|
|
2220
860
|
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
|
|
2255
|
-
|
|
2256
|
-
|
|
2257
|
-
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
|
|
2295
|
-
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
|
|
2319
|
-
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
|
|
2326
|
-
|
|
2327
|
-
|
|
2328
|
-
|
|
2329
|
-
|
|
2330
|
-
|
|
2331
|
-
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
|
|
2337
|
-
|
|
2338
|
-
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
|
|
2343
|
-
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
|
|
2349
|
-
|
|
2350
|
-
|
|
2351
|
-
|
|
2352
|
-
|
|
2353
|
-
|
|
2354
|
-
|
|
2355
|
-
|
|
2356
|
-
|
|
2357
|
-
|
|
2358
|
-
|
|
2359
|
-
|
|
2360
|
-
|
|
2361
|
-
})
|
|
2362
|
-
)
|
|
2363
|
-
)
|
|
2364
|
-
);
|
|
2365
|
-
await messagePuller.start({ startFrom });
|
|
2366
|
-
await stopProcessors();
|
|
2367
|
-
isRunning = false;
|
|
2368
|
-
})();
|
|
2369
|
-
return start;
|
|
2370
|
-
},
|
|
2371
|
-
stop,
|
|
2372
|
-
close: async () => {
|
|
2373
|
-
await stop();
|
|
2374
|
-
await pool.close();
|
|
2375
|
-
}
|
|
2376
|
-
};
|
|
861
|
+
//#endregion
|
|
862
|
+
//#region src/eventStore/consumers/sqliteEventStoreConsumer.ts
|
|
863
|
+
const sqliteEventStoreConsumer = (options) => {
|
|
864
|
+
let isRunning = false;
|
|
865
|
+
let isInitialized = false;
|
|
866
|
+
const { pulling } = options;
|
|
867
|
+
const processors = options.processors ?? [];
|
|
868
|
+
let abortController = null;
|
|
869
|
+
let start;
|
|
870
|
+
let messagePuller;
|
|
871
|
+
const startedAwaiter = asyncAwaiter();
|
|
872
|
+
const pool = options.pool ?? dumbo({
|
|
873
|
+
serialization: options.serialization,
|
|
874
|
+
transactionOptions: {
|
|
875
|
+
allowNestedTransactions: true,
|
|
876
|
+
mode: "session_based"
|
|
877
|
+
},
|
|
878
|
+
...options.driver.mapToDumboOptions(options)
|
|
879
|
+
});
|
|
880
|
+
const eachBatch = (messagesBatch) => pool.withConnection(async (connection) => {
|
|
881
|
+
const activeProcessors = processors.filter((s) => s.isActive);
|
|
882
|
+
if (activeProcessors.length === 0) return {
|
|
883
|
+
type: "STOP",
|
|
884
|
+
reason: "No active processors"
|
|
885
|
+
};
|
|
886
|
+
return (await Promise.allSettled(activeProcessors.map(async (s) => {
|
|
887
|
+
return await s.handle(messagesBatch, {
|
|
888
|
+
connection,
|
|
889
|
+
execute: connection.execute
|
|
890
|
+
});
|
|
891
|
+
}))).some((r) => r.status === "fulfilled" && r.value?.type !== "STOP") ? void 0 : { type: "STOP" };
|
|
892
|
+
});
|
|
893
|
+
const processorContext = {
|
|
894
|
+
execute: void 0,
|
|
895
|
+
connection: void 0
|
|
896
|
+
};
|
|
897
|
+
const stopProcessors = () => Promise.all(processors.map((p) => p.close(processorContext)));
|
|
898
|
+
const stop = async () => {
|
|
899
|
+
if (!isRunning) return;
|
|
900
|
+
isRunning = false;
|
|
901
|
+
if (messagePuller) {
|
|
902
|
+
abortController?.abort();
|
|
903
|
+
await messagePuller.stop();
|
|
904
|
+
}
|
|
905
|
+
await start;
|
|
906
|
+
messagePuller = void 0;
|
|
907
|
+
abortController = null;
|
|
908
|
+
await stopProcessors();
|
|
909
|
+
};
|
|
910
|
+
const init = async () => {
|
|
911
|
+
if (isInitialized) return;
|
|
912
|
+
const sqliteProcessors = processors;
|
|
913
|
+
await pool.withConnection(async (connection) => {
|
|
914
|
+
for (const processor of sqliteProcessors) if (processor.init) await processor.init({
|
|
915
|
+
...processorContext,
|
|
916
|
+
connection,
|
|
917
|
+
execute: connection.execute
|
|
918
|
+
});
|
|
919
|
+
});
|
|
920
|
+
isInitialized = true;
|
|
921
|
+
};
|
|
922
|
+
return {
|
|
923
|
+
consumerId: options.consumerId ?? v7(),
|
|
924
|
+
get isRunning() {
|
|
925
|
+
return isRunning;
|
|
926
|
+
},
|
|
927
|
+
whenStarted: () => startedAwaiter.wait,
|
|
928
|
+
processors,
|
|
929
|
+
init,
|
|
930
|
+
reactor: (options) => {
|
|
931
|
+
const processor = sqliteReactor(options);
|
|
932
|
+
processors.push(processor);
|
|
933
|
+
return processor;
|
|
934
|
+
},
|
|
935
|
+
projector: (options) => {
|
|
936
|
+
const processor = sqliteProjector(options);
|
|
937
|
+
processors.push(processor);
|
|
938
|
+
return processor;
|
|
939
|
+
},
|
|
940
|
+
workflowProcessor: (processorOptions) => {
|
|
941
|
+
const messageStore = getSQLiteEventStore({
|
|
942
|
+
...options,
|
|
943
|
+
pool,
|
|
944
|
+
schema: { autoMigration: "None" }
|
|
945
|
+
});
|
|
946
|
+
const processor = sqliteWorkflowProcessor({
|
|
947
|
+
...processorOptions,
|
|
948
|
+
messageStore
|
|
949
|
+
});
|
|
950
|
+
processors.push(processor);
|
|
951
|
+
return processor;
|
|
952
|
+
},
|
|
953
|
+
start: () => {
|
|
954
|
+
if (isRunning) return start;
|
|
955
|
+
startedAwaiter.reset();
|
|
956
|
+
if (processors.length === 0) {
|
|
957
|
+
const error = new EmmettError("Cannot start consumer without at least a single processor");
|
|
958
|
+
startedAwaiter.reject(error);
|
|
959
|
+
return Promise.reject(error);
|
|
960
|
+
}
|
|
961
|
+
isRunning = true;
|
|
962
|
+
abortController = new AbortController();
|
|
963
|
+
start = (async () => {
|
|
964
|
+
if (!isRunning) return;
|
|
965
|
+
try {
|
|
966
|
+
messagePuller = sqliteEventStoreMessageBatchPuller({
|
|
967
|
+
stopWhen: options.stopWhen,
|
|
968
|
+
executor: pool.execute,
|
|
969
|
+
eachBatch,
|
|
970
|
+
batchSize: pulling?.batchSize ?? 100,
|
|
971
|
+
pullingFrequencyInMs: pulling?.pullingFrequencyInMs ?? 50,
|
|
972
|
+
signal: abortController.signal
|
|
973
|
+
});
|
|
974
|
+
if (!isInitialized) await init();
|
|
975
|
+
const startFrom = await pool.withConnection(async (connection) => zipSQLiteEventStoreMessageBatchPullerStartFrom(await Promise.all(processors.map(async (o) => {
|
|
976
|
+
return await o.start({
|
|
977
|
+
execute: connection.execute,
|
|
978
|
+
connection
|
|
979
|
+
});
|
|
980
|
+
}))));
|
|
981
|
+
await messagePuller.start({
|
|
982
|
+
startFrom,
|
|
983
|
+
started: startedAwaiter
|
|
984
|
+
});
|
|
985
|
+
} catch (error) {
|
|
986
|
+
isRunning = false;
|
|
987
|
+
startedAwaiter.reject(error);
|
|
988
|
+
throw error;
|
|
989
|
+
} finally {
|
|
990
|
+
await stopProcessors();
|
|
991
|
+
}
|
|
992
|
+
})();
|
|
993
|
+
return start;
|
|
994
|
+
},
|
|
995
|
+
stop,
|
|
996
|
+
close: async () => {
|
|
997
|
+
await stop();
|
|
998
|
+
await pool.close();
|
|
999
|
+
}
|
|
1000
|
+
};
|
|
2377
1001
|
};
|
|
2378
1002
|
|
|
2379
|
-
|
|
2380
|
-
|
|
2381
|
-
|
|
2382
|
-
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
|
|
2386
|
-
|
|
2387
|
-
|
|
2388
|
-
|
|
2389
|
-
|
|
2390
|
-
|
|
2391
|
-
|
|
2392
|
-
|
|
2393
|
-
|
|
2394
|
-
|
|
2395
|
-
|
|
2396
|
-
|
|
2397
|
-
|
|
2398
|
-
|
|
2399
|
-
|
|
2400
|
-
|
|
2401
|
-
|
|
2402
|
-
|
|
2403
|
-
|
|
2404
|
-
|
|
2405
|
-
|
|
2406
|
-
|
|
2407
|
-
|
|
2408
|
-
|
|
2409
|
-
|
|
2410
|
-
|
|
2411
|
-
|
|
2412
|
-
|
|
2413
|
-
|
|
2414
|
-
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2420
|
-
|
|
2421
|
-
|
|
2422
|
-
|
|
2423
|
-
|
|
2424
|
-
|
|
2425
|
-
|
|
2426
|
-
|
|
2427
|
-
|
|
2428
|
-
|
|
2429
|
-
|
|
2430
|
-
|
|
2431
|
-
|
|
2432
|
-
|
|
2433
|
-
|
|
2434
|
-
|
|
2435
|
-
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
|
|
2439
|
-
|
|
2440
|
-
|
|
2441
|
-
|
|
2442
|
-
|
|
2443
|
-
|
|
2444
|
-
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
|
|
2450
|
-
|
|
2451
|
-
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
|
|
2485
|
-
|
|
2486
|
-
|
|
2487
|
-
|
|
2488
|
-
|
|
2489
|
-
|
|
2490
|
-
|
|
2491
|
-
|
|
2492
|
-
|
|
2493
|
-
|
|
2494
|
-
|
|
2495
|
-
|
|
2496
|
-
|
|
2497
|
-
|
|
2498
|
-
|
|
2499
|
-
|
|
2500
|
-
|
|
2501
|
-
|
|
2502
|
-
|
|
2503
|
-
|
|
2504
|
-
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
|
|
2511
|
-
|
|
2512
|
-
|
|
2513
|
-
|
|
2514
|
-
|
|
2515
|
-
|
|
2516
|
-
serialization: options.serialization
|
|
2517
|
-
}),
|
|
2518
|
-
transactionOptions: {
|
|
2519
|
-
allowNestedTransactions: true,
|
|
2520
|
-
mode: "session_based"
|
|
2521
|
-
},
|
|
2522
|
-
schema: {
|
|
2523
|
-
...options.schema,
|
|
2524
|
-
autoMigration: "None"
|
|
2525
|
-
},
|
|
2526
|
-
serialization: options.serialization
|
|
2527
|
-
});
|
|
2528
|
-
await ensureSchemaExists();
|
|
2529
|
-
return callback({
|
|
2530
|
-
eventStore: sessionStore,
|
|
2531
|
-
close: () => Promise.resolve()
|
|
2532
|
-
});
|
|
2533
|
-
});
|
|
2534
|
-
},
|
|
2535
|
-
close: () => pool.close(),
|
|
2536
|
-
schema: {
|
|
2537
|
-
sql: () => schemaSQL.join(""),
|
|
2538
|
-
print: () => console.log(schemaSQL.join("")),
|
|
2539
|
-
migrate: () => pool.withConnection(migrate)
|
|
2540
|
-
}
|
|
2541
|
-
};
|
|
1003
|
+
//#endregion
|
|
1004
|
+
//#region src/eventStore/SQLiteEventStore.ts
|
|
1005
|
+
const SQLiteEventStoreDefaultStreamVersion = 0n;
|
|
1006
|
+
const getSQLiteEventStore = (options) => {
|
|
1007
|
+
let autoGenerateSchema = false;
|
|
1008
|
+
const serializer = JSONSerializer.from(options);
|
|
1009
|
+
const pool = options.pool ?? dumbo({
|
|
1010
|
+
serialization: options.serialization,
|
|
1011
|
+
transactionOptions: {
|
|
1012
|
+
allowNestedTransactions: true,
|
|
1013
|
+
mode: "session_based"
|
|
1014
|
+
},
|
|
1015
|
+
...options.driver.mapToDumboOptions(options)
|
|
1016
|
+
});
|
|
1017
|
+
let migrateSchema = void 0;
|
|
1018
|
+
const inlineProjections = (options.projections ?? []).filter(({ type }) => type === "inline").map(({ projection }) => projection);
|
|
1019
|
+
const onBeforeCommitHook = options.hooks?.onBeforeCommit;
|
|
1020
|
+
if (options) autoGenerateSchema = options.schema?.autoMigration === void 0 || options.schema?.autoMigration !== "None";
|
|
1021
|
+
const migrate = (connection) => {
|
|
1022
|
+
if (!migrateSchema) migrateSchema = createEventStoreSchema(connection, {
|
|
1023
|
+
onBeforeSchemaCreated: async (context) => {
|
|
1024
|
+
for (const projection of inlineProjections) if (projection.init) await projection.init({
|
|
1025
|
+
version: projection.version ?? 1,
|
|
1026
|
+
registrationType: "async",
|
|
1027
|
+
status: "active",
|
|
1028
|
+
context: {
|
|
1029
|
+
execute: context.connection.execute,
|
|
1030
|
+
connection: context.connection,
|
|
1031
|
+
driverType: options.driver.driverType
|
|
1032
|
+
}
|
|
1033
|
+
});
|
|
1034
|
+
if (options.hooks?.onBeforeSchemaCreated) await options.hooks.onBeforeSchemaCreated(context);
|
|
1035
|
+
},
|
|
1036
|
+
onAfterSchemaCreated: options.hooks?.onAfterSchemaCreated
|
|
1037
|
+
});
|
|
1038
|
+
return migrateSchema;
|
|
1039
|
+
};
|
|
1040
|
+
const ensureSchemaExists = () => {
|
|
1041
|
+
if (!autoGenerateSchema) return Promise.resolve();
|
|
1042
|
+
return pool.withConnection((connection) => migrate(connection));
|
|
1043
|
+
};
|
|
1044
|
+
return {
|
|
1045
|
+
async aggregateStream(streamName, options) {
|
|
1046
|
+
await ensureSchemaExists();
|
|
1047
|
+
const { evolve, initialState, read } = options;
|
|
1048
|
+
const expectedStreamVersion = read?.expectedStreamVersion;
|
|
1049
|
+
let state = initialState();
|
|
1050
|
+
if (typeof streamName !== "string") throw new Error("Stream name is not string");
|
|
1051
|
+
const result = await readStream(pool.execute, streamName, {
|
|
1052
|
+
...read,
|
|
1053
|
+
serializer: read?.serialization?.serializer ?? serializer
|
|
1054
|
+
});
|
|
1055
|
+
const currentStreamVersion = result.currentStreamVersion;
|
|
1056
|
+
assertExpectedVersionMatchesCurrent(currentStreamVersion, expectedStreamVersion, SQLiteEventStoreDefaultStreamVersion);
|
|
1057
|
+
for (const event of result.events) {
|
|
1058
|
+
if (!event) continue;
|
|
1059
|
+
state = evolve(state, event);
|
|
1060
|
+
}
|
|
1061
|
+
return {
|
|
1062
|
+
currentStreamVersion,
|
|
1063
|
+
state,
|
|
1064
|
+
streamExists: result.streamExists
|
|
1065
|
+
};
|
|
1066
|
+
},
|
|
1067
|
+
readStream: async (streamName, readOptions) => {
|
|
1068
|
+
await ensureSchemaExists();
|
|
1069
|
+
return readStream(pool.execute, streamName, {
|
|
1070
|
+
...readOptions,
|
|
1071
|
+
serializer: options.serialization?.serializer ?? serializer
|
|
1072
|
+
});
|
|
1073
|
+
},
|
|
1074
|
+
appendToStream: async (streamName, events, appendOptions) => {
|
|
1075
|
+
await ensureSchemaExists();
|
|
1076
|
+
const [firstPart, ...rest] = streamName.split("-");
|
|
1077
|
+
const streamType = firstPart && rest.length > 0 ? firstPart : unknownTag;
|
|
1078
|
+
const appendResult = await pool.withConnection((connection) => appendToStream(connection, streamName, streamType, events, {
|
|
1079
|
+
...appendOptions,
|
|
1080
|
+
onBeforeCommit: async (messages, context) => {
|
|
1081
|
+
if (inlineProjections.length > 0) await handleProjections({
|
|
1082
|
+
projections: inlineProjections,
|
|
1083
|
+
events: messages,
|
|
1084
|
+
execute: context.connection.execute,
|
|
1085
|
+
connection: context.connection,
|
|
1086
|
+
driverType: options.driver.driverType
|
|
1087
|
+
});
|
|
1088
|
+
if (onBeforeCommitHook) await onBeforeCommitHook(messages, context);
|
|
1089
|
+
}
|
|
1090
|
+
}), { readonly: false });
|
|
1091
|
+
if (!appendResult.success) throw new ExpectedVersionConflictError(-1n, appendOptions?.expectedStreamVersion ?? NO_CONCURRENCY_CHECK);
|
|
1092
|
+
return {
|
|
1093
|
+
nextExpectedStreamVersion: appendResult.nextStreamPosition,
|
|
1094
|
+
lastEventGlobalPosition: appendResult.lastGlobalPosition,
|
|
1095
|
+
createdNewStream: appendResult.nextStreamPosition >= BigInt(events.length)
|
|
1096
|
+
};
|
|
1097
|
+
},
|
|
1098
|
+
async streamExists(streamName, options) {
|
|
1099
|
+
await ensureSchemaExists();
|
|
1100
|
+
return streamExists(pool.execute, streamName, options);
|
|
1101
|
+
},
|
|
1102
|
+
consumer: (consumerOptions) => sqliteEventStoreConsumer({
|
|
1103
|
+
...options ?? {},
|
|
1104
|
+
...consumerOptions ?? {},
|
|
1105
|
+
pool
|
|
1106
|
+
}),
|
|
1107
|
+
async withSession(callback) {
|
|
1108
|
+
return await pool.withConnection(async (connection) => {
|
|
1109
|
+
const sessionStore = getSQLiteEventStore({
|
|
1110
|
+
...options,
|
|
1111
|
+
pool: dumbo({
|
|
1112
|
+
...options.driver.mapToDumboOptions(options),
|
|
1113
|
+
connection,
|
|
1114
|
+
serialization: options.serialization
|
|
1115
|
+
}),
|
|
1116
|
+
transactionOptions: {
|
|
1117
|
+
allowNestedTransactions: true,
|
|
1118
|
+
mode: "session_based"
|
|
1119
|
+
},
|
|
1120
|
+
schema: {
|
|
1121
|
+
...options.schema,
|
|
1122
|
+
autoMigration: "None"
|
|
1123
|
+
},
|
|
1124
|
+
serialization: options.serialization
|
|
1125
|
+
});
|
|
1126
|
+
await ensureSchemaExists();
|
|
1127
|
+
return callback({
|
|
1128
|
+
eventStore: sessionStore,
|
|
1129
|
+
close: () => Promise.resolve()
|
|
1130
|
+
});
|
|
1131
|
+
});
|
|
1132
|
+
},
|
|
1133
|
+
close: () => pool.close(),
|
|
1134
|
+
schema: {
|
|
1135
|
+
sql: () => schemaSQL.join(""),
|
|
1136
|
+
print: () => console.log(schemaSQL.join("")),
|
|
1137
|
+
migrate: () => pool.withConnection(migrate)
|
|
1138
|
+
}
|
|
1139
|
+
};
|
|
2542
1140
|
};
|
|
2543
1141
|
|
|
2544
|
-
|
|
2545
|
-
|
|
2546
|
-
|
|
2547
|
-
|
|
2548
|
-
|
|
2549
|
-
|
|
2550
|
-
|
|
2551
|
-
|
|
2552
|
-
|
|
2553
|
-
|
|
2554
|
-
|
|
2555
|
-
|
|
2556
|
-
|
|
2557
|
-
|
|
2558
|
-
|
|
2559
|
-
|
|
2560
|
-
|
|
2561
|
-
|
|
2562
|
-
|
|
2563
|
-
|
|
2564
|
-
|
|
2565
|
-
|
|
2566
|
-
|
|
2567
|
-
|
|
2568
|
-
|
|
2569
|
-
|
|
2570
|
-
|
|
2571
|
-
|
|
2572
|
-
|
|
2573
|
-
|
|
2574
|
-
|
|
2575
|
-
|
|
2576
|
-
|
|
2577
|
-
|
|
2578
|
-
|
|
2579
|
-
|
|
2580
|
-
|
|
2581
|
-
|
|
2582
|
-
|
|
2583
|
-
|
|
2584
|
-
|
|
2585
|
-
currentStreamVersion: SQLiteEventStoreDefaultStreamVersion,
|
|
2586
|
-
events: [],
|
|
2587
|
-
streamExists: false
|
|
2588
|
-
};
|
|
1142
|
+
//#endregion
|
|
1143
|
+
//#region src/eventStore/schema/readStream.ts
|
|
1144
|
+
const { identifier: identifier$2 } = SQL;
|
|
1145
|
+
const readStream = async (execute, streamId, options) => {
|
|
1146
|
+
const { serializer } = options;
|
|
1147
|
+
const fromCondition = options.from ? SQL`AND stream_position >= ${options.from}` : SQL.EMPTY;
|
|
1148
|
+
const to = Number(options?.to ?? (options?.maxCount ? (options.from ?? 0n) + options.maxCount : NaN));
|
|
1149
|
+
const toCondition = !isNaN(to) ? SQL`AND stream_position <= ${to}` : SQL.EMPTY;
|
|
1150
|
+
const { rows: results } = await execute.query(SQL`SELECT stream_id, stream_position, global_position, message_data, message_metadata, message_schema_version, message_type, message_id
|
|
1151
|
+
FROM ${identifier$2(messagesTable.name)}
|
|
1152
|
+
WHERE stream_id = ${streamId} AND partition = ${options?.partition ?? defaultTag} AND is_archived = FALSE ${fromCondition} ${toCondition}
|
|
1153
|
+
ORDER BY stream_position ASC`);
|
|
1154
|
+
const messages = results.map((row) => {
|
|
1155
|
+
const rawEvent = {
|
|
1156
|
+
type: row.message_type,
|
|
1157
|
+
data: serializer.deserialize(row.message_data),
|
|
1158
|
+
metadata: serializer.deserialize(row.message_metadata)
|
|
1159
|
+
};
|
|
1160
|
+
const metadata = {
|
|
1161
|
+
..."metadata" in rawEvent ? rawEvent.metadata ?? {} : {},
|
|
1162
|
+
messageId: row.message_id,
|
|
1163
|
+
streamName: streamId,
|
|
1164
|
+
streamPosition: BigInt(row.stream_position),
|
|
1165
|
+
globalPosition: BigInt(row.global_position),
|
|
1166
|
+
checkpoint: bigIntProcessorCheckpoint(BigInt(row.global_position))
|
|
1167
|
+
};
|
|
1168
|
+
return upcastRecordedMessage({
|
|
1169
|
+
...rawEvent,
|
|
1170
|
+
kind: "Event",
|
|
1171
|
+
metadata
|
|
1172
|
+
}, options?.schema?.versioning);
|
|
1173
|
+
});
|
|
1174
|
+
return messages.length > 0 ? {
|
|
1175
|
+
currentStreamVersion: messages[messages.length - 1].metadata.streamPosition,
|
|
1176
|
+
events: messages,
|
|
1177
|
+
streamExists: true
|
|
1178
|
+
} : {
|
|
1179
|
+
currentStreamVersion: SQLiteEventStoreDefaultStreamVersion,
|
|
1180
|
+
events: [],
|
|
1181
|
+
streamExists: false
|
|
1182
|
+
};
|
|
2589
1183
|
};
|
|
2590
1184
|
|
|
2591
|
-
|
|
2592
|
-
|
|
2593
|
-
|
|
2594
|
-
singleOrNull as singleOrNull5,
|
|
2595
|
-
SQL as SQL9,
|
|
2596
|
-
UniqueConstraintError as UniqueConstraintError2
|
|
2597
|
-
} from "@event-driven-io/dumbo";
|
|
2598
|
-
var { identifier: identifier7 } = SQL9;
|
|
1185
|
+
//#endregion
|
|
1186
|
+
//#region src/eventStore/schema/storeProcessorCheckpoint.ts
|
|
1187
|
+
const { identifier: identifier$1 } = SQL;
|
|
2599
1188
|
async function storeSubscriptionCheckpointSQLite(execute, processorId, version, position, checkPosition, partition, processorInstanceId) {
|
|
2600
|
-
|
|
2601
|
-
|
|
2602
|
-
|
|
2603
|
-
|
|
2604
|
-
UPDATE ${identifier7(processorsTable.name)}
|
|
1189
|
+
processorInstanceId ??= unknownTag;
|
|
1190
|
+
if (checkPosition !== null) {
|
|
1191
|
+
const updateResult = await execute.command(SQL`
|
|
1192
|
+
UPDATE ${identifier$1(processorsTable.name)}
|
|
2605
1193
|
SET
|
|
2606
1194
|
last_processed_checkpoint = ${position},
|
|
2607
1195
|
processor_instance_id = ${processorInstanceId}
|
|
2608
1196
|
WHERE processor_id = ${processorId}
|
|
2609
1197
|
AND last_processed_checkpoint = ${checkPosition}
|
|
2610
1198
|
AND partition = ${partition}
|
|
2611
|
-
`
|
|
2612
|
-
|
|
2613
|
-
|
|
2614
|
-
|
|
2615
|
-
|
|
2616
|
-
|
|
2617
|
-
|
|
2618
|
-
|
|
2619
|
-
|
|
2620
|
-
|
|
2621
|
-
|
|
2622
|
-
|
|
2623
|
-
|
|
2624
|
-
|
|
2625
|
-
|
|
2626
|
-
|
|
2627
|
-
|
|
2628
|
-
|
|
2629
|
-
|
|
2630
|
-
|
|
2631
|
-
|
|
2632
|
-
try {
|
|
2633
|
-
await execute.command(
|
|
2634
|
-
SQL9`INSERT INTO ${identifier7(processorsTable.name)} (processor_id, version, last_processed_checkpoint, partition, processor_instance_id)
|
|
2635
|
-
VALUES (${processorId}, ${version}, ${position}, ${partition}, ${processorInstanceId})`
|
|
2636
|
-
);
|
|
2637
|
-
return 1;
|
|
2638
|
-
} catch (err) {
|
|
2639
|
-
if (!DumboError2.isInstanceOf(err, {
|
|
2640
|
-
errorType: UniqueConstraintError2.ErrorType
|
|
2641
|
-
})) {
|
|
2642
|
-
throw err;
|
|
2643
|
-
}
|
|
2644
|
-
const current = await singleOrNull5(
|
|
2645
|
-
execute.query(
|
|
2646
|
-
SQL9`
|
|
2647
|
-
SELECT last_processed_checkpoint FROM ${identifier7(processorsTable.name)}
|
|
2648
|
-
WHERE processor_id = ${processorId} AND partition = ${partition}`
|
|
2649
|
-
)
|
|
2650
|
-
);
|
|
2651
|
-
const currentPosition = current && current?.last_processed_checkpoint !== null ? BigInt(current.last_processed_checkpoint) : null;
|
|
2652
|
-
if (currentPosition === position) {
|
|
2653
|
-
return 0;
|
|
2654
|
-
} else {
|
|
2655
|
-
return 2;
|
|
2656
|
-
}
|
|
2657
|
-
}
|
|
2658
|
-
}
|
|
1199
|
+
`);
|
|
1200
|
+
if (updateResult.rowCount && updateResult.rowCount > 0) return 1;
|
|
1201
|
+
const current_position = await singleOrNull(execute.query(SQL`
|
|
1202
|
+
SELECT last_processed_checkpoint FROM ${identifier$1(processorsTable.name)}
|
|
1203
|
+
WHERE processor_id = ${processorId} AND partition = ${partition}`));
|
|
1204
|
+
const currentPosition = current_position && current_position?.last_processed_checkpoint !== null ? current_position.last_processed_checkpoint : null;
|
|
1205
|
+
if (currentPosition === position) return 0;
|
|
1206
|
+
else if (position !== null && currentPosition !== null && currentPosition > position) return 2;
|
|
1207
|
+
else return 2;
|
|
1208
|
+
} else try {
|
|
1209
|
+
await execute.command(SQL`INSERT INTO ${identifier$1(processorsTable.name)} (processor_id, version, last_processed_checkpoint, partition, processor_instance_id)
|
|
1210
|
+
VALUES (${processorId}, ${version}, ${position}, ${partition}, ${processorInstanceId})`);
|
|
1211
|
+
return 1;
|
|
1212
|
+
} catch (err) {
|
|
1213
|
+
if (!DumboError.isInstanceOf(err, { errorType: UniqueConstraintError.ErrorType })) throw err;
|
|
1214
|
+
const current = await singleOrNull(execute.query(SQL`
|
|
1215
|
+
SELECT last_processed_checkpoint FROM ${identifier$1(processorsTable.name)}
|
|
1216
|
+
WHERE processor_id = ${processorId} AND partition = ${partition}`));
|
|
1217
|
+
if ((current && current?.last_processed_checkpoint !== null ? BigInt(current.last_processed_checkpoint) : null) === position) return 0;
|
|
1218
|
+
else return 2;
|
|
1219
|
+
}
|
|
2659
1220
|
}
|
|
2660
1221
|
async function storeProcessorCheckpoint(execute, options) {
|
|
2661
|
-
|
|
2662
|
-
|
|
2663
|
-
|
|
2664
|
-
|
|
2665
|
-
|
|
2666
|
-
|
|
2667
|
-
|
|
2668
|
-
|
|
2669
|
-
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
}
|
|
1222
|
+
try {
|
|
1223
|
+
const result = await storeSubscriptionCheckpointSQLite(execute, options.processorId, options.version ?? 1, options.newCheckpoint, options.lastProcessedCheckpoint, options.partition ?? defaultTag);
|
|
1224
|
+
return result === 1 ? {
|
|
1225
|
+
success: true,
|
|
1226
|
+
newCheckpoint: options.newCheckpoint
|
|
1227
|
+
} : {
|
|
1228
|
+
success: false,
|
|
1229
|
+
reason: result === 0 ? "IGNORED" : "MISMATCH"
|
|
1230
|
+
};
|
|
1231
|
+
} catch (error) {
|
|
1232
|
+
console.log(error);
|
|
1233
|
+
throw error;
|
|
1234
|
+
}
|
|
2675
1235
|
}
|
|
2676
1236
|
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
execute.query(
|
|
2681
|
-
SQL10`SELECT EXISTS (
|
|
1237
|
+
//#endregion
|
|
1238
|
+
//#region src/eventStore/schema/streamExists.ts
|
|
1239
|
+
const streamExists = (execute, streamId, options) => exists(execute.query(SQL`SELECT EXISTS (
|
|
2682
1240
|
SELECT 1
|
|
2683
|
-
from ${
|
|
2684
|
-
WHERE stream_id = ${streamId} AND partition = ${options?.partition ??
|
|
2685
|
-
`
|
|
2686
|
-
)
|
|
2687
|
-
);
|
|
1241
|
+
from ${SQL.identifier(streamsTable.name)}
|
|
1242
|
+
WHERE stream_id = ${streamId} AND partition = ${options?.partition ?? defaultTag} AND is_archived = FALSE) as exists
|
|
1243
|
+
`));
|
|
2688
1244
|
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
1245
|
+
//#endregion
|
|
1246
|
+
//#region src/eventStore/schema/tables.ts
|
|
1247
|
+
const { identifier, plain } = SQL;
|
|
1248
|
+
const streamsTableSQL = SQL`CREATE TABLE IF NOT EXISTS ${identifier(streamsTable.name)}(
|
|
2693
1249
|
stream_id TEXT NOT NULL,
|
|
2694
1250
|
stream_position BIGINT NOT NULL DEFAULT 0,
|
|
2695
|
-
partition TEXT NOT NULL DEFAULT '${
|
|
1251
|
+
partition TEXT NOT NULL DEFAULT '${plain(globalTag)}',
|
|
2696
1252
|
stream_type TEXT NOT NULL,
|
|
2697
1253
|
stream_metadata JSONB NOT NULL,
|
|
2698
1254
|
is_archived BOOLEAN NOT NULL DEFAULT FALSE,
|
|
2699
1255
|
PRIMARY KEY (stream_id, partition, is_archived),
|
|
2700
1256
|
UNIQUE (stream_id, partition, is_archived)
|
|
2701
1257
|
);`;
|
|
2702
|
-
|
|
1258
|
+
const messagesTableSQL = SQL`CREATE TABLE IF NOT EXISTS ${identifier(messagesTable.name)}(
|
|
2703
1259
|
stream_id TEXT NOT NULL,
|
|
2704
1260
|
stream_position BIGINT NOT NULL,
|
|
2705
|
-
partition TEXT NOT NULL DEFAULT '${
|
|
1261
|
+
partition TEXT NOT NULL DEFAULT '${plain(globalTag)}',
|
|
2706
1262
|
message_kind CHAR(1) NOT NULL DEFAULT 'E',
|
|
2707
1263
|
message_data JSONB NOT NULL,
|
|
2708
1264
|
message_metadata JSONB NOT NULL,
|
|
@@ -2715,22 +1271,22 @@ var messagesTableSQL = SQL11`CREATE TABLE IF NOT EXISTS ${identifier8(messagesTa
|
|
|
2715
1271
|
UNIQUE (stream_id, stream_position, partition, is_archived)
|
|
2716
1272
|
);
|
|
2717
1273
|
`;
|
|
2718
|
-
|
|
2719
|
-
CREATE TABLE IF NOT EXISTS ${
|
|
1274
|
+
const processorsTableSQL = SQL`
|
|
1275
|
+
CREATE TABLE IF NOT EXISTS ${SQL.identifier(processorsTable.name)}(
|
|
2720
1276
|
processor_id TEXT NOT NULL,
|
|
2721
1277
|
version INTEGER NOT NULL DEFAULT 1,
|
|
2722
|
-
partition TEXT NOT NULL DEFAULT '${
|
|
1278
|
+
partition TEXT NOT NULL DEFAULT '${plain(globalTag)}',
|
|
2723
1279
|
status TEXT NOT NULL DEFAULT 'stopped',
|
|
2724
1280
|
last_processed_checkpoint TEXT NOT NULL,
|
|
2725
|
-
processor_instance_id TEXT DEFAULT '${
|
|
1281
|
+
processor_instance_id TEXT DEFAULT '${plain(unknownTag)}',
|
|
2726
1282
|
PRIMARY KEY (processor_id, partition, version)
|
|
2727
1283
|
);
|
|
2728
1284
|
`;
|
|
2729
|
-
|
|
2730
|
-
CREATE TABLE IF NOT EXISTS ${
|
|
1285
|
+
const projectionsTableSQL = SQL`
|
|
1286
|
+
CREATE TABLE IF NOT EXISTS ${SQL.identifier(projectionsTable.name)}(
|
|
2731
1287
|
name TEXT NOT NULL,
|
|
2732
1288
|
version INTEGER NOT NULL DEFAULT 1,
|
|
2733
|
-
partition TEXT NOT NULL DEFAULT '${
|
|
1289
|
+
partition TEXT NOT NULL DEFAULT '${plain(globalTag)}',
|
|
2734
1290
|
type CHAR(1) NOT NULL,
|
|
2735
1291
|
kind TEXT NOT NULL,
|
|
2736
1292
|
status TEXT NOT NULL,
|
|
@@ -2738,73 +1294,21 @@ var projectionsTableSQL = SQL11`
|
|
|
2738
1294
|
PRIMARY KEY (name, partition, version)
|
|
2739
1295
|
);
|
|
2740
1296
|
`;
|
|
2741
|
-
|
|
2742
|
-
|
|
2743
|
-
|
|
2744
|
-
|
|
2745
|
-
|
|
1297
|
+
const schemaSQL = [
|
|
1298
|
+
streamsTableSQL,
|
|
1299
|
+
messagesTableSQL,
|
|
1300
|
+
processorsTableSQL,
|
|
1301
|
+
projectionsTableSQL
|
|
2746
1302
|
];
|
|
2747
|
-
|
|
2748
|
-
|
|
2749
|
-
|
|
2750
|
-
|
|
2751
|
-
|
|
2752
|
-
|
|
2753
|
-
|
|
2754
|
-
}
|
|
2755
|
-
await tx.execute.batchCommand(schemaSQL);
|
|
2756
|
-
if (hooks?.onAfterSchemaCreated) {
|
|
2757
|
-
await hooks.onAfterSchemaCreated();
|
|
2758
|
-
}
|
|
2759
|
-
});
|
|
2760
|
-
};
|
|
2761
|
-
export {
|
|
2762
|
-
SQLiteEventStoreDefaultStreamVersion,
|
|
2763
|
-
SQLiteProjectionSpec,
|
|
2764
|
-
appendToStream,
|
|
2765
|
-
assertSQLQueryResultMatches,
|
|
2766
|
-
createEventStoreSchema,
|
|
2767
|
-
defaultTag2 as defaultTag,
|
|
2768
|
-
documentDoesNotExist,
|
|
2769
|
-
documentExists,
|
|
2770
|
-
documentMatchingExists,
|
|
2771
|
-
documentsAreTheSame,
|
|
2772
|
-
documentsMatchingHaveCount,
|
|
2773
|
-
emmettPrefix2 as emmettPrefix,
|
|
2774
|
-
eventInStream,
|
|
2775
|
-
eventsInStream,
|
|
2776
|
-
expectPongoDocuments,
|
|
2777
|
-
expectSQL,
|
|
2778
|
-
getSQLiteEventStore,
|
|
2779
|
-
globalNames,
|
|
2780
|
-
globalTag,
|
|
2781
|
-
handleProjections,
|
|
2782
|
-
messagesTable,
|
|
2783
|
-
messagesTableSQL,
|
|
2784
|
-
migration_0_42_0_FromSubscriptionsToProcessors,
|
|
2785
|
-
migration_0_42_0_SQLs,
|
|
2786
|
-
newEventsInStream,
|
|
2787
|
-
pongoMultiStreamProjection,
|
|
2788
|
-
pongoProjection,
|
|
2789
|
-
pongoSingleStreamProjection,
|
|
2790
|
-
processorsTable,
|
|
2791
|
-
processorsTableSQL,
|
|
2792
|
-
projectionsTable,
|
|
2793
|
-
projectionsTableSQL,
|
|
2794
|
-
readLastMessageGlobalPosition,
|
|
2795
|
-
readMessagesBatch,
|
|
2796
|
-
readProcessorCheckpoint,
|
|
2797
|
-
readStream,
|
|
2798
|
-
schemaSQL,
|
|
2799
|
-
schema_0_41_0,
|
|
2800
|
-
schema_0_42_0,
|
|
2801
|
-
sqliteProjection,
|
|
2802
|
-
sqliteRawBatchSQLProjection,
|
|
2803
|
-
sqliteRawSQLProjection,
|
|
2804
|
-
storeProcessorCheckpoint,
|
|
2805
|
-
streamExists,
|
|
2806
|
-
streamsTable,
|
|
2807
|
-
streamsTableSQL,
|
|
2808
|
-
unknownTag2 as unknownTag
|
|
1303
|
+
const createEventStoreSchema = async (pool, hooks) => {
|
|
1304
|
+
await pool.withTransaction(async (tx) => {
|
|
1305
|
+
await migration_0_42_0_FromSubscriptionsToProcessors(tx.execute);
|
|
1306
|
+
if (hooks?.onBeforeSchemaCreated) await hooks.onBeforeSchemaCreated({ connection: tx.connection });
|
|
1307
|
+
await tx.execute.batchCommand(schemaSQL);
|
|
1308
|
+
if (hooks?.onAfterSchemaCreated) await hooks.onAfterSchemaCreated();
|
|
1309
|
+
});
|
|
2809
1310
|
};
|
|
1311
|
+
|
|
1312
|
+
//#endregion
|
|
1313
|
+
export { SQLiteEventStoreDefaultStreamVersion, SQLiteProjectionSpec, appendToStream, assertSQLQueryResultMatches, createEventStoreSchema, defaultTag, documentDoesNotExist, documentExists, documentMatchingExists, documentsAreTheSame, documentsMatchingHaveCount, emmettPrefix, eventInStream, eventsInStream, expectPongoDocuments, expectSQL, getSQLiteEventStore, globalNames, globalTag, handleProjections, messagesTable, messagesTableSQL, migration_0_42_0_FromSubscriptionsToProcessors, migration_0_42_0_SQLs, newEventsInStream, pongoMultiStreamProjection, pongoProjection, pongoSingleStreamProjection, processorsTable, processorsTableSQL, projectionsTable, projectionsTableSQL, readLastMessageGlobalPosition, readMessagesBatch, readProcessorCheckpoint, readStream, schemaSQL, schema_0_41_0, schema_0_42_0, sqliteProjection, sqliteRawBatchSQLProjection, sqliteRawSQLProjection, storeProcessorCheckpoint, streamExists, streamsTable, streamsTableSQL, unknownTag };
|
|
2810
1314
|
//# sourceMappingURL=index.js.map
|