@event-driven-io/emmett-postgresql 0.42.0-rc.2 → 0.42.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -74,7 +74,7 @@ type PostgreSQLProcessorLockOptions = {
74
74
  };
75
75
  lockKey?: string | bigint;
76
76
  lockTimeoutSeconds?: number;
77
- lockPolicy?: LockAcquisitionPolicy;
77
+ lockAcquisitionPolicy?: LockAcquisitionPolicy;
78
78
  };
79
79
  type PostgreSQLProcessorLockContext = {
80
80
  execute: SQLExecutor;
@@ -370,6 +370,7 @@ declare const eventStoreSchemaMigrations: SQLMigration[];
370
370
  type CreateEventStoreSchemaOptions = {
371
371
  dryRun?: boolean | undefined;
372
372
  ignoreMigrationHashMismatch?: boolean | undefined;
373
+ migrationTimeoutMs?: number | undefined;
373
374
  };
374
375
  type EventStoreSchemaMigrationOptions = {
375
376
  migrationOptions?: CreateEventStoreSchemaOptions;
@@ -697,8 +698,10 @@ type PostgreSQLConnectionOptions = {
697
698
  connectionOptions?: PostgreSQLProcessorConnectionOptions;
698
699
  };
699
700
  type PostgreSQLProcessorOptionsBase = PostgreSQLConnectionOptions & {
700
- lockPolicy?: LockAcquisitionPolicy;
701
- lockTimeoutSeconds?: number;
701
+ lock?: {
702
+ acquisitionPolicy?: LockAcquisitionPolicy;
703
+ timeoutSeconds?: number;
704
+ };
702
705
  partition?: string;
703
706
  };
704
707
  type PostgreSQLReactorOptions<MessageType extends Message = Message> = ReactorOptions<MessageType, ReadEventMetadataWithGlobalPosition, PostgreSQLProcessorHandlerContext> & PostgreSQLProcessorOptionsBase;
@@ -737,7 +740,11 @@ type PostgreSQLEventStoreConsumer<ConsumerMessageType extends AnyMessage = any>
737
740
  declare const postgreSQLEventStoreConsumer: <ConsumerMessageType extends Message = AnyMessage>(options: PostgreSQLEventStoreConsumerOptions<ConsumerMessageType>) => PostgreSQLEventStoreConsumer<ConsumerMessageType>;
738
741
 
739
742
  declare const rebuildPostgreSQLProjections: <EventType extends AnyEvent = AnyEvent>(options: Omit<PostgreSQLEventStoreConsumerOptions<EventType>, "stopWhen" | "processors"> & {
740
- lockPolicy?: LockAcquisitionPolicy;
743
+ lock?: {
744
+ lockAcquisitionPolicy?: LockAcquisitionPolicy;
745
+ acquisitionPolicy?: LockAcquisitionPolicy;
746
+ timeoutSeconds?: number;
747
+ };
741
748
  } & ({
742
749
  projections: (ProjectorOptions<EventType, ReadEventMetadataWithGlobalPosition, PostgreSQLProcessorHandlerContext> | PostgreSQLProjectionDefinition<EventType>)[];
743
750
  } | ProjectorOptions<EventType, ReadEventMetadataWithGlobalPosition, PostgreSQLProcessorHandlerContext>)) => PostgreSQLEventStoreConsumer<EventType>;
package/dist/index.d.ts CHANGED
@@ -74,7 +74,7 @@ type PostgreSQLProcessorLockOptions = {
74
74
  };
75
75
  lockKey?: string | bigint;
76
76
  lockTimeoutSeconds?: number;
77
- lockPolicy?: LockAcquisitionPolicy;
77
+ lockAcquisitionPolicy?: LockAcquisitionPolicy;
78
78
  };
79
79
  type PostgreSQLProcessorLockContext = {
80
80
  execute: SQLExecutor;
@@ -370,6 +370,7 @@ declare const eventStoreSchemaMigrations: SQLMigration[];
370
370
  type CreateEventStoreSchemaOptions = {
371
371
  dryRun?: boolean | undefined;
372
372
  ignoreMigrationHashMismatch?: boolean | undefined;
373
+ migrationTimeoutMs?: number | undefined;
373
374
  };
374
375
  type EventStoreSchemaMigrationOptions = {
375
376
  migrationOptions?: CreateEventStoreSchemaOptions;
@@ -697,8 +698,10 @@ type PostgreSQLConnectionOptions = {
697
698
  connectionOptions?: PostgreSQLProcessorConnectionOptions;
698
699
  };
699
700
  type PostgreSQLProcessorOptionsBase = PostgreSQLConnectionOptions & {
700
- lockPolicy?: LockAcquisitionPolicy;
701
- lockTimeoutSeconds?: number;
701
+ lock?: {
702
+ acquisitionPolicy?: LockAcquisitionPolicy;
703
+ timeoutSeconds?: number;
704
+ };
702
705
  partition?: string;
703
706
  };
704
707
  type PostgreSQLReactorOptions<MessageType extends Message = Message> = ReactorOptions<MessageType, ReadEventMetadataWithGlobalPosition, PostgreSQLProcessorHandlerContext> & PostgreSQLProcessorOptionsBase;
@@ -737,7 +740,11 @@ type PostgreSQLEventStoreConsumer<ConsumerMessageType extends AnyMessage = any>
737
740
  declare const postgreSQLEventStoreConsumer: <ConsumerMessageType extends Message = AnyMessage>(options: PostgreSQLEventStoreConsumerOptions<ConsumerMessageType>) => PostgreSQLEventStoreConsumer<ConsumerMessageType>;
738
741
 
739
742
  declare const rebuildPostgreSQLProjections: <EventType extends AnyEvent = AnyEvent>(options: Omit<PostgreSQLEventStoreConsumerOptions<EventType>, "stopWhen" | "processors"> & {
740
- lockPolicy?: LockAcquisitionPolicy;
743
+ lock?: {
744
+ lockAcquisitionPolicy?: LockAcquisitionPolicy;
745
+ acquisitionPolicy?: LockAcquisitionPolicy;
746
+ timeoutSeconds?: number;
747
+ };
741
748
  } & ({
742
749
  projections: (ProjectorOptions<EventType, ReadEventMetadataWithGlobalPosition, PostgreSQLProcessorHandlerContext> | PostgreSQLProjectionDefinition<EventType>)[];
743
750
  } | ProjectorOptions<EventType, ReadEventMetadataWithGlobalPosition, PostgreSQLProcessorHandlerContext>)) => PostgreSQLEventStoreConsumer<EventType>;
package/dist/index.js CHANGED
@@ -892,10 +892,10 @@ import {
892
892
  } from "@event-driven-io/dumbo";
893
893
  import "pg";
894
894
 
895
- // src/eventStore/projections/locks/tryAcquireProcessorLock.ts
895
+ // src/eventStore/projections/locks/tryAcquireProjectionLock.ts
896
896
  import { single } from "@event-driven-io/dumbo";
897
897
 
898
- // src/eventStore/schema/processors/processorsLocks.ts
898
+ // src/eventStore/schema/projections/projectionsLocks.ts
899
899
  import { sql as sql3 } from "@event-driven-io/dumbo";
900
900
 
901
901
  // src/eventStore/schema/createFunctionIfDoesNotExist.ts
@@ -911,7 +911,97 @@ END $$;
911
911
  `
912
912
  );
913
913
 
914
+ // src/eventStore/schema/projections/projectionsLocks.ts
915
+ var tryAcquireProjectionLockSQL = createFunctionIfDoesNotExistSQL(
916
+ "emt_try_acquire_projection_lock",
917
+ `
918
+ CREATE OR REPLACE FUNCTION emt_try_acquire_projection_lock(
919
+ p_lock_key BIGINT,
920
+ p_partition TEXT,
921
+ p_name TEXT,
922
+ p_version INT
923
+ )
924
+ RETURNS TABLE (acquired BOOLEAN, is_active BOOLEAN)
925
+ LANGUAGE plpgsql
926
+ AS $emt_try_acquire_projection_lock$
927
+ BEGIN
928
+ RETURN QUERY
929
+ WITH lock_check AS (
930
+ SELECT pg_try_advisory_xact_lock_shared(p_lock_key) AS acquired
931
+ ),
932
+ status_check AS (
933
+ SELECT status = 'active' AS is_active
934
+ FROM ${projectionsTable.name}
935
+ WHERE partition = p_partition AND name = p_name AND version = p_version
936
+ )
937
+ SELECT
938
+ COALESCE((SELECT lc.acquired FROM lock_check lc), false),
939
+ COALESCE((SELECT sc.is_active FROM status_check sc), true);
940
+ END;
941
+ $emt_try_acquire_projection_lock$;
942
+ `
943
+ );
944
+ var callTryAcquireProjectionLock = (params) => sql3(
945
+ `SELECT * FROM emt_try_acquire_projection_lock(%s::BIGINT, %L, %L, %s);`,
946
+ params.lockKey,
947
+ params.partition,
948
+ params.name,
949
+ params.version
950
+ );
951
+
952
+ // src/eventStore/projections/locks/tryAcquireProjectionLock.ts
953
+ var tryAcquireProjectionLock = async (execute, {
954
+ lockKey,
955
+ projectionName,
956
+ partition,
957
+ version
958
+ }) => {
959
+ const lockKeyBigInt = isBigint(lockKey) ? lockKey : await hashText(lockKey);
960
+ const { acquired, is_active } = await single(
961
+ execute.query(
962
+ callTryAcquireProjectionLock({
963
+ lockKey: lockKeyBigInt.toString(),
964
+ partition,
965
+ name: projectionName,
966
+ version
967
+ })
968
+ )
969
+ );
970
+ return acquired === true && is_active === true;
971
+ };
972
+
973
+ // src/eventStore/projections/locks/postgreSQLProjectionLock.ts
974
+ var postgreSQLProjectionLock = (options) => {
975
+ let acquired = false;
976
+ const lockKey = options.lockKey ?? toProjectionLockKey(options);
977
+ return {
978
+ tryAcquire: async (context) => {
979
+ if (acquired) {
980
+ return true;
981
+ }
982
+ acquired = await tryAcquireProjectionLock(context.execute, {
983
+ ...options,
984
+ lockKey
985
+ });
986
+ return acquired;
987
+ },
988
+ release: (_context) => {
989
+ if (!acquired) return;
990
+ acquired = false;
991
+ }
992
+ };
993
+ };
994
+ var toProjectionLockKey = ({
995
+ projectionName,
996
+ partition,
997
+ version
998
+ }) => `${partition}:${projectionName}:${version}`;
999
+
1000
+ // src/eventStore/projections/locks/tryAcquireProcessorLock.ts
1001
+ import { single as single2 } from "@event-driven-io/dumbo";
1002
+
914
1003
  // src/eventStore/schema/processors/processorsLocks.ts
1004
+ import { sql as sql4 } from "@event-driven-io/dumbo";
915
1005
  var tryAcquireProcessorLockSQL = createFunctionIfDoesNotExistSQL(
916
1006
  "emt_try_acquire_processor_lock",
917
1007
  `
@@ -1026,7 +1116,7 @@ END;
1026
1116
  $emt_release_processor_lock$;
1027
1117
  `
1028
1118
  );
1029
- var callTryAcquireProcessorLock = (params) => sql3(
1119
+ var callTryAcquireProcessorLock = (params) => sql4(
1030
1120
  `SELECT * FROM emt_try_acquire_processor_lock(%s::BIGINT, %L, %s, %L, %L, %L, %L, %L, %s);`,
1031
1121
  params.lockKey,
1032
1122
  params.processorId,
@@ -1038,7 +1128,7 @@ var callTryAcquireProcessorLock = (params) => sql3(
1038
1128
  params.projectionKind,
1039
1129
  params.lockTimeoutSeconds
1040
1130
  );
1041
- var callReleaseProcessorLock = (params) => sql3(
1131
+ var callReleaseProcessorLock = (params) => sql4(
1042
1132
  `SELECT emt_release_processor_lock(%s::BIGINT, %L, %L, %s, %L, %L) as result;`,
1043
1133
  params.lockKey,
1044
1134
  params.processorId,
@@ -1052,7 +1142,7 @@ var callReleaseProcessorLock = (params) => sql3(
1052
1142
  var PROCESSOR_LOCK_DEFAULT_TIMEOUT_SECONDS = 300;
1053
1143
  var tryAcquireProcessorLock = async (execute, options) => {
1054
1144
  const lockKeyBigInt = isBigint(options.lockKey) ? options.lockKey : await hashText(options.lockKey);
1055
- const { acquired, checkpoint } = await single(
1145
+ const { acquired, checkpoint } = await single2(
1056
1146
  execute.command(
1057
1147
  callTryAcquireProcessorLock({
1058
1148
  lockKey: lockKeyBigInt.toString(),
@@ -1070,7 +1160,7 @@ var tryAcquireProcessorLock = async (execute, options) => {
1070
1160
  return acquired ? { acquired: true, checkpoint } : { acquired: false };
1071
1161
  };
1072
1162
  var tryAcquireProcessorLockWithRetry = async (execute, options) => {
1073
- const policy = options.lockPolicy ?? DefaultPostgreSQLProcessorLockPolicy;
1163
+ const policy = options.lockAcquisitionPolicy ?? DefaultPostgreSQLProcessorLockPolicy;
1074
1164
  if (policy.type === "retry") {
1075
1165
  return asyncRetry(() => tryAcquireProcessorLock(execute, options), {
1076
1166
  retries: policy.retries - 1,
@@ -1083,7 +1173,7 @@ var tryAcquireProcessorLockWithRetry = async (execute, options) => {
1083
1173
  };
1084
1174
  var releaseProcessorLock = async (execute, options) => {
1085
1175
  const lockKeyBigInt = isBigint(options.lockKey) ? options.lockKey : await hashText(options.lockKey);
1086
- const { result } = await single(
1176
+ const { result } = await single2(
1087
1177
  execute.command(
1088
1178
  callReleaseProcessorLock({
1089
1179
  lockKey: lockKeyBigInt.toString(),
@@ -1098,96 +1188,6 @@ var releaseProcessorLock = async (execute, options) => {
1098
1188
  return result;
1099
1189
  };
1100
1190
 
1101
- // src/eventStore/projections/locks/tryAcquireProjectionLock.ts
1102
- import { single as single2 } from "@event-driven-io/dumbo";
1103
-
1104
- // src/eventStore/schema/projections/projectionsLocks.ts
1105
- import { sql as sql4 } from "@event-driven-io/dumbo";
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) => sql4(
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 single2(
1152
- execute.query(
1153
- callTryAcquireProjectionLock({
1154
- lockKey: lockKeyBigInt.toString(),
1155
- partition,
1156
- name: projectionName,
1157
- version
1158
- })
1159
- )
1160
- );
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 = 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
- };
1184
- };
1185
- var toProjectionLockKey = ({
1186
- projectionName,
1187
- partition,
1188
- version
1189
- }) => `${partition}:${projectionName}:${version}`;
1190
-
1191
1191
  // src/eventStore/projections/locks/postgreSQLProcessorLock.ts
1192
1192
  var DefaultPostgreSQLProcessorLockPolicy = {
1193
1193
  type: "fail"
@@ -1204,7 +1204,7 @@ var postgreSQLProcessorLock = (options) => {
1204
1204
  ...options,
1205
1205
  lockKey
1206
1206
  });
1207
- if (!result.acquired && options.lockPolicy?.type !== "skip") {
1207
+ if (!result.acquired && options.lockAcquisitionPolicy?.type !== "skip") {
1208
1208
  throw new EmmettError(
1209
1209
  `Failed to acquire lock for processor '${options.processorId}'`
1210
1210
  );
@@ -4723,8 +4723,7 @@ var postgreSQLProjector = (options) => {
4723
4723
  processorInstanceId = getProcessorInstanceId(processorId),
4724
4724
  version = defaultProcessorVersion,
4725
4725
  partition = defaultProcessorPartition,
4726
- lockPolicy = DefaultPostgreSQLProcessorLockPolicy,
4727
- lockTimeoutSeconds
4726
+ lock
4728
4727
  } = options;
4729
4728
  const { pool, connectionString, close } = getProcessorPool(options);
4730
4729
  const processorLock = postgreSQLProcessorLock({
@@ -4738,8 +4737,8 @@ var postgreSQLProjector = (options) => {
4738
4737
  version: options.projection.version ?? version,
4739
4738
  handlingType: "async"
4740
4739
  } : void 0,
4741
- lockPolicy,
4742
- lockTimeoutSeconds
4740
+ lockAcquisitionPolicy: lock?.acquisitionPolicy ?? DefaultPostgreSQLProcessorLockPolicy,
4741
+ lockTimeoutSeconds: lock?.timeoutSeconds
4743
4742
  });
4744
4743
  const hooks = wrapHooksWithProcessorLocks(
4745
4744
  {
@@ -4791,7 +4790,7 @@ var postgreSQLReactor = (options) => {
4791
4790
  processorInstanceId = getProcessorInstanceId(processorId),
4792
4791
  version = defaultProcessorVersion,
4793
4792
  partition = defaultProcessorPartition,
4794
- lockPolicy = DefaultPostgreSQLProcessorLockPolicy
4793
+ lock
4795
4794
  } = options;
4796
4795
  const { pool, connectionString, close } = getProcessorPool(options);
4797
4796
  const processorLock = postgreSQLProcessorLock({
@@ -4800,7 +4799,8 @@ var postgreSQLReactor = (options) => {
4800
4799
  partition,
4801
4800
  processorInstanceId,
4802
4801
  projection: void 0,
4803
- lockPolicy
4802
+ lockAcquisitionPolicy: lock?.acquisitionPolicy ?? DefaultPostgreSQLProcessorLockPolicy,
4803
+ lockTimeoutSeconds: lock?.timeoutSeconds
4804
4804
  });
4805
4805
  const hooks = wrapHooksWithProcessorLocks(
4806
4806
  {
@@ -4986,9 +4986,9 @@ var rebuildPostgreSQLProjections = (options) => {
4986
4986
  ...options,
4987
4987
  stopWhen: { noMessagesLeft: true }
4988
4988
  });
4989
+ const lock = { acquisitionPolicy: defaultRebuildLockPolicy, ...options.lock };
4989
4990
  const projections = "projections" in options ? options.projections.map(
4990
4991
  (p) => "projection" in p ? {
4991
- lockPolicy: defaultRebuildLockPolicy,
4992
4992
  truncateOnStart: true,
4993
4993
  processorId: getProjectorId({
4994
4994
  projectionName: p.projection.name ?? unknownTag2
@@ -4999,14 +4999,14 @@ var rebuildPostgreSQLProjections = (options) => {
4999
4999
  processorId: getProjectorId({
5000
5000
  projectionName: p.name ?? unknownTag2
5001
5001
  }),
5002
- truncateOnStart: true,
5003
- lockPolicy: defaultRebuildLockPolicy
5002
+ truncateOnStart: true
5004
5003
  }
5005
5004
  ) : [options];
5006
5005
  for (const projectionDefinition of projections) {
5007
5006
  consumer.projector({
5008
5007
  ...projectionDefinition,
5009
- truncateOnStart: projectionDefinition.truncateOnStart ?? true
5008
+ truncateOnStart: projectionDefinition.truncateOnStart ?? true,
5009
+ lock
5010
5010
  });
5011
5011
  }
5012
5012
  return consumer;