@dudousxd/nestjs-durable-store-prisma 0.25.0 → 0.27.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.js CHANGED
@@ -2,7 +2,7 @@ var __defProp = Object.defineProperty;
2
2
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
3
 
4
4
  // src/prisma-state-store.ts
5
- import { LIFECYCLE_REGISTRY_WORKFLOW, RUN_VALUE_FACET_SCAN, SINGLETON_ACTIVE_STATUSES, SINGLETON_LOCK_CHUNK, TERMINAL_RUN_STATUSES, attributePredicateOperands, axisIsRunColumn, clearTerminalSingletonAdmission, mergeRunFacetRows, mergeRunValueFacetRows, normalizeAttributeRows, parseDuration, retentionKeeps, runValueFacetsFromRuns, singletonAdmissionDenialPatch, singletonAdmissionTags, singletonClaimantIds } from "@dudousxd/nestjs-durable-core";
5
+ import { LIFECYCLE_REGISTRY_WORKFLOW, RUN_VALUE_FACET_SCAN, SINGLETON_ACTIVE_STATUSES, SINGLETON_LOCK_CHUNK, TERMINAL_RUN_STATUSES, attributePredicateOperands, axisIsRunColumn, clearTerminalSingletonAdmission, mergeRunFacetRows, mergeRunValueFacetRows, normalizeAttributeRows, parseDuration, retentionKeeps, runValueFacetsFromRuns, settleResultsWithPlainCalls, singletonAdmissionDenialPatch, singletonAdmissionTags, singletonClaimantIds } from "@dudousxd/nestjs-durable-core";
6
6
  var RETENTION_EXCLUDED = [
7
7
  LIFECYCLE_REGISTRY_WORKFLOW
8
8
  ];
@@ -18,8 +18,28 @@ var PrismaStateStore = class {
18
18
  __name(this, "PrismaStateStore");
19
19
  }
20
20
  db;
21
+ // The round-trip-saving optional methods, bound on Postgres and MySQL (see `PrismaRoundTrips`);
22
+ // absent on SQLite, where the engine's read-then-write calls are used.
23
+ settleResultsIfPending;
24
+ insertCheckpointIfAbsent;
25
+ tryLockRunAndGet;
26
+ claimPendingRuns;
27
+ /** Raw single-statement writes on Postgres/MySQL (Prisma wraps `updateMany` in a transaction). */
28
+ fast;
21
29
  constructor(db) {
22
30
  this.db = db;
31
+ const provider = db._activeProvider;
32
+ const raw = typeof db.$queryRawUnsafe === "function";
33
+ const dialect = raw && provider === "postgresql" ? "postgres" : raw && provider === "mysql" ? "mysql" : void 0;
34
+ if (!dialect) return;
35
+ const fast = new PrismaRoundTrips(db, dialect, this);
36
+ this.fast = fast;
37
+ this.insertCheckpointIfAbsent = (cp) => fast.insertCheckpointIfAbsent(cp);
38
+ this.settleResultsIfPending = (results) => fast.settleResultsIfPending(results);
39
+ this.claimPendingRuns = (limit, owner, until, now, ns) => fast.claimPendingRuns(limit, owner, until, now, ns);
40
+ if (dialect === "postgres") {
41
+ this.tryLockRunAndGet = (runId, owner, until, now) => fast.tryLockRunAndGet(runId, owner, until, now);
42
+ }
23
43
  }
