@event-driven-io/emmett-postgresql 0.42.0-rc.2 → 0.42.1-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 CHANGED
@@ -657,19 +657,39 @@ var reactor = (options) => {
657
657
  type,
658
658
  init,
659
659
  start: async (startOptions) => {
660
- if (isActive) return;
660
+ if (isActive) {
661
+ console.log(
662
+ `Processor ${processorId} with instance id ${instanceId} is already active. Start request ignored.`
663
+ );
664
+ return;
665
+ }
666
+ console.log(
667
+ `Starting processor ${processorId} with instance id ${instanceId}`
668
+ );
661
669
  await init(startOptions);
662
670
  isActive = true;
663
671
  closeSignal = onShutdown(() => close({}));
664
- if (lastCheckpoint !== null)
672
+ if (lastCheckpoint !== null) {
673
+ console.log(
674
+ `Processor ${processorId} started with instance id ${instanceId}, checkpoint: ${JSONParser.stringify(lastCheckpoint)}`
675
+ );
665
676
  return {
666
677
  lastCheckpoint
667
678
  };
679
+ }
668
680
  return await processingScope(async (context) => {
669
681
  if (hooks.onStart) {
682
+ console.log(
683
+ `Executing onStart hook for processor ${processorId} with instance id ${instanceId}`
684
+ );
670
685
  await hooks.onStart(context);
671
686
  }
672
- if (startFrom && startFrom !== "CURRENT") return startFrom;
687
+ if (startFrom && startFrom !== "CURRENT") {
688
+ console.log(
689
+ `Processor ${processorId} with instance id ${instanceId} starting from: ${JSONParser.stringify(startFrom)}`
690
+ );
691
+ return startFrom;
692
+ }
673
693
  if (checkpoints) {
674
694
  const readResult = await _optionalChain([checkpoints, 'optionalAccess', _20 => _20.read, 'call', _21 => _21(
675
695
  {
@@ -680,7 +700,15 @@ var reactor = (options) => {
680
700
  )]);
681
701
  lastCheckpoint = readResult.lastCheckpoint;
682
702
  }
683
- if (lastCheckpoint === null) return "BEGINNING";
703
+ if (lastCheckpoint === null) {
704
+ console.log(
705
+ `Processor ${processorId} with instance id ${instanceId} starting from: BEGINNING`
706
+ );
707
+ return "BEGINNING";
708
+ }
709
+ console.log(
710
+ `Checkpoint read for processor ${processorId} with instance id ${instanceId}: ${JSONParser.stringify(lastCheckpoint)}`
711
+ );
684
712
  return {
685
713
  lastCheckpoint
686
714
  };
@@ -692,48 +720,64 @@ var reactor = (options) => {
692
720
  },
693
721
  handle: async (messages, partialContext) => {
694
722
  if (!isActive) return Promise.resolve();
695
- return await processingScope(async (context) => {
696
- let result = void 0;
697
- for (const message2 of messages) {
698
- if (wasMessageHandled(message2, lastCheckpoint)) continue;
699
- const upcasted = upcastRecordedMessage(
700
- // TODO: Make it smarter
701
- message2,
702
- _optionalChain([options, 'access', _22 => _22.messageOptions, 'optionalAccess', _23 => _23.schema, 'optionalAccess', _24 => _24.versioning])
703
- );
704
- if (canHandle !== void 0 && !canHandle.includes(upcasted.type))
705
- continue;
706
- const messageProcessingResult = await eachMessage(upcasted, context);
707
- if (checkpoints) {
708
- const storeCheckpointResult = await checkpoints.store(
709
- {
710
- processorId,
711
- version,
712
- message: upcasted,
713
- lastCheckpoint,
714
- partition
715
- },
723
+ try {
724
+ return await processingScope(async (context) => {
725
+ let result = void 0;
726
+ for (const message2 of messages) {
727
+ if (wasMessageHandled(message2, lastCheckpoint)) continue;
728
+ const upcasted = upcastRecordedMessage(
729
+ // TODO: Make it smarter
730
+ message2,
731
+ _optionalChain([options, 'access', _22 => _22.messageOptions, 'optionalAccess', _23 => _23.schema, 'optionalAccess', _24 => _24.versioning])
732
+ );
733
+ if (canHandle !== void 0 && !canHandle.includes(upcasted.type))
734
+ continue;
735
+ const messageProcessingResult = await eachMessage(
736
+ upcasted,
716
737
  context
717
738
  );
718
- if (storeCheckpointResult.success) {
719
- lastCheckpoint = storeCheckpointResult.newCheckpoint;
739
+ if (checkpoints) {
740
+ const storeCheckpointResult = await checkpoints.store(
741
+ {
742
+ processorId,
743
+ version,
744
+ message: upcasted,
745
+ lastCheckpoint,
746
+ partition
747
+ },
748
+ context
749
+ );
750
+ if (storeCheckpointResult.success) {
751
+ lastCheckpoint = storeCheckpointResult.newCheckpoint;
752
+ }
720
753
  }
754
+ if (messageProcessingResult && messageProcessingResult.type === "STOP") {
755
+ isActive = false;
756
+ result = messageProcessingResult;
757
+ break;
758
+ }
759
+ if (stopAfter && stopAfter(upcasted)) {
760
+ isActive = false;
761
+ result = { type: "STOP", reason: "Stop condition reached" };
762
+ break;
763
+ }
764
+ if (messageProcessingResult && messageProcessingResult.type === "SKIP")
765
+ continue;
721
766
  }
722
- if (messageProcessingResult && messageProcessingResult.type === "STOP") {
723
- isActive = false;
724
- result = messageProcessingResult;
725
- break;
726
- }
727
- if (stopAfter && stopAfter(upcasted)) {
728
- isActive = false;
729
- result = { type: "STOP", reason: "Stop condition reached" };
730
- break;
731
- }
732
- if (messageProcessingResult && messageProcessingResult.type === "SKIP")
733
- continue;
734
- }
735
- return result;
736
- }, partialContext);
767
+ return result;
768
+ }, partialContext);
769
+ } catch (error2) {
770
+ console.log(
771
+ `Error during message processing for processor ${processorId} with instance id ${instanceId}. Stopping the processor.`,
772
+ error2
773
+ );
774
+ isActive = false;
775
+ return {
776
+ type: "STOP",
777
+ error: error2,
778
+ reason: "Error during message processing"
779
+ };
780
+ }
737
781
  }
738
782
  };
739
783
  };
@@ -828,32 +872,40 @@ var postgreSQLEventStoreMessageBatchPuller = ({
828
872
  let isRunning = false;
829
873
  let start;
830
874
  const pullMessages = async (options) => {
831
- const after = options.startFrom === "BEGINNING" ? 0n : options.startFrom === "END" ? await _asyncNullishCoalesce((await readLastMessageGlobalPosition(executor)).currentGlobalPosition, async () => ( 0n)) : options.startFrom.lastCheckpoint;
832
- const readMessagesOptions = {
833
- after,
834
- batchSize
835
- };
836
- let waitTime = 100;
837
- while (isRunning && !_optionalChain([signal, 'optionalAccess', _37 => _37.aborted])) {
838
- const { messages, currentGlobalPosition, areMessagesLeft } = await readMessagesBatch(executor, readMessagesOptions);
839
- if (messages.length > 0) {
840
- const result = await eachBatch(messages);
841
- if (result && result.type === "STOP") {
875
+ try {
876
+ const after = options.startFrom === "BEGINNING" ? 0n : options.startFrom === "END" ? await _asyncNullishCoalesce((await readLastMessageGlobalPosition(executor)).currentGlobalPosition, async () => ( 0n)) : options.startFrom.lastCheckpoint;
877
+ const readMessagesOptions = {
878
+ after,
879
+ batchSize
880
+ };
881
+ let waitTime = 100;
882
+ while (isRunning && !_optionalChain([signal, 'optionalAccess', _37 => _37.aborted])) {
883
+ const { messages, currentGlobalPosition, areMessagesLeft } = await readMessagesBatch(executor, readMessagesOptions);
884
+ if (messages.length > 0) {
885
+ const result = await eachBatch(messages);
886
+ if (result && result.type === "STOP") {
887
+ isRunning = false;
888
+ break;
889
+ }
890
+ }
891
+ readMessagesOptions.after = currentGlobalPosition;
892
+ await new Promise((resolve) => setTimeout(resolve, waitTime));
893
+ if (_optionalChain([stopWhen, 'optionalAccess', _38 => _38.noMessagesLeft]) === true && !areMessagesLeft) {
894
+ console.log(
895
+ `No messages left to process after reaching global position ${currentGlobalPosition}. Stopping the puller.`
896
+ );
842
897
  isRunning = false;
843
898
  break;
844
899
  }
900
+ if (!areMessagesLeft) {
901
+ waitTime = Math.min(waitTime * 2, 1e3);
902
+ } else {
903
+ waitTime = pullingFrequencyInMs;
904
+ }
845
905
  }
846
- readMessagesOptions.after = currentGlobalPosition;
847
- await new Promise((resolve) => setTimeout(resolve, waitTime));
848
- if (_optionalChain([stopWhen, 'optionalAccess', _38 => _38.noMessagesLeft]) === true && !areMessagesLeft) {
849
- isRunning = false;
850
- break;
851
- }
852
- if (!areMessagesLeft) {
853
- waitTime = Math.min(waitTime * 2, 1e3);
854
- } else {
855
- waitTime = pullingFrequencyInMs;
856
- }
906
+ } catch (error) {
907
+ console.log("Error occurred during message pulling:", error);
908
+ throw error;
857
909
  }
858
910
  };
859
911
  return {
@@ -886,16 +938,19 @@ var zipPostgreSQLEventStoreMessageBatchPullerStartFrom = (options) => {
886
938
 
887
939
 
888
940
 
941
+
942
+
943
+
889
944
  // src/eventStore/consumers/postgreSQLProcessor.ts
890
945
 
891
946
 
892
947
 
893
948
  require('pg');
894
949
 
895
- // src/eventStore/projections/locks/tryAcquireProcessorLock.ts
950
+ // src/eventStore/projections/locks/tryAcquireProjectionLock.ts
896
951
 
897
952
 
898
- // src/eventStore/schema/processors/processorsLocks.ts
953
+ // src/eventStore/schema/projections/projectionsLocks.ts
899
954
 
900
955
 
901
956
  // src/eventStore/schema/createFunctionIfDoesNotExist.ts
@@ -911,7 +966,97 @@ END $$;
911
966
  `
912
967
  );
913
968
 
969
+ // src/eventStore/schema/projections/projectionsLocks.ts
970
+ var tryAcquireProjectionLockSQL = createFunctionIfDoesNotExistSQL(
971
+ "emt_try_acquire_projection_lock",
972
+ `
973
+ CREATE OR REPLACE FUNCTION emt_try_acquire_projection_lock(
974
+ p_lock_key BIGINT,
975
+ p_partition TEXT,
976
+ p_name TEXT,
977
+ p_version INT
978
+ )
979
+ RETURNS TABLE (acquired BOOLEAN, is_active BOOLEAN)
980
+ LANGUAGE plpgsql
981
+ AS $emt_try_acquire_projection_lock$
982
+ BEGIN
983
+ RETURN QUERY
984
+ WITH lock_check AS (
985
+ SELECT pg_try_advisory_xact_lock_shared(p_lock_key) AS acquired
986
+ ),
987
+ status_check AS (
988
+ SELECT status = 'active' AS is_active
989
+ FROM ${projectionsTable.name}
990
+ WHERE partition = p_partition AND name = p_name AND version = p_version
991
+ )
992
+ SELECT
993
+ COALESCE((SELECT lc.acquired FROM lock_check lc), false),
994
+ COALESCE((SELECT sc.is_active FROM status_check sc), true);
995
+ END;
996
+ $emt_try_acquire_projection_lock$;
997
+ `
998
+ );
999
+ var callTryAcquireProjectionLock = (params) => _dumbo.sql.call(void 0,
1000
+ `SELECT * FROM emt_try_acquire_projection_lock(%s::BIGINT, %L, %L, %s);`,
1001
+ params.lockKey,
1002
+ params.partition,
1003
+ params.name,
1004
+ params.version
1005
+ );
1006
+
1007
+ // src/eventStore/projections/locks/tryAcquireProjectionLock.ts
1008
+ var tryAcquireProjectionLock = async (execute, {
1009
+ lockKey,
1010
+ projectionName,
1011
+ partition,
1012
+ version
1013
+ }) => {
1014
+ const lockKeyBigInt = isBigint(lockKey) ? lockKey : await hashText(lockKey);
1015
+ const { acquired, is_active } = await _dumbo.single.call(void 0,
1016
+ execute.query(
1017
+ callTryAcquireProjectionLock({
1018
+ lockKey: lockKeyBigInt.toString(),
1019
+ partition,
1020
+ name: projectionName,
1021
+ version
1022
+ })
1023
+ )
1024
+ );
1025
+ return acquired === true && is_active === true;
1026
+ };
1027
+
1028
+ // src/eventStore/projections/locks/postgreSQLProjectionLock.ts
1029
+ var postgreSQLProjectionLock = (options) => {
1030
+ let acquired = false;
1031
+ const lockKey = _nullishCoalesce(options.lockKey, () => ( toProjectionLockKey(options)));
1032
+ return {
1033
+ tryAcquire: async (context) => {
1034
+ if (acquired) {
1035
+ return true;
1036
+ }
1037
+ acquired = await tryAcquireProjectionLock(context.execute, {
1038
+ ...options,
1039
+ lockKey
1040
+ });
1041
+ return acquired;
1042
+ },
1043
+ release: (_context) => {
1044
+ if (!acquired) return;
1045
+ acquired = false;
1046
+ }
1047
+ };
1048
+ };
1049
+ var toProjectionLockKey = ({
1050
+ projectionName,
1051
+ partition,
1052
+ version
1053
+ }) => `${partition}:${projectionName}:${version}`;
1054
+
1055
+ // src/eventStore/projections/locks/tryAcquireProcessorLock.ts
1056
+
1057
+
914
1058
  // src/eventStore/schema/processors/processorsLocks.ts
1059
+
915
1060
  var tryAcquireProcessorLockSQL = createFunctionIfDoesNotExistSQL(
916
1061
  "emt_try_acquire_processor_lock",
917
1062
  `
@@ -1067,10 +1212,13 @@ var tryAcquireProcessorLock = async (execute, options) => {
1067
1212
  })
1068
1213
  )
1069
1214
  );
1215
+ console.log(
1216
+ `Lock acquisition attempt for processor '${options.processorId}' with processor instance '${options.processorInstanceId}' and lock '${options.lockKey}' resulted in: ${acquired}`
1217
+ );
1070
1218
  return acquired ? { acquired: true, checkpoint } : { acquired: false };
1071
1219
  };
1072
1220
  var tryAcquireProcessorLockWithRetry = async (execute, options) => {
1073
- const policy = _nullishCoalesce(options.lockPolicy, () => ( DefaultPostgreSQLProcessorLockPolicy));
1221
+ const policy = _nullishCoalesce(options.lockAcquisitionPolicy, () => ( DefaultPostgreSQLProcessorLockPolicy));
1074
1222
  if (policy.type === "retry") {
1075
1223
  return asyncRetry(() => tryAcquireProcessorLock(execute, options), {
1076
1224
  retries: policy.retries - 1,
@@ -1095,98 +1243,11 @@ var releaseProcessorLock = async (execute, options) => {
1095
1243
  })
1096
1244
  )
1097
1245
  );
1098
- return result;
1099
- };
1100
-
1101
- // src/eventStore/projections/locks/tryAcquireProjectionLock.ts
1102
-
1103
-
1104
- // src/eventStore/schema/projections/projectionsLocks.ts
1105
-
1106
- var tryAcquireProjectionLockSQL = createFunctionIfDoesNotExistSQL(
1107
- "emt_try_acquire_projection_lock",
1108
- `
1109
- CREATE OR REPLACE FUNCTION emt_try_acquire_projection_lock(
1110
- p_lock_key BIGINT,
1111
- p_partition TEXT,
1112
- p_name TEXT,
1113
- p_version INT
1114
- )
1115
- RETURNS TABLE (acquired BOOLEAN, is_active BOOLEAN)
1116
- LANGUAGE plpgsql
1117
- AS $emt_try_acquire_projection_lock$
1118
- BEGIN
1119
- RETURN QUERY
1120
- WITH lock_check AS (
1121
- SELECT pg_try_advisory_xact_lock_shared(p_lock_key) AS acquired
1122
- ),
1123
- status_check AS (
1124
- SELECT status = 'active' AS is_active
1125
- FROM ${projectionsTable.name}
1126
- WHERE partition = p_partition AND name = p_name AND version = p_version
1127
- )
1128
- SELECT
1129
- COALESCE((SELECT lc.acquired FROM lock_check lc), false),
1130
- COALESCE((SELECT sc.is_active FROM status_check sc), true);
1131
- END;
1132
- $emt_try_acquire_projection_lock$;
1133
- `
1134
- );
1135
- var callTryAcquireProjectionLock = (params) => _dumbo.sql.call(void 0,
1136
- `SELECT * FROM emt_try_acquire_projection_lock(%s::BIGINT, %L, %L, %s);`,
1137
- params.lockKey,
1138
- params.partition,
1139
- params.name,
1140
- params.version
1141
- );
1142
-
1143
- // src/eventStore/projections/locks/tryAcquireProjectionLock.ts
1144
- var tryAcquireProjectionLock = async (execute, {
1145
- lockKey,
1146
- projectionName,
1147
- partition,
1148
- version
1149
- }) => {
1150
- const lockKeyBigInt = isBigint(lockKey) ? lockKey : await hashText(lockKey);
1151
- const { acquired, is_active } = await _dumbo.single.call(void 0,
1152
- execute.query(
1153
- callTryAcquireProjectionLock({
1154
- lockKey: lockKeyBigInt.toString(),
1155
- partition,
1156
- name: projectionName,
1157
- version
1158
- })
1159
- )
1246
+ console.log(
1247
+ `Lock for processor '${options.processorId}' with processor instance '${options.processorInstanceId}' and lock '${options.lockKey}' released: ${result}`
1160
1248
  );
1161
- return acquired === true && is_active === true;
1162
- };
1163
-
1164
- // src/eventStore/projections/locks/postgreSQLProjectionLock.ts
1165
- var postgreSQLProjectionLock = (options) => {
1166
- let acquired = false;
1167
- const lockKey = _nullishCoalesce(options.lockKey, () => ( toProjectionLockKey(options)));
1168
- return {
1169
- tryAcquire: async (context) => {
1170
- if (acquired) {
1171
- return true;
1172
- }
1173
- acquired = await tryAcquireProjectionLock(context.execute, {
1174
- ...options,
1175
- lockKey
1176
- });
1177
- return acquired;
1178
- },
1179
- release: (_context) => {
1180
- if (!acquired) return;
1181
- acquired = false;
1182
- }
1183
- };
1249
+ return result;
1184
1250
  };
1185
- var toProjectionLockKey = ({
1186
- projectionName,
1187
- partition,
1188
- version
1189
- }) => `${partition}:${projectionName}:${version}`;
1190
1251
 
1191
1252
  // src/eventStore/projections/locks/postgreSQLProcessorLock.ts
1192
1253
  var DefaultPostgreSQLProcessorLockPolicy = {
@@ -1198,13 +1259,19 @@ var postgreSQLProcessorLock = (options) => {
1198
1259
  return {
1199
1260
  tryAcquire: async (context) => {
1200
1261
  if (acquired) {
1262
+ console.log(
1263
+ `Lock for processor '${options.processorId}' is already acquired by this instance. Reusing the lock.`
1264
+ );
1201
1265
  return true;
1202
1266
  }
1203
1267
  const result = await tryAcquireProcessorLockWithRetry(context.execute, {
1204
1268
  ...options,
1205
1269
  lockKey
1206
1270
  });
1207
- if (!result.acquired && _optionalChain([options, 'access', _45 => _45.lockPolicy, 'optionalAccess', _46 => _46.type]) !== "skip") {
1271
+ if (!result.acquired && _optionalChain([options, 'access', _45 => _45.lockAcquisitionPolicy, 'optionalAccess', _46 => _46.type]) !== "skip") {
1272
+ console.log(
1273
+ `Failed to acquire lock for processor '${options.processorId}' with policy '${_optionalChain([options, 'access', _47 => _47.lockAcquisitionPolicy, 'optionalAccess', _48 => _48.type])}'.`
1274
+ );
1208
1275
  throw new EmmettError(
1209
1276
  `Failed to acquire lock for processor '${options.processorId}'`
1210
1277
  );
@@ -1213,12 +1280,17 @@ var postgreSQLProcessorLock = (options) => {
1213
1280
  return acquired;
1214
1281
  },
1215
1282
  release: async (context) => {
1216
- if (!acquired) return;
1283
+ if (!acquired) {
1284
+ console.log(
1285
+ `Lock for processor '${options.processorId}' is not acquired by this instance. Skipping release.`
1286
+ );
1287
+ return;
1288
+ }
1217
1289
  const { projection: projection2, ...releaseOptions } = options;
1218
1290
  await releaseProcessorLock(context.execute, {
1219
1291
  ...releaseOptions,
1220
1292
  lockKey,
1221
- projectionName: _optionalChain([projection2, 'optionalAccess', _47 => _47.name])
1293
+ projectionName: _optionalChain([projection2, 'optionalAccess', _49 => _49.name])
1222
1294
  });
1223
1295
  acquired = false;
1224
1296
  }
@@ -1889,7 +1961,7 @@ var appendToStream = (pool, streamName, streamType, messages, options) => pool.w
1889
1961
  return { success: false, result: { success: false } };
1890
1962
  try {
1891
1963
  const expectedStreamVersion = toExpectedVersion(
1892
- _optionalChain([options, 'optionalAccess', _48 => _48.expectedStreamVersion])
1964
+ _optionalChain([options, 'optionalAccess', _50 => _50.expectedStreamVersion])
1893
1965
  );
1894
1966
  const messagesToAppend = messages.map((e) => ({
1895
1967
  ...e,
@@ -1929,7 +2001,7 @@ var appendToStream = (pool, streamName, streamType, messages, options) => pool.w
1929
2001
  globalPosition
1930
2002
  };
1931
2003
  });
1932
- if (_optionalChain([options, 'optionalAccess', _49 => _49.beforeCommitHook]))
2004
+ if (_optionalChain([options, 'optionalAccess', _51 => _51.beforeCommitHook]))
1933
2005
  await options.beforeCommitHook(messagesToAppend, { transaction });
1934
2006
  return {
1935
2007
  success: true,
@@ -1970,8 +2042,8 @@ var appendEventsRaw = (execute, streamId, streamType, messages, options) => _dum
1970
2042
  messageKinds: messages.map((e) => _dumbo.sql.call(void 0, "%L", e.kind === "Event" ? "E" : "C")).join(","),
1971
2043
  streamId,
1972
2044
  streamType,
1973
- expectedStreamPosition: _nullishCoalesce(_optionalChain([options, 'optionalAccess', _50 => _50.expectedStreamVersion, 'optionalAccess', _51 => _51.toString, 'call', _52 => _52()]), () => ( "NULL")),
1974
- partition: _nullishCoalesce(_optionalChain([options, 'optionalAccess', _53 => _53.partition]), () => ( defaultTag))
2045
+ expectedStreamPosition: _nullishCoalesce(_optionalChain([options, 'optionalAccess', _52 => _52.expectedStreamVersion, 'optionalAccess', _53 => _53.toString, 'call', _54 => _54()]), () => ( "NULL")),
2046
+ partition: _nullishCoalesce(_optionalChain([options, 'optionalAccess', _55 => _55.partition]), () => ( defaultTag))
1975
2047
  })
1976
2048
  )
1977
2049
  );
@@ -2673,7 +2745,7 @@ BEGIN
2673
2745
  END $$;
2674
2746
  `);
2675
2747
  var migration_0_42_0_FromSubscriptionsToProcessors = _dumbo.sqlMigration.call(void 0,
2676
- "emt:postgresql:eventstore:0.42.0:from-subscriptions-to-processors",
2748
+ "emt:postgresql:eventstore:0.42.1-alpha.1:from-subscriptions-to-processors",
2677
2749
  [migration_0_42_0_FromSubscriptionsToProcessorsSQL]
2678
2750
  );
2679
2751
  var migration_0_42_0_2_AddProcessorProjectionFunctionsSQL = _dumbo.rawSql.call(void 0, `
@@ -2950,7 +3022,7 @@ END;
2950
3022
  $emt_deactivate_projection$;
2951
3023
  `);
2952
3024
  var migration_0_42_0_2_AddProcessorProjectionFunctions = _dumbo.sqlMigration.call(void 0,
2953
- "emt:postgresql:eventstore:0.42.0-2:add-processor-projection-functions",
3025
+ "emt:postgresql:eventstore:0.42.1-alpha.1-2:add-processor-projection-functions",
2954
3026
  [migration_0_42_0_2_AddProcessorProjectionFunctionsSQL]
2955
3027
  );
2956
3028
 
@@ -4059,7 +4131,7 @@ var readProcessorCheckpoint = async (execute, options) => {
4059
4131
  FROM ${processorsTable.name}
4060
4132
  WHERE partition = %L AND processor_id = %L AND version = %s
4061
4133
  LIMIT 1`,
4062
- _nullishCoalesce(_optionalChain([options, 'optionalAccess', _54 => _54.partition]), () => ( defaultTag)),
4134
+ _nullishCoalesce(_optionalChain([options, 'optionalAccess', _56 => _56.partition]), () => ( defaultTag)),
4063
4135
  options.processorId,
4064
4136
  _nullishCoalesce(options.version, () => ( 1))
4065
4137
  )
@@ -4073,9 +4145,9 @@ var readProcessorCheckpoint = async (execute, options) => {
4073
4145
  // src/eventStore/schema/readStream.ts
4074
4146
 
4075
4147
  var readStream = async (execute, streamId, options) => {
4076
- const fromCondition = _optionalChain([options, 'optionalAccess', _55 => _55.from]) ? `AND stream_position >= ${options.from}` : "";
4148
+ const fromCondition = _optionalChain([options, 'optionalAccess', _57 => _57.from]) ? `AND stream_position >= ${options.from}` : "";
4077
4149
  const to = Number(
4078
- _nullishCoalesce(_optionalChain([options, 'optionalAccess', _56 => _56.to]), () => ( (_optionalChain([options, 'optionalAccess', _57 => _57.maxCount]) ? (_nullishCoalesce(options.from, () => ( 0n))) + options.maxCount : NaN)))
4150
+ _nullishCoalesce(_optionalChain([options, 'optionalAccess', _58 => _58.to]), () => ( (_optionalChain([options, 'optionalAccess', _59 => _59.maxCount]) ? (_nullishCoalesce(options.from, () => ( 0n))) + options.maxCount : NaN)))
4079
4151
  );
4080
4152
  const toCondition = !isNaN(to) ? `AND stream_position <= ${to}` : "";
4081
4153
  const events = await _dumbo.mapRows.call(void 0,
@@ -4086,7 +4158,7 @@ var readStream = async (execute, streamId, options) => {
4086
4158
  WHERE stream_id = %L AND partition = %L AND is_archived = FALSE ${fromCondition} ${toCondition}
4087
4159
  ORDER BY stream_position ASC`,
4088
4160
  streamId,
4089
- _nullishCoalesce(_optionalChain([options, 'optionalAccess', _58 => _58.partition]), () => ( defaultTag))
4161
+ _nullishCoalesce(_optionalChain([options, 'optionalAccess', _60 => _60.partition]), () => ( defaultTag))
4090
4162
  )
4091
4163
  ),
4092
4164
  (row) => {
@@ -4107,7 +4179,7 @@ var readStream = async (execute, streamId, options) => {
4107
4179
  kind: "Event",
4108
4180
  metadata
4109
4181
  };
4110
- return upcastRecordedMessage(event, _optionalChain([options, 'optionalAccess', _59 => _59.schema, 'optionalAccess', _60 => _60.versioning]));
4182
+ return upcastRecordedMessage(event, _optionalChain([options, 'optionalAccess', _61 => _61.schema, 'optionalAccess', _62 => _62.versioning]));
4111
4183
  }
4112
4184
  );
4113
4185
  return events.length > 0 ? {
@@ -4132,10 +4204,10 @@ var streamExists = async (execute, streamId, options) => {
4132
4204
  WHERE stream_id = %L AND partition = %L AND is_archived = FALSE)
4133
4205
  `,
4134
4206
  streamId,
4135
- _nullishCoalesce(_optionalChain([options, 'optionalAccess', _61 => _61.partition]), () => ( defaultTag))
4207
+ _nullishCoalesce(_optionalChain([options, 'optionalAccess', _63 => _63.partition]), () => ( defaultTag))
4136
4208
  )
4137
4209
  );
4138
- return _nullishCoalesce(_optionalChain([queryResult, 'access', _62 => _62.rows, 'access', _63 => _63[0], 'optionalAccess', _64 => _64.exists]), () => ( false));
4210
+ return _nullishCoalesce(_optionalChain([queryResult, 'access', _64 => _64.rows, 'access', _65 => _65[0], 'optionalAccess', _66 => _66.exists]), () => ( false));
4139
4211
  };
4140
4212
 
4141
4213
  // src/eventStore/schema/index.ts
@@ -4180,7 +4252,7 @@ var createEventStoreSchema = (connectionString, pool, hooks, options) => {
4180
4252
  };
4181
4253
  const nestedPool = _dumbo.dumbo.call(void 0, { connectionString, connection: tx.connection });
4182
4254
  try {
4183
- if (_optionalChain([hooks, 'optionalAccess', _65 => _65.onBeforeSchemaCreated])) {
4255
+ if (_optionalChain([hooks, 'optionalAccess', _67 => _67.onBeforeSchemaCreated])) {
4184
4256
  await hooks.onBeforeSchemaCreated(context);
4185
4257
  }
4186
4258
  const result = await _dumbo.runPostgreSQLMigrations.call(void 0,
@@ -4188,7 +4260,7 @@ var createEventStoreSchema = (connectionString, pool, hooks, options) => {
4188
4260
  eventStoreSchemaMigrations,
4189
4261
  options
4190
4262
  );
4191
- if (_optionalChain([hooks, 'optionalAccess', _66 => _66.onAfterSchemaCreated])) {
4263
+ if (_optionalChain([hooks, 'optionalAccess', _68 => _68.onAfterSchemaCreated])) {
4192
4264
  await hooks.onAfterSchemaCreated(context);
4193
4265
  }
4194
4266
  return result;
@@ -4203,7 +4275,7 @@ var createEventStoreSchema = (connectionString, pool, hooks, options) => {
4203
4275
  var truncateTables = async (execute, options) => {
4204
4276
  await execute.command(
4205
4277
  _dumbo.rawSql.call(void 0,
4206
- `TRUNCATE TABLE ${streamsTable.name}, ${messagesTable.name}, ${processorsTable.name}, ${projectionsTable.name} CASCADE${_optionalChain([options, 'optionalAccess', _67 => _67.resetSequences]) ? "; ALTER SEQUENCE emt_global_message_position RESTART WITH 1" : ""};`
4278
+ `TRUNCATE TABLE ${streamsTable.name}, ${messagesTable.name}, ${processorsTable.name}, ${projectionsTable.name} CASCADE${_optionalChain([options, 'optionalAccess', _69 => _69.resetSequences]) ? "; ALTER SEQUENCE emt_global_message_position RESTART WITH 1" : ""};`
4207
4279
  )
4208
4280
  );
4209
4281
  };
@@ -4221,7 +4293,7 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
4221
4293
  };
4222
4294
  const pool = "dumbo" in poolOptions ? poolOptions.dumbo : _dumbo.dumbo.call(void 0, poolOptions);
4223
4295
  let migrateSchema = void 0;
4224
- const autoGenerateSchema = _optionalChain([options, 'access', _68 => _68.schema, 'optionalAccess', _69 => _69.autoMigration]) === void 0 || _optionalChain([options, 'access', _70 => _70.schema, 'optionalAccess', _71 => _71.autoMigration]) !== "None";
4296
+ const autoGenerateSchema = _optionalChain([options, 'access', _70 => _70.schema, 'optionalAccess', _71 => _71.autoMigration]) === void 0 || _optionalChain([options, 'access', _72 => _72.schema, 'optionalAccess', _73 => _73.autoMigration]) !== "None";
4225
4297
  const inlineProjections = (_nullishCoalesce(options.projections, () => ( []))).filter(({ type }) => type === "inline").map(({ projection: projection2 }) => projection2);
4226
4298
  const migrate = async (migrationOptions) => {
4227
4299
  if (!migrateSchema) {
@@ -4230,7 +4302,7 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
4230
4302
  pool,
4231
4303
  {
4232
4304
  onBeforeSchemaCreated: async (context) => {
4233
- if (_optionalChain([options, 'access', _72 => _72.hooks, 'optionalAccess', _73 => _73.onBeforeSchemaCreated])) {
4305
+ if (_optionalChain([options, 'access', _74 => _74.hooks, 'optionalAccess', _75 => _75.onBeforeSchemaCreated])) {
4234
4306
  await options.hooks.onBeforeSchemaCreated(context);
4235
4307
  }
4236
4308
  },
@@ -4245,7 +4317,7 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
4245
4317
  });
4246
4318
  }
4247
4319
  }
4248
- if (_optionalChain([options, 'access', _74 => _74.hooks, 'optionalAccess', _75 => _75.onAfterSchemaCreated])) {
4320
+ if (_optionalChain([options, 'access', _76 => _76.hooks, 'optionalAccess', _77 => _77.onAfterSchemaCreated])) {
4249
4321
  await options.hooks.onAfterSchemaCreated(context);
4250
4322
  }
4251
4323
  }
@@ -4279,13 +4351,13 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
4279
4351
  truncate: (truncateOptions) => pool.withTransaction(async (transaction) => {
4280
4352
  await ensureSchemaExists();
4281
4353
  await truncateTables(transaction.execute, truncateOptions);
4282
- if (_optionalChain([truncateOptions, 'optionalAccess', _76 => _76.truncateProjections])) {
4354
+ if (_optionalChain([truncateOptions, 'optionalAccess', _78 => _78.truncateProjections])) {
4283
4355
  const projectionContext = await transactionToPostgreSQLProjectionHandlerContext(
4284
4356
  connectionString,
4285
4357
  pool,
4286
4358
  transaction
4287
4359
  );
4288
- for (const projection2 of _nullishCoalesce(_optionalChain([options, 'optionalAccess', _77 => _77.projections]), () => ( []))) {
4360
+ for (const projection2 of _nullishCoalesce(_optionalChain([options, 'optionalAccess', _79 => _79.projections]), () => ( []))) {
4289
4361
  if (projection2.projection.truncate)
4290
4362
  await projection2.projection.truncate(projectionContext);
4291
4363
  }
@@ -4295,7 +4367,7 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
4295
4367
  },
4296
4368
  async aggregateStream(streamName, options2) {
4297
4369
  const { evolve, initialState, read } = options2;
4298
- const expectedStreamVersion = _optionalChain([read, 'optionalAccess', _78 => _78.expectedStreamVersion]);
4370
+ const expectedStreamVersion = _optionalChain([read, 'optionalAccess', _80 => _80.expectedStreamVersion]);
4299
4371
  let state = initialState();
4300
4372
  const result = await this.readStream(
4301
4373
  streamName,
@@ -4333,7 +4405,7 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
4333
4405
  pool,
4334
4406
  streamName,
4335
4407
  streamType,
4336
- downcastRecordedMessages(events, _optionalChain([options2, 'optionalAccess', _79 => _79.schema, 'optionalAccess', _80 => _80.versioning])),
4408
+ downcastRecordedMessages(events, _optionalChain([options2, 'optionalAccess', _81 => _81.schema, 'optionalAccess', _82 => _82.versioning])),
4337
4409
  {
4338
4410
  ...options2,
4339
4411
  beforeCommitHook
@@ -4343,7 +4415,7 @@ var getPostgreSQLEventStore = (connectionString, options = defaultPostgreSQLOpti
4343
4415
  throw new ExpectedVersionConflictError(
4344
4416
  -1n,
4345
4417
  //TODO: Return actual version in case of error
4346
- _nullishCoalesce(_optionalChain([options2, 'optionalAccess', _81 => _81.expectedStreamVersion]), () => ( NO_CONCURRENCY_CHECK))
4418
+ _nullishCoalesce(_optionalChain([options2, 'optionalAccess', _83 => _83.expectedStreamVersion]), () => ( NO_CONCURRENCY_CHECK))
4347
4419
  );
4348
4420
  return {
4349
4421
  nextExpectedStreamVersion: appendResult.nextStreamPosition,
@@ -4426,7 +4498,7 @@ var PostgreSQLProjectionSpec = {
4426
4498
  const allEvents = [];
4427
4499
  const run = async (pool) => {
4428
4500
  let globalPosition = 0n;
4429
- const numberOfTimes = _nullishCoalesce(_optionalChain([options2, 'optionalAccess', _82 => _82.numberOfTimes]), () => ( 1));
4501
+ const numberOfTimes = _nullishCoalesce(_optionalChain([options2, 'optionalAccess', _84 => _84.numberOfTimes]), () => ( 1));
4430
4502
  for (const event of [
4431
4503
  ...givenEvents,
4432
4504
  ...Array.from({ length: numberOfTimes }).flatMap(() => events)
@@ -4484,18 +4556,18 @@ var PostgreSQLProjectionSpec = {
4484
4556
  if (!isErrorConstructor(args[0])) {
4485
4557
  assertTrue(
4486
4558
  args[0](error),
4487
- `Error didn't match the error condition: ${_optionalChain([error, 'optionalAccess', _83 => _83.toString, 'call', _84 => _84()])}`
4559
+ `Error didn't match the error condition: ${_optionalChain([error, 'optionalAccess', _85 => _85.toString, 'call', _86 => _86()])}`
4488
4560
  );
4489
4561
  return;
4490
4562
  }
4491
4563
  assertTrue(
4492
4564
  error instanceof args[0],
4493
- `Caught error is not an instance of the expected type: ${_optionalChain([error, 'optionalAccess', _85 => _85.toString, 'call', _86 => _86()])}`
4565
+ `Caught error is not an instance of the expected type: ${_optionalChain([error, 'optionalAccess', _87 => _87.toString, 'call', _88 => _88()])}`
4494
4566
  );
4495
4567
  if (args[1]) {
4496
4568
  assertTrue(
4497
4569
  args[1](error),
4498
- `Error didn't match the error condition: ${_optionalChain([error, 'optionalAccess', _87 => _87.toString, 'call', _88 => _88()])}`
4570
+ `Error didn't match the error condition: ${_optionalChain([error, 'optionalAccess', _89 => _89.toString, 'call', _90 => _90()])}`
4499
4571
  );
4500
4572
  }
4501
4573
  } finally {
@@ -4514,7 +4586,7 @@ var eventInStream = (streamName, event) => {
4514
4586
  ...event,
4515
4587
  metadata: {
4516
4588
  ..._nullishCoalesce(event.metadata, () => ( {})),
4517
- streamName: _nullishCoalesce(_optionalChain([event, 'access', _89 => _89.metadata, 'optionalAccess', _90 => _90.streamName]), () => ( streamName))
4589
+ streamName: _nullishCoalesce(_optionalChain([event, 'access', _91 => _91.metadata, 'optionalAccess', _92 => _92.streamName]), () => ( streamName))
4518
4590
  }
4519
4591
  };
4520
4592
  };
@@ -4643,7 +4715,7 @@ var postgreSQLRawSQLProjection = (options) => {
4643
4715
  var postgreSQLCheckpointer = () => ({
4644
4716
  read: async (options, context) => {
4645
4717
  const result = await readProcessorCheckpoint(context.execute, options);
4646
- return { lastCheckpoint: _optionalChain([result, 'optionalAccess', _91 => _91.lastProcessedCheckpoint]) };
4718
+ return { lastCheckpoint: _optionalChain([result, 'optionalAccess', _93 => _93.lastProcessedCheckpoint]) };
4647
4719
  },
4648
4720
  store: async (options, context) => {
4649
4721
  const newPosition = getCheckpoint(options.message);
@@ -4661,13 +4733,13 @@ var postgreSQLProcessingScope = (options) => {
4661
4733
  const processorConnectionString = options.connectionString;
4662
4734
  const processorPool = options.pool;
4663
4735
  const processingScope = async (handler, partialContext) => {
4664
- const connection = _optionalChain([partialContext, 'optionalAccess', _92 => _92.connection]);
4665
- const connectionString = _nullishCoalesce(processorConnectionString, () => ( _optionalChain([connection, 'optionalAccess', _93 => _93.connectionString])));
4736
+ const connection = _optionalChain([partialContext, 'optionalAccess', _94 => _94.connection]);
4737
+ const connectionString = _nullishCoalesce(processorConnectionString, () => ( _optionalChain([connection, 'optionalAccess', _95 => _95.connectionString])));
4666
4738
  if (!connectionString)
4667
4739
  throw new EmmettError(
4668
4740
  `PostgreSQL processor '${options.processorId}' is missing connection string. Ensure that you passed it through options`
4669
4741
  );
4670
- const pool = _nullishCoalesce((!processorConnectionString || connectionString == processorConnectionString ? _optionalChain([connection, 'optionalAccess', _94 => _94.pool]) : processorPool), () => ( processorPool));
4742
+ const pool = _nullishCoalesce((!processorConnectionString || connectionString == processorConnectionString ? _optionalChain([connection, 'optionalAccess', _96 => _96.pool]) : processorPool), () => ( processorPool));
4671
4743
  if (!pool)
4672
4744
  throw new EmmettError(
4673
4745
  `PostgreSQL processor '${options.processorId}' is missing connection string. Ensure that you passed it through options`
@@ -4708,11 +4780,11 @@ var wrapHooksWithProcessorLocks = (hooks, processorLock) => ({
4708
4780
  ..._nullishCoalesce(hooks, () => ( {})),
4709
4781
  onStart: async (context) => {
4710
4782
  await processorLock.tryAcquire({ execute: context.execute });
4711
- if (_optionalChain([hooks, 'optionalAccess', _95 => _95.onStart])) await hooks.onStart(context);
4783
+ if (_optionalChain([hooks, 'optionalAccess', _97 => _97.onStart])) await hooks.onStart(context);
4712
4784
  },
4713
- onClose: _optionalChain([hooks, 'optionalAccess', _96 => _96.onClose]) || processorLock ? async (context) => {
4785
+ onClose: _optionalChain([hooks, 'optionalAccess', _98 => _98.onClose]) || processorLock ? async (context) => {
4714
4786
  await processorLock.release({ execute: context.execute });
4715
- if (_optionalChain([hooks, 'optionalAccess', _97 => _97.onClose])) await hooks.onClose(context);
4787
+ if (_optionalChain([hooks, 'optionalAccess', _99 => _99.onClose])) await hooks.onClose(context);
4716
4788
  } : void 0
4717
4789
  });
4718
4790
  var postgreSQLProjector = (options) => {
@@ -4723,8 +4795,7 @@ var postgreSQLProjector = (options) => {
4723
4795
  processorInstanceId = getProcessorInstanceId(processorId),
4724
4796
  version = defaultProcessorVersion,
4725
4797
  partition = defaultProcessorPartition,
4726
- lockPolicy = DefaultPostgreSQLProcessorLockPolicy,
4727
- lockTimeoutSeconds
4798
+ lock
4728
4799
  } = options;
4729
4800
  const { pool, connectionString, close } = getProcessorPool(options);
4730
4801
  const processorLock = postgreSQLProcessorLock({
@@ -4738,13 +4809,13 @@ var postgreSQLProjector = (options) => {
4738
4809
  version: _nullishCoalesce(options.projection.version, () => ( version)),
4739
4810
  handlingType: "async"
4740
4811
  } : void 0,
4741
- lockPolicy,
4742
- lockTimeoutSeconds
4812
+ lockAcquisitionPolicy: _nullishCoalesce(_optionalChain([lock, 'optionalAccess', _100 => _100.acquisitionPolicy]), () => ( DefaultPostgreSQLProcessorLockPolicy)),
4813
+ lockTimeoutSeconds: _optionalChain([lock, 'optionalAccess', _101 => _101.timeoutSeconds])
4743
4814
  });
4744
4815
  const hooks = wrapHooksWithProcessorLocks(
4745
4816
  {
4746
4817
  ..._nullishCoalesce(options.hooks, () => ( {})),
4747
- onInit: options.projection.init !== void 0 || _optionalChain([options, 'access', _98 => _98.hooks, 'optionalAccess', _99 => _99.onInit]) ? async (context) => {
4818
+ onInit: options.projection.init !== void 0 || _optionalChain([options, 'access', _102 => _102.hooks, 'optionalAccess', _103 => _103.onInit]) ? async (context) => {
4748
4819
  if (options.projection.init)
4749
4820
  await options.projection.init({
4750
4821
  version: _nullishCoalesce(options.projection.version, () => ( version)),
@@ -4755,16 +4826,16 @@ var postgreSQLProjector = (options) => {
4755
4826
  migrationOptions: options.migrationOptions
4756
4827
  }
4757
4828
  });
4758
- if (_optionalChain([options, 'access', _100 => _100.hooks, 'optionalAccess', _101 => _101.onInit]))
4829
+ if (_optionalChain([options, 'access', _104 => _104.hooks, 'optionalAccess', _105 => _105.onInit]))
4759
4830
  await options.hooks.onInit({
4760
4831
  ...context,
4761
4832
  migrationOptions: options.migrationOptions
4762
4833
  });
4763
- } : _optionalChain([options, 'access', _102 => _102.hooks, 'optionalAccess', _103 => _103.onInit]),
4834
+ } : _optionalChain([options, 'access', _106 => _106.hooks, 'optionalAccess', _107 => _107.onInit]),
4764
4835
  onClose: close ? async (context) => {
4765
- if (_optionalChain([options, 'access', _104 => _104.hooks, 'optionalAccess', _105 => _105.onClose])) await _optionalChain([options, 'access', _106 => _106.hooks, 'optionalAccess', _107 => _107.onClose, 'call', _108 => _108(context)]);
4836
+ if (_optionalChain([options, 'access', _108 => _108.hooks, 'optionalAccess', _109 => _109.onClose])) await _optionalChain([options, 'access', _110 => _110.hooks, 'optionalAccess', _111 => _111.onClose, 'call', _112 => _112(context)]);
4766
4837
  if (close) await close();
4767
- } : _optionalChain([options, 'access', _109 => _109.hooks, 'optionalAccess', _110 => _110.onClose])
4838
+ } : _optionalChain([options, 'access', _113 => _113.hooks, 'optionalAccess', _114 => _114.onClose])
4768
4839
  },
4769
4840
  processorLock
4770
4841
  );
@@ -4791,7 +4862,7 @@ var postgreSQLReactor = (options) => {
4791
4862
  processorInstanceId = getProcessorInstanceId(processorId),
4792
4863
  version = defaultProcessorVersion,
4793
4864
  partition = defaultProcessorPartition,
4794
- lockPolicy = DefaultPostgreSQLProcessorLockPolicy
4865
+ lock
4795
4866
  } = options;
4796
4867
  const { pool, connectionString, close } = getProcessorPool(options);
4797
4868
  const processorLock = postgreSQLProcessorLock({
@@ -4800,15 +4871,16 @@ var postgreSQLReactor = (options) => {
4800
4871
  partition,
4801
4872
  processorInstanceId,
4802
4873
  projection: void 0,
4803
- lockPolicy
4874
+ lockAcquisitionPolicy: _nullishCoalesce(_optionalChain([lock, 'optionalAccess', _115 => _115.acquisitionPolicy]), () => ( DefaultPostgreSQLProcessorLockPolicy)),
4875
+ lockTimeoutSeconds: _optionalChain([lock, 'optionalAccess', _116 => _116.timeoutSeconds])
4804
4876
  });
4805
4877
  const hooks = wrapHooksWithProcessorLocks(
4806
4878
  {
4807
4879
  ..._nullishCoalesce(options.hooks, () => ( {})),
4808
4880
  onClose: close ? async (context) => {
4809
- if (_optionalChain([options, 'access', _111 => _111.hooks, 'optionalAccess', _112 => _112.onClose])) await _optionalChain([options, 'access', _113 => _113.hooks, 'optionalAccess', _114 => _114.onClose, 'call', _115 => _115(context)]);
4881
+ if (_optionalChain([options, 'access', _117 => _117.hooks, 'optionalAccess', _118 => _118.onClose])) await _optionalChain([options, 'access', _119 => _119.hooks, 'optionalAccess', _120 => _120.onClose, 'call', _121 => _121(context)]);
4810
4882
  if (close) await close();
4811
- } : _optionalChain([options, 'access', _116 => _116.hooks, 'optionalAccess', _117 => _117.onClose])
4883
+ } : _optionalChain([options, 'access', _122 => _122.hooks, 'optionalAccess', _123 => _123.onClose])
4812
4884
  },
4813
4885
  processorLock
4814
4886
  );
@@ -4856,16 +4928,24 @@ var postgreSQLEventStoreConsumer = (options) => {
4856
4928
  };
4857
4929
  const result = await Promise.allSettled(
4858
4930
  activeProcessors.map(async (s) => {
4859
- return await s.handle(messagesBatch, {
4860
- connection: {
4861
- connectionString: options.connectionString,
4862
- pool
4863
- }
4864
- });
4931
+ try {
4932
+ return await s.handle(messagesBatch, {
4933
+ connection: {
4934
+ connectionString: options.connectionString,
4935
+ pool
4936
+ }
4937
+ });
4938
+ } catch (error) {
4939
+ console.log(
4940
+ `Error during message batch processing for processor: ${s.id}`,
4941
+ error
4942
+ );
4943
+ throw error;
4944
+ }
4865
4945
  })
4866
4946
  );
4867
4947
  return result.some(
4868
- (r) => r.status === "fulfilled" && _optionalChain([r, 'access', _118 => _118.value, 'optionalAccess', _119 => _119.type]) !== "STOP"
4948
+ (r) => r.status === "fulfilled" && _optionalChain([r, 'access', _124 => _124.value, 'optionalAccess', _125 => _125.type]) !== "STOP"
4869
4949
  ) ? void 0 : {
4870
4950
  type: "STOP"
4871
4951
  };
@@ -4884,7 +4964,7 @@ var postgreSQLEventStoreConsumer = (options) => {
4884
4964
  if (!isRunning) return;
4885
4965
  isRunning = false;
4886
4966
  if (messagePuller) {
4887
- _optionalChain([abortController, 'optionalAccess', _120 => _120.abort, 'call', _121 => _121()]);
4967
+ _optionalChain([abortController, 'optionalAccess', _126 => _126.abort, 'call', _127 => _127()]);
4888
4968
  await messagePuller.stop();
4889
4969
  messagePuller = void 0;
4890
4970
  abortController = null;
@@ -4897,7 +4977,24 @@ var postgreSQLEventStoreConsumer = (options) => {
4897
4977
  const postgresProcessors = processors;
4898
4978
  for (const processor of postgresProcessors) {
4899
4979
  if (processor.init) {
4900
- await processor.init(processorContext);
4980
+ try {
4981
+ await processor.init(processorContext);
4982
+ } catch (error) {
4983
+ console.log(
4984
+ `Error during processor initialization for processor: ${processor.id}. Stopping it.`,
4985
+ error
4986
+ );
4987
+ await processor.close(processorContext).catch((closeError) => {
4988
+ console.log(
4989
+ `Error during processor cleanup after failed initialization for processor: ${processor.id}`,
4990
+ closeError
4991
+ );
4992
+ });
4993
+ console.log(
4994
+ `Processor ${processor.id} stopped successfully after failed initialization.`
4995
+ );
4996
+ throw error;
4997
+ }
4901
4998
  }
4902
4999
  }
4903
5000
  isInitialized = true;
@@ -4926,40 +5023,63 @@ var postgreSQLEventStoreConsumer = (options) => {
4926
5023
  return processor;
4927
5024
  },
4928
5025
  start: () => {
4929
- if (isRunning) return start;
4930
- if (processors.length === 0)
5026
+ if (isRunning) {
5027
+ console.log(
5028
+ "Consumer is already running. Returning the existing start promise."
5029
+ );
5030
+ return start;
5031
+ }
5032
+ if (processors.length === 0) {
5033
+ console.log(
5034
+ "Cannot start consumer without at least a single processor"
5035
+ );
4931
5036
  throw new EmmettError(
4932
5037
  "Cannot start consumer without at least a single processor"
4933
5038
  );
5039
+ }
4934
5040
  isRunning = true;
4935
5041
  abortController = new AbortController();
4936
5042
  messagePuller = postgreSQLEventStoreMessageBatchPuller({
4937
5043
  stopWhen: options.stopWhen,
4938
5044
  executor: pool.execute,
4939
5045
  eachBatch,
4940
- batchSize: _nullishCoalesce(_optionalChain([pulling, 'optionalAccess', _122 => _122.batchSize]), () => ( DefaultPostgreSQLEventStoreProcessorBatchSize)),
4941
- pullingFrequencyInMs: _nullishCoalesce(_optionalChain([pulling, 'optionalAccess', _123 => _123.pullingFrequencyInMs]), () => ( DefaultPostgreSQLEventStoreProcessorPullingFrequencyInMs)),
5046
+ batchSize: _nullishCoalesce(_optionalChain([pulling, 'optionalAccess', _128 => _128.batchSize]), () => ( DefaultPostgreSQLEventStoreProcessorBatchSize)),
5047
+ pullingFrequencyInMs: _nullishCoalesce(_optionalChain([pulling, 'optionalAccess', _129 => _129.pullingFrequencyInMs]), () => ( DefaultPostgreSQLEventStoreProcessorPullingFrequencyInMs)),
4942
5048
  signal: abortController.signal
4943
5049
  });
4944
5050
  start = (async () => {
4945
5051
  if (!isRunning) return;
4946
5052
  if (!isInitialized) {
5053
+ console.log("Initializing consumer before starting message pulling.");
4947
5054
  await init();
4948
5055
  }
4949
5056
  const startFrom = zipPostgreSQLEventStoreMessageBatchPullerStartFrom(
4950
5057
  await Promise.all(
4951
5058
  processors.map(async (o) => {
4952
- const result = await o.start({
4953
- execute: pool.execute,
4954
- connection: {
4955
- connectionString: options.connectionString,
4956
- pool
4957
- }
4958
- });
4959
- return result;
5059
+ try {
5060
+ const result = await o.start({
5061
+ execute: pool.execute,
5062
+ connection: {
5063
+ connectionString: options.connectionString,
5064
+ pool
5065
+ }
5066
+ });
5067
+ return result;
5068
+ } catch (error) {
5069
+ console.log(
5070
+ `Error during processor start position retrieval for processor: ${o.id}. Stopping it.`,
5071
+ error
5072
+ );
5073
+ throw error;
5074
+ }
4960
5075
  })
4961
5076
  )
4962
5077
  );
5078
+ console.log(
5079
+ `Starting message pulling with start position: ${_dumbo.JSONSerializer.serialize(
5080
+ startFrom
5081
+ )}. Waiting for messages...`
5082
+ );
4963
5083
  await messagePuller.start({ startFrom });
4964
5084
  await stopProcessors();
4965
5085
  isRunning = false;
@@ -4986,9 +5106,9 @@ var rebuildPostgreSQLProjections = (options) => {
4986
5106
  ...options,
4987
5107
  stopWhen: { noMessagesLeft: true }
4988
5108
  });
5109
+ const lock = { acquisitionPolicy: defaultRebuildLockPolicy, ...options.lock };
4989
5110
  const projections = "projections" in options ? options.projections.map(
4990
5111
  (p) => "projection" in p ? {
4991
- lockPolicy: defaultRebuildLockPolicy,
4992
5112
  truncateOnStart: true,
4993
5113
  processorId: getProjectorId({
4994
5114
  projectionName: _nullishCoalesce(p.projection.name, () => ( unknownTag2))
@@ -4999,14 +5119,14 @@ var rebuildPostgreSQLProjections = (options) => {
4999
5119
  processorId: getProjectorId({
5000
5120
  projectionName: _nullishCoalesce(p.name, () => ( unknownTag2))
5001
5121
  }),
5002
- truncateOnStart: true,
5003
- lockPolicy: defaultRebuildLockPolicy
5122
+ truncateOnStart: true
5004
5123
  }
5005
5124
  ) : [options];
5006
5125
  for (const projectionDefinition of projections) {
5007
5126
  consumer.projector({
5008
5127
  ...projectionDefinition,
5009
- truncateOnStart: _nullishCoalesce(projectionDefinition.truncateOnStart, () => ( true))
5128
+ truncateOnStart: _nullishCoalesce(projectionDefinition.truncateOnStart, () => ( true)),
5129
+ lock
5010
5130
  });
5011
5131
  }
5012
5132
  return consumer;