@event-driven-io/emmett-postgresql 0.43.0-beta.1 → 0.43.0-beta.2
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 +659 -347
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +169 -165
- package/dist/index.d.ts +169 -165
- package/dist/index.js +613 -301
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -53,9 +53,20 @@ var _uuid = require('uuid');
|
|
|
53
53
|
var _asyncretry = require('async-retry'); var _asyncretry2 = _interopRequireDefault(_asyncretry);
|
|
54
54
|
|
|
55
55
|
|
|
56
|
+
|
|
56
57
|
var emmettPrefix = "emt";
|
|
57
58
|
var defaultTag = `${emmettPrefix}:default`;
|
|
58
59
|
var unknownTag = `${emmettPrefix}:unknown`;
|
|
60
|
+
var canCreateEventStoreSession = (eventStore) => "withSession" in eventStore;
|
|
61
|
+
var nulloSessionFactory = (eventStore) => ({
|
|
62
|
+
withSession: (callback) => {
|
|
63
|
+
const nulloSession = {
|
|
64
|
+
eventStore,
|
|
65
|
+
close: () => Promise.resolve()
|
|
66
|
+
};
|
|
67
|
+
return callback(nulloSession);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
59
70
|
var STREAM_EXISTS = "STREAM_EXISTS";
|
|
60
71
|
var STREAM_DOES_NOT_EXIST = "STREAM_DOES_NOT_EXIST";
|
|
61
72
|
var NO_CONCURRENCY_CHECK = "NO_CONCURRENCY_CHECK";
|
|
@@ -76,6 +87,10 @@ var ExpectedVersionConflictError = class _ExpectedVersionConflictError extends C
|
|
|
76
87
|
Object.setPrototypeOf(this, _ExpectedVersionConflictError.prototype);
|
|
77
88
|
}
|
|
78
89
|
};
|
|
90
|
+
var isExpectedVersionConflictError = (error) => error instanceof ExpectedVersionConflictError || EmmettError.isInstanceOf(
|
|
91
|
+
error,
|
|
92
|
+
ExpectedVersionConflictError.Codes.ConcurrencyError
|
|
93
|
+
);
|
|
79
94
|
var isPrimitive = (value) => {
|
|
80
95
|
const type = typeof value;
|
|
81
96
|
return value === null || value === void 0 || type === "boolean" || type === "number" || type === "string" || type === "symbol" || type === "bigint";
|
|
@@ -302,6 +317,7 @@ var JSONParser = {
|
|
|
302
317
|
return _optionalChain([options, 'optionalAccess', _13 => _13.map]) ? options.map(parsed) : parsed;
|
|
303
318
|
}
|
|
304
319
|
};
|
|
320
|
+
var NoRetries = { retries: 0 };
|
|
305
321
|
var asyncRetry = async (fn, opts) => {
|
|
306
322
|
if (opts === void 0 || opts.retries === 0) return fn();
|
|
307
323
|
return _asyncretry2.default.call(void 0,
|
|
@@ -314,12 +330,12 @@ var asyncRetry = async (fn, opts) => {
|
|
|
314
330
|
);
|
|
315
331
|
}
|
|
316
332
|
return result;
|
|
317
|
-
} catch (
|
|
318
|
-
if (_optionalChain([opts, 'optionalAccess', _15 => _15.shouldRetryError]) && !opts.shouldRetryError(
|
|
319
|
-
bail(
|
|
333
|
+
} catch (error) {
|
|
334
|
+
if (_optionalChain([opts, 'optionalAccess', _15 => _15.shouldRetryError]) && !opts.shouldRetryError(error)) {
|
|
335
|
+
bail(error);
|
|
320
336
|
return void 0;
|
|
321
337
|
}
|
|
322
|
-
throw
|
|
338
|
+
throw error;
|
|
323
339
|
}
|
|
324
340
|
},
|
|
325
341
|
_nullishCoalesce(opts, () => ( { retries: 0 }))
|
|
@@ -423,6 +439,7 @@ var reactor = (options) => {
|
|
|
423
439
|
id: processorId,
|
|
424
440
|
instanceId,
|
|
425
441
|
type,
|
|
442
|
+
canHandle,
|
|
426
443
|
init,
|
|
427
444
|
start: async (startOptions) => {
|
|
428
445
|
if (isActive) return;
|
|
@@ -690,6 +707,243 @@ var upcastRecordedMessage = (recordedMessage, options) => {
|
|
|
690
707
|
};
|
|
691
708
|
};
|
|
692
709
|
var projection = (definition) => definition;
|
|
710
|
+
var WorkflowHandlerStreamVersionConflictRetryOptions = {
|
|
711
|
+
retries: 3,
|
|
712
|
+
minTimeout: 100,
|
|
713
|
+
factor: 1.5,
|
|
714
|
+
shouldRetryError: isExpectedVersionConflictError
|
|
715
|
+
};
|
|
716
|
+
var fromWorkflowHandlerRetryOptions = (retryOptions) => {
|
|
717
|
+
if (retryOptions === void 0) return NoRetries;
|
|
718
|
+
if ("onVersionConflict" in retryOptions) {
|
|
719
|
+
if (typeof retryOptions.onVersionConflict === "boolean")
|
|
720
|
+
return WorkflowHandlerStreamVersionConflictRetryOptions;
|
|
721
|
+
else if (typeof retryOptions.onVersionConflict === "number")
|
|
722
|
+
return {
|
|
723
|
+
...WorkflowHandlerStreamVersionConflictRetryOptions,
|
|
724
|
+
retries: retryOptions.onVersionConflict
|
|
725
|
+
};
|
|
726
|
+
else return retryOptions.onVersionConflict;
|
|
727
|
+
}
|
|
728
|
+
return retryOptions;
|
|
729
|
+
};
|
|
730
|
+
var emptyHandlerResult = (nextExpectedStreamVersion = 0n) => ({
|
|
731
|
+
newMessages: [],
|
|
732
|
+
createdNewStream: false,
|
|
733
|
+
nextExpectedStreamVersion
|
|
734
|
+
});
|
|
735
|
+
var createInputMetadata = (originalMessageId, action) => ({
|
|
736
|
+
originalMessageId,
|
|
737
|
+
input: true,
|
|
738
|
+
action
|
|
739
|
+
});
|
|
740
|
+
var tagOutputMessage = (msg, action) => {
|
|
741
|
+
const existingMetadata = "metadata" in msg && msg.metadata ? msg.metadata : {};
|
|
742
|
+
return {
|
|
743
|
+
...msg,
|
|
744
|
+
metadata: {
|
|
745
|
+
...existingMetadata,
|
|
746
|
+
action
|
|
747
|
+
}
|
|
748
|
+
};
|
|
749
|
+
};
|
|
750
|
+
var createWrappedInitialState = (initialState) => {
|
|
751
|
+
return () => ({
|
|
752
|
+
userState: initialState(),
|
|
753
|
+
processedInputIds: /* @__PURE__ */ new Set()
|
|
754
|
+
});
|
|
755
|
+
};
|
|
756
|
+
var createWrappedEvolve = (evolve, workflowName, separateInputInboxFromProcessing) => {
|
|
757
|
+
return (state, event2) => {
|
|
758
|
+
const metadata = event2.metadata;
|
|
759
|
+
let processedInputIds = state.processedInputIds;
|
|
760
|
+
if (_optionalChain([metadata, 'optionalAccess', _35 => _35.input]) === true && typeof _optionalChain([metadata, 'optionalAccess', _36 => _36.originalMessageId]) === "string") {
|
|
761
|
+
processedInputIds = new Set(state.processedInputIds);
|
|
762
|
+
processedInputIds.add(metadata.originalMessageId);
|
|
763
|
+
}
|
|
764
|
+
if (separateInputInboxFromProcessing && _optionalChain([metadata, 'optionalAccess', _37 => _37.input]) === true) {
|
|
765
|
+
return {
|
|
766
|
+
userState: state.userState,
|
|
767
|
+
processedInputIds
|
|
768
|
+
};
|
|
769
|
+
}
|
|
770
|
+
const eventType = event2.type;
|
|
771
|
+
const eventForEvolve = eventType.startsWith(`${workflowName}:`) ? {
|
|
772
|
+
...event2,
|
|
773
|
+
type: eventType.replace(`${workflowName}:`, "")
|
|
774
|
+
} : event2;
|
|
775
|
+
return {
|
|
776
|
+
userState: evolve(state.userState, eventForEvolve),
|
|
777
|
+
processedInputIds
|
|
778
|
+
};
|
|
779
|
+
};
|
|
780
|
+
};
|
|
781
|
+
var workflowStreamName = ({
|
|
782
|
+
workflowName,
|
|
783
|
+
workflowId
|
|
784
|
+
}) => `emt:workflow:${workflowName}:${workflowId}`;
|
|
785
|
+
var WorkflowHandler = (options) => async (store, message2, handleOptions) => asyncRetry(
|
|
786
|
+
async () => {
|
|
787
|
+
const result = await withSession2(store, async ({ eventStore }) => {
|
|
788
|
+
const {
|
|
789
|
+
workflow: { evolve, initialState, decide, name: workflowName },
|
|
790
|
+
getWorkflowId: getWorkflowId2
|
|
791
|
+
} = options;
|
|
792
|
+
const inputMessageId = (
|
|
793
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
794
|
+
_nullishCoalesce(("metadata" in message2 && _optionalChain([message2, 'access', _38 => _38.metadata, 'optionalAccess', _39 => _39.messageId]) ? (
|
|
795
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
796
|
+
message2.metadata.messageId
|
|
797
|
+
) : void 0), () => ( _uuid.v7.call(void 0, )))
|
|
798
|
+
);
|
|
799
|
+
const messageWithMetadata = {
|
|
800
|
+
...message2,
|
|
801
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
802
|
+
metadata: {
|
|
803
|
+
messageId: inputMessageId,
|
|
804
|
+
...message2.metadata
|
|
805
|
+
}
|
|
806
|
+
};
|
|
807
|
+
const workflowId = getWorkflowId2(messageWithMetadata);
|
|
808
|
+
if (!workflowId) {
|
|
809
|
+
return emptyHandlerResult();
|
|
810
|
+
}
|
|
811
|
+
const streamName = options.mapWorkflowId ? options.mapWorkflowId(workflowId) : workflowStreamName({ workflowName, workflowId });
|
|
812
|
+
const messageType = messageWithMetadata.type;
|
|
813
|
+
const hasWorkflowPrefix = messageType.startsWith(`${workflowName}:`);
|
|
814
|
+
if (options.separateInputInboxFromProcessing && !hasWorkflowPrefix) {
|
|
815
|
+
const inputMetadata2 = createInputMetadata(
|
|
816
|
+
inputMessageId,
|
|
817
|
+
"InitiatedBy"
|
|
818
|
+
);
|
|
819
|
+
const inputToStore2 = {
|
|
820
|
+
type: `${workflowName}:${messageWithMetadata.type}`,
|
|
821
|
+
data: messageWithMetadata.data,
|
|
822
|
+
kind: messageWithMetadata.kind,
|
|
823
|
+
metadata: inputMetadata2
|
|
824
|
+
};
|
|
825
|
+
const appendResult2 = await eventStore.appendToStream(
|
|
826
|
+
streamName,
|
|
827
|
+
[inputToStore2],
|
|
828
|
+
{
|
|
829
|
+
...handleOptions,
|
|
830
|
+
expectedStreamVersion: _nullishCoalesce(_optionalChain([handleOptions, 'optionalAccess', _40 => _40.expectedStreamVersion]), () => ( NO_CONCURRENCY_CHECK))
|
|
831
|
+
}
|
|
832
|
+
);
|
|
833
|
+
return {
|
|
834
|
+
...appendResult2,
|
|
835
|
+
newMessages: []
|
|
836
|
+
};
|
|
837
|
+
}
|
|
838
|
+
const wrappedInitialState = createWrappedInitialState(initialState);
|
|
839
|
+
const wrappedEvolve = createWrappedEvolve(
|
|
840
|
+
evolve,
|
|
841
|
+
workflowName,
|
|
842
|
+
_nullishCoalesce(options.separateInputInboxFromProcessing, () => ( false))
|
|
843
|
+
);
|
|
844
|
+
const aggregationResult = await eventStore.aggregateStream(streamName, {
|
|
845
|
+
evolve: wrappedEvolve,
|
|
846
|
+
initialState: wrappedInitialState,
|
|
847
|
+
read: {
|
|
848
|
+
...handleOptions,
|
|
849
|
+
// expected stream version is passed to fail fast
|
|
850
|
+
// if stream is in the wrong state
|
|
851
|
+
expectedStreamVersion: _nullishCoalesce(_optionalChain([handleOptions, 'optionalAccess', _41 => _41.expectedStreamVersion]), () => ( NO_CONCURRENCY_CHECK))
|
|
852
|
+
}
|
|
853
|
+
});
|
|
854
|
+
const { currentStreamVersion } = aggregationResult;
|
|
855
|
+
const { userState: state, processedInputIds } = aggregationResult.state;
|
|
856
|
+
if (processedInputIds.has(inputMessageId)) {
|
|
857
|
+
return emptyHandlerResult(currentStreamVersion);
|
|
858
|
+
}
|
|
859
|
+
const messageForDecide = hasWorkflowPrefix ? {
|
|
860
|
+
...messageWithMetadata,
|
|
861
|
+
type: messageType.replace(`${workflowName}:`, "")
|
|
862
|
+
} : messageWithMetadata;
|
|
863
|
+
const result2 = decide(messageForDecide, state);
|
|
864
|
+
const inputMetadata = createInputMetadata(
|
|
865
|
+
inputMessageId,
|
|
866
|
+
aggregationResult.streamExists ? "Received" : "InitiatedBy"
|
|
867
|
+
);
|
|
868
|
+
const inputToStore = {
|
|
869
|
+
type: `${workflowName}:${messageWithMetadata.type}`,
|
|
870
|
+
data: messageWithMetadata.data,
|
|
871
|
+
kind: messageWithMetadata.kind,
|
|
872
|
+
metadata: inputMetadata
|
|
873
|
+
};
|
|
874
|
+
const outputMessages = (Array.isArray(result2) ? result2 : [result2]).filter((msg) => msg !== void 0 && msg !== null);
|
|
875
|
+
const outputCommandTypes = _nullishCoalesce(_optionalChain([options, 'access', _42 => _42.outputs, 'optionalAccess', _43 => _43.commands]), () => ( []));
|
|
876
|
+
const taggedOutputMessages = outputMessages.map((msg) => {
|
|
877
|
+
const action = outputCommandTypes.includes(
|
|
878
|
+
msg.type
|
|
879
|
+
) ? "Sent" : "Published";
|
|
880
|
+
return tagOutputMessage(msg, action);
|
|
881
|
+
});
|
|
882
|
+
const messagesToAppend = options.separateInputInboxFromProcessing && hasWorkflowPrefix ? [...taggedOutputMessages] : [inputToStore, ...taggedOutputMessages];
|
|
883
|
+
if (messagesToAppend.length === 0) {
|
|
884
|
+
return emptyHandlerResult(currentStreamVersion);
|
|
885
|
+
}
|
|
886
|
+
const expectedStreamVersion = _nullishCoalesce(_optionalChain([handleOptions, 'optionalAccess', _44 => _44.expectedStreamVersion]), () => ( (aggregationResult.streamExists ? currentStreamVersion : STREAM_DOES_NOT_EXIST)));
|
|
887
|
+
const appendResult = await eventStore.appendToStream(
|
|
888
|
+
streamName,
|
|
889
|
+
// TODO: Fix this cast
|
|
890
|
+
messagesToAppend,
|
|
891
|
+
{
|
|
892
|
+
...handleOptions,
|
|
893
|
+
expectedStreamVersion
|
|
894
|
+
}
|
|
895
|
+
);
|
|
896
|
+
return {
|
|
897
|
+
...appendResult,
|
|
898
|
+
newMessages: outputMessages
|
|
899
|
+
};
|
|
900
|
+
});
|
|
901
|
+
return result;
|
|
902
|
+
},
|
|
903
|
+
fromWorkflowHandlerRetryOptions(
|
|
904
|
+
handleOptions && "retry" in handleOptions ? handleOptions.retry : options.retry
|
|
905
|
+
)
|
|
906
|
+
);
|
|
907
|
+
var withSession2 = (eventStore, callback) => {
|
|
908
|
+
const sessionFactory = canCreateEventStoreSession(eventStore) ? eventStore : nulloSessionFactory(eventStore);
|
|
909
|
+
return sessionFactory.withSession(callback);
|
|
910
|
+
};
|
|
911
|
+
var getWorkflowId = (options) => `emt:processor:workflow:${options.workflowName}`;
|
|
912
|
+
var workflowProcessor = (options) => {
|
|
913
|
+
const { workflow, ...rest } = options;
|
|
914
|
+
const canHandle = options.separateInputInboxFromProcessing ? [
|
|
915
|
+
...options.inputs.commands,
|
|
916
|
+
...options.inputs.events,
|
|
917
|
+
...options.inputs.commands.map((t) => `${workflow.name}:${t}`),
|
|
918
|
+
...options.inputs.events.map((t) => `${workflow.name}:${t}`)
|
|
919
|
+
] : [...options.inputs.commands, ...options.inputs.events];
|
|
920
|
+
const handle = WorkflowHandler(options);
|
|
921
|
+
return reactor({
|
|
922
|
+
...rest,
|
|
923
|
+
processorId: _nullishCoalesce(options.processorId, () => ( getWorkflowId({ workflowName: workflow.name }))),
|
|
924
|
+
canHandle,
|
|
925
|
+
type: MessageProcessorType.PROJECTOR,
|
|
926
|
+
eachMessage: async (message2, context) => {
|
|
927
|
+
const messageType = message2.type;
|
|
928
|
+
if (!canHandle.includes(messageType)) return;
|
|
929
|
+
const result = await handle(
|
|
930
|
+
context.connection.messageStore,
|
|
931
|
+
message2,
|
|
932
|
+
context
|
|
933
|
+
);
|
|
934
|
+
if (options.stopAfter && result.newMessages.length > 0) {
|
|
935
|
+
for (const outputMessage of result.newMessages) {
|
|
936
|
+
if (options.stopAfter(
|
|
937
|
+
outputMessage
|
|
938
|
+
)) {
|
|
939
|
+
return { type: "STOP", reason: "Stop condition reached" };
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
return;
|
|
944
|
+
}
|
|
945
|
+
});
|
|
946
|
+
};
|
|
693
947
|
|
|
694
948
|
// src/eventStore/schema/readLastMessageGlobalPosition.ts
|
|
695
949
|
var _dumbo = require('@event-driven-io/dumbo');
|
|
@@ -736,7 +990,7 @@ var readLastMessageGlobalPosition = async (execute, options) => {
|
|
|
736
990
|
execute.query(
|
|
737
991
|
_dumbo.SQL`SELECT global_position
|
|
738
992
|
FROM ${_dumbo.SQL.identifier(messagesTable.name)}
|
|
739
|
-
WHERE partition = ${_nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
993
|
+
WHERE partition = ${_nullishCoalesce(_optionalChain([options, 'optionalAccess', _45 => _45.partition]), () => ( defaultTag2))} AND is_archived = FALSE AND transaction_id < pg_snapshot_xmin(pg_current_snapshot())
|
|
740
994
|
ORDER BY transaction_id, global_position
|
|
741
995
|
LIMIT 1`
|
|
742
996
|
)
|
|
@@ -760,7 +1014,7 @@ var readMessagesBatch = async (execute, options) => {
|
|
|
760
1014
|
_dumbo.SQL`
|
|
761
1015
|
SELECT stream_id, stream_position, global_position, message_data, message_metadata, message_schema_version, message_type, message_id
|
|
762
1016
|
FROM ${_dumbo.SQL.identifier(messagesTable.name)}
|
|
763
|
-
WHERE partition = ${_nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
1017
|
+
WHERE partition = ${_nullishCoalesce(_optionalChain([options, 'optionalAccess', _46 => _46.partition]), () => ( defaultTag2))} AND is_archived = FALSE AND transaction_id < pg_snapshot_xmin(pg_current_snapshot()) ${fromCondition} ${toCondition}
|
|
764
1018
|
ORDER BY transaction_id, global_position
|
|
765
1019
|
${limitCondition}`
|
|
766
1020
|
),
|
|
@@ -816,7 +1070,7 @@ var postgreSQLEventStoreMessageBatchPuller = ({
|
|
|
816
1070
|
batchSize
|
|
817
1071
|
};
|
|
818
1072
|
let waitTime = 100;
|
|
819
|
-
while (isRunning && !_optionalChain([signal, 'optionalAccess',
|
|
1073
|
+
while (isRunning && !_optionalChain([signal, 'optionalAccess', _47 => _47.aborted])) {
|
|
820
1074
|
const { messages, currentGlobalPosition, areMessagesLeft } = await readMessagesBatch(executor, readMessagesOptions);
|
|
821
1075
|
if (messages.length > 0) {
|
|
822
1076
|
const result = await eachBatch(messages);
|
|
@@ -827,7 +1081,7 @@ var postgreSQLEventStoreMessageBatchPuller = ({
|
|
|
827
1081
|
}
|
|
828
1082
|
readMessagesOptions.after = currentGlobalPosition;
|
|
829
1083
|
await new Promise((resolve) => setTimeout(resolve, waitTime));
|
|
830
|
-
if (_optionalChain([stopWhen, 'optionalAccess',
|
|
1084
|
+
if (_optionalChain([stopWhen, 'optionalAccess', _48 => _48.noMessagesLeft]) === true && !areMessagesLeft) {
|
|
831
1085
|
isRunning = false;
|
|
832
1086
|
break;
|
|
833
1087
|
}
|
|
@@ -871,6 +1125,14 @@ var zipPostgreSQLEventStoreMessageBatchPullerStartFrom = (options) => {
|
|
|
871
1125
|
// src/eventStore/consumers/postgreSQLProcessor.ts
|
|
872
1126
|
|
|
873
1127
|
|
|
1128
|
+
// src/eventStore/postgreSQLEventStore.ts
|
|
1129
|
+
|
|
1130
|
+
|
|
1131
|
+
|
|
1132
|
+
|
|
1133
|
+
|
|
1134
|
+
|
|
1135
|
+
|
|
874
1136
|
// src/eventStore/projections/locks/tryAcquireProjectionLock.ts
|
|
875
1137
|
|
|
876
1138
|
|
|
@@ -1121,9 +1383,9 @@ var tryAcquireProcessorLock = async (execute, options) => {
|
|
|
1121
1383
|
version: options.version,
|
|
1122
1384
|
partition: options.partition,
|
|
1123
1385
|
processorInstanceId: options.processorInstanceId,
|
|
1124
|
-
projectionName: _nullishCoalesce(_optionalChain([options, 'access',
|
|
1125
|
-
projectionType: _optionalChain([options, 'access',
|
|
1126
|
-
projectionKind: _nullishCoalesce(_optionalChain([options, 'access',
|
|
1386
|
+
projectionName: _nullishCoalesce(_optionalChain([options, 'access', _49 => _49.projection, 'optionalAccess', _50 => _50.name]), () => ( null)),
|
|
1387
|
+
projectionType: _optionalChain([options, 'access', _51 => _51.projection, 'optionalAccess', _52 => _52.handlingType]) ? options.projection.handlingType === "inline" ? "i" : "a" : null,
|
|
1388
|
+
projectionKind: _nullishCoalesce(_optionalChain([options, 'access', _53 => _53.projection, 'optionalAccess', _54 => _54.kind]), () => ( null)),
|
|
1127
1389
|
lockTimeoutSeconds: _nullishCoalesce(options.lockTimeoutSeconds, () => ( PROCESSOR_LOCK_DEFAULT_TIMEOUT_SECONDS))
|
|
1128
1390
|
})
|
|
1129
1391
|
)
|
|
@@ -1175,7 +1437,7 @@ var postgreSQLProcessorLock = (options) => {
|
|
|
1175
1437
|
...options,
|
|
1176
1438
|
lockKey
|
|
1177
1439
|
});
|
|
1178
|
-
if (!result.acquired && _optionalChain([options, 'access',
|
|
1440
|
+
if (!result.acquired && _optionalChain([options, 'access', _55 => _55.lockAcquisitionPolicy, 'optionalAccess', _56 => _56.type]) !== "skip") {
|
|
1179
1441
|
throw new EmmettError(
|
|
1180
1442
|
`Failed to acquire lock for processor '${options.processorId}'`
|
|
1181
1443
|
);
|
|
@@ -1189,7 +1451,7 @@ var postgreSQLProcessorLock = (options) => {
|
|
|
1189
1451
|
await releaseProcessorLock(context.execute, {
|
|
1190
1452
|
...releaseOptions,
|
|
1191
1453
|
lockKey,
|
|
1192
|
-
projectionName: _optionalChain([projection2, 'optionalAccess',
|
|
1454
|
+
projectionName: _optionalChain([projection2, 'optionalAccess', _57 => _57.name])
|
|
1193
1455
|
});
|
|
1194
1456
|
acquired = false;
|
|
1195
1457
|
}
|
|
@@ -1702,46 +1964,284 @@ var expectPongoDocuments = {
|
|
|
1702
1964
|
|
|
1703
1965
|
|
|
1704
1966
|
|
|
1967
|
+
var PostgreSQLProjectionSpec = {
|
|
1968
|
+
for: (options) => {
|
|
1969
|
+
{
|
|
1970
|
+
const { projection: projection2, ...dumoOptions } = options;
|
|
1971
|
+
const { connectionString } = dumoOptions;
|
|
1972
|
+
let wasInitialised = false;
|
|
1973
|
+
const initialize = async (pool) => {
|
|
1974
|
+
const eventStore = getPostgreSQLEventStore(connectionString, {
|
|
1975
|
+
// TODO: This will need to change when we support other drivers
|
|
1976
|
+
connectionOptions: { dumbo: pool }
|
|
1977
|
+
});
|
|
1978
|
+
if (wasInitialised) return;
|
|
1979
|
+
wasInitialised = true;
|
|
1980
|
+
await eventStore.schema.migrate();
|
|
1981
|
+
if (projection2.init)
|
|
1982
|
+
await pool.withTransaction(async (transaction) => {
|
|
1983
|
+
await projection2.init({
|
|
1984
|
+
registrationType: "async",
|
|
1985
|
+
version: _nullishCoalesce(projection2.version, () => ( 1)),
|
|
1986
|
+
status: "active",
|
|
1987
|
+
context: await transactionToPostgreSQLProjectionHandlerContext(
|
|
1988
|
+
connectionString,
|
|
1989
|
+
pool,
|
|
1990
|
+
transaction
|
|
1991
|
+
)
|
|
1992
|
+
});
|
|
1993
|
+
});
|
|
1994
|
+
};
|
|
1995
|
+
return (givenEvents) => {
|
|
1996
|
+
return {
|
|
1997
|
+
when: (events, options2) => {
|
|
1998
|
+
const allEvents = [];
|
|
1999
|
+
const run = async (pool) => {
|
|
2000
|
+
let globalPosition = 0n;
|
|
2001
|
+
const numberOfTimes = _nullishCoalesce(_optionalChain([options2, 'optionalAccess', _58 => _58.numberOfTimes]), () => ( 1));
|
|
2002
|
+
for (const event of [
|
|
2003
|
+
...givenEvents,
|
|
2004
|
+
...Array.from({ length: numberOfTimes }).flatMap(() => events)
|
|
2005
|
+
]) {
|
|
2006
|
+
const metadata = {
|
|
2007
|
+
checkpoint: bigIntProcessorCheckpoint(++globalPosition),
|
|
2008
|
+
globalPosition,
|
|
2009
|
+
streamPosition: globalPosition,
|
|
2010
|
+
streamName: `test-${_uuid.v4.call(void 0, )}`,
|
|
2011
|
+
messageId: _uuid.v4.call(void 0, )
|
|
2012
|
+
};
|
|
2013
|
+
allEvents.push({
|
|
2014
|
+
...event,
|
|
2015
|
+
kind: "Event",
|
|
2016
|
+
metadata: {
|
|
2017
|
+
...metadata,
|
|
2018
|
+
..."metadata" in event ? _nullishCoalesce(event.metadata, () => ( {})) : {}
|
|
2019
|
+
}
|
|
2020
|
+
});
|
|
2021
|
+
}
|
|
2022
|
+
await initialize(pool);
|
|
2023
|
+
await pool.withTransaction(async (transaction) => {
|
|
2024
|
+
await handleProjections({
|
|
2025
|
+
events: allEvents,
|
|
2026
|
+
projections: [projection2],
|
|
2027
|
+
...await transactionToPostgreSQLProjectionHandlerContext(
|
|
2028
|
+
connectionString,
|
|
2029
|
+
pool,
|
|
2030
|
+
transaction
|
|
2031
|
+
)
|
|
2032
|
+
});
|
|
2033
|
+
});
|
|
2034
|
+
};
|
|
2035
|
+
return {
|
|
2036
|
+
then: async (assert, message) => {
|
|
2037
|
+
const pool = _dumbo.dumbo.call(void 0, dumoOptions);
|
|
2038
|
+
try {
|
|
2039
|
+
await run(pool);
|
|
2040
|
+
const succeeded = await assert({ pool, connectionString });
|
|
2041
|
+
if (succeeded !== void 0 && succeeded === false)
|
|
2042
|
+
assertFails(
|
|
2043
|
+
_nullishCoalesce(message, () => ( "Projection specification didn't match the criteria"))
|
|
2044
|
+
);
|
|
2045
|
+
} finally {
|
|
2046
|
+
await pool.close();
|
|
2047
|
+
}
|
|
2048
|
+
},
|
|
2049
|
+
thenThrows: async (...args) => {
|
|
2050
|
+
const pool = _dumbo.dumbo.call(void 0, dumoOptions);
|
|
2051
|
+
try {
|
|
2052
|
+
await run(pool);
|
|
2053
|
+
throw new AssertionError("Handler did not fail as expected");
|
|
2054
|
+
} catch (error) {
|
|
2055
|
+
if (error instanceof AssertionError) throw error;
|
|
2056
|
+
if (args.length === 0) return;
|
|
2057
|
+
if (!isErrorConstructor(args[0])) {
|
|
2058
|
+
assertTrue(
|
|
2059
|
+
args[0](error),
|
|
2060
|
+
`Error didn't match the error condition: ${_optionalChain([error, 'optionalAccess', _59 => _59.toString, 'call', _60 => _60()])}`
|
|
2061
|
+
);
|
|
2062
|
+
return;
|
|
2063
|
+
}
|
|
2064
|
+
assertTrue(
|
|
2065
|
+
error instanceof args[0],
|
|
2066
|
+
`Caught error is not an instance of the expected type: ${_optionalChain([error, 'optionalAccess', _61 => _61.toString, 'call', _62 => _62()])}`
|
|
2067
|
+
);
|
|
2068
|
+
if (args[1]) {
|
|
2069
|
+
assertTrue(
|
|
2070
|
+
args[1](error),
|
|
2071
|
+
`Error didn't match the error condition: ${_optionalChain([error, 'optionalAccess', _63 => _63.toString, 'call', _64 => _64()])}`
|
|
2072
|
+
);
|
|
2073
|
+
}
|
|
2074
|
+
} finally {
|
|
2075
|
+
await pool.close();
|
|
2076
|
+
}
|
|
2077
|
+
}
|
|
2078
|
+
};
|
|
2079
|
+
}
|
|
2080
|
+
};
|
|
2081
|
+
};
|
|
2082
|
+
}
|
|
2083
|
+
}
|
|
2084
|
+
};
|
|
2085
|
+
var eventInStream = (streamName, event) => {
|
|
2086
|
+
return {
|
|
2087
|
+
...event,
|
|
2088
|
+
metadata: {
|
|
2089
|
+
..._nullishCoalesce(event.metadata, () => ( {})),
|
|
2090
|
+
streamName: _nullishCoalesce(_optionalChain([event, 'access', _65 => _65.metadata, 'optionalAccess', _66 => _66.streamName]), () => ( streamName))
|
|
2091
|
+
}
|
|
2092
|
+
};
|
|
2093
|
+
};
|
|
2094
|
+
var eventsInStream = (streamName, events) => {
|
|
2095
|
+
return events.map((e) => eventInStream(streamName, e));
|
|
2096
|
+
};
|
|
2097
|
+
var newEventsInStream = eventsInStream;
|
|
2098
|
+
var assertSQLQueryResultMatches = (sql, rows) => async ({ pool: { execute } }) => {
|
|
2099
|
+
const result = await execute.query(sql);
|
|
2100
|
+
assertThatArray(rows).containsExactlyInAnyOrder(result.rows);
|
|
2101
|
+
};
|
|
2102
|
+
var expectSQL = {
|
|
2103
|
+
query: (sql) => ({
|
|
2104
|
+
resultRows: {
|
|
2105
|
+
toBeTheSame: (rows) => assertSQLQueryResultMatches(sql, rows)
|
|
2106
|
+
}
|
|
2107
|
+
})
|
|
2108
|
+
};
|
|
1705
2109
|
|
|
1706
|
-
// src/eventStore/
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
2110
|
+
// src/eventStore/projections/postgreSQLProjection.ts
|
|
2111
|
+
var transactionToPostgreSQLProjectionHandlerContext = async (connectionString, pool, transaction) => ({
|
|
2112
|
+
execute: transaction.execute,
|
|
2113
|
+
connection: {
|
|
2114
|
+
connectionString,
|
|
2115
|
+
client: await transaction.connection.open(),
|
|
2116
|
+
transaction,
|
|
2117
|
+
pool
|
|
2118
|
+
}
|
|
2119
|
+
});
|
|
2120
|
+
var handleProjections = async (options) => {
|
|
2121
|
+
const {
|
|
2122
|
+
projections: allProjections,
|
|
2123
|
+
events,
|
|
2124
|
+
connection: { pool, transaction, connectionString },
|
|
2125
|
+
partition = defaultTag2
|
|
2126
|
+
} = options;
|
|
2127
|
+
const eventTypes = events.map((e) => e.type);
|
|
2128
|
+
const projections = allProjections.filter(
|
|
2129
|
+
(p) => p.canHandle.some((type) => eventTypes.includes(type))
|
|
2130
|
+
);
|
|
2131
|
+
const client = await transaction.connection.open();
|
|
2132
|
+
for (const projection2 of projections) {
|
|
2133
|
+
if (projection2.name) {
|
|
2134
|
+
const lockAcquired = await postgreSQLProjectionLock({
|
|
2135
|
+
projectionName: projection2.name,
|
|
2136
|
+
partition,
|
|
2137
|
+
version: _nullishCoalesce(projection2.version, () => ( 1))
|
|
2138
|
+
}).tryAcquire({ execute: transaction.execute });
|
|
2139
|
+
if (!lockAcquired) {
|
|
2140
|
+
continue;
|
|
2141
|
+
}
|
|
2142
|
+
}
|
|
2143
|
+
await projection2.handle(events, {
|
|
2144
|
+
connection: {
|
|
2145
|
+
connectionString,
|
|
2146
|
+
pool,
|
|
2147
|
+
client,
|
|
2148
|
+
transaction
|
|
2149
|
+
},
|
|
2150
|
+
execute: transaction.execute
|
|
2151
|
+
});
|
|
2152
|
+
}
|
|
2153
|
+
};
|
|
2154
|
+
var postgreSQLProjection = (definition) => projection({
|
|
2155
|
+
...definition,
|
|
2156
|
+
init: async (options) => {
|
|
2157
|
+
await registerProjection(options.context.execute, {
|
|
2158
|
+
// TODO: pass partition from options
|
|
2159
|
+
partition: defaultTag2,
|
|
2160
|
+
status: "active",
|
|
2161
|
+
registration: {
|
|
2162
|
+
type: "async",
|
|
2163
|
+
// TODO: fix this
|
|
2164
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any
|
|
2165
|
+
projection: definition
|
|
2166
|
+
}
|
|
2167
|
+
});
|
|
2168
|
+
if (definition.init) {
|
|
2169
|
+
await definition.init(options);
|
|
2170
|
+
}
|
|
2171
|
+
}
|
|
2172
|
+
});
|
|
2173
|
+
var postgreSQLRawBatchSQLProjection = (options) => postgreSQLProjection({
|
|
2174
|
+
name: options.name,
|
|
2175
|
+
kind: _nullishCoalesce(options.kind, () => ( "emt:projections:postgresql:raw_sql:batch")),
|
|
2176
|
+
version: options.version,
|
|
2177
|
+
canHandle: options.canHandle,
|
|
2178
|
+
eventsOptions: options.eventsOptions,
|
|
2179
|
+
handle: async (events, context) => {
|
|
2180
|
+
const sqls = await options.evolve(events, context);
|
|
2181
|
+
await context.execute.batchCommand(sqls);
|
|
2182
|
+
},
|
|
2183
|
+
init: async (initOptions) => {
|
|
2184
|
+
const initSQL = options.init ? await options.init(initOptions) : void 0;
|
|
2185
|
+
if (initSQL) {
|
|
2186
|
+
if (Array.isArray(initSQL)) {
|
|
2187
|
+
await initOptions.context.execute.batchCommand(initSQL);
|
|
2188
|
+
} else {
|
|
2189
|
+
await initOptions.context.execute.command(initSQL);
|
|
2190
|
+
}
|
|
2191
|
+
}
|
|
2192
|
+
}
|
|
2193
|
+
});
|
|
2194
|
+
var postgreSQLRawSQLProjection = (options) => {
|
|
2195
|
+
const { evolve, kind, ...rest } = options;
|
|
2196
|
+
return postgreSQLRawBatchSQLProjection({
|
|
2197
|
+
kind: _nullishCoalesce(kind, () => ( "emt:projections:postgresql:raw:_sql:single")),
|
|
2198
|
+
...rest,
|
|
2199
|
+
evolve: async (events, context) => {
|
|
2200
|
+
const sqls = [];
|
|
2201
|
+
for (const event of events) {
|
|
2202
|
+
const pendingSqls = await evolve(event, context);
|
|
2203
|
+
if (Array.isArray(pendingSqls)) {
|
|
2204
|
+
sqls.push(...pendingSqls);
|
|
2205
|
+
} else {
|
|
2206
|
+
sqls.push(pendingSqls);
|
|
2207
|
+
}
|
|
2208
|
+
}
|
|
2209
|
+
return sqls;
|
|
2210
|
+
}
|
|
2211
|
+
});
|
|
2212
|
+
};
|
|
2213
|
+
|
|
2214
|
+
// src/eventStore/schema/index.ts
|
|
2215
|
+
|
|
2216
|
+
|
|
2217
|
+
|
|
2218
|
+
|
|
2219
|
+
|
|
2220
|
+
|
|
2221
|
+
// src/eventStore/schema/appendToStream.ts
|
|
2222
|
+
|
|
2223
|
+
|
|
2224
|
+
|
|
2225
|
+
|
|
2226
|
+
|
|
2227
|
+
|
|
2228
|
+
|
|
2229
|
+
var appendToStreamSQL = createFunctionIfDoesNotExistSQL(
|
|
2230
|
+
"emt_append_to_stream",
|
|
2231
|
+
_dumbo.SQL`CREATE OR REPLACE FUNCTION emt_append_to_stream(
|
|
2232
|
+
v_message_ids text[],
|
|
2233
|
+
v_messages_data jsonb[],
|
|
2234
|
+
v_messages_metadata jsonb[],
|
|
2235
|
+
v_message_schema_versions text[],
|
|
2236
|
+
v_message_types text[],
|
|
2237
|
+
v_message_kinds text[],
|
|
2238
|
+
v_stream_id text,
|
|
2239
|
+
v_stream_type text,
|
|
2240
|
+
v_expected_stream_position bigint DEFAULT NULL,
|
|
2241
|
+
v_partition text DEFAULT emt_sanitize_name('default_partition')
|
|
2242
|
+
) RETURNS TABLE (
|
|
2243
|
+
success boolean,
|
|
2244
|
+
next_stream_position bigint,
|
|
1745
2245
|
global_positions bigint[],
|
|
1746
2246
|
transaction_id xid8
|
|
1747
2247
|
) LANGUAGE plpgsql
|
|
@@ -1838,7 +2338,7 @@ var appendToStream = (pool, streamName, streamType, messages, options) => pool.w
|
|
|
1838
2338
|
return { success: false, result: { success: false } };
|
|
1839
2339
|
try {
|
|
1840
2340
|
const expectedStreamVersion = toExpectedVersion(
|
|
1841
|
-
_optionalChain([options, 'optionalAccess',
|
|
2341
|
+
_optionalChain([options, 'optionalAccess', _67 => _67.expectedStreamVersion])
|
|
1842
2342
|
);
|
|
1843
2343
|
const messagesToAppend = messages.map((e) => ({
|
|
1844
2344
|
...e,
|
|
@@ -1878,7 +2378,7 @@ var appendToStream = (pool, streamName, streamType, messages, options) => pool.w
|
|
|
1878
2378
|
globalPosition
|
|
1879
2379
|
};
|
|
1880
2380
|
});
|
|
1881
|
-
if (_optionalChain([options, 'optionalAccess',
|
|
2381
|
+
if (_optionalChain([options, 'optionalAccess', _68 => _68.beforeCommitHook]))
|
|
1882
2382
|
await options.beforeCommitHook(messagesToAppend, { transaction });
|
|
1883
2383
|
return {
|
|
1884
2384
|
success: true,
|
|
@@ -1921,8 +2421,8 @@ var appendEventsRaw = (execute, streamId, streamType, messages, options) => _dum
|
|
|
1921
2421
|
messageKinds: messages.map((e) => e.kind === "Event" ? "E" : "C"),
|
|
1922
2422
|
streamId,
|
|
1923
2423
|
streamType,
|
|
1924
|
-
expectedStreamPosition: _nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
1925
|
-
partition: _nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
2424
|
+
expectedStreamPosition: _nullishCoalesce(_optionalChain([options, 'optionalAccess', _69 => _69.expectedStreamVersion]), () => ( null)),
|
|
2425
|
+
partition: _nullishCoalesce(_optionalChain([options, 'optionalAccess', _70 => _70.partition]), () => ( defaultTag2))
|
|
1926
2426
|
})
|
|
1927
2427
|
)
|
|
1928
2428
|
);
|
|
@@ -3984,7 +4484,7 @@ var readProcessorCheckpoint = async (execute, options) => {
|
|
|
3984
4484
|
execute.query(
|
|
3985
4485
|
_dumbo.SQL`SELECT last_processed_checkpoint
|
|
3986
4486
|
FROM ${_dumbo.SQL.identifier(processorsTable.name)}
|
|
3987
|
-
WHERE partition = ${_nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
4487
|
+
WHERE partition = ${_nullishCoalesce(_optionalChain([options, 'optionalAccess', _71 => _71.partition]), () => ( defaultTag2))} AND processor_id = ${options.processorId} AND version = ${_nullishCoalesce(options.version, () => ( 1))}
|
|
3988
4488
|
LIMIT 1`
|
|
3989
4489
|
)
|
|
3990
4490
|
);
|
|
@@ -3996,16 +4496,16 @@ var readProcessorCheckpoint = async (execute, options) => {
|
|
|
3996
4496
|
// src/eventStore/schema/readStream.ts
|
|
3997
4497
|
|
|
3998
4498
|
var readStream = async (execute, streamId, options) => {
|
|
3999
|
-
const fromCondition = _optionalChain([options, 'optionalAccess',
|
|
4499
|
+
const fromCondition = _optionalChain([options, 'optionalAccess', _72 => _72.from]) ? `AND stream_position >= ${options.from}` : "";
|
|
4000
4500
|
const to = Number(
|
|
4001
|
-
_nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
4501
|
+
_nullishCoalesce(_optionalChain([options, 'optionalAccess', _73 => _73.to]), () => ( (_optionalChain([options, 'optionalAccess', _74 => _74.maxCount]) ? (_nullishCoalesce(options.from, () => ( 0n))) + options.maxCount : NaN)))
|
|
4002
4502
|
);
|
|
4003
4503
|
const toCondition = !isNaN(to) ? `AND stream_position <= ${to}` : "";
|
|
4004
4504
|
const events = await _dumbo.mapRows.call(void 0,
|
|
4005
4505
|
execute.query(
|
|
4006
4506
|
_dumbo.SQL`SELECT stream_id, stream_position, global_position, message_data, message_metadata, message_schema_version, message_type, message_id
|
|
4007
4507
|
FROM ${_dumbo.SQL.identifier(messagesTable.name)}
|
|
4008
|
-
WHERE stream_id = ${streamId} AND partition = ${_nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
4508
|
+
WHERE stream_id = ${streamId} AND partition = ${_nullishCoalesce(_optionalChain([options, 'optionalAccess', _75 => _75.partition]), () => ( defaultTag2))} AND is_archived = FALSE ${_dumbo.SQL.plain(fromCondition)} ${_dumbo.SQL.plain(toCondition)}
|
|
4009
4509
|
ORDER BY stream_position ASC`
|
|
4010
4510
|
),
|
|
4011
4511
|
(row) => {
|
|
@@ -4027,7 +4527,7 @@ var readStream = async (execute, streamId, options) => {
|
|
|
4027
4527
|
kind: "Event",
|
|
4028
4528
|
metadata
|
|
4029
4529
|
};
|
|
4030
|
-
return upcastRecordedMessage(event, _optionalChain([options, 'optionalAccess',
|
|
4530
|
+
return upcastRecordedMessage(event, _optionalChain([options, 'optionalAccess', _76 => _76.schema, 'optionalAccess', _77 => _77.versioning]));
|
|
4031
4531
|
}
|
|
4032
4532
|
);
|
|
4033
4533
|
return events.length > 0 ? {
|
|
@@ -4048,10 +4548,10 @@ var streamExists = async (execute, streamId, options) => {
|
|
|
4048
4548
|
_dumbo.SQL`SELECT EXISTS (
|
|
4049
4549
|
SELECT 1
|
|
4050
4550
|
from ${_dumbo.SQL.identifier(streamsTable.name)}
|
|
4051
|
-
WHERE stream_id = ${streamId} AND partition = ${_nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
4551
|
+
WHERE stream_id = ${streamId} AND partition = ${_nullishCoalesce(_optionalChain([options, 'optionalAccess', _78 => _78.partition]), () => ( defaultTag2))} AND is_archived = FALSE)
|
|
4052
4552
|
`
|
|
4053
4553
|
);
|
|
4054
|
-
return _nullishCoalesce(_optionalChain([queryResult, 'access',
|
|
4554
|
+
return _nullishCoalesce(_optionalChain([queryResult, 'access', _79 => _79.rows, 'access', _80 => _80[0], 'optionalAccess', _81 => _81.exists]), () => ( false));
|
|
4055
4555
|
};
|
|
4056
4556
|
|
|
4057
4557
|
// src/eventStore/schema/index.ts
|
|
@@ -4091,7 +4591,7 @@ var createEventStoreSchema = (connectionString, pool, hooks, options) => {
|
|
|
4091
4591
|
);
|
|
4092
4592
|
const nestedPool = _dumbo.dumbo.call(void 0, { connectionString, connection: tx.connection });
|
|
4093
4593
|
try {
|
|
4094
|
-
if (_optionalChain([hooks, 'optionalAccess',
|
|
4594
|
+
if (_optionalChain([hooks, 'optionalAccess', _82 => _82.onBeforeSchemaCreated])) {
|
|
4095
4595
|
await hooks.onBeforeSchemaCreated(context);
|
|
4096
4596
|
}
|
|
4097
4597
|
const result = await _dumbo.runSQLMigrations.call(void 0,
|
|
@@ -4099,7 +4599,7 @@ var createEventStoreSchema = (connectionString, pool, hooks, options) => {
|
|
|
4099
4599
|
eventStoreSchemaMigrations,
|
|
4100
4600
|
options
|
|
4101
4601
|
);
|
|
4102
|
-
if (_optionalChain([hooks, 'optionalAccess',
|
|
4602
|
+
if (_optionalChain([hooks, 'optionalAccess', _83 => _83.onAfterSchemaCreated])) {
|
|
4103
4603
|
await hooks.onAfterSchemaCreated(context);
|
|
4104
4604
|
}
|
|
4105
4605
|
return result;
|
|
@@ -4118,7 +4618,7 @@ var truncateTables = async (execute, options) => {
|
|
|
4118
4618
|
${_dumbo.SQL.identifier(messagesTable.name)},
|
|
4119
4619
|
${_dumbo.SQL.identifier(processorsTable.name)},
|
|
4120
4620
|
${_dumbo.SQL.identifier(projectionsTable.name)}
|
|
4121
|
-
CASCADE${_dumbo.SQL.plain(_optionalChain([options, 'optionalAccess',
|
|
4621
|
+
CASCADE${_dumbo.SQL.plain(_optionalChain([options, 'optionalAccess', _84 => _84.resetSequences]) ? "; ALTER SEQUENCE emt_global_message_position RESTART WITH 1" : "")};`
|
|
4122
4622
|
);
|
|
4123
4623
|
};
|
|
4124
4624
|
|
|
@@ -4135,7 +4635,7 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
|
|
|
4135
4635
|
};
|
|
4136
4636
|
const pool = "dumbo" in poolOptions ? poolOptions.dumbo : _dumbo.dumbo.call(void 0, poolOptions);
|
|
4137
4637
|
let migrateSchema = void 0;
|
|
4138
|
-
const autoGenerateSchema = _optionalChain([options, 'access',
|
|
4638
|
+
const autoGenerateSchema = _optionalChain([options, 'access', _85 => _85.schema, 'optionalAccess', _86 => _86.autoMigration]) === void 0 || _optionalChain([options, 'access', _87 => _87.schema, 'optionalAccess', _88 => _88.autoMigration]) !== "None";
|
|
4139
4639
|
const inlineProjections = (_nullishCoalesce(options.projections, () => ( []))).filter(({ type }) => type === "inline").map(({ projection: projection2 }) => projection2);
|
|
4140
4640
|
const migrate = async (migrationOptions) => {
|
|
4141
4641
|
if (!migrateSchema) {
|
|
@@ -4144,7 +4644,7 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
|
|
|
4144
4644
|
pool,
|
|
4145
4645
|
{
|
|
4146
4646
|
onBeforeSchemaCreated: async (context) => {
|
|
4147
|
-
if (_optionalChain([options, 'access',
|
|
4647
|
+
if (_optionalChain([options, 'access', _89 => _89.hooks, 'optionalAccess', _90 => _90.onBeforeSchemaCreated])) {
|
|
4148
4648
|
await options.hooks.onBeforeSchemaCreated(context);
|
|
4149
4649
|
}
|
|
4150
4650
|
},
|
|
@@ -4159,7 +4659,7 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
|
|
|
4159
4659
|
});
|
|
4160
4660
|
}
|
|
4161
4661
|
}
|
|
4162
|
-
if (_optionalChain([options, 'access',
|
|
4662
|
+
if (_optionalChain([options, 'access', _91 => _91.hooks, 'optionalAccess', _92 => _92.onAfterSchemaCreated])) {
|
|
4163
4663
|
await options.hooks.onAfterSchemaCreated(context);
|
|
4164
4664
|
}
|
|
4165
4665
|
}
|
|
@@ -4201,13 +4701,13 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
|
|
|
4201
4701
|
truncate: (truncateOptions) => pool.withTransaction(async (transaction) => {
|
|
4202
4702
|
await ensureSchemaExists();
|
|
4203
4703
|
await truncateTables(transaction.execute, truncateOptions);
|
|
4204
|
-
if (_optionalChain([truncateOptions, 'optionalAccess',
|
|
4704
|
+
if (_optionalChain([truncateOptions, 'optionalAccess', _93 => _93.truncateProjections])) {
|
|
4205
4705
|
const projectionContext = await transactionToPostgreSQLProjectionHandlerContext(
|
|
4206
4706
|
connectionString,
|
|
4207
4707
|
pool,
|
|
4208
4708
|
transaction
|
|
4209
4709
|
);
|
|
4210
|
-
for (const projection2 of _nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
4710
|
+
for (const projection2 of _nullishCoalesce(_optionalChain([options, 'optionalAccess', _94 => _94.projections]), () => ( []))) {
|
|
4211
4711
|
if (projection2.projection.truncate)
|
|
4212
4712
|
await projection2.projection.truncate(projectionContext);
|
|
4213
4713
|
}
|
|
@@ -4217,7 +4717,7 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
|
|
|
4217
4717
|
},
|
|
4218
4718
|
async aggregateStream(streamName, options2) {
|
|
4219
4719
|
const { evolve, initialState, read } = options2;
|
|
4220
|
-
const expectedStreamVersion = _optionalChain([read, 'optionalAccess',
|
|
4720
|
+
const expectedStreamVersion = _optionalChain([read, 'optionalAccess', _95 => _95.expectedStreamVersion]);
|
|
4221
4721
|
let state = initialState();
|
|
4222
4722
|
const result = await this.readStream(
|
|
4223
4723
|
streamName,
|
|
@@ -4256,7 +4756,7 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
|
|
|
4256
4756
|
pool,
|
|
4257
4757
|
streamName,
|
|
4258
4758
|
streamType,
|
|
4259
|
-
downcastRecordedMessages(events, _optionalChain([options2, 'optionalAccess',
|
|
4759
|
+
downcastRecordedMessages(events, _optionalChain([options2, 'optionalAccess', _96 => _96.schema, 'optionalAccess', _97 => _97.versioning])),
|
|
4260
4760
|
{
|
|
4261
4761
|
...options2,
|
|
4262
4762
|
beforeCommitHook
|
|
@@ -4266,7 +4766,7 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
|
|
|
4266
4766
|
throw new ExpectedVersionConflictError(
|
|
4267
4767
|
-1n,
|
|
4268
4768
|
//TODO: Return actual version in case of error
|
|
4269
|
-
_nullishCoalesce(_optionalChain([options2, 'optionalAccess',
|
|
4769
|
+
_nullishCoalesce(_optionalChain([options2, 'optionalAccess', _98 => _98.expectedStreamVersion]), () => ( NO_CONCURRENCY_CHECK))
|
|
4270
4770
|
);
|
|
4271
4771
|
return {
|
|
4272
4772
|
nextExpectedStreamVersion: appendResult.nextStreamPosition,
|
|
@@ -4311,259 +4811,11 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
|
|
|
4311
4811
|
};
|
|
4312
4812
|
};
|
|
4313
4813
|
|
|
4314
|
-
// src/eventStore/projections/postgresProjectionSpec.ts
|
|
4315
|
-
var PostgreSQLProjectionSpec = {
|
|
4316
|
-
for: (options) => {
|
|
4317
|
-
{
|
|
4318
|
-
const { projection: projection2, ...dumoOptions } = options;
|
|
4319
|
-
const { connectionString } = dumoOptions;
|
|
4320
|
-
let wasInitialised = false;
|
|
4321
|
-
const initialize = async (pool) => {
|
|
4322
|
-
const eventStore = getPostgreSQLEventStore(connectionString, {
|
|
4323
|
-
// TODO: This will need to change when we support other drivers
|
|
4324
|
-
connectionOptions: { dumbo: pool }
|
|
4325
|
-
});
|
|
4326
|
-
if (wasInitialised) return;
|
|
4327
|
-
wasInitialised = true;
|
|
4328
|
-
await eventStore.schema.migrate();
|
|
4329
|
-
if (projection2.init)
|
|
4330
|
-
await pool.withTransaction(async (transaction) => {
|
|
4331
|
-
await projection2.init({
|
|
4332
|
-
registrationType: "async",
|
|
4333
|
-
version: _nullishCoalesce(projection2.version, () => ( 1)),
|
|
4334
|
-
status: "active",
|
|
4335
|
-
context: await transactionToPostgreSQLProjectionHandlerContext(
|
|
4336
|
-
connectionString,
|
|
4337
|
-
pool,
|
|
4338
|
-
transaction
|
|
4339
|
-
)
|
|
4340
|
-
});
|
|
4341
|
-
});
|
|
4342
|
-
};
|
|
4343
|
-
return (givenEvents) => {
|
|
4344
|
-
return {
|
|
4345
|
-
when: (events, options2) => {
|
|
4346
|
-
const allEvents = [];
|
|
4347
|
-
const run = async (pool) => {
|
|
4348
|
-
let globalPosition = 0n;
|
|
4349
|
-
const numberOfTimes = _nullishCoalesce(_optionalChain([options2, 'optionalAccess', _80 => _80.numberOfTimes]), () => ( 1));
|
|
4350
|
-
for (const event of [
|
|
4351
|
-
...givenEvents,
|
|
4352
|
-
...Array.from({ length: numberOfTimes }).flatMap(() => events)
|
|
4353
|
-
]) {
|
|
4354
|
-
const metadata = {
|
|
4355
|
-
checkpoint: bigIntProcessorCheckpoint(++globalPosition),
|
|
4356
|
-
globalPosition,
|
|
4357
|
-
streamPosition: globalPosition,
|
|
4358
|
-
streamName: `test-${_uuid.v4.call(void 0, )}`,
|
|
4359
|
-
messageId: _uuid.v4.call(void 0, )
|
|
4360
|
-
};
|
|
4361
|
-
allEvents.push({
|
|
4362
|
-
...event,
|
|
4363
|
-
kind: "Event",
|
|
4364
|
-
metadata: {
|
|
4365
|
-
...metadata,
|
|
4366
|
-
..."metadata" in event ? _nullishCoalesce(event.metadata, () => ( {})) : {}
|
|
4367
|
-
}
|
|
4368
|
-
});
|
|
4369
|
-
}
|
|
4370
|
-
await initialize(pool);
|
|
4371
|
-
await pool.withTransaction(async (transaction) => {
|
|
4372
|
-
await handleProjections({
|
|
4373
|
-
events: allEvents,
|
|
4374
|
-
projections: [projection2],
|
|
4375
|
-
...await transactionToPostgreSQLProjectionHandlerContext(
|
|
4376
|
-
connectionString,
|
|
4377
|
-
pool,
|
|
4378
|
-
transaction
|
|
4379
|
-
)
|
|
4380
|
-
});
|
|
4381
|
-
});
|
|
4382
|
-
};
|
|
4383
|
-
return {
|
|
4384
|
-
then: async (assert, message) => {
|
|
4385
|
-
const pool = _dumbo.dumbo.call(void 0, dumoOptions);
|
|
4386
|
-
try {
|
|
4387
|
-
await run(pool);
|
|
4388
|
-
const succeeded = await assert({ pool, connectionString });
|
|
4389
|
-
if (succeeded !== void 0 && succeeded === false)
|
|
4390
|
-
assertFails(
|
|
4391
|
-
_nullishCoalesce(message, () => ( "Projection specification didn't match the criteria"))
|
|
4392
|
-
);
|
|
4393
|
-
} finally {
|
|
4394
|
-
await pool.close();
|
|
4395
|
-
}
|
|
4396
|
-
},
|
|
4397
|
-
thenThrows: async (...args) => {
|
|
4398
|
-
const pool = _dumbo.dumbo.call(void 0, dumoOptions);
|
|
4399
|
-
try {
|
|
4400
|
-
await run(pool);
|
|
4401
|
-
throw new AssertionError("Handler did not fail as expected");
|
|
4402
|
-
} catch (error) {
|
|
4403
|
-
if (error instanceof AssertionError) throw error;
|
|
4404
|
-
if (args.length === 0) return;
|
|
4405
|
-
if (!isErrorConstructor(args[0])) {
|
|
4406
|
-
assertTrue(
|
|
4407
|
-
args[0](error),
|
|
4408
|
-
`Error didn't match the error condition: ${_optionalChain([error, 'optionalAccess', _81 => _81.toString, 'call', _82 => _82()])}`
|
|
4409
|
-
);
|
|
4410
|
-
return;
|
|
4411
|
-
}
|
|
4412
|
-
assertTrue(
|
|
4413
|
-
error instanceof args[0],
|
|
4414
|
-
`Caught error is not an instance of the expected type: ${_optionalChain([error, 'optionalAccess', _83 => _83.toString, 'call', _84 => _84()])}`
|
|
4415
|
-
);
|
|
4416
|
-
if (args[1]) {
|
|
4417
|
-
assertTrue(
|
|
4418
|
-
args[1](error),
|
|
4419
|
-
`Error didn't match the error condition: ${_optionalChain([error, 'optionalAccess', _85 => _85.toString, 'call', _86 => _86()])}`
|
|
4420
|
-
);
|
|
4421
|
-
}
|
|
4422
|
-
} finally {
|
|
4423
|
-
await pool.close();
|
|
4424
|
-
}
|
|
4425
|
-
}
|
|
4426
|
-
};
|
|
4427
|
-
}
|
|
4428
|
-
};
|
|
4429
|
-
};
|
|
4430
|
-
}
|
|
4431
|
-
}
|
|
4432
|
-
};
|
|
4433
|
-
var eventInStream = (streamName, event) => {
|
|
4434
|
-
return {
|
|
4435
|
-
...event,
|
|
4436
|
-
metadata: {
|
|
4437
|
-
..._nullishCoalesce(event.metadata, () => ( {})),
|
|
4438
|
-
streamName: _nullishCoalesce(_optionalChain([event, 'access', _87 => _87.metadata, 'optionalAccess', _88 => _88.streamName]), () => ( streamName))
|
|
4439
|
-
}
|
|
4440
|
-
};
|
|
4441
|
-
};
|
|
4442
|
-
var eventsInStream = (streamName, events) => {
|
|
4443
|
-
return events.map((e) => eventInStream(streamName, e));
|
|
4444
|
-
};
|
|
4445
|
-
var newEventsInStream = eventsInStream;
|
|
4446
|
-
var assertSQLQueryResultMatches = (sql, rows) => async ({ pool: { execute } }) => {
|
|
4447
|
-
const result = await execute.query(sql);
|
|
4448
|
-
assertThatArray(rows).containsExactlyInAnyOrder(result.rows);
|
|
4449
|
-
};
|
|
4450
|
-
var expectSQL = {
|
|
4451
|
-
query: (sql) => ({
|
|
4452
|
-
resultRows: {
|
|
4453
|
-
toBeTheSame: (rows) => assertSQLQueryResultMatches(sql, rows)
|
|
4454
|
-
}
|
|
4455
|
-
})
|
|
4456
|
-
};
|
|
4457
|
-
|
|
4458
|
-
// src/eventStore/projections/postgreSQLProjection.ts
|
|
4459
|
-
var transactionToPostgreSQLProjectionHandlerContext = async (connectionString, pool, transaction) => ({
|
|
4460
|
-
execute: transaction.execute,
|
|
4461
|
-
connection: {
|
|
4462
|
-
connectionString,
|
|
4463
|
-
client: await transaction.connection.open(),
|
|
4464
|
-
transaction,
|
|
4465
|
-
pool
|
|
4466
|
-
}
|
|
4467
|
-
});
|
|
4468
|
-
var handleProjections = async (options) => {
|
|
4469
|
-
const {
|
|
4470
|
-
projections: allProjections,
|
|
4471
|
-
events,
|
|
4472
|
-
connection: { pool, transaction, connectionString },
|
|
4473
|
-
partition = defaultTag2
|
|
4474
|
-
} = options;
|
|
4475
|
-
const eventTypes = events.map((e) => e.type);
|
|
4476
|
-
const projections = allProjections.filter(
|
|
4477
|
-
(p) => p.canHandle.some((type) => eventTypes.includes(type))
|
|
4478
|
-
);
|
|
4479
|
-
const client = await transaction.connection.open();
|
|
4480
|
-
for (const projection2 of projections) {
|
|
4481
|
-
if (projection2.name) {
|
|
4482
|
-
const lockAcquired = await postgreSQLProjectionLock({
|
|
4483
|
-
projectionName: projection2.name,
|
|
4484
|
-
partition,
|
|
4485
|
-
version: _nullishCoalesce(projection2.version, () => ( 1))
|
|
4486
|
-
}).tryAcquire({ execute: transaction.execute });
|
|
4487
|
-
if (!lockAcquired) {
|
|
4488
|
-
continue;
|
|
4489
|
-
}
|
|
4490
|
-
}
|
|
4491
|
-
await projection2.handle(events, {
|
|
4492
|
-
connection: {
|
|
4493
|
-
connectionString,
|
|
4494
|
-
pool,
|
|
4495
|
-
client,
|
|
4496
|
-
transaction
|
|
4497
|
-
},
|
|
4498
|
-
execute: transaction.execute
|
|
4499
|
-
});
|
|
4500
|
-
}
|
|
4501
|
-
};
|
|
4502
|
-
var postgreSQLProjection = (definition) => projection({
|
|
4503
|
-
...definition,
|
|
4504
|
-
init: async (options) => {
|
|
4505
|
-
await registerProjection(options.context.execute, {
|
|
4506
|
-
// TODO: pass partition from options
|
|
4507
|
-
partition: defaultTag2,
|
|
4508
|
-
status: "active",
|
|
4509
|
-
registration: {
|
|
4510
|
-
type: "async",
|
|
4511
|
-
// TODO: fix this
|
|
4512
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any
|
|
4513
|
-
projection: definition
|
|
4514
|
-
}
|
|
4515
|
-
});
|
|
4516
|
-
if (definition.init) {
|
|
4517
|
-
await definition.init(options);
|
|
4518
|
-
}
|
|
4519
|
-
}
|
|
4520
|
-
});
|
|
4521
|
-
var postgreSQLRawBatchSQLProjection = (options) => postgreSQLProjection({
|
|
4522
|
-
name: options.name,
|
|
4523
|
-
kind: _nullishCoalesce(options.kind, () => ( "emt:projections:postgresql:raw_sql:batch")),
|
|
4524
|
-
version: options.version,
|
|
4525
|
-
canHandle: options.canHandle,
|
|
4526
|
-
eventsOptions: options.eventsOptions,
|
|
4527
|
-
handle: async (events, context) => {
|
|
4528
|
-
const sqls = await options.evolve(events, context);
|
|
4529
|
-
await context.execute.batchCommand(sqls);
|
|
4530
|
-
},
|
|
4531
|
-
init: async (initOptions) => {
|
|
4532
|
-
const initSQL = options.init ? await options.init(initOptions) : void 0;
|
|
4533
|
-
if (initSQL) {
|
|
4534
|
-
if (Array.isArray(initSQL)) {
|
|
4535
|
-
await initOptions.context.execute.batchCommand(initSQL);
|
|
4536
|
-
} else {
|
|
4537
|
-
await initOptions.context.execute.command(initSQL);
|
|
4538
|
-
}
|
|
4539
|
-
}
|
|
4540
|
-
}
|
|
4541
|
-
});
|
|
4542
|
-
var postgreSQLRawSQLProjection = (options) => {
|
|
4543
|
-
const { evolve, kind, ...rest } = options;
|
|
4544
|
-
return postgreSQLRawBatchSQLProjection({
|
|
4545
|
-
kind: _nullishCoalesce(kind, () => ( "emt:projections:postgresql:raw:_sql:single")),
|
|
4546
|
-
...rest,
|
|
4547
|
-
evolve: async (events, context) => {
|
|
4548
|
-
const sqls = [];
|
|
4549
|
-
for (const event of events) {
|
|
4550
|
-
const pendingSqls = await evolve(event, context);
|
|
4551
|
-
if (Array.isArray(pendingSqls)) {
|
|
4552
|
-
sqls.push(...pendingSqls);
|
|
4553
|
-
} else {
|
|
4554
|
-
sqls.push(pendingSqls);
|
|
4555
|
-
}
|
|
4556
|
-
}
|
|
4557
|
-
return sqls;
|
|
4558
|
-
}
|
|
4559
|
-
});
|
|
4560
|
-
};
|
|
4561
|
-
|
|
4562
4814
|
// src/eventStore/consumers/postgreSQLProcessor.ts
|
|
4563
4815
|
var postgreSQLCheckpointer = () => ({
|
|
4564
4816
|
read: async (options, context) => {
|
|
4565
4817
|
const result = await readProcessorCheckpoint(context.execute, options);
|
|
4566
|
-
return { lastCheckpoint: _optionalChain([result, 'optionalAccess',
|
|
4818
|
+
return { lastCheckpoint: _optionalChain([result, 'optionalAccess', _99 => _99.lastProcessedCheckpoint]) };
|
|
4567
4819
|
},
|
|
4568
4820
|
store: async (options, context) => {
|
|
4569
4821
|
const newCheckpoint = getCheckpoint(options.message);
|
|
@@ -4581,13 +4833,13 @@ var postgreSQLProcessingScope = (options) => {
|
|
|
4581
4833
|
const processorConnectionString = options.connectionString;
|
|
4582
4834
|
const processorPool = options.pool;
|
|
4583
4835
|
const processingScope = async (handler, partialContext) => {
|
|
4584
|
-
const connection = _optionalChain([partialContext, 'optionalAccess',
|
|
4585
|
-
const connectionString = _nullishCoalesce(processorConnectionString, () => ( _optionalChain([connection, 'optionalAccess',
|
|
4836
|
+
const connection = _optionalChain([partialContext, 'optionalAccess', _100 => _100.connection]);
|
|
4837
|
+
const connectionString = _nullishCoalesce(processorConnectionString, () => ( _optionalChain([connection, 'optionalAccess', _101 => _101.connectionString])));
|
|
4586
4838
|
if (!connectionString)
|
|
4587
4839
|
throw new EmmettError(
|
|
4588
4840
|
`PostgreSQL processor '${options.processorId}' is missing connection string. Ensure that you passed it through options`
|
|
4589
4841
|
);
|
|
4590
|
-
const pool = _nullishCoalesce((!processorConnectionString || connectionString == processorConnectionString ? _optionalChain([connection, 'optionalAccess',
|
|
4842
|
+
const pool = _nullishCoalesce((!processorConnectionString || connectionString == processorConnectionString ? _optionalChain([connection, 'optionalAccess', _102 => _102.pool]) : processorPool), () => ( processorPool));
|
|
4591
4843
|
if (!pool)
|
|
4592
4844
|
throw new EmmettError(
|
|
4593
4845
|
`PostgreSQL processor '${options.processorId}' is missing connection string. Ensure that you passed it through options`
|
|
@@ -4602,7 +4854,10 @@ var postgreSQLProcessingScope = (options) => {
|
|
|
4602
4854
|
connectionString,
|
|
4603
4855
|
pool,
|
|
4604
4856
|
client,
|
|
4605
|
-
transaction
|
|
4857
|
+
transaction,
|
|
4858
|
+
messageStore: getPostgreSQLEventStore(connectionString, {
|
|
4859
|
+
connectionOptions: { client }
|
|
4860
|
+
})
|
|
4606
4861
|
}
|
|
4607
4862
|
});
|
|
4608
4863
|
});
|
|
@@ -4628,11 +4883,11 @@ var wrapHooksWithProcessorLocks = (hooks, processorLock) => ({
|
|
|
4628
4883
|
..._nullishCoalesce(hooks, () => ( {})),
|
|
4629
4884
|
onStart: async (context) => {
|
|
4630
4885
|
await processorLock.tryAcquire({ execute: context.execute });
|
|
4631
|
-
if (_optionalChain([hooks, 'optionalAccess',
|
|
4886
|
+
if (_optionalChain([hooks, 'optionalAccess', _103 => _103.onStart])) await hooks.onStart(context);
|
|
4632
4887
|
},
|
|
4633
|
-
onClose: _optionalChain([hooks, 'optionalAccess',
|
|
4888
|
+
onClose: _optionalChain([hooks, 'optionalAccess', _104 => _104.onClose]) || processorLock ? async (context) => {
|
|
4634
4889
|
await processorLock.release({ execute: context.execute });
|
|
4635
|
-
if (_optionalChain([hooks, 'optionalAccess',
|
|
4890
|
+
if (_optionalChain([hooks, 'optionalAccess', _105 => _105.onClose])) await hooks.onClose(context);
|
|
4636
4891
|
} : void 0
|
|
4637
4892
|
});
|
|
4638
4893
|
var postgreSQLProjector = (options) => {
|
|
@@ -4657,13 +4912,13 @@ var postgreSQLProjector = (options) => {
|
|
|
4657
4912
|
version: _nullishCoalesce(options.projection.version, () => ( version)),
|
|
4658
4913
|
handlingType: "async"
|
|
4659
4914
|
} : void 0,
|
|
4660
|
-
lockAcquisitionPolicy: _nullishCoalesce(_optionalChain([lock, 'optionalAccess',
|
|
4661
|
-
lockTimeoutSeconds: _optionalChain([lock, 'optionalAccess',
|
|
4915
|
+
lockAcquisitionPolicy: _nullishCoalesce(_optionalChain([lock, 'optionalAccess', _106 => _106.acquisitionPolicy]), () => ( DefaultPostgreSQLProcessorLockPolicy)),
|
|
4916
|
+
lockTimeoutSeconds: _optionalChain([lock, 'optionalAccess', _107 => _107.timeoutSeconds])
|
|
4662
4917
|
});
|
|
4663
4918
|
const hooks = wrapHooksWithProcessorLocks(
|
|
4664
4919
|
{
|
|
4665
4920
|
..._nullishCoalesce(options.hooks, () => ( {})),
|
|
4666
|
-
onInit: options.projection.init !== void 0 || _optionalChain([options, 'access',
|
|
4921
|
+
onInit: options.projection.init !== void 0 || _optionalChain([options, 'access', _108 => _108.hooks, 'optionalAccess', _109 => _109.onInit]) ? async (context) => {
|
|
4667
4922
|
if (options.projection.init)
|
|
4668
4923
|
await options.projection.init({
|
|
4669
4924
|
version: _nullishCoalesce(options.projection.version, () => ( version)),
|
|
@@ -4674,16 +4929,16 @@ var postgreSQLProjector = (options) => {
|
|
|
4674
4929
|
migrationOptions: options.migrationOptions
|
|
4675
4930
|
}
|
|
4676
4931
|
});
|
|
4677
|
-
if (_optionalChain([options, 'access',
|
|
4932
|
+
if (_optionalChain([options, 'access', _110 => _110.hooks, 'optionalAccess', _111 => _111.onInit]))
|
|
4678
4933
|
await options.hooks.onInit({
|
|
4679
4934
|
...context,
|
|
4680
4935
|
migrationOptions: options.migrationOptions
|
|
4681
4936
|
});
|
|
4682
|
-
} : _optionalChain([options, 'access',
|
|
4937
|
+
} : _optionalChain([options, 'access', _112 => _112.hooks, 'optionalAccess', _113 => _113.onInit]),
|
|
4683
4938
|
onClose: close ? async (context) => {
|
|
4684
|
-
if (_optionalChain([options, 'access',
|
|
4939
|
+
if (_optionalChain([options, 'access', _114 => _114.hooks, 'optionalAccess', _115 => _115.onClose])) await _optionalChain([options, 'access', _116 => _116.hooks, 'optionalAccess', _117 => _117.onClose, 'call', _118 => _118(context)]);
|
|
4685
4940
|
if (close) await close();
|
|
4686
|
-
} : _optionalChain([options, 'access',
|
|
4941
|
+
} : _optionalChain([options, 'access', _119 => _119.hooks, 'optionalAccess', _120 => _120.onClose])
|
|
4687
4942
|
},
|
|
4688
4943
|
processorLock
|
|
4689
4944
|
);
|
|
@@ -4704,6 +4959,53 @@ var postgreSQLProjector = (options) => {
|
|
|
4704
4959
|
});
|
|
4705
4960
|
return processor;
|
|
4706
4961
|
};
|
|
4962
|
+
var postgreSQLWorkflowProcessor = (options) => {
|
|
4963
|
+
const {
|
|
4964
|
+
processorId = _nullishCoalesce(options.processorId, () => ( getWorkflowId({
|
|
4965
|
+
workflowName: _nullishCoalesce(options.workflow.name, () => ( "unknown"))
|
|
4966
|
+
}))),
|
|
4967
|
+
processorInstanceId = getProcessorInstanceId(processorId),
|
|
4968
|
+
version = defaultProcessorVersion,
|
|
4969
|
+
partition = defaultProcessorPartition,
|
|
4970
|
+
lock
|
|
4971
|
+
} = options;
|
|
4972
|
+
const { pool, connectionString, close } = getProcessorPool(options);
|
|
4973
|
+
const processorLock = postgreSQLProcessorLock({
|
|
4974
|
+
processorId,
|
|
4975
|
+
version,
|
|
4976
|
+
partition,
|
|
4977
|
+
processorInstanceId,
|
|
4978
|
+
projection: void 0,
|
|
4979
|
+
lockAcquisitionPolicy: _nullishCoalesce(_optionalChain([lock, 'optionalAccess', _121 => _121.acquisitionPolicy]), () => ( DefaultPostgreSQLProcessorLockPolicy)),
|
|
4980
|
+
lockTimeoutSeconds: _optionalChain([lock, 'optionalAccess', _122 => _122.timeoutSeconds])
|
|
4981
|
+
});
|
|
4982
|
+
const hooks = wrapHooksWithProcessorLocks(
|
|
4983
|
+
{
|
|
4984
|
+
..._nullishCoalesce(options.hooks, () => ( {})),
|
|
4985
|
+
onClose: close ? async (context) => {
|
|
4986
|
+
if (_optionalChain([options, 'access', _123 => _123.hooks, 'optionalAccess', _124 => _124.onClose]))
|
|
4987
|
+
await _optionalChain([options, 'access', _125 => _125.hooks, 'optionalAccess', _126 => _126.onClose, 'call', _127 => _127(context)]);
|
|
4988
|
+
if (close) await close();
|
|
4989
|
+
} : _optionalChain([options, 'access', _128 => _128.hooks, 'optionalAccess', _129 => _129.onClose])
|
|
4990
|
+
},
|
|
4991
|
+
processorLock
|
|
4992
|
+
);
|
|
4993
|
+
return workflowProcessor({
|
|
4994
|
+
...options,
|
|
4995
|
+
processorId,
|
|
4996
|
+
processorInstanceId,
|
|
4997
|
+
version,
|
|
4998
|
+
partition,
|
|
4999
|
+
hooks,
|
|
5000
|
+
processingScope: postgreSQLProcessingScope({
|
|
5001
|
+
pool,
|
|
5002
|
+
connectionString,
|
|
5003
|
+
processorId,
|
|
5004
|
+
partition
|
|
5005
|
+
}),
|
|
5006
|
+
checkpoints: postgreSQLCheckpointer()
|
|
5007
|
+
});
|
|
5008
|
+
};
|
|
4707
5009
|
var postgreSQLReactor = (options) => {
|
|
4708
5010
|
const {
|
|
4709
5011
|
processorId = options.processorId,
|
|
@@ -4719,16 +5021,16 @@ var postgreSQLReactor = (options) => {
|
|
|
4719
5021
|
partition,
|
|
4720
5022
|
processorInstanceId,
|
|
4721
5023
|
projection: void 0,
|
|
4722
|
-
lockAcquisitionPolicy: _nullishCoalesce(_optionalChain([lock, 'optionalAccess',
|
|
4723
|
-
lockTimeoutSeconds: _optionalChain([lock, 'optionalAccess',
|
|
5024
|
+
lockAcquisitionPolicy: _nullishCoalesce(_optionalChain([lock, 'optionalAccess', _130 => _130.acquisitionPolicy]), () => ( DefaultPostgreSQLProcessorLockPolicy)),
|
|
5025
|
+
lockTimeoutSeconds: _optionalChain([lock, 'optionalAccess', _131 => _131.timeoutSeconds])
|
|
4724
5026
|
});
|
|
4725
5027
|
const hooks = wrapHooksWithProcessorLocks(
|
|
4726
5028
|
{
|
|
4727
5029
|
..._nullishCoalesce(options.hooks, () => ( {})),
|
|
4728
5030
|
onClose: close ? async (context) => {
|
|
4729
|
-
if (_optionalChain([options, 'access',
|
|
5031
|
+
if (_optionalChain([options, 'access', _132 => _132.hooks, 'optionalAccess', _133 => _133.onClose])) await _optionalChain([options, 'access', _134 => _134.hooks, 'optionalAccess', _135 => _135.onClose, 'call', _136 => _136(context)]);
|
|
4730
5032
|
if (close) await close();
|
|
4731
|
-
} : _optionalChain([options, 'access',
|
|
5033
|
+
} : _optionalChain([options, 'access', _137 => _137.hooks, 'optionalAccess', _138 => _138.onClose])
|
|
4732
5034
|
},
|
|
4733
5035
|
processorLock
|
|
4734
5036
|
);
|
|
@@ -4777,7 +5079,7 @@ var postgreSQLEventStoreConsumer = (options) => {
|
|
|
4777
5079
|
})
|
|
4778
5080
|
);
|
|
4779
5081
|
return result.some(
|
|
4780
|
-
(r) => r.status === "fulfilled" && _optionalChain([r, 'access',
|
|
5082
|
+
(r) => r.status === "fulfilled" && _optionalChain([r, 'access', _139 => _139.value, 'optionalAccess', _140 => _140.type]) !== "STOP"
|
|
4781
5083
|
) ? void 0 : {
|
|
4782
5084
|
type: "STOP"
|
|
4783
5085
|
};
|
|
@@ -4788,7 +5090,8 @@ var postgreSQLEventStoreConsumer = (options) => {
|
|
|
4788
5090
|
connectionString: options.connectionString,
|
|
4789
5091
|
pool,
|
|
4790
5092
|
client: void 0,
|
|
4791
|
-
transaction: void 0
|
|
5093
|
+
transaction: void 0,
|
|
5094
|
+
messageStore: void 0
|
|
4792
5095
|
}
|
|
4793
5096
|
};
|
|
4794
5097
|
const stopProcessors = () => Promise.all(processors.map((p) => p.close(processorContext)));
|
|
@@ -4796,7 +5099,7 @@ var postgreSQLEventStoreConsumer = (options) => {
|
|
|
4796
5099
|
if (!isRunning) return;
|
|
4797
5100
|
isRunning = false;
|
|
4798
5101
|
if (messagePuller) {
|
|
4799
|
-
_optionalChain([abortController, 'optionalAccess',
|
|
5102
|
+
_optionalChain([abortController, 'optionalAccess', _141 => _141.abort, 'call', _142 => _142()]);
|
|
4800
5103
|
await messagePuller.stop();
|
|
4801
5104
|
messagePuller = void 0;
|
|
4802
5105
|
abortController = null;
|
|
@@ -4837,6 +5140,14 @@ var postgreSQLEventStoreConsumer = (options) => {
|
|
|
4837
5140
|
);
|
|
4838
5141
|
return processor;
|
|
4839
5142
|
},
|
|
5143
|
+
workflowProcessor: (options2) => {
|
|
5144
|
+
const processor = postgreSQLWorkflowProcessor(options2);
|
|
5145
|
+
processors.push(
|
|
5146
|
+
// TODO: change that
|
|
5147
|
+
processor
|
|
5148
|
+
);
|
|
5149
|
+
return processor;
|
|
5150
|
+
},
|
|
4840
5151
|
start: () => {
|
|
4841
5152
|
if (isRunning) return start;
|
|
4842
5153
|
if (processors.length === 0)
|
|
@@ -4849,8 +5160,8 @@ var postgreSQLEventStoreConsumer = (options) => {
|
|
|
4849
5160
|
stopWhen: options.stopWhen,
|
|
4850
5161
|
executor: pool.execute,
|
|
4851
5162
|
eachBatch,
|
|
4852
|
-
batchSize: _nullishCoalesce(_optionalChain([pulling, 'optionalAccess',
|
|
4853
|
-
pullingFrequencyInMs: _nullishCoalesce(_optionalChain([pulling, 'optionalAccess',
|
|
5163
|
+
batchSize: _nullishCoalesce(_optionalChain([pulling, 'optionalAccess', _143 => _143.batchSize]), () => ( DefaultPostgreSQLEventStoreProcessorBatchSize)),
|
|
5164
|
+
pullingFrequencyInMs: _nullishCoalesce(_optionalChain([pulling, 'optionalAccess', _144 => _144.pullingFrequencyInMs]), () => ( DefaultPostgreSQLEventStoreProcessorPullingFrequencyInMs)),
|
|
4854
5165
|
signal: abortController.signal
|
|
4855
5166
|
});
|
|
4856
5167
|
start = (async () => {
|
|
@@ -5014,5 +5325,6 @@ var rebuildPostgreSQLProjections = (options) => {
|
|
|
5014
5325
|
|
|
5015
5326
|
|
|
5016
5327
|
|
|
5017
|
-
|
|
5328
|
+
|
|
5329
|
+
exports.DefaultPostgreSQLEventStoreProcessorBatchSize = DefaultPostgreSQLEventStoreProcessorBatchSize; exports.DefaultPostgreSQLEventStoreProcessorPullingFrequencyInMs = DefaultPostgreSQLEventStoreProcessorPullingFrequencyInMs; exports.DefaultPostgreSQLProcessorLockPolicy = DefaultPostgreSQLProcessorLockPolicy; exports.PostgreSQLEventStoreDefaultStreamVersion = PostgreSQLEventStoreDefaultStreamVersion; exports.PostgreSQLProjectionSpec = PostgreSQLProjectionSpec; exports.activateProjection = activateProjection; exports.activateProjectionSQL = activateProjectionSQL; exports.addDefaultPartitionSQL = addDefaultPartitionSQL; exports.addModuleForAllTenantsSQL = addModuleForAllTenantsSQL; exports.addModuleSQL = addModuleSQL; exports.addPartitionSQL = addPartitionSQL; exports.addTablePartitions = addTablePartitions; exports.addTenantForAllModulesSQL = addTenantForAllModulesSQL; exports.addTenantSQL = addTenantSQL; exports.appendToStream = appendToStream; exports.appendToStreamSQL = appendToStreamSQL; exports.assertSQLQueryResultMatches = assertSQLQueryResultMatches; exports.callActivateProjection = callActivateProjection; exports.callAppendToStream = callAppendToStream; exports.callDeactivateProjection = callDeactivateProjection; exports.callRegisterProjection = callRegisterProjection; exports.callReleaseProcessorLock = callReleaseProcessorLock; exports.callStoreProcessorCheckpoint = callStoreProcessorCheckpoint; exports.callTryAcquireProcessorLock = callTryAcquireProcessorLock; exports.callTryAcquireProjectionLock = callTryAcquireProjectionLock; exports.cleanupLegacySubscriptionTables = cleanupLegacySubscriptionTables; exports.createEventStoreSchema = createEventStoreSchema; exports.deactivateProjection = deactivateProjection; exports.deactivateProjectionSQL = deactivateProjectionSQL; exports.defaultPostgreSQLOptions = defaultPostgreSQLOptions; exports.defaultTag = defaultTag2; exports.documentDoesNotExist = documentDoesNotExist; exports.documentExists = documentExists; exports.documentMatchingExists = documentMatchingExists; exports.documentsAreTheSame = documentsAreTheSame; exports.documentsMatchingHaveCount = documentsMatchingHaveCount; exports.emmettPrefix = emmettPrefix2; exports.eventInStream = eventInStream; exports.eventStoreSchemaMigrations = eventStoreSchemaMigrations; exports.eventsInStream = eventsInStream; exports.expectPongoDocuments = expectPongoDocuments; exports.expectSQL = expectSQL; exports.getPostgreSQLEventStore = getPostgreSQLEventStore; exports.globalNames = globalNames; exports.globalTag = globalTag; exports.handleProjections = handleProjections; exports.messagesTable = messagesTable; exports.messagesTableSQL = messagesTableSQL; exports.newEventsInStream = newEventsInStream; exports.pongoMultiStreamProjection = pongoMultiStreamProjection; exports.pongoProjection = pongoProjection; exports.pongoSingleStreamProjection = pongoSingleStreamProjection; exports.postgreSQLCheckpointer = postgreSQLCheckpointer; exports.postgreSQLEventStoreConsumer = postgreSQLEventStoreConsumer; exports.postgreSQLEventStoreMessageBatchPuller = postgreSQLEventStoreMessageBatchPuller; exports.postgreSQLProcessorLock = postgreSQLProcessorLock; exports.postgreSQLProjection = postgreSQLProjection; exports.postgreSQLProjectionLock = postgreSQLProjectionLock; exports.postgreSQLProjector = postgreSQLProjector; exports.postgreSQLRawBatchSQLProjection = postgreSQLRawBatchSQLProjection; exports.postgreSQLRawSQLProjection = postgreSQLRawSQLProjection; exports.postgreSQLReactor = postgreSQLReactor; exports.postgreSQLWorkflowProcessor = postgreSQLWorkflowProcessor; exports.processorsTable = processorsTable; exports.processorsTableSQL = processorsTableSQL; exports.projectionsTable = projectionsTable; exports.projectionsTableSQL = projectionsTableSQL; exports.readLastMessageGlobalPosition = readLastMessageGlobalPosition; exports.readMessagesBatch = readMessagesBatch; exports.readProcessorCheckpoint = readProcessorCheckpoint; exports.readProjectionInfo = readProjectionInfo; exports.readStream = readStream; exports.rebuildPostgreSQLProjections = rebuildPostgreSQLProjections; exports.registerProjection = registerProjection; exports.registerProjectionSQL = registerProjectionSQL; exports.releaseProcessorLockSQL = releaseProcessorLockSQL; exports.sanitizeNameSQL = sanitizeNameSQL; exports.schemaMigration = schemaMigration; exports.schemaSQL = schemaSQL; exports.storeProcessorCheckpoint = storeProcessorCheckpoint; exports.storeSubscriptionCheckpointSQL = storeSubscriptionCheckpointSQL; exports.streamExists = streamExists; exports.streamsTable = streamsTable; exports.streamsTableSQL = streamsTableSQL; exports.toProcessorLockKey = toProcessorLockKey; exports.toProjectionLockKey = toProjectionLockKey; exports.transactionToPostgreSQLProjectionHandlerContext = transactionToPostgreSQLProjectionHandlerContext; exports.tryAcquireProcessorLockSQL = tryAcquireProcessorLockSQL; exports.tryAcquireProjectionLockSQL = tryAcquireProjectionLockSQL; exports.unknownTag = unknownTag2; exports.zipPostgreSQLEventStoreMessageBatchPullerStartFrom = zipPostgreSQLEventStoreMessageBatchPullerStartFrom;
|
|
5018
5330
|
//# sourceMappingURL=index.cjs.map
|