24
44
  async tryAdmitSingleton(runId, tag, workflow, limit, retryWakeAt) {
25
45
  const where = {
@@ -117,7 +137,8 @@ var PrismaStateStore = class {
117
137
  }
118
138
  };
119
139
  const data = toRunPatch(patch);
120
- const count = Object.keys(data).length ? (await this.db.durableWorkflowRun.updateMany({
140
+ const raw = Object.keys(data).length ? this.fast?.updateRunWhere(runId, expectedStatuses, patch) : void 0;
141
+ const count = raw ? await raw : Object.keys(data).length ? (await this.db.durableWorkflowRun.updateMany({
121
142
  where,
122
143
  data
123
144
  })).count : await this.db.durableWorkflowRun.count({
@@ -412,7 +433,7 @@ var PrismaStateStore = class {
412
433
  seen.add(key);
413
434
  firsts.push(i);
414
435
  });
415
- const dialect = rawSettleDialect(this.db);
436
+ const dialect = firsts.length === 1 ? void 0 : rawSettleDialect(this.db);
416
437
  if (!dialect) {
417
438
  const settle = /* @__PURE__ */ __name(async (db) => {
418
439
  for (const i of firsts) {
@@ -581,6 +602,10 @@ var PrismaStateStore = class {
581
602
  return result.count === 1;
582
603
  }
583
604
  async releaseRunLock(runId, owner) {
605
+ if (this.fast) {
606
+ await this.fast.releaseRunLock(runId, owner);
607
+ return;
608
+ }
584
609
  await this.db.durableWorkflowRun.updateMany({
585
610
  where: owner === void 0 ? {
586
611
  id: runId
@@ -635,6 +660,12 @@ var PrismaStateStore = class {
635
660
  if (query.origins) and.push(anyOf(query.origins.map((o) => ({
636
661
  origin: o
637
662
  }))));
663
+ if (query.tenantId !== void 0) and.push({
664
+ tenantId: query.tenantId
665
+ });
666
+ if (query.tenantIds) and.push(anyOf(query.tenantIds.map((t) => ({
667
+ tenantId: t
668
+ }))));
638
669
  if (query.status && query.statuses) {
639
670
  and.push({
640
671
  status: query.status
@@ -761,6 +792,8 @@ var PrismaStateStore = class {
761
792
  if ("tags" in patch) data.tags = jsonOrNull(patch.tags);
762
793
  if (patch.spec !== void 0) data.spec = patch.spec;
763
794
  if (patch.state !== void 0) data.state = patch.state;
795
+ if ("tenantId" in patch) data.tenantId = patch.tenantId ?? null;
796
+ if ("tenantKind" in patch) data.tenantKind = patch.tenantKind ?? null;
764
797
  if (patch.updatedAt !== void 0) data.updatedAt = patch.updatedAt;
765
798
  if (!Object.keys(data).length) return await this.getSchedule(id) !== null;
766
799
  const where = {
@@ -789,18 +822,23 @@ var PrismaStateStore = class {
789
822
  if (query.tag !== void 0) where.tags = {
790
823
  array_contains: query.tag
791
824
  };
825
+ const and = [];
826
+ if (query.tenantId !== void 0) and.push({
827
+ tenantId: query.tenantId
828
+ });
829
+ if (query.tenantIds) and.push(anyOf(query.tenantIds.map((t) => ({
830
+ tenantId: t
831
+ }))));
792
832
  if (query.dueBy !== void 0) {
793
- where.AND = [
794
- {
795
- paused: false
796
- },
797
- {
798
- nextFireAt: {
799
- lte: BigInt(query.dueBy)
800
- }
833
+ and.push({
834
+ paused: false
835
+ }, {
836
+ nextFireAt: {
837
+ lte: BigInt(query.dueBy)
801
838
  }
802
- ];
839
+ });
803
840
  }
841
+ if (and.length) where.AND = and;
804
842
  const rows = await this.schedules().findMany({
805
843
  where,
806
844
  // Soonest-due first, never-firing (NULL) last, id as the stable tie-break.
@@ -1235,6 +1273,112 @@ var PrismaStateStore = class {
1235
1273
  payload: row.payload ?? void 0
1236
1274
  };
1237
1275
  }
1276
+ async takeOldestBufferedSignal(tokens) {
1277
+ if (tokens.length === 0) return null;
1278
+ const unique = [
1279
+ ...new Set(tokens)
1280
+ ];
1281
+ for (; ; ) {
1282
+ const row = await this.db.durableBufferedSignal.findFirst({
1283
+ where: {
1284
+ token: {
1285
+ in: unique
1286
+ }
1287
+ },
1288
+ orderBy: {
1289
+ id: "asc"
1290
+ }
1291
+ });
1292
+ if (!row) return null;
1293
+ const { count } = await this.db.durableBufferedSignal.deleteMany({
1294
+ where: {
1295
+ id: row.id
1296
+ }
1297
+ });
1298
+ if (count === 1) return {
1299
+ token: row.token,
1300
+ payload: row.payload ?? void 0
1301
+ };
1302
+ }
1303
+ }
1304
+ signalIds() {
1305
+ const delegate = this.db.durableSignalId;
1306
+ if (!delegate) {
1307
+ 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`");
1308
+ }
1309
+ return delegate;
1310
+ }
1311
+ async claimSignalId(token, id, expiresAt, nowMs) {
1312
+ const ids = this.signalIds();
1313
+ for (; ; ) {
1314
+ try {
1315
+ await ids.create({
1316
+ data: {
1317
+ token,
1318
+ signalId: id,
1319
+ expiresAt: BigInt(expiresAt)
1320
+ }
1321
+ });
1322
+ return true;
1323
+ } catch (err) {
1324
+ if (err?.code !== "P2002") throw err;
1325
+ }
1326
+ const { count } = await ids.updateMany({
1327
+ where: {
1328
+ token,
1329
+ signalId: id,
1330
+ expiresAt: {
1331
+ lte: BigInt(nowMs)
1332
+ }
1333
+ },
1334
+ data: {
1335
+ expiresAt: BigInt(expiresAt)
1336
+ }
1337
+ });
1338
+ if (count === 1) return true;
1339
+ if (await ids.count({
1340
+ where: {
1341
+ token,
1342
+ signalId: id
1343
+ }
1344
+ }) > 0) return false;
1345
+ }
1346
+ }
1347
+ async releaseSignalId(token, id) {
1348
+ await this.signalIds().deleteMany({
1349
+ where: {
1350
+ token,
1351
+ signalId: id
1352
+ }
1353
+ });
1354
+ }
1355
+ async deleteExpiredSignalIds(nowMs, limit) {
1356
+ const ids = this.signalIds();
1357
+ const expired = {
1358
+ lte: BigInt(nowMs)
1359
+ };
1360
+ const rows = await ids.findMany({
1361
+ where: {
1362
+ expiresAt: expired
1363
+ },
1364
+ select: {
1365
+ token: true,
1366
+ signalId: true
1367
+ },
1368
+ take: limit
1369
+ });
1370
+ if (rows.length === 0) return 0;
1371
+ const { count } = await ids.deleteMany({
1372
+ where: {
1373
+ expiresAt: expired,
1374
+ OR: rows.map((r) => ({
1375
+ token: r.token,
1376
+ signalId: r.signalId
1377
+ }))
1378
+ }
1379
+ });
1380
+ return count;
1381
+ }
1238
1382
  async listBufferedSignals(tokenPrefix, afterId, limit) {
1239
1383
  const rows = await this.db.durableBufferedSignal.findMany({
1240
1384
  where: {
@@ -1359,6 +1503,8 @@ function toRunData(run) {
1359
1503
  namespace: run.namespace || "default",
1360
1504
  // Absent origin stays SQL NULL — "unknown", never coerced into a real-looking library name.
1361
1505
  origin: run.origin ?? null,
1506
+ tenantId: run.tenantId ?? null,
1507
+ tenantKind: run.tenantKind ?? null,
1362
1508
  createdAt: run.createdAt,
1363
1509
  updatedAt: run.updatedAt
1364
1510
  };
@@ -1382,6 +1528,8 @@ function toRunPatch(patch) {
1382
1528
  if ("priority" in patch) data.priority = patch.priority ?? null;
1383
1529
  if ("namespace" in patch) data.namespace = patch.namespace || "default";
1384
1530
  if ("origin" in patch) data.origin = patch.origin ?? null;
1531
+ if ("tenantId" in patch) data.tenantId = patch.tenantId ?? null;
1532
+ if ("tenantKind" in patch) data.tenantKind = patch.tenantKind ?? null;
1385
1533
  if (patch.createdAt != null) data.createdAt = patch.createdAt;
1386
1534
  if (patch.updatedAt != null) data.updatedAt = patch.updatedAt;
1387
1535
  return data;
@@ -1412,6 +1560,13 @@ function fromRunRow(row) {
1412
1560
  namespace: row.namespace,
1413
1561
  // NULL (a row written before the column existed) surfaces as `undefined` = unknown origin.
1414
1562
  origin: row.origin ?? void 0,
1563
+ // NULL = no tenant; left off entirely so a tenancy-off run reads back unchanged.
1564
+ ...row.tenantId != null ? {
1565
+ tenantId: row.tenantId
1566
+ } : {},
1567
+ ...row.tenantKind != null ? {
1568
+ tenantKind: row.tenantKind
1569
+ } : {},
1415
1570
  createdAt: row.createdAt,
1416
1571
  updatedAt: row.updatedAt
1417
1572
  };
@@ -1693,6 +1848,8 @@ function toScheduleData(r) {
1693
1848
  tags: jsonOrNull(r.tags),
1694
1849
  spec: r.spec,
1695
1850
  state: r.state,
1851
+ tenantId: r.tenantId ?? null,
1852
+ tenantKind: r.tenantKind ?? null,
1696
1853
  createdAt: r.createdAt,
1697
1854
  updatedAt: r.updatedAt
1698
1855
  };
@@ -1711,11 +1868,365 @@ function fromScheduleRow(row) {
1711
1868
  } : {},
1712
1869
  spec: row.spec ?? {},
1713
1870
  state: row.state ?? {},
1871
+ ...row.tenantId != null ? {
1872
+ tenantId: row.tenantId
1873
+ } : {},
1874
+ ...row.tenantKind != null ? {
1875
+ tenantKind: row.tenantKind
1876
+ } : {},
1714
1877
  createdAt: row.createdAt,
1715
1878
  updatedAt: row.updatedAt
1716
1879
  };
1717
1880
  }
1718
1881
  __name(fromScheduleRow, "fromScheduleRow");
1882
+ function fromRawRow(raw) {
1883
+ const row = {};
1884
+ for (const [key, value] of Object.entries(raw)) {
1885
+ row[key.replace(/_([a-z])/g, (_, c) => c.toUpperCase())] = value;
1886
+ }
1887
+ return row;
1888
+ }
1889
+ __name(fromRawRow, "fromRawRow");
1890
+ 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";
1891
+ var PrismaRoundTrips = class PrismaRoundTrips2 {
1892
+ static {
1893
+ __name(this, "PrismaRoundTrips");
1894
+ }
1895
+ db;
1896
+ store;
1897
+ pg;
1898
+ constructor(db, dialect, store) {
1899
+ this.db = db;
1900
+ this.store = store;
1901
+ this.pg = dialect === "postgres";
1902
+ }
1903
+ get raw() {
1904
+ return this.db;
1905
+ }
1906
+ async tryLockRunAndGet(runId, owner, leaseUntilMs, nowMs) {
1907
+ 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());
1908
+ const [row] = rows;
1909
+ return row ? fromRunRow(fromRawRow(row)) : null;
1910
+ }
1911
+ /** A parameter placeholder for `kind` at position `n` (1-based), in this dialect. */
1912
+ param(kind, n) {
1913
+ if (this.pg) {
1914
+ const p = `$${n}`;
1915
+ if (kind === "json") return `${p}::jsonb`;
1916
+ if (kind === "date") return `(${p}::timestamptz AT TIME ZONE 'UTC')`;
1917
+ if (kind === "bigint") return `${p}::bigint`;
1918
+ if (kind === "int") return `${p}::integer`;
1919
+ return p;
1920
+ }
1921
+ return kind === "json" ? "CAST(? AS JSON)" : "?";
1922
+ }
1923
+ /** A value of `kind` as the raw statement binds it (dates as UTC, JSON as text). */
1924
+ value(kind, v) {
1925
+ if (v == null) return null;
1926
+ if (kind === "json") return JSON.stringify(v);
1927
+ if (kind === "date") {
1928
+ const iso = (v instanceof Date ? v : new Date(v)).toISOString();
1929
+ return this.pg ? iso : iso.replace("T", " ").replace("Z", "");
1930
+ }
1931
+ return v;
1932
+ }
1933
+ q(name) {
1934
+ return this.pg ? `"${name}"` : `\`${name}\``;
1935
+ }
1936
+ async insertCheckpointIfAbsent(checkpoint) {
1937
+ const columns = [
1938
+ [
1939
+ "run_id",
1940
+ "text",
1941
+ checkpoint.runId
1942
+ ],
1943
+ [
1944
+ "seq",
1945
+ "int",
1946
+ checkpoint.seq
1947
+ ],
1948
+ [
1949
+ "name",
1950
+ "text",
1951
+ checkpoint.name
1952
+ ],
1953
+ [
1954
+ "kind",
1955
+ "text",
1956
+ checkpoint.kind
1957
+ ],
1958
+ [
1959
+ "step_id",
1960
+ "text",
1961
+ checkpoint.stepId
1962
+ ],
1963
+ [
1964
+ "status",
1965
+ "text",
1966
+ checkpoint.status
1967
+ ],
1968
+ [
1969
+ "input",
1970
+ "json",
1971
+ checkpoint.input
1972
+ ],
1973
+ [
1974
+ "output",
1975
+ "json",
1976
+ checkpoint.output
1977
+ ],
1978
+ [
1979
+ "error",
1980
+ "json",
1981
+ checkpoint.error
1982
+ ],
1983
+ [
1984
+ "events",
1985
+ "json",
1986
+ checkpoint.events
1987
+ ],
1988
+ [
1989
+ "attempts",
1990
+ "int",
1991
+ checkpoint.attempts
1992
+ ],
1993
+ [
1994
+ "worker_group",
1995
+ "text",
1996
+ checkpoint.workerGroup
1997
+ ],
1998
+ [
1999
+ "parallel_group",
2000
+ "text",
2001
+ checkpoint.parallelGroup
2002
+ ],
2003
+ [
2004
+ "wake_at",
2005
+ "bigint",
2006
+ checkpoint.wakeAt
2007
+ ],
2008
+ [
2009
+ "enqueued_at",
2010
+ "date",
2011
+ checkpoint.enqueuedAt ?? checkpoint.startedAt
2012
+ ],
2013
+ [
2014
+ "started_at",
2015
+ "date",
2016
+ checkpoint.startedAt
2017
+ ],
2018
+ [
2019
+ "finished_at",
2020
+ "date",
2021
+ checkpoint.finishedAt
2022
+ ]
2023
+ ];
2024
+ const names = columns.map(([name]) => this.q(name)).join(", ");
2025
+ const marks = columns.map(([, kind], i) => this.param(kind, i + 1)).join(", ");
2026
+ const values = columns.map(([, kind, v]) => this.value(kind, v));
2027
+ const insert = `INSERT INTO ${this.q("durable_step_checkpoints")} (${names}) VALUES (${marks})`;
2028
+ if (this.pg) {
2029
+ const rows = await this.raw.$queryRawUnsafe(`${insert} ON CONFLICT (run_id, seq) DO NOTHING RETURNING seq`, ...values);
2030
+ return rows.length === 1;
2031
+ }
2032
+ try {
2033
+ await this.raw.$executeRawUnsafe(insert, ...values);
2034
+ return true;
2035
+ } catch (err) {
2036
+ const code = String(err.meta?.code ?? "");
2037
+ if (code === "1062" || /Duplicate entry/i.test(String(err.message))) return false;
2038
+ throw err;
2039
+ }
2040
+ }
2041
+ /** The run columns a raw patch may write, with their kinds; any other goes through `updateMany`. */
2042
+ static RUN_COLUMNS = {
2043
+ workflow: [
2044
+ "workflow",
2045
+ "text"
2046
+ ],
2047
+ workflowVersion: [
2048
+ "workflow_version",
2049
+ "text"
2050
+ ],
2051
+ status: [
2052
+ "status",
2053
+ "text"
2054
+ ],
2055
+ input: [
2056
+ "input",
2057
+ "json"
2058
+ ],
2059
+ output: [
2060
+ "output",
2061
+ "json"
2062
+ ],
2063
+ error: [
2064
+ "error",
2065
+ "json"
2066
+ ],
2067
+ wakeAt: [
2068
+ "wake_at",
2069
+ "bigint"
2070
+ ],
2071
+ lockedBy: [
2072
+ "locked_by",
2073
+ "text"
2074
+ ],
2075
+ lockedUntil: [
2076
+ "locked_until",
2077
+ "date"
2078
+ ],
2079
+ awaitingDecisionTaskId: [
2080
+ "awaiting_decision_task_id",
2081
+ "text"
2082
+ ],
2083
+ recoveryAttempts: [
2084
+ "recovery_attempts",
2085
+ "int"
2086
+ ],
2087
+ tags: [
2088
+ "tags",
2089
+ "json"
2090
+ ],
2091
+ priority: [
2092
+ "priority",
2093
+ "int"
2094
+ ],
2095
+ origin: [
2096
+ "origin",
2097
+ "text"
2098
+ ],
2099
+ tenantId: [
2100
+ "tenant_id",
2101
+ "text"
2102
+ ],
2103
+ tenantKind: [
2104
+ "tenant_kind",
2105
+ "text"
2106
+ ],
2107
+ updatedAt: [
2108
+ "updated_at",
2109
+ "date"
2110
+ ]
2111
+ };
2112
+ /**
2113
+ * `updateRunIf`'s compare-and-set as one raw UPDATE (Prisma's `updateMany` opens a transaction
2114
+ * around it), resolving the rows it changed; `undefined` for a patch left to `updateMany` (search
2115
+ * attributes, which reindex a side table, namespace, createdAt).
2116
+ */
2117
+ updateRunWhere(runId, expected, patch) {
2118
+ const keys = Object.keys(patch).filter(
2119
+ // A non-null date field left undefined is left alone, as toRunPatch does.
2120
+ (k) => !(k === "updatedAt" && patch.updatedAt == null)
2121
+ );
2122
+ if (keys.length === 0 || keys.some((k) => !PrismaRoundTrips2.RUN_COLUMNS[k])) return void 0;
2123
+ const values = [];
2124
+ const set = keys.map((k) => {
2125
+ const [column, kind] = PrismaRoundTrips2.RUN_COLUMNS[k];
2126
+ values.push(this.value(kind, patch[k]));
2127
+ return `${this.q(column)} = ${this.param(kind, values.length)}`;
2128
+ });
2129
+ values.push(runId);
2130
+ const id = this.param("text", values.length);
2131
+ const statuses = expected.map((status) => {
2132
+ values.push(status);
2133
+ return this.param("text", values.length);
2134
+ });
2135
+ return this.raw.$executeRawUnsafe(`UPDATE ${this.q("durable_workflow_runs")} SET ${set.join(", ")} WHERE id = ${id} AND status IN (${statuses.join(", ")})`, ...values);
2136
+ }
2137
+ /** `releaseRunLock` as one raw UPDATE (no transaction around it). */
2138
+ async releaseRunLock(runId, owner) {
2139
+ const where = owner === void 0 ? "" : ` AND locked_by = ${this.param("text", 2)}`;
2140
+ 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 ? [] : [
2141
+ owner
2142
+ ]);
2143
+ }
2144
+ async settleResultsIfPending(results) {
2145
+ const out = results.map(() => null);
2146
+ const seen = /* @__PURE__ */ new Set();
2147
+ const sent = results.flatMap((r, i) => {
2148
+ const key = checkpointKey(r.runId, r.seq);
2149
+ if (seen.has(key)) return [];
2150
+ seen.add(key);
2151
+ return [
2152
+ i
2153
+ ];
2154
+ });
2155
+ if (sent.length === 0) return out;
2156
+ const finished = `NOT EXISTS (SELECT 1 FROM durable_workflow_runs r WHERE r.id = c.run_id AND r.status IN ('cancelled', 'completed'))`;
2157
+ if (!this.pg) {
2158
+ if (sent.length > 1) return settleResultsWithPlainCalls(this.store, results);
2159
+ const i = sent[0];
2160
+ const s = results[i];
2161
+ const done = s.status === "completed";
2162
+ const at = /* @__PURE__ */ __name((d) => d.toISOString().replace("T", " ").replace("Z", ""), "at");
2163
+ 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 ? [
2164
+ jsonText(s.output)
2165
+ ] : [], jsonText(s.error), jsonText(s.events), s.startedAt ? at(s.startedAt) : null, at(s.finishedAt), s.runId, s.seq);
2166
+ if (wrote !== 1) return out;
2167
+ const [row] = await this.raw.$queryRawUnsafe(`SELECT ${SETTLED_CHECKPOINT_COLUMNS} FROM durable_step_checkpoints WHERE run_id = ? AND seq = ?`, s.runId, s.seq);
2168
+ out[i] = row ? fromCheckpointRow(fromRawRow({
2169
+ ...row,
2170
+ input: null
2171
+ })) : null;
2172
+ return out;
2173
+ }
2174
+ for (let at = 0; at < sent.length; at += 500) {
2175
+ const chunk = sent.slice(at, at + 500);
2176
+ const values = [];
2177
+ const rows = chunk.map((i) => {
2178
+ const s = results[i];
2179
+ const n = values.length;
2180
+ 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());
2181
+ const p = /* @__PURE__ */ __name((k) => `$${n + k}`, "p");
2182
+ 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'))`;
2183
+ });
2184
+ 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);
2185
+ const byKey = new Map(chunk.map((i) => {
2186
+ const s = results[i];
2187
+ return [
2188
+ checkpointKey(s.runId, s.seq),
2189
+ i
2190
+ ];
2191
+ }));
2192
+ for (const raw of settled) {
2193
+ const cp = fromCheckpointRow(fromRawRow({
2194
+ ...raw,
2195
+ input: null
2196
+ }));
2197
+ const i = byKey.get(checkpointKey(cp.runId, cp.seq));
2198
+ if (i !== void 0) out[i] = cp;
2199
+ }
2200
+ }
2201
+ return out;
2202
+ }
2203
+ async claimPendingRuns(limit, owner, leaseUntilMs, nowMs, namespace) {
2204
+ if (limit <= 0) return [];
2205
+ const sort = /* @__PURE__ */ __name((runs) => runs.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime() || a.id.localeCompare(b.id)), "sort");
2206
+ if (this.pg) {
2207
+ const ns = namespace !== void 0 ? " AND namespace = $5" : "";
2208
+ 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 ? [
2209
+ namespace
2210
+ ] : []);
2211
+ return sort(rows.map((raw) => fromRunRow(fromRawRow(raw))));
2212
+ }
2213
+ return this.db.$transaction(async (tx) => {
2214
+ const raw = tx;
2215
+ const at = /* @__PURE__ */ __name((ms) => new Date(ms).toISOString().replace("T", " ").replace("Z", ""), "at");
2216
+ const ns = namespace !== void 0 ? " AND namespace = ?" : "";
2217
+ 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 ? [
2218
+ namespace
2219
+ ] : [], limit)).map((row) => fromRunRow(fromRawRow(row)));
2220
+ if (picked.length === 0) return [];
2221
+ 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));
2222
+ return sort(picked.map((run) => ({
2223
+ ...run,
2224
+ lockedBy: owner,
2225
+ lockedUntil: leaseUntilMs
2226
+ })));
2227
+ });
2228
+ }
2229
+ };
1719
2230
 
1720
2231
  // src/schema.ts
1721
2232
  function durableManagedTables() {
@@ -1726,6 +2237,7 @@ function durableManagedTables() {
1726
2237
  "durable_signal_waiters",
1727
2238
  "durable_buffered_signals",
1728
2239
  "durable_buffered_events",
2240
+ "durable_signal_ids",
1729
2241
  "durable_schedules"
1730
2242
  ];
1731
2243
  }