@event-driven-io/emmett-sqlite 0.39.1 → 0.40.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +781 -329
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +56 -11
- package/dist/index.d.ts +56 -11
- package/dist/index.js +761 -309
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -103,6 +103,10 @@ var rollbackTransaction = (db) => new Promise((resolve, reject) => {
|
|
|
103
103
|
var isNumber = (val) => typeof val === "number" && val === val;
|
|
104
104
|
var isBigint = (val) => typeof val === "bigint" && val === val;
|
|
105
105
|
var isString = (val) => typeof val === "string";
|
|
106
|
+
var isErrorConstructor = (expect) => {
|
|
107
|
+
return typeof expect === "function" && expect.prototype && // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
108
|
+
expect.prototype.constructor === expect;
|
|
109
|
+
};
|
|
106
110
|
var EmmettError = class _EmmettError extends Error {
|
|
107
111
|
static Codes = {
|
|
108
112
|
ValidationError: 400,
|
|
@@ -169,6 +173,207 @@ var ExpectedVersionConflictError = class _ExpectedVersionConflictError extends C
|
|
|
169
173
|
Object.setPrototypeOf(this, _ExpectedVersionConflictError.prototype);
|
|
170
174
|
}
|
|
171
175
|
};
|
|
176
|
+
var isPrimitive = (value) => {
|
|
177
|
+
const type = typeof value;
|
|
178
|
+
return value === null || value === void 0 || type === "boolean" || type === "number" || type === "string" || type === "symbol" || type === "bigint";
|
|
179
|
+
};
|
|
180
|
+
var compareArrays = (left, right) => {
|
|
181
|
+
if (left.length !== right.length) {
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
for (let i = 0; i < left.length; i++) {
|
|
185
|
+
const leftHas = i in left;
|
|
186
|
+
const rightHas = i in right;
|
|
187
|
+
if (leftHas !== rightHas) return false;
|
|
188
|
+
if (leftHas && !deepEquals(left[i], right[i])) return false;
|
|
189
|
+
}
|
|
190
|
+
return true;
|
|
191
|
+
};
|
|
192
|
+
var compareDates = (left, right) => {
|
|
193
|
+
return left.getTime() === right.getTime();
|
|
194
|
+
};
|
|
195
|
+
var compareRegExps = (left, right) => {
|
|
196
|
+
return left.toString() === right.toString();
|
|
197
|
+
};
|
|
198
|
+
var compareErrors = (left, right) => {
|
|
199
|
+
if (left.message !== right.message || left.name !== right.name) {
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
const leftKeys = Object.keys(left);
|
|
203
|
+
const rightKeys = Object.keys(right);
|
|
204
|
+
if (leftKeys.length !== rightKeys.length) return false;
|
|
205
|
+
const rightKeySet = new Set(rightKeys);
|
|
206
|
+
for (const key of leftKeys) {
|
|
207
|
+
if (!rightKeySet.has(key)) return false;
|
|
208
|
+
if (!deepEquals(left[key], right[key])) return false;
|
|
209
|
+
}
|
|
210
|
+
return true;
|
|
211
|
+
};
|
|
212
|
+
var compareMaps = (left, right) => {
|
|
213
|
+
if (left.size !== right.size) return false;
|
|
214
|
+
for (const [key, value] of left) {
|
|
215
|
+
if (isPrimitive(key)) {
|
|
216
|
+
if (!right.has(key) || !deepEquals(value, right.get(key))) {
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
} else {
|
|
220
|
+
let found = false;
|
|
221
|
+
for (const [rightKey, rightValue] of right) {
|
|
222
|
+
if (deepEquals(key, rightKey) && deepEquals(value, rightValue)) {
|
|
223
|
+
found = true;
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
if (!found) return false;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return true;
|
|
231
|
+
};
|
|
232
|
+
var compareSets = (left, right) => {
|
|
233
|
+
if (left.size !== right.size) return false;
|
|
234
|
+
for (const leftItem of left) {
|
|
235
|
+
if (isPrimitive(leftItem)) {
|
|
236
|
+
if (!right.has(leftItem)) return false;
|
|
237
|
+
} else {
|
|
238
|
+
let found = false;
|
|
239
|
+
for (const rightItem of right) {
|
|
240
|
+
if (deepEquals(leftItem, rightItem)) {
|
|
241
|
+
found = true;
|
|
242
|
+
break;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
if (!found) return false;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
return true;
|
|
249
|
+
};
|
|
250
|
+
var compareArrayBuffers = (left, right) => {
|
|
251
|
+
if (left.byteLength !== right.byteLength) return false;
|
|
252
|
+
const leftView = new Uint8Array(left);
|
|
253
|
+
const rightView = new Uint8Array(right);
|
|
254
|
+
for (let i = 0; i < leftView.length; i++) {
|
|
255
|
+
if (leftView[i] !== rightView[i]) return false;
|
|
256
|
+
}
|
|
257
|
+
return true;
|
|
258
|
+
};
|
|
259
|
+
var compareTypedArrays = (left, right) => {
|
|
260
|
+
if (left.constructor !== right.constructor) return false;
|
|
261
|
+
if (left.byteLength !== right.byteLength) return false;
|
|
262
|
+
const leftArray = new Uint8Array(
|
|
263
|
+
left.buffer,
|
|
264
|
+
left.byteOffset,
|
|
265
|
+
left.byteLength
|
|
266
|
+
);
|
|
267
|
+
const rightArray = new Uint8Array(
|
|
268
|
+
right.buffer,
|
|
269
|
+
right.byteOffset,
|
|
270
|
+
right.byteLength
|
|
271
|
+
);
|
|
272
|
+
for (let i = 0; i < leftArray.length; i++) {
|
|
273
|
+
if (leftArray[i] !== rightArray[i]) return false;
|
|
274
|
+
}
|
|
275
|
+
return true;
|
|
276
|
+
};
|
|
277
|
+
var compareObjects = (left, right) => {
|
|
278
|
+
const keys1 = Object.keys(left);
|
|
279
|
+
const keys2 = Object.keys(right);
|
|
280
|
+
if (keys1.length !== keys2.length) {
|
|
281
|
+
return false;
|
|
282
|
+
}
|
|
283
|
+
for (const key of keys1) {
|
|
284
|
+
if (left[key] instanceof Function && right[key] instanceof Function) {
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
const isEqual = deepEquals(left[key], right[key]);
|
|
288
|
+
if (!isEqual) {
|
|
289
|
+
return false;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
return true;
|
|
293
|
+
};
|
|
294
|
+
var getType = (value) => {
|
|
295
|
+
if (value === null) return "null";
|
|
296
|
+
if (value === void 0) return "undefined";
|
|
297
|
+
const primitiveType = typeof value;
|
|
298
|
+
if (primitiveType !== "object") return primitiveType;
|
|
299
|
+
if (Array.isArray(value)) return "array";
|
|
300
|
+
if (value instanceof Boolean) return "boxed-boolean";
|
|
301
|
+
if (value instanceof Number) return "boxed-number";
|
|
302
|
+
if (value instanceof String) return "boxed-string";
|
|
303
|
+
if (value instanceof Date) return "date";
|
|
304
|
+
if (value instanceof RegExp) return "regexp";
|
|
305
|
+
if (value instanceof Error) return "error";
|
|
306
|
+
if (value instanceof Map) return "map";
|
|
307
|
+
if (value instanceof Set) return "set";
|
|
308
|
+
if (value instanceof ArrayBuffer) return "arraybuffer";
|
|
309
|
+
if (value instanceof DataView) return "dataview";
|
|
310
|
+
if (value instanceof WeakMap) return "weakmap";
|
|
311
|
+
if (value instanceof WeakSet) return "weakset";
|
|
312
|
+
if (ArrayBuffer.isView(value)) return "typedarray";
|
|
313
|
+
return "object";
|
|
314
|
+
};
|
|
315
|
+
var deepEquals = (left, right) => {
|
|
316
|
+
if (left === right) return true;
|
|
317
|
+
if (isEquatable(left)) {
|
|
318
|
+
return left.equals(right);
|
|
319
|
+
}
|
|
320
|
+
const leftType = getType(left);
|
|
321
|
+
const rightType = getType(right);
|
|
322
|
+
if (leftType !== rightType) return false;
|
|
323
|
+
switch (leftType) {
|
|
324
|
+
case "null":
|
|
325
|
+
case "undefined":
|
|
326
|
+
case "boolean":
|
|
327
|
+
case "number":
|
|
328
|
+
case "bigint":
|
|
329
|
+
case "string":
|
|
330
|
+
case "symbol":
|
|
331
|
+
case "function":
|
|
332
|
+
return left === right;
|
|
333
|
+
case "array":
|
|
334
|
+
return compareArrays(left, right);
|
|
335
|
+
case "date":
|
|
336
|
+
return compareDates(left, right);
|
|
337
|
+
case "regexp":
|
|
338
|
+
return compareRegExps(left, right);
|
|
339
|
+
case "error":
|
|
340
|
+
return compareErrors(left, right);
|
|
341
|
+
case "map":
|
|
342
|
+
return compareMaps(
|
|
343
|
+
left,
|
|
344
|
+
right
|
|
345
|
+
);
|
|
346
|
+
case "set":
|
|
347
|
+
return compareSets(left, right);
|
|
348
|
+
case "arraybuffer":
|
|
349
|
+
return compareArrayBuffers(left, right);
|
|
350
|
+
case "dataview":
|
|
351
|
+
case "weakmap":
|
|
352
|
+
case "weakset":
|
|
353
|
+
return false;
|
|
354
|
+
case "typedarray":
|
|
355
|
+
return compareTypedArrays(
|
|
356
|
+
left,
|
|
357
|
+
right
|
|
358
|
+
);
|
|
359
|
+
case "boxed-boolean":
|
|
360
|
+
return left.valueOf() === right.valueOf();
|
|
361
|
+
case "boxed-number":
|
|
362
|
+
return left.valueOf() === right.valueOf();
|
|
363
|
+
case "boxed-string":
|
|
364
|
+
return left.valueOf() === right.valueOf();
|
|
365
|
+
case "object":
|
|
366
|
+
return compareObjects(
|
|
367
|
+
left,
|
|
368
|
+
right
|
|
369
|
+
);
|
|
370
|
+
default:
|
|
371
|
+
return false;
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
var isEquatable = (left) => {
|
|
375
|
+
return left !== null && left !== void 0 && typeof left === "object" && "equals" in left && typeof left["equals"] === "function";
|
|
376
|
+
};
|
|
172
377
|
var ParseError = class extends Error {
|
|
173
378
|
constructor(text) {
|
|
174
379
|
super(`Cannot parse! ${text}`);
|
|
@@ -190,9 +395,110 @@ var JSONParser = {
|
|
|
190
395
|
return options?.map ? options.map(parsed) : parsed;
|
|
191
396
|
}
|
|
192
397
|
};
|
|
398
|
+
var AssertionError = class extends Error {
|
|
399
|
+
constructor(message2) {
|
|
400
|
+
super(message2);
|
|
401
|
+
}
|
|
402
|
+
};
|
|
403
|
+
var isSubset = (superObj, subObj) => {
|
|
404
|
+
const sup = superObj;
|
|
405
|
+
const sub = subObj;
|
|
406
|
+
assertOk(sup);
|
|
407
|
+
assertOk(sub);
|
|
408
|
+
return Object.keys(sub).every((ele) => {
|
|
409
|
+
if (typeof sub[ele] == "object") {
|
|
410
|
+
return isSubset(sup[ele], sub[ele]);
|
|
411
|
+
}
|
|
412
|
+
return sub[ele] === sup[ele];
|
|
413
|
+
});
|
|
414
|
+
};
|
|
415
|
+
var assertFails = (message2) => {
|
|
416
|
+
throw new AssertionError(message2 ?? "That should not ever happened, right?");
|
|
417
|
+
};
|
|
418
|
+
function assertTrue(condition, message2) {
|
|
419
|
+
if (condition !== true)
|
|
420
|
+
throw new AssertionError(message2 ?? `Condition is false`);
|
|
421
|
+
}
|
|
422
|
+
function assertOk(obj, message2) {
|
|
423
|
+
if (!obj) throw new AssertionError(message2 ?? `Condition is not truthy`);
|
|
424
|
+
}
|
|
425
|
+
function assertEqual(expected, actual, message2) {
|
|
426
|
+
if (expected !== actual)
|
|
427
|
+
throw new AssertionError(
|
|
428
|
+
`${message2 ?? "Objects are not equal"}:
|
|
429
|
+
Expected: ${JSONParser.stringify(expected)}
|
|
430
|
+
Actual: ${JSONParser.stringify(actual)}`
|
|
431
|
+
);
|
|
432
|
+
}
|
|
433
|
+
function assertNotEqual(obj, other, message2) {
|
|
434
|
+
if (obj === other)
|
|
435
|
+
throw new AssertionError(
|
|
436
|
+
message2 ?? `Objects are equal: ${JSONParser.stringify(obj)}`
|
|
437
|
+
);
|
|
438
|
+
}
|
|
439
|
+
var assertThatArray = (array) => {
|
|
440
|
+
return {
|
|
441
|
+
isEmpty: () => assertEqual(
|
|
442
|
+
array.length,
|
|
443
|
+
0,
|
|
444
|
+
`Array is not empty ${JSONParser.stringify(array)}`
|
|
445
|
+
),
|
|
446
|
+
isNotEmpty: () => assertNotEqual(array.length, 0, `Array is empty`),
|
|
447
|
+
hasSize: (length) => assertEqual(array.length, length),
|
|
448
|
+
containsElements: (other) => {
|
|
449
|
+
assertTrue(other.every((ts) => array.some((o) => deepEquals(ts, o))));
|
|
450
|
+
},
|
|
451
|
+
containsElementsMatching: (other) => {
|
|
452
|
+
assertTrue(other.every((ts) => array.some((o) => isSubset(o, ts))));
|
|
453
|
+
},
|
|
454
|
+
containsOnlyElementsMatching: (other) => {
|
|
455
|
+
assertEqual(array.length, other.length, `Arrays lengths don't match`);
|
|
456
|
+
assertTrue(other.every((ts) => array.some((o) => isSubset(o, ts))));
|
|
457
|
+
},
|
|
458
|
+
containsExactlyInAnyOrder: (other) => {
|
|
459
|
+
assertEqual(array.length, other.length);
|
|
460
|
+
assertTrue(array.every((ts) => other.some((o) => deepEquals(ts, o))));
|
|
461
|
+
},
|
|
462
|
+
containsExactlyInAnyOrderElementsOf: (other) => {
|
|
463
|
+
assertEqual(array.length, other.length);
|
|
464
|
+
assertTrue(array.every((ts) => other.some((o) => deepEquals(ts, o))));
|
|
465
|
+
},
|
|
466
|
+
containsExactlyElementsOf: (other) => {
|
|
467
|
+
assertEqual(array.length, other.length);
|
|
468
|
+
for (let i = 0; i < array.length; i++) {
|
|
469
|
+
assertTrue(deepEquals(array[i], other[i]));
|
|
470
|
+
}
|
|
471
|
+
},
|
|
472
|
+
containsExactly: (elem) => {
|
|
473
|
+
assertEqual(array.length, 1);
|
|
474
|
+
assertTrue(deepEquals(array[0], elem));
|
|
475
|
+
},
|
|
476
|
+
contains: (elem) => {
|
|
477
|
+
assertTrue(array.some((a) => deepEquals(a, elem)));
|
|
478
|
+
},
|
|
479
|
+
containsOnlyOnceElementsOf: (other) => {
|
|
480
|
+
assertTrue(
|
|
481
|
+
other.map((o) => array.filter((a) => deepEquals(a, o)).length).filter((a) => a === 1).length === other.length
|
|
482
|
+
);
|
|
483
|
+
},
|
|
484
|
+
containsAnyOf: (other) => {
|
|
485
|
+
assertTrue(array.some((a) => other.some((o) => deepEquals(a, o))));
|
|
486
|
+
},
|
|
487
|
+
allMatch: (matches) => {
|
|
488
|
+
assertTrue(array.every(matches));
|
|
489
|
+
},
|
|
490
|
+
anyMatches: (matches) => {
|
|
491
|
+
assertTrue(array.some(matches));
|
|
492
|
+
},
|
|
493
|
+
allMatchAsync: async (matches) => {
|
|
494
|
+
for (const item of array) {
|
|
495
|
+
assertTrue(await matches(item));
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
};
|
|
499
|
+
};
|
|
193
500
|
var getCheckpoint = (message2) => {
|
|
194
|
-
return "checkpoint" in message2.metadata
|
|
195
|
-
isBigint(message2.metadata.checkpoint) ? (
|
|
501
|
+
return "checkpoint" in message2.metadata ? (
|
|
196
502
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
197
503
|
message2.metadata.checkpoint
|
|
198
504
|
) : "globalPosition" in message2.metadata && // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
@@ -205,9 +511,43 @@ var getCheckpoint = (message2) => {
|
|
|
205
511
|
message2.metadata.streamPosition
|
|
206
512
|
) : null;
|
|
207
513
|
};
|
|
514
|
+
var projection = (definition) => definition;
|
|
208
515
|
|
|
209
|
-
// src/eventStore/
|
|
210
|
-
|
|
516
|
+
// src/eventStore/projections/sqliteProjection.ts
|
|
517
|
+
var handleProjections = async (options) => {
|
|
518
|
+
const { projections: allProjections, events, connection } = options;
|
|
519
|
+
const eventTypes = events.map((e) => e.type);
|
|
520
|
+
for (const projection2 of allProjections) {
|
|
521
|
+
if (!projection2.canHandle.some((type) => eventTypes.includes(type))) {
|
|
522
|
+
continue;
|
|
523
|
+
}
|
|
524
|
+
await projection2.handle(events, {
|
|
525
|
+
connection
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
};
|
|
529
|
+
var sqliteProjection = (definition) => projection(definition);
|
|
530
|
+
var sqliteRawBatchSQLProjection = (handle, ...canHandle) => sqliteProjection({
|
|
531
|
+
canHandle,
|
|
532
|
+
handle: async (events, context) => {
|
|
533
|
+
const sqls = await handle(events, context);
|
|
534
|
+
for (const sql2 of sqls) await context.connection.command(sql2);
|
|
535
|
+
}
|
|
536
|
+
});
|
|
537
|
+
var sqliteRawSQLProjection = (handle, ...canHandle) => sqliteRawBatchSQLProjection(
|
|
538
|
+
async (events, context) => {
|
|
539
|
+
const sqls = [];
|
|
540
|
+
for (const event of events) {
|
|
541
|
+
sqls.push(await handle(event, context));
|
|
542
|
+
}
|
|
543
|
+
return sqls;
|
|
544
|
+
},
|
|
545
|
+
...canHandle
|
|
546
|
+
);
|
|
547
|
+
|
|
548
|
+
// src/eventStore/projections/sqliteProjectionSpec.ts
|
|
549
|
+
import "@event-driven-io/dumbo";
|
|
550
|
+
import { v4 as uuid5 } from "uuid";
|
|
211
551
|
|
|
212
552
|
// src/eventStore/schema/typing.ts
|
|
213
553
|
var emmettPrefix = "emt";
|
|
@@ -240,7 +580,196 @@ var subscriptionsTable = {
|
|
|
240
580
|
name: `${emmettPrefix}_subscriptions`
|
|
241
581
|
};
|
|
242
582
|
|
|
583
|
+
// src/eventStore/schema/tables.ts
|
|
584
|
+
var sql = (sql2) => sql2;
|
|
585
|
+
var streamsTableSQL = sql(
|
|
586
|
+
`CREATE TABLE IF NOT EXISTS ${streamsTable.name}(
|
|
587
|
+
stream_id TEXT NOT NULL,
|
|
588
|
+
stream_position BIGINT NOT NULL DEFAULT 0,
|
|
589
|
+
partition TEXT NOT NULL DEFAULT '${globalTag}',
|
|
590
|
+
stream_type TEXT NOT NULL,
|
|
591
|
+
stream_metadata JSONB NOT NULL,
|
|
592
|
+
is_archived BOOLEAN NOT NULL DEFAULT FALSE,
|
|
593
|
+
PRIMARY KEY (stream_id, partition, is_archived),
|
|
594
|
+
UNIQUE (stream_id, partition, is_archived)
|
|
595
|
+
);`
|
|
596
|
+
);
|
|
597
|
+
var messagesTableSQL = sql(
|
|
598
|
+
`CREATE TABLE IF NOT EXISTS ${messagesTable.name}(
|
|
599
|
+
stream_id TEXT NOT NULL,
|
|
600
|
+
stream_position BIGINT NOT NULL,
|
|
601
|
+
partition TEXT NOT NULL DEFAULT '${globalTag}',
|
|
602
|
+
message_kind CHAR(1) NOT NULL DEFAULT 'E',
|
|
603
|
+
message_data JSONB NOT NULL,
|
|
604
|
+
message_metadata JSONB NOT NULL,
|
|
605
|
+
message_schema_version TEXT NOT NULL,
|
|
606
|
+
message_type TEXT NOT NULL,
|
|
607
|
+
message_id TEXT NOT NULL,
|
|
608
|
+
is_archived BOOLEAN NOT NULL DEFAULT FALSE,
|
|
609
|
+
global_position INTEGER PRIMARY KEY,
|
|
610
|
+
created DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
611
|
+
UNIQUE (stream_id, stream_position, partition, is_archived)
|
|
612
|
+
);
|
|
613
|
+
`
|
|
614
|
+
);
|
|
615
|
+
var subscriptionsTableSQL = sql(
|
|
616
|
+
`
|
|
617
|
+
CREATE TABLE IF NOT EXISTS ${subscriptionsTable.name}(
|
|
618
|
+
subscription_id TEXT NOT NULL,
|
|
619
|
+
version INTEGER NOT NULL DEFAULT 1,
|
|
620
|
+
partition TEXT NOT NULL DEFAULT '${globalTag}',
|
|
621
|
+
last_processed_position BIGINT NOT NULL,
|
|
622
|
+
PRIMARY KEY (subscription_id, partition, version)
|
|
623
|
+
);
|
|
624
|
+
`
|
|
625
|
+
);
|
|
626
|
+
var schemaSQL = [
|
|
627
|
+
streamsTableSQL,
|
|
628
|
+
messagesTableSQL,
|
|
629
|
+
subscriptionsTableSQL
|
|
630
|
+
];
|
|
631
|
+
var createEventStoreSchema = async (db) => {
|
|
632
|
+
for (const sql2 of schemaSQL) {
|
|
633
|
+
await db.command(sql2);
|
|
634
|
+
}
|
|
635
|
+
};
|
|
636
|
+
|
|
637
|
+
// src/eventStore/schema/utils.ts
|
|
638
|
+
var singleOrNull = async (getResult) => {
|
|
639
|
+
const result = await getResult;
|
|
640
|
+
if (result.length > 1) throw new Error("Query had more than one result");
|
|
641
|
+
return result.length > 0 ? result[0] ?? null : null;
|
|
642
|
+
};
|
|
643
|
+
|
|
644
|
+
// src/eventStore/schema/readLastMessageGlobalPosition.ts
|
|
645
|
+
var readLastMessageGlobalPosition = async (db, options) => {
|
|
646
|
+
const result = await singleOrNull(
|
|
647
|
+
db.query(
|
|
648
|
+
sql(
|
|
649
|
+
`SELECT global_position
|
|
650
|
+
FROM ${messagesTable.name}
|
|
651
|
+
WHERE partition = ? AND is_archived = FALSE
|
|
652
|
+
ORDER BY global_position
|
|
653
|
+
LIMIT 1`
|
|
654
|
+
),
|
|
655
|
+
[options?.partition ?? defaultTag]
|
|
656
|
+
)
|
|
657
|
+
);
|
|
658
|
+
return {
|
|
659
|
+
currentGlobalPosition: result !== null ? BigInt(result.global_position) : null
|
|
660
|
+
};
|
|
661
|
+
};
|
|
662
|
+
|
|
663
|
+
// src/eventStore/schema/readMessagesBatch.ts
|
|
664
|
+
var readMessagesBatch = async (db, options) => {
|
|
665
|
+
const from = "from" in options ? options.from : "after" in options ? options.after + 1n : 0n;
|
|
666
|
+
const batchSize = options && "batchSize" in options ? options.batchSize : options.to - options.from;
|
|
667
|
+
const fromCondition = from !== -0n ? `AND global_position >= ${from}` : "";
|
|
668
|
+
const toCondition = "to" in options ? `AND global_position <= ${options.to}` : "";
|
|
669
|
+
const limitCondition = "batchSize" in options ? `LIMIT ${options.batchSize}` : "";
|
|
670
|
+
const events = (await db.query(
|
|
671
|
+
sql(
|
|
672
|
+
`SELECT stream_id, stream_position, global_position, message_data, message_metadata, message_schema_version, message_type, message_id
|
|
673
|
+
FROM ${messagesTable.name}
|
|
674
|
+
WHERE partition = ? AND is_archived = FALSE ${fromCondition} ${toCondition}
|
|
675
|
+
ORDER BY global_position
|
|
676
|
+
${limitCondition}`
|
|
677
|
+
),
|
|
678
|
+
[options?.partition ?? defaultTag]
|
|
679
|
+
)).map((row) => {
|
|
680
|
+
const rawEvent = {
|
|
681
|
+
type: row.message_type,
|
|
682
|
+
data: JSONParser.parse(row.message_data),
|
|
683
|
+
metadata: JSONParser.parse(row.message_metadata)
|
|
684
|
+
};
|
|
685
|
+
const metadata = {
|
|
686
|
+
..."metadata" in rawEvent ? rawEvent.metadata ?? {} : {},
|
|
687
|
+
messageId: row.message_id,
|
|
688
|
+
streamName: row.stream_id,
|
|
689
|
+
streamPosition: BigInt(row.stream_position),
|
|
690
|
+
globalPosition: BigInt(row.global_position)
|
|
691
|
+
};
|
|
692
|
+
return {
|
|
693
|
+
...rawEvent,
|
|
694
|
+
kind: "Event",
|
|
695
|
+
metadata
|
|
696
|
+
};
|
|
697
|
+
});
|
|
698
|
+
return events.length > 0 ? {
|
|
699
|
+
currentGlobalPosition: events[events.length - 1].metadata.globalPosition,
|
|
700
|
+
messages: events,
|
|
701
|
+
areEventsLeft: events.length === batchSize
|
|
702
|
+
} : {
|
|
703
|
+
currentGlobalPosition: "from" in options ? options.from : "after" in options ? options.after : 0n,
|
|
704
|
+
messages: [],
|
|
705
|
+
areEventsLeft: false
|
|
706
|
+
};
|
|
707
|
+
};
|
|
708
|
+
|
|
709
|
+
// src/eventStore/consumers/messageBatchProcessing/index.ts
|
|
710
|
+
var DefaultSQLiteEventStoreProcessorBatchSize = 100;
|
|
711
|
+
var DefaultSQLiteEventStoreProcessorPullingFrequencyInMs = 50;
|
|
712
|
+
var sqliteEventStoreMessageBatchPuller = ({
|
|
713
|
+
connection,
|
|
714
|
+
batchSize,
|
|
715
|
+
eachBatch,
|
|
716
|
+
pullingFrequencyInMs
|
|
717
|
+
}) => {
|
|
718
|
+
let isRunning = false;
|
|
719
|
+
let start;
|
|
720
|
+
const pullMessages = async (options) => {
|
|
721
|
+
const after = options.startFrom === "BEGINNING" ? 0n : options.startFrom === "END" ? (await readLastMessageGlobalPosition(connection)).currentGlobalPosition ?? 0n : options.startFrom.globalPosition;
|
|
722
|
+
const readMessagesOptions = {
|
|
723
|
+
after,
|
|
724
|
+
batchSize
|
|
725
|
+
};
|
|
726
|
+
let waitTime = 100;
|
|
727
|
+
do {
|
|
728
|
+
const { messages, currentGlobalPosition, areEventsLeft } = await readMessagesBatch(connection, readMessagesOptions);
|
|
729
|
+
if (messages.length > 0) {
|
|
730
|
+
const result = await eachBatch({ messages });
|
|
731
|
+
if (result && result.type === "STOP") {
|
|
732
|
+
isRunning = false;
|
|
733
|
+
break;
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
readMessagesOptions.after = currentGlobalPosition;
|
|
737
|
+
await new Promise((resolve) => setTimeout(resolve, waitTime));
|
|
738
|
+
if (!areEventsLeft) {
|
|
739
|
+
waitTime = Math.min(waitTime * 2, 1e3);
|
|
740
|
+
} else {
|
|
741
|
+
waitTime = pullingFrequencyInMs;
|
|
742
|
+
}
|
|
743
|
+
} while (isRunning);
|
|
744
|
+
};
|
|
745
|
+
return {
|
|
746
|
+
get isRunning() {
|
|
747
|
+
return isRunning;
|
|
748
|
+
},
|
|
749
|
+
start: (options) => {
|
|
750
|
+
if (isRunning) return start;
|
|
751
|
+
start = (async () => {
|
|
752
|
+
isRunning = true;
|
|
753
|
+
return pullMessages(options);
|
|
754
|
+
})();
|
|
755
|
+
return start;
|
|
756
|
+
},
|
|
757
|
+
stop: async () => {
|
|
758
|
+
if (!isRunning) return;
|
|
759
|
+
isRunning = false;
|
|
760
|
+
await start;
|
|
761
|
+
}
|
|
762
|
+
};
|
|
763
|
+
};
|
|
764
|
+
var zipSQLiteEventStoreMessageBatchPullerStartFrom = (options) => {
|
|
765
|
+
if (options.length === 0 || options.some((o) => o === void 0 || o === "BEGINNING"))
|
|
766
|
+
return "BEGINNING";
|
|
767
|
+
if (options.every((o) => o === "END")) return "END";
|
|
768
|
+
return options.filter((o) => o !== void 0 && o !== "BEGINNING" && o !== "END").sort((a, b) => a > b ? 1 : -1)[0];
|
|
769
|
+
};
|
|
770
|
+
|
|
243
771
|
// src/eventStore/schema/appendToStream.ts
|
|
772
|
+
import { v4 as uuid4 } from "uuid";
|
|
244
773
|
var appendToStream = async (connection, streamName, streamType, messages, options) => {
|
|
245
774
|
if (messages.length === 0) return { success: false };
|
|
246
775
|
const expectedStreamVersion = toExpectedVersion(
|
|
@@ -432,103 +961,39 @@ var buildMessageInsertQuery = (messages, expectedStreamVersion, streamId, partit
|
|
|
432
961
|
return { sqlString, values: query.values };
|
|
433
962
|
};
|
|
434
963
|
|
|
435
|
-
// src/eventStore/schema/
|
|
436
|
-
var
|
|
437
|
-
var streamsTableSQL = sql(
|
|
438
|
-
`CREATE TABLE IF NOT EXISTS ${streamsTable.name}(
|
|
439
|
-
stream_id TEXT NOT NULL,
|
|
440
|
-
stream_position BIGINT NOT NULL DEFAULT 0,
|
|
441
|
-
partition TEXT NOT NULL DEFAULT '${globalTag}',
|
|
442
|
-
stream_type TEXT NOT NULL,
|
|
443
|
-
stream_metadata JSONB NOT NULL,
|
|
444
|
-
is_archived BOOLEAN NOT NULL DEFAULT FALSE,
|
|
445
|
-
PRIMARY KEY (stream_id, partition, is_archived),
|
|
446
|
-
UNIQUE (stream_id, partition, is_archived)
|
|
447
|
-
);`
|
|
448
|
-
);
|
|
449
|
-
var messagesTableSQL = sql(
|
|
450
|
-
`CREATE TABLE IF NOT EXISTS ${messagesTable.name}(
|
|
451
|
-
stream_id TEXT NOT NULL,
|
|
452
|
-
stream_position BIGINT NOT NULL,
|
|
453
|
-
partition TEXT NOT NULL DEFAULT '${globalTag}',
|
|
454
|
-
message_kind CHAR(1) NOT NULL DEFAULT 'E',
|
|
455
|
-
message_data JSONB NOT NULL,
|
|
456
|
-
message_metadata JSONB NOT NULL,
|
|
457
|
-
message_schema_version TEXT NOT NULL,
|
|
458
|
-
message_type TEXT NOT NULL,
|
|
459
|
-
message_id TEXT NOT NULL,
|
|
460
|
-
is_archived BOOLEAN NOT NULL DEFAULT FALSE,
|
|
461
|
-
global_position INTEGER PRIMARY KEY,
|
|
462
|
-
created DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
463
|
-
UNIQUE (stream_id, stream_position, partition, is_archived)
|
|
464
|
-
);
|
|
465
|
-
`
|
|
466
|
-
);
|
|
467
|
-
var subscriptionsTableSQL = sql(
|
|
468
|
-
`
|
|
469
|
-
CREATE TABLE IF NOT EXISTS ${subscriptionsTable.name}(
|
|
470
|
-
subscription_id TEXT NOT NULL,
|
|
471
|
-
version INTEGER NOT NULL DEFAULT 1,
|
|
472
|
-
partition TEXT NOT NULL DEFAULT '${globalTag}',
|
|
473
|
-
last_processed_position BIGINT NOT NULL,
|
|
474
|
-
PRIMARY KEY (subscription_id, partition, version)
|
|
475
|
-
);
|
|
476
|
-
`
|
|
477
|
-
);
|
|
478
|
-
var schemaSQL = [
|
|
479
|
-
streamsTableSQL,
|
|
480
|
-
messagesTableSQL,
|
|
481
|
-
subscriptionsTableSQL
|
|
482
|
-
];
|
|
483
|
-
var createEventStoreSchema = async (db) => {
|
|
484
|
-
for (const sql2 of schemaSQL) {
|
|
485
|
-
await db.command(sql2);
|
|
486
|
-
}
|
|
487
|
-
};
|
|
488
|
-
|
|
489
|
-
// src/eventStore/schema/utils.ts
|
|
490
|
-
var singleOrNull = async (getResult) => {
|
|
491
|
-
const result = await getResult;
|
|
492
|
-
if (result.length > 1) throw new Error("Query had more than one result");
|
|
493
|
-
return result.length > 0 ? result[0] ?? null : null;
|
|
494
|
-
};
|
|
495
|
-
|
|
496
|
-
// src/eventStore/schema/readLastMessageGlobalPosition.ts
|
|
497
|
-
var readLastMessageGlobalPosition = async (db, options) => {
|
|
964
|
+
// src/eventStore/schema/readProcessorCheckpoint.ts
|
|
965
|
+
var readProcessorCheckpoint = async (db, options) => {
|
|
498
966
|
const result = await singleOrNull(
|
|
499
967
|
db.query(
|
|
500
968
|
sql(
|
|
501
|
-
`SELECT
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
LIMIT 1`
|
|
969
|
+
`SELECT last_processed_position
|
|
970
|
+
FROM ${subscriptionsTable.name}
|
|
971
|
+
WHERE partition = ? AND subscription_id = ?
|
|
972
|
+
LIMIT 1`
|
|
506
973
|
),
|
|
507
|
-
[options?.partition ?? defaultTag]
|
|
974
|
+
[options?.partition ?? defaultTag, options.processorId]
|
|
508
975
|
)
|
|
509
976
|
);
|
|
510
977
|
return {
|
|
511
|
-
|
|
978
|
+
lastProcessedPosition: result !== null ? BigInt(result.last_processed_position) : null
|
|
512
979
|
};
|
|
513
980
|
};
|
|
514
981
|
|
|
515
|
-
// src/eventStore/schema/
|
|
516
|
-
var
|
|
517
|
-
const
|
|
518
|
-
const
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
const
|
|
522
|
-
const
|
|
523
|
-
|
|
524
|
-
`SELECT stream_id, stream_position, global_position, message_data, message_metadata, message_schema_version, message_type, message_id
|
|
982
|
+
// src/eventStore/schema/readStream.ts
|
|
983
|
+
var readStream = async (db, streamId, options) => {
|
|
984
|
+
const fromCondition = options && "from" in options ? `AND stream_position >= ${options.from}` : "";
|
|
985
|
+
const to = Number(
|
|
986
|
+
options && "to" in options ? options.to : options && "maxCount" in options && options.maxCount ? options.from + options.maxCount : NaN
|
|
987
|
+
);
|
|
988
|
+
const toCondition = !isNaN(to) ? `AND stream_position <= ${to}` : "";
|
|
989
|
+
const results = await db.query(
|
|
990
|
+
`SELECT stream_id, stream_position, global_position, message_data, message_metadata, message_schema_version, message_type, message_id
|
|
525
991
|
FROM ${messagesTable.name}
|
|
526
|
-
WHERE partition = ? AND is_archived = FALSE ${fromCondition} ${toCondition}
|
|
527
|
-
ORDER BY
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
)).map((row) => {
|
|
992
|
+
WHERE stream_id = ? AND partition = ? AND is_archived = FALSE ${fromCondition} ${toCondition}
|
|
993
|
+
ORDER BY stream_position ASC`,
|
|
994
|
+
[streamId, options?.partition ?? defaultTag]
|
|
995
|
+
);
|
|
996
|
+
const messages = results.map((row) => {
|
|
532
997
|
const rawEvent = {
|
|
533
998
|
type: row.message_type,
|
|
534
999
|
data: JSONParser.parse(row.message_data),
|
|
@@ -537,7 +1002,7 @@ var readMessagesBatch = async (db, options) => {
|
|
|
537
1002
|
const metadata = {
|
|
538
1003
|
..."metadata" in rawEvent ? rawEvent.metadata ?? {} : {},
|
|
539
1004
|
messageId: row.message_id,
|
|
540
|
-
streamName:
|
|
1005
|
+
streamName: streamId,
|
|
541
1006
|
streamPosition: BigInt(row.stream_position),
|
|
542
1007
|
globalPosition: BigInt(row.global_position)
|
|
543
1008
|
};
|
|
@@ -547,96 +1012,95 @@ var readMessagesBatch = async (db, options) => {
|
|
|
547
1012
|
metadata
|
|
548
1013
|
};
|
|
549
1014
|
});
|
|
550
|
-
return
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
1015
|
+
return messages.length > 0 ? {
|
|
1016
|
+
currentStreamVersion: messages[messages.length - 1].metadata.streamPosition,
|
|
1017
|
+
events: messages,
|
|
1018
|
+
streamExists: true
|
|
554
1019
|
} : {
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
};
|
|
559
|
-
};
|
|
560
|
-
|
|
561
|
-
// src/eventStore/schema/
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
db.
|
|
565
|
-
sql(
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
const readMessagesOptions = {
|
|
593
|
-
after,
|
|
594
|
-
batchSize
|
|
595
|
-
};
|
|
596
|
-
let waitTime = 100;
|
|
597
|
-
do {
|
|
598
|
-
const { messages, currentGlobalPosition, areEventsLeft } = await readMessagesBatch(connection, readMessagesOptions);
|
|
599
|
-
if (messages.length > 0) {
|
|
600
|
-
const result = await eachBatch({ messages });
|
|
601
|
-
if (result && result.type === "STOP") {
|
|
602
|
-
isRunning = false;
|
|
603
|
-
break;
|
|
604
|
-
}
|
|
1020
|
+
currentStreamVersion: SQLiteEventStoreDefaultStreamVersion,
|
|
1021
|
+
events: [],
|
|
1022
|
+
streamExists: false
|
|
1023
|
+
};
|
|
1024
|
+
};
|
|
1025
|
+
|
|
1026
|
+
// src/eventStore/schema/storeProcessorCheckpoint.ts
|
|
1027
|
+
async function storeSubscriptionCheckpointSQLite(db, processorId, version, position, checkPosition, partition) {
|
|
1028
|
+
if (checkPosition !== null) {
|
|
1029
|
+
const updateResult = await db.command(
|
|
1030
|
+
sql(`
|
|
1031
|
+
UPDATE ${subscriptionsTable.name}
|
|
1032
|
+
SET last_processed_position = ?
|
|
1033
|
+
WHERE subscription_id = ?
|
|
1034
|
+
AND last_processed_position = ?
|
|
1035
|
+
AND partition = ?
|
|
1036
|
+
`),
|
|
1037
|
+
[position.toString(), processorId, checkPosition.toString(), partition]
|
|
1038
|
+
);
|
|
1039
|
+
if (updateResult.changes > 0) {
|
|
1040
|
+
return 1;
|
|
1041
|
+
} else {
|
|
1042
|
+
const current_position = await singleOrNull(
|
|
1043
|
+
db.query(
|
|
1044
|
+
sql(
|
|
1045
|
+
`SELECT last_processed_position FROM ${subscriptionsTable.name}
|
|
1046
|
+
WHERE subscription_id = ? AND partition = ?`
|
|
1047
|
+
),
|
|
1048
|
+
[processorId, partition]
|
|
1049
|
+
)
|
|
1050
|
+
);
|
|
1051
|
+
if (current_position?.last_processed_position === position) {
|
|
1052
|
+
return 0;
|
|
1053
|
+
} else if (position !== null && current_position !== null && current_position?.last_processed_position > position) {
|
|
1054
|
+
return 2;
|
|
1055
|
+
} else {
|
|
1056
|
+
return 2;
|
|
605
1057
|
}
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
1058
|
+
}
|
|
1059
|
+
} else {
|
|
1060
|
+
try {
|
|
1061
|
+
await db.command(
|
|
1062
|
+
sql(
|
|
1063
|
+
`INSERT INTO ${subscriptionsTable.name} (subscription_id, version, last_processed_position, partition) VALUES (?, ?, ?, ?)`
|
|
1064
|
+
),
|
|
1065
|
+
[processorId, version, position.toString(), partition]
|
|
1066
|
+
);
|
|
1067
|
+
return 1;
|
|
1068
|
+
} catch (err) {
|
|
1069
|
+
if (!(isSQLiteError(err) && (err.errno === 19 || err.errno === 2067))) {
|
|
1070
|
+
throw err;
|
|
1071
|
+
}
|
|
1072
|
+
const current = await singleOrNull(
|
|
1073
|
+
db.query(
|
|
1074
|
+
sql(
|
|
1075
|
+
`SELECT last_processed_position FROM ${subscriptionsTable.name} WHERE subscription_id = ? AND partition = ?`
|
|
1076
|
+
),
|
|
1077
|
+
[processorId, partition]
|
|
1078
|
+
)
|
|
1079
|
+
);
|
|
1080
|
+
if (current?.last_processed_position === position) {
|
|
1081
|
+
return 0;
|
|
610
1082
|
} else {
|
|
611
|
-
|
|
1083
|
+
return 2;
|
|
612
1084
|
}
|
|
613
|
-
} while (isRunning);
|
|
614
|
-
};
|
|
615
|
-
return {
|
|
616
|
-
get isRunning() {
|
|
617
|
-
return isRunning;
|
|
618
|
-
},
|
|
619
|
-
start: (options) => {
|
|
620
|
-
if (isRunning) return start;
|
|
621
|
-
start = (async () => {
|
|
622
|
-
isRunning = true;
|
|
623
|
-
return pullMessages(options);
|
|
624
|
-
})();
|
|
625
|
-
return start;
|
|
626
|
-
},
|
|
627
|
-
stop: async () => {
|
|
628
|
-
if (!isRunning) return;
|
|
629
|
-
isRunning = false;
|
|
630
|
-
await start;
|
|
631
1085
|
}
|
|
632
|
-
}
|
|
633
|
-
}
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
async function storeProcessorCheckpoint(db, options) {
|
|
1089
|
+
try {
|
|
1090
|
+
const result = await storeSubscriptionCheckpointSQLite(
|
|
1091
|
+
db,
|
|
1092
|
+
options.processorId,
|
|
1093
|
+
options.version ?? 1,
|
|
1094
|
+
options.newPosition,
|
|
1095
|
+
options.lastProcessedPosition,
|
|
1096
|
+
options.partition ?? defaultTag
|
|
1097
|
+
);
|
|
1098
|
+
return result === 1 ? { success: true, newPosition: options.newPosition } : { success: false, reason: result === 0 ? "IGNORED" : "MISMATCH" };
|
|
1099
|
+
} catch (error) {
|
|
1100
|
+
console.log(error);
|
|
1101
|
+
throw error;
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
640
1104
|
|
|
641
1105
|
// src/eventStore/consumers/sqliteProcessor.ts
|
|
642
1106
|
var genericSQLiteProcessor = (options) => {
|
|
@@ -806,20 +1270,6 @@ var sqliteEventStoreConsumer = (options) => {
|
|
|
806
1270
|
};
|
|
807
1271
|
};
|
|
808
1272
|
|
|
809
|
-
// src/eventStore/projections/index.ts
|
|
810
|
-
var handleProjections = async (options) => {
|
|
811
|
-
const { projections: allProjections, events, connection } = options;
|
|
812
|
-
const eventTypes = events.map((e) => e.type);
|
|
813
|
-
for (const projection2 of allProjections) {
|
|
814
|
-
if (!projection2.canHandle.some((type) => eventTypes.includes(type))) {
|
|
815
|
-
continue;
|
|
816
|
-
}
|
|
817
|
-
await projection2.handle(events, {
|
|
818
|
-
connection
|
|
819
|
-
});
|
|
820
|
-
}
|
|
821
|
-
};
|
|
822
|
-
|
|
823
1273
|
// src/eventStore/SQLiteEventStore.ts
|
|
824
1274
|
var SQLiteEventStoreDefaultStreamVersion = 0n;
|
|
825
1275
|
var getSQLiteEventStore = (options) => {
|
|
@@ -942,142 +1392,141 @@ var getSQLiteEventStore = (options) => {
|
|
|
942
1392
|
};
|
|
943
1393
|
};
|
|
944
1394
|
|
|
945
|
-
// src/eventStore/
|
|
946
|
-
var
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
1395
|
+
// src/eventStore/projections/sqliteProjectionSpec.ts
|
|
1396
|
+
var SQLiteProjectionSpec = {
|
|
1397
|
+
for: (options) => {
|
|
1398
|
+
{
|
|
1399
|
+
const connection = options.connection;
|
|
1400
|
+
const projection2 = options.projection;
|
|
1401
|
+
return (givenEvents) => {
|
|
1402
|
+
return {
|
|
1403
|
+
when: (events, options2) => {
|
|
1404
|
+
const allEvents = [];
|
|
1405
|
+
const run = async (connection2) => {
|
|
1406
|
+
let globalPosition = 0n;
|
|
1407
|
+
const numberOfTimes = options2?.numberOfTimes ?? 1;
|
|
1408
|
+
for (const event of [
|
|
1409
|
+
...givenEvents,
|
|
1410
|
+
...Array.from({ length: numberOfTimes }).flatMap(() => events)
|
|
1411
|
+
]) {
|
|
1412
|
+
const metadata = {
|
|
1413
|
+
globalPosition: ++globalPosition,
|
|
1414
|
+
streamPosition: globalPosition,
|
|
1415
|
+
streamName: `test-${uuid5()}`,
|
|
1416
|
+
messageId: uuid5()
|
|
1417
|
+
};
|
|
1418
|
+
allEvents.push({
|
|
1419
|
+
...event,
|
|
1420
|
+
kind: "Event",
|
|
1421
|
+
metadata: {
|
|
1422
|
+
...metadata,
|
|
1423
|
+
..."metadata" in event ? event.metadata ?? {} : {}
|
|
1424
|
+
}
|
|
1425
|
+
});
|
|
1426
|
+
}
|
|
1427
|
+
await connection2.withTransaction(
|
|
1428
|
+
() => handleProjections({
|
|
1429
|
+
events: allEvents,
|
|
1430
|
+
projections: [projection2],
|
|
1431
|
+
connection: connection2
|
|
1432
|
+
})
|
|
1433
|
+
);
|
|
1434
|
+
};
|
|
1435
|
+
return {
|
|
1436
|
+
then: async (assert, message) => {
|
|
1437
|
+
try {
|
|
1438
|
+
await run(connection);
|
|
1439
|
+
const succeeded = await assert({
|
|
1440
|
+
connection
|
|
1441
|
+
});
|
|
1442
|
+
if (succeeded !== void 0 && succeeded === false)
|
|
1443
|
+
assertFails(
|
|
1444
|
+
message ?? "Projection specification didn't match the criteria"
|
|
1445
|
+
);
|
|
1446
|
+
} finally {
|
|
1447
|
+
connection.close();
|
|
1448
|
+
}
|
|
1449
|
+
},
|
|
1450
|
+
thenThrows: async (...args) => {
|
|
1451
|
+
try {
|
|
1452
|
+
await run(connection);
|
|
1453
|
+
throw new AssertionError("Handler did not fail as expected");
|
|
1454
|
+
} catch (error) {
|
|
1455
|
+
if (error instanceof AssertionError) throw error;
|
|
1456
|
+
if (args.length === 0) return;
|
|
1457
|
+
if (!isErrorConstructor(args[0])) {
|
|
1458
|
+
assertTrue(
|
|
1459
|
+
args[0](error),
|
|
1460
|
+
`Error didn't match the error condition: ${error?.toString()}`
|
|
1461
|
+
);
|
|
1462
|
+
return;
|
|
1463
|
+
}
|
|
1464
|
+
assertTrue(
|
|
1465
|
+
error instanceof args[0],
|
|
1466
|
+
`Caught error is not an instance of the expected type: ${error?.toString()}`
|
|
1467
|
+
);
|
|
1468
|
+
if (args[1]) {
|
|
1469
|
+
assertTrue(
|
|
1470
|
+
args[1](error),
|
|
1471
|
+
`Error didn't match the error condition: ${error?.toString()}`
|
|
1472
|
+
);
|
|
1473
|
+
}
|
|
1474
|
+
} finally {
|
|
1475
|
+
connection.close();
|
|
1476
|
+
}
|
|
1477
|
+
}
|
|
1478
|
+
};
|
|
1479
|
+
}
|
|
1480
|
+
};
|
|
1481
|
+
};
|
|
1482
|
+
}
|
|
1483
|
+
}
|
|
987
1484
|
};
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
UPDATE ${subscriptionsTable.name}
|
|
995
|
-
SET last_processed_position = ?
|
|
996
|
-
WHERE subscription_id = ?
|
|
997
|
-
AND last_processed_position = ?
|
|
998
|
-
AND partition = ?
|
|
999
|
-
`),
|
|
1000
|
-
[position.toString(), processorId, checkPosition.toString(), partition]
|
|
1001
|
-
);
|
|
1002
|
-
if (updateResult.changes > 0) {
|
|
1003
|
-
return 1;
|
|
1004
|
-
} else {
|
|
1005
|
-
const current_position = await singleOrNull(
|
|
1006
|
-
db.query(
|
|
1007
|
-
sql(
|
|
1008
|
-
`SELECT last_processed_position FROM ${subscriptionsTable.name}
|
|
1009
|
-
WHERE subscription_id = ? AND partition = ?`
|
|
1010
|
-
),
|
|
1011
|
-
[processorId, partition]
|
|
1012
|
-
)
|
|
1013
|
-
);
|
|
1014
|
-
if (current_position?.last_processed_position === position) {
|
|
1015
|
-
return 0;
|
|
1016
|
-
} else if (position !== null && current_position !== null && current_position?.last_processed_position > position) {
|
|
1017
|
-
return 2;
|
|
1018
|
-
} else {
|
|
1019
|
-
return 2;
|
|
1020
|
-
}
|
|
1485
|
+
var eventInStream = (streamName, event) => {
|
|
1486
|
+
return {
|
|
1487
|
+
...event,
|
|
1488
|
+
metadata: {
|
|
1489
|
+
...event.metadata ?? {},
|
|
1490
|
+
streamName: event.metadata?.streamName ?? streamName
|
|
1021
1491
|
}
|
|
1022
|
-
}
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
db.query(
|
|
1037
|
-
sql(
|
|
1038
|
-
`SELECT last_processed_position FROM ${subscriptionsTable.name} WHERE subscription_id = ? AND partition = ?`
|
|
1039
|
-
),
|
|
1040
|
-
[processorId, partition]
|
|
1041
|
-
)
|
|
1042
|
-
);
|
|
1043
|
-
if (current?.last_processed_position === position) {
|
|
1044
|
-
return 0;
|
|
1045
|
-
} else {
|
|
1046
|
-
return 2;
|
|
1047
|
-
}
|
|
1492
|
+
};
|
|
1493
|
+
};
|
|
1494
|
+
var eventsInStream = (streamName, events) => {
|
|
1495
|
+
return events.map((e) => eventInStream(streamName, e));
|
|
1496
|
+
};
|
|
1497
|
+
var newEventsInStream = eventsInStream;
|
|
1498
|
+
var assertSQLQueryResultMatches = (sql2, rows) => async ({ connection }) => {
|
|
1499
|
+
const result = await connection.query(sql2);
|
|
1500
|
+
assertThatArray(rows).containsExactlyInAnyOrder(result);
|
|
1501
|
+
};
|
|
1502
|
+
var expectSQL = {
|
|
1503
|
+
query: (sql2) => ({
|
|
1504
|
+
resultRows: {
|
|
1505
|
+
toBeTheSame: (rows) => assertSQLQueryResultMatches(sql2, rows)
|
|
1048
1506
|
}
|
|
1049
|
-
}
|
|
1050
|
-
}
|
|
1051
|
-
async function storeProcessorCheckpoint(db, options) {
|
|
1052
|
-
try {
|
|
1053
|
-
const result = await storeSubscriptionCheckpointSQLite(
|
|
1054
|
-
db,
|
|
1055
|
-
options.processorId,
|
|
1056
|
-
options.version ?? 1,
|
|
1057
|
-
options.newPosition,
|
|
1058
|
-
options.lastProcessedPosition,
|
|
1059
|
-
options.partition ?? defaultTag
|
|
1060
|
-
);
|
|
1061
|
-
return result === 1 ? { success: true, newPosition: options.newPosition } : { success: false, reason: result === 0 ? "IGNORED" : "MISMATCH" };
|
|
1062
|
-
} catch (error) {
|
|
1063
|
-
console.log(error);
|
|
1064
|
-
throw error;
|
|
1065
|
-
}
|
|
1066
|
-
}
|
|
1507
|
+
})
|
|
1508
|
+
};
|
|
1067
1509
|
export {
|
|
1068
1510
|
InMemorySQLiteDatabase,
|
|
1069
1511
|
InMemorySharedCacheSQLiteDatabase,
|
|
1070
1512
|
SQLiteEventStoreDefaultStreamVersion,
|
|
1513
|
+
SQLiteProjectionSpec,
|
|
1071
1514
|
appendToStream,
|
|
1515
|
+
assertSQLQueryResultMatches,
|
|
1072
1516
|
createEventStoreSchema,
|
|
1073
1517
|
defaultTag,
|
|
1074
1518
|
emmettPrefix,
|
|
1519
|
+
eventInStream,
|
|
1520
|
+
eventsInStream,
|
|
1521
|
+
expectSQL,
|
|
1075
1522
|
getSQLiteEventStore,
|
|
1076
1523
|
globalNames,
|
|
1077
1524
|
globalTag,
|
|
1525
|
+
handleProjections,
|
|
1078
1526
|
isSQLiteError,
|
|
1079
1527
|
messagesTable,
|
|
1080
1528
|
messagesTableSQL,
|
|
1529
|
+
newEventsInStream,
|
|
1081
1530
|
readLastMessageGlobalPosition,
|
|
1082
1531
|
readMessagesBatch,
|
|
1083
1532
|
readProcessorCheckpoint,
|
|
@@ -1085,6 +1534,9 @@ export {
|
|
|
1085
1534
|
schemaSQL,
|
|
1086
1535
|
sql,
|
|
1087
1536
|
sqliteConnection,
|
|
1537
|
+
sqliteProjection,
|
|
1538
|
+
sqliteRawBatchSQLProjection,
|
|
1539
|
+
sqliteRawSQLProjection,
|
|
1088
1540
|
storeProcessorCheckpoint,
|
|
1089
1541
|
streamsTable,
|
|
1090
1542
|
streamsTableSQL,
|