@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.cjs +115 -115
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +11 -4
- package/dist/index.d.ts +11 -4
- package/dist/index.js +108 -108
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -892,10 +892,10 @@ var zipPostgreSQLEventStoreMessageBatchPullerStartFrom = (options) => {
|
|
|
892
892
|
|
|
893
893
|
require('pg');
|
|
894
894
|
|
|
895
|
-
// src/eventStore/projections/locks/
|
|
895
|
+
// src/eventStore/projections/locks/tryAcquireProjectionLock.ts
|
|
896
896
|
|
|
897
897
|
|
|
898
|
-
// src/eventStore/schema/
|
|
898
|
+
// src/eventStore/schema/projections/projectionsLocks.ts
|
|
899
899
|
|
|
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) => _dumbo.sql.call(void 0,
|
|
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 _dumbo.single.call(void 0,
|
|
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 = _nullishCoalesce(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
|
+
|
|
1002
|
+
|
|
914
1003
|
// src/eventStore/schema/processors/processorsLocks.ts
|
|
1004
|
+
|
|
915
1005
|
var tryAcquireProcessorLockSQL = createFunctionIfDoesNotExistSQL(
|
|
916
1006
|
"emt_try_acquire_processor_lock",
|
|
917
1007
|
`
|
|
@@ -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 = _nullishCoalesce(options.
|
|
1163
|
+
const policy = _nullishCoalesce(options.lockAcquisitionPolicy, () => ( DefaultPostgreSQLProcessorLockPolicy));
|
|
1074
1164
|
if (policy.type === "retry") {
|
|
1075
1165
|
return asyncRetry(() => tryAcquireProcessorLock(execute, options), {
|
|
1076
1166
|
retries: policy.retries - 1,
|
|
@@ -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
|
-
|
|
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
|
-
)
|
|
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 = _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
|
-
};
|
|
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 && _optionalChain([options, 'access', _45 => _45.
|
|
1207
|
+
if (!result.acquired && _optionalChain([options, 'access', _45 => _45.lockAcquisitionPolicy, 'optionalAccess', _46 => _46.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
|
-
|
|
4727
|
-
lockTimeoutSeconds
|
|
4726
|
+
lock
|
|
4728
4727
|
} = options;
|
|
4729
4728
|
const { pool, connectionString, close } = getProcessorPool(options);
|
|
4730
4729
|
const processorLock = postgreSQLProcessorLock({
|
|
@@ -4738,13 +4737,13 @@ var postgreSQLProjector = (options) => {
|
|
|
4738
4737
|
version: _nullishCoalesce(options.projection.version, () => ( version)),
|
|
4739
4738
|
handlingType: "async"
|
|
4740
4739
|
} : void 0,
|
|
4741
|
-
|
|
4742
|
-
lockTimeoutSeconds
|
|
4740
|
+
lockAcquisitionPolicy: _nullishCoalesce(_optionalChain([lock, 'optionalAccess', _98 => _98.acquisitionPolicy]), () => ( DefaultPostgreSQLProcessorLockPolicy)),
|
|
4741
|
+
lockTimeoutSeconds: _optionalChain([lock, 'optionalAccess', _99 => _99.timeoutSeconds])
|
|
4743
4742
|
});
|
|
4744
4743
|
const hooks = wrapHooksWithProcessorLocks(
|
|
4745
4744
|
{
|
|
4746
4745
|
..._nullishCoalesce(options.hooks, () => ( {})),
|
|
4747
|
-
onInit: options.projection.init !== void 0 || _optionalChain([options, 'access',
|
|
4746
|
+
onInit: options.projection.init !== void 0 || _optionalChain([options, 'access', _100 => _100.hooks, 'optionalAccess', _101 => _101.onInit]) ? async (context) => {
|
|
4748
4747
|
if (options.projection.init)
|
|
4749
4748
|
await options.projection.init({
|
|
4750
4749
|
version: _nullishCoalesce(options.projection.version, () => ( version)),
|
|
@@ -4755,16 +4754,16 @@ var postgreSQLProjector = (options) => {
|
|
|
4755
4754
|
migrationOptions: options.migrationOptions
|
|
4756
4755
|
}
|
|
4757
4756
|
});
|
|
4758
|
-
if (_optionalChain([options, 'access',
|
|
4757
|
+
if (_optionalChain([options, 'access', _102 => _102.hooks, 'optionalAccess', _103 => _103.onInit]))
|
|
4759
4758
|
await options.hooks.onInit({
|
|
4760
4759
|
...context,
|
|
4761
4760
|
migrationOptions: options.migrationOptions
|
|
4762
4761
|
});
|
|
4763
|
-
} : _optionalChain([options, 'access',
|
|
4762
|
+
} : _optionalChain([options, 'access', _104 => _104.hooks, 'optionalAccess', _105 => _105.onInit]),
|
|
4764
4763
|
onClose: close ? async (context) => {
|
|
4765
|
-
if (_optionalChain([options, 'access',
|
|
4764
|
+
if (_optionalChain([options, 'access', _106 => _106.hooks, 'optionalAccess', _107 => _107.onClose])) await _optionalChain([options, 'access', _108 => _108.hooks, 'optionalAccess', _109 => _109.onClose, 'call', _110 => _110(context)]);
|
|
4766
4765
|
if (close) await close();
|
|
4767
|
-
} : _optionalChain([options, 'access',
|
|
4766
|
+
} : _optionalChain([options, 'access', _111 => _111.hooks, 'optionalAccess', _112 => _112.onClose])
|
|
4768
4767
|
},
|
|
4769
4768
|
processorLock
|
|
4770
4769
|
);
|
|
@@ -4791,7 +4790,7 @@ var postgreSQLReactor = (options) => {
|
|
|
4791
4790
|
processorInstanceId = getProcessorInstanceId(processorId),
|
|
4792
4791
|
version = defaultProcessorVersion,
|
|
4793
4792
|
partition = defaultProcessorPartition,
|
|
4794
|
-
|
|
4793
|
+
lock
|
|
4795
4794
|
} = options;
|
|
4796
4795
|
const { pool, connectionString, close } = getProcessorPool(options);
|
|
4797
4796
|
const processorLock = postgreSQLProcessorLock({
|
|
@@ -4800,15 +4799,16 @@ var postgreSQLReactor = (options) => {
|
|
|
4800
4799
|
partition,
|
|
4801
4800
|
processorInstanceId,
|
|
4802
4801
|
projection: void 0,
|
|
4803
|
-
|
|
4802
|
+
lockAcquisitionPolicy: _nullishCoalesce(_optionalChain([lock, 'optionalAccess', _113 => _113.acquisitionPolicy]), () => ( DefaultPostgreSQLProcessorLockPolicy)),
|
|
4803
|
+
lockTimeoutSeconds: _optionalChain([lock, 'optionalAccess', _114 => _114.timeoutSeconds])
|
|
4804
4804
|
});
|
|
4805
4805
|
const hooks = wrapHooksWithProcessorLocks(
|
|
4806
4806
|
{
|
|
4807
4807
|
..._nullishCoalesce(options.hooks, () => ( {})),
|
|
4808
4808
|
onClose: close ? async (context) => {
|
|
4809
|
-
if (_optionalChain([options, 'access',
|
|
4809
|
+
if (_optionalChain([options, 'access', _115 => _115.hooks, 'optionalAccess', _116 => _116.onClose])) await _optionalChain([options, 'access', _117 => _117.hooks, 'optionalAccess', _118 => _118.onClose, 'call', _119 => _119(context)]);
|
|
4810
4810
|
if (close) await close();
|
|
4811
|
-
} : _optionalChain([options, 'access',
|
|
4811
|
+
} : _optionalChain([options, 'access', _120 => _120.hooks, 'optionalAccess', _121 => _121.onClose])
|
|
4812
4812
|
},
|
|
4813
4813
|
processorLock
|
|
4814
4814
|
);
|
|
@@ -4865,7 +4865,7 @@ var postgreSQLEventStoreConsumer = (options) => {
|
|
|
4865
4865
|
})
|
|
4866
4866
|
);
|
|
4867
4867
|
return result.some(
|
|
4868
|
-
(r) => r.status === "fulfilled" && _optionalChain([r, 'access',
|
|
4868
|
+
(r) => r.status === "fulfilled" && _optionalChain([r, 'access', _122 => _122.value, 'optionalAccess', _123 => _123.type]) !== "STOP"
|
|
4869
4869
|
) ? void 0 : {
|
|
4870
4870
|
type: "STOP"
|
|
4871
4871
|
};
|
|
@@ -4884,7 +4884,7 @@ var postgreSQLEventStoreConsumer = (options) => {
|
|
|
4884
4884
|
if (!isRunning) return;
|
|
4885
4885
|
isRunning = false;
|
|
4886
4886
|
if (messagePuller) {
|
|
4887
|
-
_optionalChain([abortController, 'optionalAccess',
|
|
4887
|
+
_optionalChain([abortController, 'optionalAccess', _124 => _124.abort, 'call', _125 => _125()]);
|
|
4888
4888
|
await messagePuller.stop();
|
|
4889
4889
|
messagePuller = void 0;
|
|
4890
4890
|
abortController = null;
|
|
@@ -4937,8 +4937,8 @@ var postgreSQLEventStoreConsumer = (options) => {
|
|
|
4937
4937
|
stopWhen: options.stopWhen,
|
|
4938
4938
|
executor: pool.execute,
|
|
4939
4939
|
eachBatch,
|
|
4940
|
-
batchSize: _nullishCoalesce(_optionalChain([pulling, 'optionalAccess',
|
|
4941
|
-
pullingFrequencyInMs: _nullishCoalesce(_optionalChain([pulling, 'optionalAccess',
|
|
4940
|
+
batchSize: _nullishCoalesce(_optionalChain([pulling, 'optionalAccess', _126 => _126.batchSize]), () => ( DefaultPostgreSQLEventStoreProcessorBatchSize)),
|
|
4941
|
+
pullingFrequencyInMs: _nullishCoalesce(_optionalChain([pulling, 'optionalAccess', _127 => _127.pullingFrequencyInMs]), () => ( DefaultPostgreSQLEventStoreProcessorPullingFrequencyInMs)),
|
|
4942
4942
|
signal: abortController.signal
|
|
4943
4943
|
});
|
|
4944
4944
|
start = (async () => {
|
|
@@ -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: _nullishCoalesce(p.projection.name, () => ( unknownTag2))
|
|
@@ -4999,14 +4999,14 @@ var rebuildPostgreSQLProjections = (options) => {
|
|
|
4999
4999
|
processorId: getProjectorId({
|
|
5000
5000
|
projectionName: _nullishCoalesce(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: _nullishCoalesce(projectionDefinition.truncateOnStart, () => ( true))
|
|
5008
|
+
truncateOnStart: _nullishCoalesce(projectionDefinition.truncateOnStart, () => ( true)),
|
|
5009
|
+
lock
|
|
5010
5010
|
});
|
|
5011
5011
|
}
|
|
5012
5012
|
return consumer;
|