@dudousxd/nestjs-durable-store-prisma 0.24.7 → 0.26.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 CHANGED
@@ -28,6 +28,9 @@ module.exports = __toCommonJS(index_exports);
28
28
 
29
29
  // src/prisma-state-store.ts
30
30
  var import_nestjs_durable_core = require("@dudousxd/nestjs-durable-core");
31
+ var RETENTION_EXCLUDED = [
32
+ import_nestjs_durable_core.LIFECYCLE_REGISTRY_WORKFLOW
33
+ ];
31
34
  function namespaceWhere(namespace) {
32
35
  if (namespace === void 0) return {};
33
36
  return {
@@ -40,8 +43,28 @@ var PrismaStateStore = class {
40
43
  __name(this, "PrismaStateStore");
41
44
  }
42
45
  db;
46
+ // The round-trip-saving optional methods, bound on Postgres and MySQL (see `PrismaRoundTrips`);
47
+ // absent on SQLite, where the engine's read-then-write calls are used.
48
+ settleResultsIfPending;
49
+ insertCheckpointIfAbsent;
50
+ tryLockRunAndGet;
51
+ claimPendingRuns;
52
+ /** Raw single-statement writes on Postgres/MySQL (Prisma wraps `updateMany` in a transaction). */
53
+ fast;
43
54
  constructor(db) {
44
55
  this.db = db;
56
+ const provider = db._activeProvider;
57
+ const raw = typeof db.$queryRawUnsafe === "function";
58
+ const dialect = raw && provider === "postgresql" ? "postgres" : raw && provider === "mysql" ? "mysql" : void 0;
59
+ if (!dialect) return;
60
+ const fast = new PrismaRoundTrips(db, dialect, this);
61
+ this.fast = fast;
62
+ this.insertCheckpointIfAbsent = (cp) => fast.insertCheckpointIfAbsent(cp);
63
+ this.settleResultsIfPending = (results) => fast.settleResultsIfPending(results);
64
+ this.claimPendingRuns = (limit, owner, until, now, ns) => fast.claimPendingRuns(limit, owner, until, now, ns);
65
+ if (dialect === "postgres") {
66
+ this.tryLockRunAndGet = (runId, owner, until, now) => fast.tryLockRunAndGet(runId, owner, until, now);
67
+ }
45
68
  }
46
69
  async tryAdmitSingleton(runId, tag, workflow, limit, retryWakeAt) {
47
70
  const where = {
@@ -139,7 +162,8 @@ var PrismaStateStore = class {
139
162
  }
140
163
  };
141
164
  const data = toRunPatch(patch);
142
- const count = Object.keys(data).length ? (await this.db.durableWorkflowRun.updateMany({
165
+ const raw = Object.keys(data).length ? this.fast?.updateRunWhere(runId, expectedStatuses, patch) : void 0;
166
+ const count = raw ? await raw : Object.keys(data).length ? (await this.db.durableWorkflowRun.updateMany({
143
167
  where,
144
168
  data
145
169
  })).count : await this.db.durableWorkflowRun.count({
@@ -244,7 +268,8 @@ var PrismaStateStore = class {
244
268
  if (policy.maxAge == null && policy.maxCount == null) return 0;
245
269
  const scoped = this.runWhere({
246
270
  ...policy.scope ?? {},
247
- statuses
271
+ statuses,
272
+ excludeWorkflows: RETENTION_EXCLUDED
248
273
  });
249
274
  const ids = /* @__PURE__ */ new Set();
250
275
  if (policy.maxAge != null) {
@@ -261,20 +286,22 @@ var PrismaStateStore = class {
261
286
  ]
262
287
  },
263
288
  select: {
264
- id: true
289
+ id: true,
290
+ tags: true
265
291
  },
266
292
  orderBy: {
267
293
  updatedAt: "asc"
268
294
  },
269
295
  take: limit
270
296
  });
271
- for (const r of rows) ids.add(r.id);
297
+ for (const r of rows) if (!(0, import_nestjs_durable_core.retentionKeeps)(r.tags)) ids.add(r.id);
272
298
  }
273
299
  if (policy.maxCount != null && ids.size < limit) {
274
300
  const rows = await this.db.durableWorkflowRun.findMany({
275
301
  where: scoped,
276
302
  select: {
277
- id: true
303
+ id: true,
304
+ tags: true
278
305
  },
279
306
  orderBy: [
280
307
  {
@@ -289,7 +316,7 @@ var PrismaStateStore = class {
289
316
  });
290
317
  for (const r of rows) {
291
318
  if (ids.size >= limit) break;
292
- ids.add(r.id);
319
+ if (!(0, import_nestjs_durable_core.retentionKeeps)(r.tags)) ids.add(r.id);
293
320
  }
294
321
  }
295
322
  const doomed = [
@@ -431,7 +458,7 @@ var PrismaStateStore = class {
431
458
  seen.add(key);
432
459
  firsts.push(i);
433
460
  });
434
- const dialect = rawSettleDialect(this.db);
461
+ const dialect = firsts.length === 1 ? void 0 : rawSettleDialect(this.db);
435
462
  if (!dialect) {
436
463
  const settle = /* @__PURE__ */ __name(async (db) => {
437
464
  for (const i of firsts) {
@@ -600,6 +627,10 @@ var PrismaStateStore = class {
600
627
  return result.count === 1;
601
628
  }
602
629
  async releaseRunLock(runId, owner) {
630
+ if (this.fast) {
631
+ await this.fast.releaseRunLock(runId, owner);
632
+ return;
633
+ }
603
634
  await this.db.durableWorkflowRun.updateMany({
604
635
  where: owner === void 0 ? {
605
636
  id: runId
@@ -635,6 +666,18 @@ var PrismaStateStore = class {
635
666
  if (query.workflows) and.push(anyOf(query.workflows.map((w) => ({
636
667
  workflow: w
637
668
  }))));
669
+ if (query.excludeWorkflows?.length) and.push({
670
+ workflow: {
671
+ notIn: query.excludeWorkflows
672
+ }
673
+ });
674
+ if (query.updatedAfter !== void 0) {
675
+ and.push({
676
+ updatedAt: {
677
+ gte: new Date(query.updatedAfter)
678
+ }
679
+ });
680
+ }
638
681
  if (query.namespaces) and.push(anyOf(query.namespaces.map((n) => ({
639
682
  namespace: n
640
683
  }))));
@@ -1242,6 +1285,112 @@ var PrismaStateStore = class {
1242
1285
  payload: row.payload ?? void 0
1243
1286
  };
1244
1287
  }
1288
+ async takeOldestBufferedSignal(tokens) {
1289
+ if (tokens.length === 0) return null;
1290
+ const unique = [
1291
+ ...new Set(tokens)
1292
+ ];
1293
+ for (; ; ) {
1294
+ const row = await this.db.durableBufferedSignal.findFirst({
1295
+ where: {
1296
+ token: {
1297
+ in: unique
1298
+ }
1299
+ },
1300
+ orderBy: {
1301
+ id: "asc"
1302
+ }
1303
+ });
1304
+ if (!row) return null;
1305
+ const { count } = await this.db.durableBufferedSignal.deleteMany({
1306
+ where: {
1307
+ id: row.id
1308
+ }
1309
+ });
1310
+ if (count === 1) return {
1311
+ token: row.token,
1312
+ payload: row.payload ?? void 0
1313
+ };
1314
+ }
1315
+ }
1316
+ signalIds() {
1317
+ const delegate = this.db.durableSignalId;
1318
+ if (!delegate) {
1319
+ throw new Error("signal dedupe (`signal(..., { id })`) needs the DurableSignalId model \u2014 copy it from prisma/schema.prisma into your schema and re-run `prisma generate`");
1320
+ }
1321
+ return delegate;
1322
+ }
1323
+ async claimSignalId(token, id, expiresAt, nowMs) {
1324
+ const ids = this.signalIds();
1325
+ for (; ; ) {
1326
+ try {
1327
+ await ids.create({
1328
+ data: {
1329
+ token,
1330
+ signalId: id,
1331
+ expiresAt: BigInt(expiresAt)
1332
+ }
1333
+ });
1334
+ return true;
1335
+ } catch (err) {
1336
+ if (err?.code !== "P2002") throw err;
1337
+ }
1338
+ const { count } = await ids.updateMany({
1339
+ where: {
1340
+ token,
1341
+ signalId: id,
1342
+ expiresAt: {
1343
+ lte: BigInt(nowMs)
1344
+ }
1345
+ },
1346
+ data: {
1347
+ expiresAt: BigInt(expiresAt)
1348
+ }
1349
+ });
1350
+ if (count === 1) return true;
1351
+ if (await ids.count({
1352
+ where: {
1353
+ token,
1354
+ signalId: id
1355
+ }
1356
+ }) > 0) return false;
1357
+ }
1358
+ }
1359
+ async releaseSignalId(token, id) {
1360
+ await this.signalIds().deleteMany({
1361
+ where: {
1362
+ token,
1363
+ signalId: id
1364
+ }
1365
+ });
1366
+ }
1367
+ async deleteExpiredSignalIds(nowMs, limit) {
1368
+ const ids = this.signalIds();
1369
+ const expired = {
1370
+ lte: BigInt(nowMs)
1371
+ };
1372
+ const rows = await ids.findMany({
1373
+ where: {
1374
+ expiresAt: expired
1375
+ },
1376
+ select: {
1377
+ token: true,
1378
+ signalId: true
1379
+ },
1380
+ take: limit
1381
+ });
1382
+ if (rows.length === 0) return 0;
1383
+ const { count } = await ids.deleteMany({
1384
+ where: {
1385
+ expiresAt: expired,
1386
+ OR: rows.map((r) => ({
1387
+ token: r.token,
1388
+ signalId: r.signalId
1389
+ }))
1390
+ }
1391
+ });
1392
+ return count;
1393
+ }
1245
1394
  async listBufferedSignals(tokenPrefix, afterId, limit) {
1246
1395
  const rows = await this.db.durableBufferedSignal.findMany({
1247
1396
  where: {
@@ -1723,6 +1872,346 @@ function fromScheduleRow(row) {
1723
1872
  };
1724
1873
  }
1725
1874
  __name(fromScheduleRow, "fromScheduleRow");
1875
+ function fromRawRow(raw) {
1876
+ const row = {};
1877
+ for (const [key, value] of Object.entries(raw)) {
1878
+ row[key.replace(/_([a-z])/g, (_, c) => c.toUpperCase())] = value;
1879
+ }
1880
+ return row;
1881
+ }
1882
+ __name(fromRawRow, "fromRawRow");
1883
+ var SETTLED_CHECKPOINT_COLUMNS = "run_id, seq, name, kind, step_id, status, output, error, events, attempts, worker_group, parallel_group, wake_at, enqueued_at, started_at, finished_at";
1884
+ var PrismaRoundTrips = class PrismaRoundTrips2 {
1885
+ static {
1886
+ __name(this, "PrismaRoundTrips");
1887
+ }
1888
+ db;
1889
+ store;
1890
+ pg;
1891
+ constructor(db, dialect, store) {
1892
+ this.db = db;
1893
+ this.store = store;
1894
+ this.pg = dialect === "postgres";
1895
+ }
1896
+ get raw() {
1897
+ return this.db;
1898
+ }
1899
+ async tryLockRunAndGet(runId, owner, leaseUntilMs, nowMs) {
1900
+ const rows = await this.raw.$queryRawUnsafe(`UPDATE "durable_workflow_runs" SET locked_by = $1, locked_until = ($2::timestamptz AT TIME ZONE 'UTC') WHERE id = $3 AND (locked_until IS NULL OR locked_until <= ($4::timestamptz AT TIME ZONE 'UTC')) RETURNING *`, owner, new Date(leaseUntilMs).toISOString(), runId, new Date(nowMs).toISOString());
1901
+ const [row] = rows;
1902
+ return row ? fromRunRow(fromRawRow(row)) : null;
1903
+ }
1904
+ /** A parameter placeholder for `kind` at position `n` (1-based), in this dialect. */
1905
+ param(kind, n) {
1906
+ if (this.pg) {
1907
+ const p = `$${n}`;
1908
+ if (kind === "json") return `${p}::jsonb`;
1909
+ if (kind === "date") return `(${p}::timestamptz AT TIME ZONE 'UTC')`;
1910
+ if (kind === "bigint") return `${p}::bigint`;
1911
+ if (kind === "int") return `${p}::integer`;
1912
+ return p;
1913
+ }
1914
+ return kind === "json" ? "CAST(? AS JSON)" : "?";
1915
+ }
1916
+ /** A value of `kind` as the raw statement binds it (dates as UTC, JSON as text). */
1917
+ value(kind, v) {
1918
+ if (v == null) return null;
1919
+ if (kind === "json") return JSON.stringify(v);
1920
+ if (kind === "date") {
1921
+ const iso = (v instanceof Date ? v : new Date(v)).toISOString();
1922
+ return this.pg ? iso : iso.replace("T", " ").replace("Z", "");
1923
+ }
1924
+ return v;
1925
+ }
1926
+ q(name) {
1927
+ return this.pg ? `"${name}"` : `\`${name}\``;
1928
+ }
1929
+ async insertCheckpointIfAbsent(checkpoint) {
1930
+ const columns = [
1931
+ [
1932
+ "run_id",
1933
+ "text",
1934
+ checkpoint.runId
1935
+ ],
1936
+ [
1937
+ "seq",
1938
+ "int",
1939
+ checkpoint.seq
1940
+ ],
1941
+ [
1942
+ "name",
1943
+ "text",
1944
+ checkpoint.name
1945
+ ],
1946
+ [
1947
+ "kind",
1948
+ "text",
1949
+ checkpoint.kind
1950
+ ],
1951
+ [
1952
+ "step_id",
1953
+ "text",
1954
+ checkpoint.stepId
1955
+ ],
1956
+ [
1957
+ "status",
1958
+ "text",
1959
+ checkpoint.status
1960
+ ],
1961
+ [
1962
+ "input",
1963
+ "json",
1964
+ checkpoint.input
1965
+ ],
1966
+ [
1967
+ "output",
1968
+ "json",
1969
+ checkpoint.output
1970
+ ],
1971
+ [
1972
+ "error",
1973
+ "json",
1974
+ checkpoint.error
1975
+ ],
1976
+ [
1977
+ "events",
1978
+ "json",
1979
+ checkpoint.events
1980
+ ],
1981
+ [
1982
+ "attempts",
1983
+ "int",
1984
+ checkpoint.attempts
1985
+ ],
1986
+ [
1987
+ "worker_group",
1988
+ "text",
1989
+ checkpoint.workerGroup
1990
+ ],
1991
+ [
1992
+ "parallel_group",
1993
+ "text",
1994
+ checkpoint.parallelGroup
1995
+ ],
1996
+ [
1997
+ "wake_at",
1998
+ "bigint",
1999
+ checkpoint.wakeAt
2000
+ ],
2001
+ [
2002
+ "enqueued_at",
2003
+ "date",
2004
+ checkpoint.enqueuedAt ?? checkpoint.startedAt
2005
+ ],
2006
+ [
2007
+ "started_at",
2008
+ "date",
2009
+ checkpoint.startedAt
2010
+ ],
2011
+ [
2012
+ "finished_at",
2013
+ "date",
2014
+ checkpoint.finishedAt
2015
+ ]
2016
+ ];
2017
+ const names = columns.map(([name]) => this.q(name)).join(", ");
2018
+ const marks = columns.map(([, kind], i) => this.param(kind, i + 1)).join(", ");
2019
+ const values = columns.map(([, kind, v]) => this.value(kind, v));
2020
+ const insert = `INSERT INTO ${this.q("durable_step_checkpoints")} (${names}) VALUES (${marks})`;
2021
+ if (this.pg) {
2022
+ const rows = await this.raw.$queryRawUnsafe(`${insert} ON CONFLICT (run_id, seq) DO NOTHING RETURNING seq`, ...values);
2023
+ return rows.length === 1;
2024
+ }
2025
+ try {
2026
+ await this.raw.$executeRawUnsafe(insert, ...values);
2027
+ return true;
2028
+ } catch (err) {
2029
+ const code = String(err.meta?.code ?? "");
2030
+ if (code === "1062" || /Duplicate entry/i.test(String(err.message))) return false;
2031
+ throw err;
2032
+ }
2033
+ }
2034
+ /** The run columns a raw patch may write, with their kinds; any other goes through `updateMany`. */
2035
+ static RUN_COLUMNS = {
2036
+ workflow: [
2037
+ "workflow",
2038
+ "text"
2039
+ ],
2040
+ workflowVersion: [
2041
+ "workflow_version",
2042
+ "text"
2043
+ ],
2044
+ status: [
2045
+ "status",
2046
+ "text"
2047
+ ],
2048
+ input: [
2049
+ "input",
2050
+ "json"
2051
+ ],
2052
+ output: [
2053
+ "output",
2054
+ "json"
2055
+ ],
2056
+ error: [
2057
+ "error",
2058
+ "json"
2059
+ ],
2060
+ wakeAt: [
2061
+ "wake_at",
2062
+ "bigint"
2063
+ ],
2064
+ lockedBy: [
2065
+ "locked_by",
2066
+ "text"
2067
+ ],
2068
+ lockedUntil: [
2069
+ "locked_until",
2070
+ "date"
2071
+ ],
2072
+ awaitingDecisionTaskId: [
2073
+ "awaiting_decision_task_id",
2074
+ "text"
2075
+ ],
2076
+ recoveryAttempts: [
2077
+ "recovery_attempts",
2078
+ "int"
2079
+ ],
2080
+ tags: [
2081
+ "tags",
2082
+ "json"
2083
+ ],
2084
+ priority: [
2085
+ "priority",
2086
+ "int"
2087
+ ],
2088
+ origin: [
2089
+ "origin",
2090
+ "text"
2091
+ ],
2092
+ updatedAt: [
2093
+ "updated_at",
2094
+ "date"
2095
+ ]
2096
+ };
2097
+ /**
2098
+ * `updateRunIf`'s compare-and-set as one raw UPDATE (Prisma's `updateMany` opens a transaction
2099
+ * around it), resolving the rows it changed; `undefined` for a patch left to `updateMany` (search
2100
+ * attributes, which reindex a side table, namespace, createdAt).
2101
+ */
2102
+ updateRunWhere(runId, expected, patch) {
2103
+ const keys = Object.keys(patch).filter(
2104
+ // A non-null date field left undefined is left alone, as toRunPatch does.
2105
+ (k) => !(k === "updatedAt" && patch.updatedAt == null)
2106
+ );
2107
+ if (keys.length === 0 || keys.some((k) => !PrismaRoundTrips2.RUN_COLUMNS[k])) return void 0;
2108
+ const values = [];
2109
+ const set = keys.map((k) => {
2110
+ const [column, kind] = PrismaRoundTrips2.RUN_COLUMNS[k];
2111
+ values.push(this.value(kind, patch[k]));
2112
+ return `${this.q(column)} = ${this.param(kind, values.length)}`;
2113
+ });
2114
+ values.push(runId);
2115
+ const id = this.param("text", values.length);
2116
+ const statuses = expected.map((status) => {
2117
+ values.push(status);
2118
+ return this.param("text", values.length);
2119
+ });
2120
+ return this.raw.$executeRawUnsafe(`UPDATE ${this.q("durable_workflow_runs")} SET ${set.join(", ")} WHERE id = ${id} AND status IN (${statuses.join(", ")})`, ...values);
2121
+ }
2122
+ /** `releaseRunLock` as one raw UPDATE (no transaction around it). */
2123
+ async releaseRunLock(runId, owner) {
2124
+ const where = owner === void 0 ? "" : ` AND locked_by = ${this.param("text", 2)}`;
2125
+ await this.raw.$executeRawUnsafe(`UPDATE ${this.q("durable_workflow_runs")} SET locked_by = NULL, locked_until = NULL WHERE id = ${this.param("text", 1)}${where}`, runId, ...owner === void 0 ? [] : [
2126
+ owner
2127
+ ]);
2128
+ }
2129
+ async settleResultsIfPending(results) {
2130
+ const out = results.map(() => null);
2131
+ const seen = /* @__PURE__ */ new Set();
2132
+ const sent = results.flatMap((r, i) => {
2133
+ const key = checkpointKey(r.runId, r.seq);
2134
+ if (seen.has(key)) return [];
2135
+ seen.add(key);
2136
+ return [
2137
+ i
2138
+ ];
2139
+ });
2140
+ if (sent.length === 0) return out;
2141
+ const finished = `NOT EXISTS (SELECT 1 FROM durable_workflow_runs r WHERE r.id = c.run_id AND r.status IN ('cancelled', 'completed'))`;
2142
+ if (!this.pg) {
2143
+ if (sent.length > 1) return (0, import_nestjs_durable_core.settleResultsWithPlainCalls)(this.store, results);
2144
+ const i = sent[0];
2145
+ const s = results[i];
2146
+ const done = s.status === "completed";
2147
+ const at = /* @__PURE__ */ __name((d) => d.toISOString().replace("T", " ").replace("Z", ""), "at");
2148
+ const wrote = await this.raw.$executeRawUnsafe(`UPDATE durable_step_checkpoints c SET c.status = ?, c.output = ${done ? "CAST(? AS JSON)" : "c.output"}, c.error = CAST(? AS JSON), c.events = COALESCE(CAST(? AS JSON), c.events), c.started_at = COALESCE(?, c.started_at), c.finished_at = ? WHERE c.run_id = ? AND c.seq = ? AND c.status = 'pending' AND ${finished}`, s.status, ...done ? [
2149
+ jsonText(s.output)
2150
+ ] : [], jsonText(s.error), jsonText(s.events), s.startedAt ? at(s.startedAt) : null, at(s.finishedAt), s.runId, s.seq);
2151
+ if (wrote !== 1) return out;
2152
+ const [row] = await this.raw.$queryRawUnsafe(`SELECT ${SETTLED_CHECKPOINT_COLUMNS} FROM durable_step_checkpoints WHERE run_id = ? AND seq = ?`, s.runId, s.seq);
2153
+ out[i] = row ? fromCheckpointRow(fromRawRow({
2154
+ ...row,
2155
+ input: null
2156
+ })) : null;
2157
+ return out;
2158
+ }
2159
+ for (let at = 0; at < sent.length; at += 500) {
2160
+ const chunk = sent.slice(at, at + 500);
2161
+ const values = [];
2162
+ const rows = chunk.map((i) => {
2163
+ const s = results[i];
2164
+ const n = values.length;
2165
+ values.push(s.runId, s.seq, s.status, jsonText(s.status === "completed" ? s.output : null), jsonText(s.error), jsonText(s.events), s.startedAt ? s.startedAt.toISOString() : null, s.finishedAt.toISOString());
2166
+ const p = /* @__PURE__ */ __name((k) => `$${n + k}`, "p");
2167
+ return `(${p(1)}::text, ${p(2)}::integer, ${p(3)}::text, ${p(4)}::jsonb, ${p(5)}::jsonb, ${p(6)}::jsonb, (${p(7)}::timestamptz AT TIME ZONE 'UTC'), (${p(8)}::timestamptz AT TIME ZONE 'UTC'))`;
2168
+ });
2169
+ const settled = await this.raw.$queryRawUnsafe(`UPDATE "durable_step_checkpoints" AS c SET status = v.status, output = CASE WHEN v.status = 'completed' THEN v.output ELSE c.output END, error = v.error, events = COALESCE(v.events, c.events), started_at = COALESCE(v.started_at, c.started_at), finished_at = v.finished_at FROM (VALUES ${rows.join(", ")}) AS v(run_id, seq, status, output, error, events, started_at, finished_at) WHERE c.run_id = v.run_id AND c.seq = v.seq AND c.status = 'pending' AND ${finished} RETURNING ${SETTLED_CHECKPOINT_COLUMNS.split(", ").map((col) => `c.${col}`).join(", ")}`, ...values);
2170
+ const byKey = new Map(chunk.map((i) => {
2171
+ const s = results[i];
2172
+ return [
2173
+ checkpointKey(s.runId, s.seq),
2174
+ i
2175
+ ];
2176
+ }));
2177
+ for (const raw of settled) {
2178
+ const cp = fromCheckpointRow(fromRawRow({
2179
+ ...raw,
2180
+ input: null
2181
+ }));
2182
+ const i = byKey.get(checkpointKey(cp.runId, cp.seq));
2183
+ if (i !== void 0) out[i] = cp;
2184
+ }
2185
+ }
2186
+ return out;
2187
+ }
2188
+ async claimPendingRuns(limit, owner, leaseUntilMs, nowMs, namespace) {
2189
+ if (limit <= 0) return [];
2190
+ const sort = /* @__PURE__ */ __name((runs) => runs.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime() || a.id.localeCompare(b.id)), "sort");
2191
+ if (this.pg) {
2192
+ const ns = namespace !== void 0 ? " AND namespace = $5" : "";
2193
+ const rows = await this.raw.$queryRawUnsafe(`WITH claimed AS MATERIALIZED (SELECT id AS claimed_id FROM "durable_workflow_runs" WHERE status = 'pending' AND (locked_until IS NULL OR locked_until <= ($3::timestamptz AT TIME ZONE 'UTC'))${ns} ORDER BY created_at, id LIMIT $4 FOR UPDATE SKIP LOCKED) UPDATE "durable_workflow_runs" AS r SET locked_by = $1, locked_until = ($2::timestamptz AT TIME ZONE 'UTC') FROM claimed WHERE r.id = claimed.claimed_id RETURNING r.*`, owner, new Date(leaseUntilMs).toISOString(), new Date(nowMs).toISOString(), limit, ...namespace !== void 0 ? [
2194
+ namespace
2195
+ ] : []);
2196
+ return sort(rows.map((raw) => fromRunRow(fromRawRow(raw))));
2197
+ }
2198
+ return this.db.$transaction(async (tx) => {
2199
+ const raw = tx;
2200
+ const at = /* @__PURE__ */ __name((ms) => new Date(ms).toISOString().replace("T", " ").replace("Z", ""), "at");
2201
+ const ns = namespace !== void 0 ? " AND namespace = ?" : "";
2202
+ const picked = (await raw.$queryRawUnsafe(`SELECT * FROM durable_workflow_runs WHERE status = 'pending' AND (locked_until IS NULL OR locked_until <= ?)${ns} ORDER BY created_at, id LIMIT ? FOR UPDATE SKIP LOCKED`, at(nowMs), ...namespace !== void 0 ? [
2203
+ namespace
2204
+ ] : [], limit)).map((row) => fromRunRow(fromRawRow(row)));
2205
+ if (picked.length === 0) return [];
2206
+ await raw.$executeRawUnsafe(`UPDATE durable_workflow_runs SET locked_by = ?, locked_until = ? WHERE id IN (${picked.map(() => "?").join(", ")})`, owner, at(leaseUntilMs), ...picked.map((run) => run.id));
2207
+ return sort(picked.map((run) => ({
2208
+ ...run,
2209
+ lockedBy: owner,
2210
+ lockedUntil: leaseUntilMs
2211
+ })));
2212
+ });
2213
+ }
2214
+ };
1726
2215
 
1727
2216
  // src/schema.ts
1728
2217
  function durableManagedTables() {
@@ -1733,6 +2222,7 @@ function durableManagedTables() {
1733
2222
  "durable_signal_waiters",
1734
2223
  "durable_buffered_signals",
1735
2224
  "durable_buffered_events",
2225
+ "durable_signal_ids",
1736
2226
  "durable_schedules"
1737
2227
  ];
1738
2228
  }