@event-driven-io/emmett-sqlite 0.36.0 → 0.38.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +106 -90
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +8 -8
- package/dist/index.d.ts +8 -8
- package/dist/index.js +80 -64
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -130,6 +130,7 @@ var _webstreamspolyfill = require('web-streams-polyfill');
|
|
|
130
130
|
|
|
131
131
|
|
|
132
132
|
|
|
133
|
+
|
|
133
134
|
var _asyncretry = require('async-retry'); var _asyncretry2 = _interopRequireDefault(_asyncretry);
|
|
134
135
|
|
|
135
136
|
|
|
@@ -203,22 +204,6 @@ var NotifyAboutNoActiveReadersStream = (_class = class extends _webstreamspolyfi
|
|
|
203
204
|
}
|
|
204
205
|
}
|
|
205
206
|
}, _class);
|
|
206
|
-
var asyncRetry = async (fn, opts) => {
|
|
207
|
-
if (opts === void 0 || opts.retries === 0) return fn();
|
|
208
|
-
return _asyncretry2.default.call(void 0,
|
|
209
|
-
async (bail) => {
|
|
210
|
-
try {
|
|
211
|
-
return await fn();
|
|
212
|
-
} catch (error2) {
|
|
213
|
-
if (_optionalChain([opts, 'optionalAccess', _10 => _10.shouldRetryError]) && !opts.shouldRetryError(error2)) {
|
|
214
|
-
bail(error2);
|
|
215
|
-
}
|
|
216
|
-
throw error2;
|
|
217
|
-
}
|
|
218
|
-
},
|
|
219
|
-
_nullishCoalesce(opts, () => ( { retries: 0 }))
|
|
220
|
-
);
|
|
221
|
-
};
|
|
222
207
|
var ParseError = class extends Error {
|
|
223
208
|
constructor(text) {
|
|
224
209
|
super(`Cannot parse! ${text}`);
|
|
@@ -227,19 +212,41 @@ var ParseError = class extends Error {
|
|
|
227
212
|
var JSONParser = {
|
|
228
213
|
stringify: (value, options) => {
|
|
229
214
|
return JSON.stringify(
|
|
230
|
-
_optionalChain([options, 'optionalAccess',
|
|
215
|
+
_optionalChain([options, 'optionalAccess', _10 => _10.map]) ? options.map(value) : value,
|
|
231
216
|
//TODO: Consider adding support to DateTime and adding specific format to mark that's a bigint
|
|
232
217
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
233
218
|
(_, v) => typeof v === "bigint" ? v.toString() : v
|
|
234
219
|
);
|
|
235
220
|
},
|
|
236
221
|
parse: (text, options) => {
|
|
237
|
-
const parsed = JSON.parse(text, _optionalChain([options, 'optionalAccess',
|
|
238
|
-
if (_optionalChain([options, 'optionalAccess',
|
|
222
|
+
const parsed = JSON.parse(text, _optionalChain([options, 'optionalAccess', _11 => _11.reviver]));
|
|
223
|
+
if (_optionalChain([options, 'optionalAccess', _12 => _12.typeCheck]) && !_optionalChain([options, 'optionalAccess', _13 => _13.typeCheck, 'call', _14 => _14(parsed)]))
|
|
239
224
|
throw new ParseError(text);
|
|
240
|
-
return _optionalChain([options, 'optionalAccess',
|
|
225
|
+
return _optionalChain([options, 'optionalAccess', _15 => _15.map]) ? options.map(parsed) : parsed;
|
|
241
226
|
}
|
|
242
227
|
};
|
|
228
|
+
var asyncRetry = async (fn, opts) => {
|
|
229
|
+
if (opts === void 0 || opts.retries === 0) return fn();
|
|
230
|
+
return _asyncretry2.default.call(void 0,
|
|
231
|
+
async (bail) => {
|
|
232
|
+
try {
|
|
233
|
+
const result = await fn();
|
|
234
|
+
if (_optionalChain([opts, 'optionalAccess', _16 => _16.shouldRetryResult]) && opts.shouldRetryResult(result)) {
|
|
235
|
+
throw new EmmettError(
|
|
236
|
+
`Retrying because of result: ${JSONParser.stringify(result)}`
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
return result;
|
|
240
|
+
} catch (error2) {
|
|
241
|
+
if (_optionalChain([opts, 'optionalAccess', _17 => _17.shouldRetryError]) && !opts.shouldRetryError(error2)) {
|
|
242
|
+
bail(error2);
|
|
243
|
+
}
|
|
244
|
+
throw error2;
|
|
245
|
+
}
|
|
246
|
+
},
|
|
247
|
+
_nullishCoalesce(opts, () => ( { retries: 0 }))
|
|
248
|
+
);
|
|
249
|
+
};
|
|
243
250
|
var filter = (filter2) => new (0, _webstreamspolyfill.TransformStream)({
|
|
244
251
|
transform(chunk, controller) {
|
|
245
252
|
if (filter2(chunk)) {
|
|
@@ -417,10 +424,10 @@ var subscriptionsTable = {
|
|
|
417
424
|
};
|
|
418
425
|
|
|
419
426
|
// src/eventStore/schema/appendToStream.ts
|
|
420
|
-
var appendToStream = async (
|
|
427
|
+
var appendToStream = async (connection, streamName, streamType, messages, options) => {
|
|
421
428
|
if (messages.length === 0) return { success: false };
|
|
422
429
|
const expectedStreamVersion = toExpectedVersion(
|
|
423
|
-
_optionalChain([options, 'optionalAccess',
|
|
430
|
+
_optionalChain([options, 'optionalAccess', _18 => _18.expectedStreamVersion])
|
|
424
431
|
);
|
|
425
432
|
const messagesToAppend = messages.map(
|
|
426
433
|
(m, i) => ({
|
|
@@ -435,9 +442,9 @@ var appendToStream = async (db, streamName, streamType, messages, options) => {
|
|
|
435
442
|
})
|
|
436
443
|
);
|
|
437
444
|
let result;
|
|
438
|
-
return await
|
|
445
|
+
return await connection.withTransaction(async () => {
|
|
439
446
|
result = await appendToStreamRaw(
|
|
440
|
-
|
|
447
|
+
connection,
|
|
441
448
|
streamName,
|
|
442
449
|
streamType,
|
|
443
450
|
messagesToAppend,
|
|
@@ -445,8 +452,8 @@ var appendToStream = async (db, streamName, streamType, messages, options) => {
|
|
|
445
452
|
expectedStreamVersion
|
|
446
453
|
}
|
|
447
454
|
);
|
|
448
|
-
if (_optionalChain([options, 'optionalAccess',
|
|
449
|
-
await options.onBeforeCommit(messagesToAppend, {
|
|
455
|
+
if (_optionalChain([options, 'optionalAccess', _19 => _19.onBeforeCommit]))
|
|
456
|
+
await options.onBeforeCommit(messagesToAppend, { connection });
|
|
450
457
|
return result;
|
|
451
458
|
});
|
|
452
459
|
};
|
|
@@ -457,21 +464,21 @@ var toExpectedVersion = (expected) => {
|
|
|
457
464
|
if (expected == STREAM_EXISTS) return null;
|
|
458
465
|
return expected;
|
|
459
466
|
};
|
|
460
|
-
var appendToStreamRaw = async (
|
|
467
|
+
var appendToStreamRaw = async (connection, streamId, streamType, messages, options) => {
|
|
461
468
|
let streamPosition;
|
|
462
469
|
let globalPosition;
|
|
463
470
|
try {
|
|
464
|
-
let expectedStreamVersion = _nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
471
|
+
let expectedStreamVersion = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _20 => _20.expectedStreamVersion]), () => ( null));
|
|
465
472
|
if (expectedStreamVersion == null) {
|
|
466
473
|
expectedStreamVersion = await getLastStreamPosition(
|
|
467
|
-
|
|
474
|
+
connection,
|
|
468
475
|
streamId,
|
|
469
476
|
expectedStreamVersion
|
|
470
477
|
);
|
|
471
478
|
}
|
|
472
479
|
let position;
|
|
473
480
|
if (expectedStreamVersion === 0n) {
|
|
474
|
-
position = await
|
|
481
|
+
position = await connection.querySingle(
|
|
475
482
|
`INSERT INTO ${streamsTable.name}
|
|
476
483
|
(stream_id, stream_position, partition, stream_type, stream_metadata, is_archived)
|
|
477
484
|
VALUES (
|
|
@@ -487,12 +494,12 @@ var appendToStreamRaw = async (db, streamId, streamType, messages, options) => {
|
|
|
487
494
|
[
|
|
488
495
|
streamId,
|
|
489
496
|
messages.length,
|
|
490
|
-
_nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
497
|
+
_nullishCoalesce(_optionalChain([options, 'optionalAccess', _21 => _21.partition]), () => ( streamsTable.columns.partition)),
|
|
491
498
|
streamType
|
|
492
499
|
]
|
|
493
500
|
);
|
|
494
501
|
} else {
|
|
495
|
-
position = await
|
|
502
|
+
position = await connection.querySingle(
|
|
496
503
|
`UPDATE ${streamsTable.name}
|
|
497
504
|
SET stream_position = stream_position + ?
|
|
498
505
|
WHERE stream_id = ?
|
|
@@ -503,7 +510,7 @@ var appendToStreamRaw = async (db, streamId, streamType, messages, options) => {
|
|
|
503
510
|
[
|
|
504
511
|
messages.length,
|
|
505
512
|
streamId,
|
|
506
|
-
_nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
513
|
+
_nullishCoalesce(_optionalChain([options, 'optionalAccess', _22 => _22.partition]), () => ( streamsTable.columns.partition))
|
|
507
514
|
]
|
|
508
515
|
);
|
|
509
516
|
}
|
|
@@ -523,10 +530,10 @@ var appendToStreamRaw = async (db, streamId, streamType, messages, options) => {
|
|
|
523
530
|
messages,
|
|
524
531
|
expectedStreamVersion,
|
|
525
532
|
streamId,
|
|
526
|
-
_nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
533
|
+
_nullishCoalesce(_optionalChain([options, 'optionalAccess', _23 => _23.partition, 'optionalAccess', _24 => _24.toString, 'call', _25 => _25()]), () => ( defaultTag))
|
|
527
534
|
);
|
|
528
|
-
const returningIds = await
|
|
529
|
-
if (returningIds.length === 0 || !_optionalChain([returningIds, 'access',
|
|
535
|
+
const returningIds = await connection.query(sqlString, values);
|
|
536
|
+
if (returningIds.length === 0 || !_optionalChain([returningIds, 'access', _26 => _26[returningIds.length - 1], 'optionalAccess', _27 => _27.global_position])) {
|
|
530
537
|
throw new Error("Could not find global position");
|
|
531
538
|
}
|
|
532
539
|
globalPosition = BigInt(
|
|
@@ -547,14 +554,14 @@ var appendToStreamRaw = async (db, streamId, streamType, messages, options) => {
|
|
|
547
554
|
};
|
|
548
555
|
};
|
|
549
556
|
var isOptimisticConcurrencyError = (error) => {
|
|
550
|
-
return _optionalChain([error, 'optionalAccess',
|
|
557
|
+
return _optionalChain([error, 'optionalAccess', _28 => _28.errno]) !== void 0 && error.errno === 19;
|
|
551
558
|
};
|
|
552
|
-
async function getLastStreamPosition(
|
|
553
|
-
const result = await
|
|
559
|
+
async function getLastStreamPosition(connection, streamId, expectedStreamVersion) {
|
|
560
|
+
const result = await connection.querySingle(
|
|
554
561
|
`SELECT CAST(stream_position AS VARCHAR) AS stream_position FROM ${streamsTable.name} WHERE stream_id = ?`,
|
|
555
562
|
[streamId]
|
|
556
563
|
);
|
|
557
|
-
if (_optionalChain([result, 'optionalAccess',
|
|
564
|
+
if (_optionalChain([result, 'optionalAccess', _29 => _29.stream_position]) == null) {
|
|
558
565
|
expectedStreamVersion = 0n;
|
|
559
566
|
} else {
|
|
560
567
|
expectedStreamVersion = BigInt(result.stream_position);
|
|
@@ -564,7 +571,7 @@ async function getLastStreamPosition(db, streamId, expectedStreamVersion) {
|
|
|
564
571
|
var buildMessageInsertQuery = (messages, expectedStreamVersion, streamId, partition) => {
|
|
565
572
|
const query = messages.reduce(
|
|
566
573
|
(queryBuilder, message) => {
|
|
567
|
-
if (_optionalChain([message, 'access',
|
|
574
|
+
if (_optionalChain([message, 'access', _30 => _30.metadata, 'optionalAccess', _31 => _31.streamPosition]) == null || typeof message.metadata.streamPosition !== "bigint") {
|
|
568
575
|
throw new Error("Stream position is required");
|
|
569
576
|
}
|
|
570
577
|
const streamPosition = BigInt(message.metadata.streamPosition) + BigInt(expectedStreamVersion);
|
|
@@ -576,7 +583,7 @@ var buildMessageInsertQuery = (messages, expectedStreamVersion, streamId, partit
|
|
|
576
583
|
message.kind === "Event" ? "E" : "C",
|
|
577
584
|
JSONParser.stringify(message.data),
|
|
578
585
|
JSONParser.stringify(message.metadata),
|
|
579
|
-
_nullishCoalesce(_optionalChain([expectedStreamVersion, 'optionalAccess',
|
|
586
|
+
_nullishCoalesce(_optionalChain([expectedStreamVersion, 'optionalAccess', _32 => _32.toString, 'call', _33 => _33()]), () => ( 0)),
|
|
580
587
|
message.type,
|
|
581
588
|
message.metadata.messageId,
|
|
582
589
|
false
|
|
@@ -680,7 +687,7 @@ var readLastMessageGlobalPosition = async (db, options) => {
|
|
|
680
687
|
ORDER BY global_position
|
|
681
688
|
LIMIT 1`
|
|
682
689
|
),
|
|
683
|
-
[_nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
690
|
+
[_nullishCoalesce(_optionalChain([options, 'optionalAccess', _34 => _34.partition]), () => ( defaultTag))]
|
|
684
691
|
)
|
|
685
692
|
);
|
|
686
693
|
return {
|
|
@@ -703,7 +710,7 @@ var readMessagesBatch = async (db, options) => {
|
|
|
703
710
|
ORDER BY global_position
|
|
704
711
|
${limitCondition}`
|
|
705
712
|
),
|
|
706
|
-
[_nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
713
|
+
[_nullishCoalesce(_optionalChain([options, 'optionalAccess', _35 => _35.partition]), () => ( defaultTag))]
|
|
707
714
|
)).map((row) => {
|
|
708
715
|
const rawEvent = {
|
|
709
716
|
type: row.message_type,
|
|
@@ -744,7 +751,7 @@ var readProcessorCheckpoint = async (db, options) => {
|
|
|
744
751
|
WHERE partition = ? AND subscription_id = ?
|
|
745
752
|
LIMIT 1`
|
|
746
753
|
),
|
|
747
|
-
[_nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
754
|
+
[_nullishCoalesce(_optionalChain([options, 'optionalAccess', _36 => _36.partition]), () => ( defaultTag)), options.processorId]
|
|
748
755
|
)
|
|
749
756
|
);
|
|
750
757
|
return {
|
|
@@ -756,7 +763,7 @@ var readProcessorCheckpoint = async (db, options) => {
|
|
|
756
763
|
var DefaultSQLiteEventStoreProcessorBatchSize = 100;
|
|
757
764
|
var DefaultSQLiteEventStoreProcessorPullingFrequencyInMs = 50;
|
|
758
765
|
var sqliteEventStoreMessageBatchPuller = ({
|
|
759
|
-
|
|
766
|
+
connection,
|
|
760
767
|
batchSize,
|
|
761
768
|
eachBatch,
|
|
762
769
|
pullingFrequencyInMs
|
|
@@ -764,14 +771,14 @@ var sqliteEventStoreMessageBatchPuller = ({
|
|
|
764
771
|
let isRunning = false;
|
|
765
772
|
let start;
|
|
766
773
|
const pullMessages = async (options) => {
|
|
767
|
-
const after = options.startFrom === "BEGINNING" ? 0n : options.startFrom === "END" ? await _asyncNullishCoalesce((await readLastMessageGlobalPosition(
|
|
774
|
+
const after = options.startFrom === "BEGINNING" ? 0n : options.startFrom === "END" ? await _asyncNullishCoalesce((await readLastMessageGlobalPosition(connection)).currentGlobalPosition, async () => ( 0n)) : options.startFrom.globalPosition;
|
|
768
775
|
const readMessagesOptions = {
|
|
769
776
|
after,
|
|
770
777
|
batchSize
|
|
771
778
|
};
|
|
772
779
|
let waitTime = 100;
|
|
773
780
|
do {
|
|
774
|
-
const { messages, currentGlobalPosition, areEventsLeft } = await readMessagesBatch(
|
|
781
|
+
const { messages, currentGlobalPosition, areEventsLeft } = await readMessagesBatch(connection, readMessagesOptions);
|
|
775
782
|
if (messages.length > 0) {
|
|
776
783
|
const result = await eachBatch({ messages });
|
|
777
784
|
if (result && result.type === "STOP") {
|
|
@@ -819,23 +826,26 @@ var genericSQLiteProcessor = (options) => {
|
|
|
819
826
|
const { eachMessage } = options;
|
|
820
827
|
let isActive = true;
|
|
821
828
|
const getDb = (context) => {
|
|
822
|
-
const fileName = _nullishCoalesce(context.fileName, () => ( _optionalChain([options, 'access',
|
|
829
|
+
const fileName = _nullishCoalesce(context.fileName, () => ( _optionalChain([options, 'access', _37 => _37.connectionOptions, 'optionalAccess', _38 => _38.fileName])));
|
|
823
830
|
if (!fileName)
|
|
824
831
|
throw new EmmettError(
|
|
825
832
|
`SQLite processor '${options.processorId}' is missing file name. Ensure that you passed it through options`
|
|
826
833
|
);
|
|
827
|
-
const
|
|
828
|
-
return {
|
|
834
|
+
const connection = _nullishCoalesce(_nullishCoalesce(context.connection, () => ( _optionalChain([options, 'access', _39 => _39.connectionOptions, 'optionalAccess', _40 => _40.connection]))), () => ( sqliteConnection({ fileName })));
|
|
835
|
+
return { connection, fileName };
|
|
829
836
|
};
|
|
830
837
|
return {
|
|
831
838
|
id: options.processorId,
|
|
832
|
-
start: async (
|
|
839
|
+
start: async (connection) => {
|
|
833
840
|
isActive = true;
|
|
834
841
|
if (options.startFrom !== "CURRENT") return options.startFrom;
|
|
835
|
-
const { lastProcessedPosition } = await readProcessorCheckpoint(
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
842
|
+
const { lastProcessedPosition } = await readProcessorCheckpoint(
|
|
843
|
+
connection,
|
|
844
|
+
{
|
|
845
|
+
processorId: options.processorId,
|
|
846
|
+
partition: options.partition
|
|
847
|
+
}
|
|
848
|
+
);
|
|
839
849
|
if (lastProcessedPosition === null) return "BEGINNING";
|
|
840
850
|
return { globalPosition: lastProcessedPosition };
|
|
841
851
|
},
|
|
@@ -844,17 +854,17 @@ var genericSQLiteProcessor = (options) => {
|
|
|
844
854
|
},
|
|
845
855
|
handle: async ({ messages }, context) => {
|
|
846
856
|
if (!isActive) return;
|
|
847
|
-
const {
|
|
848
|
-
return
|
|
857
|
+
const { connection, fileName } = getDb(context);
|
|
858
|
+
return connection.withTransaction(async () => {
|
|
849
859
|
let result = void 0;
|
|
850
860
|
let lastProcessedPosition = null;
|
|
851
861
|
for (const message of messages) {
|
|
852
862
|
const typedMessage = message;
|
|
853
863
|
const messageProcessingResult = await eachMessage(typedMessage, {
|
|
854
|
-
|
|
864
|
+
connection,
|
|
855
865
|
fileName
|
|
856
866
|
});
|
|
857
|
-
await storeProcessorCheckpoint(
|
|
867
|
+
await storeProcessorCheckpoint(connection, {
|
|
858
868
|
processorId: options.processorId,
|
|
859
869
|
version: options.version,
|
|
860
870
|
lastProcessedPosition,
|
|
@@ -886,7 +896,7 @@ var sqliteProjectionProcessor = (options) => {
|
|
|
886
896
|
processorId: _nullishCoalesce(options.processorId, () => ( `projection:${projection2.name}`)),
|
|
887
897
|
eachMessage: async (event, context) => {
|
|
888
898
|
if (!projection2.canHandle.includes(event.type)) return;
|
|
889
|
-
await projection2.handle([event], context);
|
|
899
|
+
await projection2.handle([event], { connection: context.connection });
|
|
890
900
|
},
|
|
891
901
|
...options
|
|
892
902
|
});
|
|
@@ -905,7 +915,7 @@ var sqliteEventStoreConsumer = (options) => {
|
|
|
905
915
|
const processors = _nullishCoalesce(options.processors, () => ( []));
|
|
906
916
|
let start;
|
|
907
917
|
let currentMessagePuller;
|
|
908
|
-
const
|
|
918
|
+
const connection = _nullishCoalesce(options.connection, () => ( sqliteConnection({ fileName: options.fileName })));
|
|
909
919
|
const eachBatch = async (messagesBatch) => {
|
|
910
920
|
const activeProcessors = processors.filter((s) => s.isActive);
|
|
911
921
|
if (activeProcessors.length === 0)
|
|
@@ -915,20 +925,23 @@ var sqliteEventStoreConsumer = (options) => {
|
|
|
915
925
|
};
|
|
916
926
|
const result = await Promise.allSettled(
|
|
917
927
|
activeProcessors.map((s) => {
|
|
918
|
-
return s.handle(messagesBatch, {
|
|
928
|
+
return s.handle(messagesBatch, {
|
|
929
|
+
connection,
|
|
930
|
+
fileName: options.fileName
|
|
931
|
+
});
|
|
919
932
|
})
|
|
920
933
|
);
|
|
921
934
|
return result.some(
|
|
922
|
-
(r) => r.status === "fulfilled" && _optionalChain([r, 'access',
|
|
935
|
+
(r) => r.status === "fulfilled" && _optionalChain([r, 'access', _41 => _41.value, 'optionalAccess', _42 => _42.type]) !== "STOP"
|
|
923
936
|
) ? void 0 : {
|
|
924
937
|
type: "STOP"
|
|
925
938
|
};
|
|
926
939
|
};
|
|
927
940
|
const messagePooler = currentMessagePuller = sqliteEventStoreMessageBatchPuller({
|
|
928
|
-
|
|
941
|
+
connection,
|
|
929
942
|
eachBatch,
|
|
930
|
-
batchSize: _nullishCoalesce(_optionalChain([pulling, 'optionalAccess',
|
|
931
|
-
pullingFrequencyInMs: _nullishCoalesce(_optionalChain([pulling, 'optionalAccess',
|
|
943
|
+
batchSize: _nullishCoalesce(_optionalChain([pulling, 'optionalAccess', _43 => _43.batchSize]), () => ( DefaultSQLiteEventStoreProcessorBatchSize)),
|
|
944
|
+
pullingFrequencyInMs: _nullishCoalesce(_optionalChain([pulling, 'optionalAccess', _44 => _44.pullingFrequencyInMs]), () => ( DefaultSQLiteEventStoreProcessorPullingFrequencyInMs))
|
|
932
945
|
});
|
|
933
946
|
const stop = async () => {
|
|
934
947
|
if (!isRunning) return;
|
|
@@ -960,7 +973,7 @@ var sqliteEventStoreConsumer = (options) => {
|
|
|
960
973
|
);
|
|
961
974
|
isRunning = true;
|
|
962
975
|
const startFrom = zipSQLiteEventStoreMessageBatchPullerStartFrom(
|
|
963
|
-
await Promise.all(processors.map((o) => o.start(
|
|
976
|
+
await Promise.all(processors.map((o) => o.start(connection)))
|
|
964
977
|
);
|
|
965
978
|
return messagePooler.start({ startFrom });
|
|
966
979
|
})();
|
|
@@ -969,7 +982,7 @@ var sqliteEventStoreConsumer = (options) => {
|
|
|
969
982
|
stop,
|
|
970
983
|
close: async () => {
|
|
971
984
|
await stop();
|
|
972
|
-
|
|
985
|
+
connection.close();
|
|
973
986
|
await new Promise((resolve) => setTimeout(resolve, 250));
|
|
974
987
|
}
|
|
975
988
|
};
|
|
@@ -977,14 +990,14 @@ var sqliteEventStoreConsumer = (options) => {
|
|
|
977
990
|
|
|
978
991
|
// src/eventStore/projections/index.ts
|
|
979
992
|
var handleProjections = async (options) => {
|
|
980
|
-
const { projections: allProjections, events,
|
|
993
|
+
const { projections: allProjections, events, connection } = options;
|
|
981
994
|
const eventTypes = events.map((e) => e.type);
|
|
982
|
-
const
|
|
983
|
-
(
|
|
984
|
-
|
|
985
|
-
|
|
995
|
+
for (const projection2 of allProjections) {
|
|
996
|
+
if (!projection2.canHandle.some((type) => eventTypes.includes(type))) {
|
|
997
|
+
continue;
|
|
998
|
+
}
|
|
986
999
|
await projection2.handle(events, {
|
|
987
|
-
|
|
1000
|
+
connection
|
|
988
1001
|
});
|
|
989
1002
|
}
|
|
990
1003
|
};
|
|
@@ -998,7 +1011,7 @@ var getSQLiteEventStore = (options) => {
|
|
|
998
1011
|
const fileName = _nullishCoalesce(options.fileName, () => ( InMemorySQLiteDatabase));
|
|
999
1012
|
const isInMemory = fileName === InMemorySQLiteDatabase || fileName === InMemorySharedCacheSQLiteDatabase;
|
|
1000
1013
|
const inlineProjections = (_nullishCoalesce(options.projections, () => ( []))).filter(({ type }) => type === "inline").map(({ projection: projection2 }) => projection2);
|
|
1001
|
-
const onBeforeCommitHook = _optionalChain([options, 'access',
|
|
1014
|
+
const onBeforeCommitHook = _optionalChain([options, 'access', _45 => _45.hooks, 'optionalAccess', _46 => _46.onBeforeCommit]);
|
|
1002
1015
|
const createConnection = () => {
|
|
1003
1016
|
if (database != null) {
|
|
1004
1017
|
return database;
|
|
@@ -1028,12 +1041,12 @@ var getSQLiteEventStore = (options) => {
|
|
|
1028
1041
|
}
|
|
1029
1042
|
};
|
|
1030
1043
|
if (options) {
|
|
1031
|
-
autoGenerateSchema = _optionalChain([options, 'access',
|
|
1044
|
+
autoGenerateSchema = _optionalChain([options, 'access', _47 => _47.schema, 'optionalAccess', _48 => _48.autoMigration]) === void 0 || _optionalChain([options, 'access', _49 => _49.schema, 'optionalAccess', _50 => _50.autoMigration]) !== "None";
|
|
1032
1045
|
}
|
|
1033
|
-
const ensureSchemaExists = async (
|
|
1046
|
+
const ensureSchemaExists = async (connection) => {
|
|
1034
1047
|
if (!autoGenerateSchema) return Promise.resolve();
|
|
1035
1048
|
if (!schemaMigrated) {
|
|
1036
|
-
await createEventStoreSchema(
|
|
1049
|
+
await createEventStoreSchema(connection);
|
|
1037
1050
|
schemaMigrated = true;
|
|
1038
1051
|
}
|
|
1039
1052
|
return Promise.resolve();
|
|
@@ -1041,7 +1054,7 @@ var getSQLiteEventStore = (options) => {
|
|
|
1041
1054
|
return {
|
|
1042
1055
|
async aggregateStream(streamName, options2) {
|
|
1043
1056
|
const { evolve, initialState, read } = options2;
|
|
1044
|
-
const expectedStreamVersion = _optionalChain([read, 'optionalAccess',
|
|
1057
|
+
const expectedStreamVersion = _optionalChain([read, 'optionalAccess', _51 => _51.expectedStreamVersion]);
|
|
1045
1058
|
let state = initialState();
|
|
1046
1059
|
if (typeof streamName !== "string") {
|
|
1047
1060
|
throw new Error("Stream name is not string");
|
|
@@ -1050,7 +1063,7 @@ var getSQLiteEventStore = (options) => {
|
|
|
1050
1063
|
database = createConnection();
|
|
1051
1064
|
}
|
|
1052
1065
|
const result = await withConnection(
|
|
1053
|
-
(
|
|
1066
|
+
(connection) => readStream(connection, streamName, options2.read)
|
|
1054
1067
|
);
|
|
1055
1068
|
const currentStreamVersion = result.currentStreamVersion;
|
|
1056
1069
|
assertExpectedVersionMatchesCurrent(
|
|
@@ -1068,7 +1081,9 @@ var getSQLiteEventStore = (options) => {
|
|
|
1068
1081
|
streamExists: result.streamExists
|
|
1069
1082
|
};
|
|
1070
1083
|
},
|
|
1071
|
-
readStream: async (streamName, options2) => withConnection(
|
|
1084
|
+
readStream: async (streamName, options2) => withConnection(
|
|
1085
|
+
(connection) => readStream(connection, streamName, options2)
|
|
1086
|
+
),
|
|
1072
1087
|
appendToStream: async (streamName, events, options2) => {
|
|
1073
1088
|
if (database == null) {
|
|
1074
1089
|
database = createConnection();
|
|
@@ -1076,7 +1091,7 @@ var getSQLiteEventStore = (options) => {
|
|
|
1076
1091
|
const [firstPart, ...rest] = streamName.split("-");
|
|
1077
1092
|
const streamType = firstPart && rest.length > 0 ? firstPart : "emt:unknown";
|
|
1078
1093
|
const appendResult = await withConnection(
|
|
1079
|
-
(
|
|
1094
|
+
(connection) => appendToStream(connection, streamName, streamType, events, {
|
|
1080
1095
|
...options2,
|
|
1081
1096
|
onBeforeCommit: async (messages, context) => {
|
|
1082
1097
|
if (inlineProjections.length > 0)
|
|
@@ -1093,7 +1108,7 @@ var getSQLiteEventStore = (options) => {
|
|
|
1093
1108
|
throw new ExpectedVersionConflictError(
|
|
1094
1109
|
-1n,
|
|
1095
1110
|
//TODO: Return actual version in case of error
|
|
1096
|
-
_nullishCoalesce(_optionalChain([options2, 'optionalAccess',
|
|
1111
|
+
_nullishCoalesce(_optionalChain([options2, 'optionalAccess', _52 => _52.expectedStreamVersion]), () => ( NO_CONCURRENCY_CHECK))
|
|
1097
1112
|
);
|
|
1098
1113
|
return {
|
|
1099
1114
|
nextExpectedStreamVersion: appendResult.nextStreamPosition,
|
|
@@ -1104,7 +1119,7 @@ var getSQLiteEventStore = (options) => {
|
|
|
1104
1119
|
consumer: (options2) => sqliteEventStoreConsumer({
|
|
1105
1120
|
..._nullishCoalesce(options2, () => ( {})),
|
|
1106
1121
|
fileName,
|
|
1107
|
-
|
|
1122
|
+
connection: _nullishCoalesce(database, () => ( void 0))
|
|
1108
1123
|
})
|
|
1109
1124
|
};
|
|
1110
1125
|
};
|
|
@@ -1119,8 +1134,9 @@ var readStream = async (db, streamId, options) => {
|
|
|
1119
1134
|
const results = await db.query(
|
|
1120
1135
|
`SELECT stream_id, stream_position, global_position, message_data, message_metadata, message_schema_version, message_type, message_id
|
|
1121
1136
|
FROM ${messagesTable.name}
|
|
1122
|
-
WHERE stream_id = ? AND partition = ? AND is_archived = FALSE ${fromCondition} ${toCondition}
|
|
1123
|
-
|
|
1137
|
+
WHERE stream_id = ? AND partition = ? AND is_archived = FALSE ${fromCondition} ${toCondition}
|
|
1138
|
+
ORDER BY stream_position ASC`,
|
|
1139
|
+
[streamId, _nullishCoalesce(_optionalChain([options, 'optionalAccess', _53 => _53.partition]), () => ( defaultTag))]
|
|
1124
1140
|
);
|
|
1125
1141
|
const messages = results.map((row) => {
|
|
1126
1142
|
const rawEvent = {
|
|
@@ -1177,9 +1193,9 @@ async function storeSubscriptionCheckpointSQLite(db, processorId, version, posit
|
|
|
1177
1193
|
[processorId, partition]
|
|
1178
1194
|
)
|
|
1179
1195
|
);
|
|
1180
|
-
if (_optionalChain([current_position, 'optionalAccess',
|
|
1196
|
+
if (_optionalChain([current_position, 'optionalAccess', _54 => _54.last_processed_position]) === position) {
|
|
1181
1197
|
return 0;
|
|
1182
|
-
} else if (position !== null && current_position !== null && _optionalChain([current_position, 'optionalAccess',
|
|
1198
|
+
} else if (position !== null && current_position !== null && _optionalChain([current_position, 'optionalAccess', _55 => _55.last_processed_position]) > position) {
|
|
1183
1199
|
return 2;
|
|
1184
1200
|
} else {
|
|
1185
1201
|
return 2;
|
|
@@ -1206,7 +1222,7 @@ async function storeSubscriptionCheckpointSQLite(db, processorId, version, posit
|
|
|
1206
1222
|
[processorId, partition]
|
|
1207
1223
|
)
|
|
1208
1224
|
);
|
|
1209
|
-
if (_optionalChain([current, 'optionalAccess',
|
|
1225
|
+
if (_optionalChain([current, 'optionalAccess', _56 => _56.last_processed_position]) === position) {
|
|
1210
1226
|
return 0;
|
|
1211
1227
|
} else {
|
|
1212
1228
|
return 2;
|