@dudousxd/nestjs-durable-store-typeorm 0.25.8 → 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.cjs CHANGED
@@ -29,6 +29,7 @@ __export(index_exports, {
29
29
  MYSQL_DATETIME_TYPE: () => MYSQL_DATETIME_TYPE,
30
30
  RunAttributeEntity: () => RunAttributeEntity,
31
31
  ScheduleEntity: () => ScheduleEntity,
32
+ SignalIdEntity: () => SignalIdEntity,
32
33
  SignalWaiterEntity: () => SignalWaiterEntity,
33
34
  StepCheckpointEntity: () => StepCheckpointEntity,
34
35
  TypeOrmStateStore: () => TypeOrmStateStore,
@@ -157,6 +158,15 @@ var BufferedEventEntity = class {
157
158
  payload;
158
159
  publishedAt;
159
160
  };
161
+ var SignalIdEntity = class {
162
+ static {
163
+ __name(this, "SignalIdEntity");
164
+ }
165
+ token;
166
+ signalId;
167
+ /** Epoch ms as a `bigint` (drivers hand it back as a string): exact, unlike a DATETIME. */
168
+ expiresAt;
169
+ };
160
170
  var ScheduleEntity = class {
161
171
  static {
162
172
  __name(this, "ScheduleEntity");
@@ -521,6 +531,29 @@ function durableEntities(options = {}) {
521
531
  }
522
532
  }
523
533
  });
534
+ const signalIds = new import_typeorm.EntitySchema({
535
+ name: "SignalIdEntity",
536
+ target: SignalIdEntity,
537
+ tableName: "durable_signal_ids",
538
+ // Its `(token, signal_id)` key and `expires_at` index are raw DDL in `schema.ts`, like every
539
+ // other durable table's.
540
+ columns: {
541
+ token: {
542
+ type: "text",
543
+ primary: true,
544
+ name: col("token")
545
+ },
546
+ signalId: {
547
+ type: "text",
548
+ primary: true,
549
+ name: col("signalId")
550
+ },
551
+ expiresAt: {
552
+ type: "bigint",
553
+ name: col("expiresAt")
554
+ }
555
+ }
556
+ });
524
557
  const schedules = new import_typeorm.EntitySchema({
525
558
  name: "ScheduleEntity",
526
559
  target: ScheduleEntity,
@@ -583,6 +616,7 @@ function durableEntities(options = {}) {
583
616
  signalWaiters,
584
617
  bufferedSignals,
585
618
  bufferedEvents,
619
+ signalIds,
586
620
  schedules
587
621
  ];
588
622
  }
@@ -635,6 +669,7 @@ var TABLE_TO_ENTITY = {
635
669
  durable_signal_waiters: SignalWaiterEntity,
636
670
  durable_buffered_signals: BufferedSignalEntity,
637
671
  durable_buffered_events: BufferedEventEntity,
672
+ durable_signal_ids: SignalIdEntity,
638
673
  durable_schedules: ScheduleEntity
639
674
  };
640
675
  function durableManagedTables() {
@@ -717,6 +752,8 @@ async function ensureTypeOrmDurableSchema(dataSource, options = {}) {
717
752
  const bevCol = /* @__PURE__ */ __name((property) => q(resolve("durable_buffered_events", property)), "bevCol");
718
753
  const schedules = q("durable_schedules");
719
754
  const schCol = /* @__PURE__ */ __name((property) => q(resolve("durable_schedules", property)), "schCol");
755
+ const signalIds = q("durable_signal_ids");
756
+ const sidCol = /* @__PURE__ */ __name((property) => q(resolve("durable_signal_ids", property)), "sidCol");
720
757
  const num = isPg ? "double precision" : isMysql ? "double" : "real";
721
758
  const bufferedId = isSqlite ? `${bufCol("id")} integer PRIMARY KEY AUTOINCREMENT` : isMysql ? `${bufCol("id")} bigint NOT NULL AUTO_INCREMENT PRIMARY KEY` : isPg ? `${bufCol("id")} bigserial PRIMARY KEY` : `${bufCol("id")} bigint NOT NULL AUTO_INCREMENT PRIMARY KEY`;
722
759
  const tables = [
@@ -779,6 +816,16 @@ async function ensureTypeOrmDurableSchema(dataSource, options = {}) {
779
816
  ${bevCol("payload")} ${txt}, ${bevCol("publishedAt")} ${ts} NOT NULL
780
817
  )`
781
818
  ],
819
+ // Signal dedupe ids (`signal(..., { id })`): one row per (token, id) until `expires_at` (epoch
820
+ // ms as a bigint — exact, unlike a MySQL DATETIME).
821
+ [
822
+ "durable_signal_ids",
823
+ `CREATE TABLE IF NOT EXISTS ${signalIds} (
824
+ ${sidCol("token")} ${str} NOT NULL, ${sidCol("signalId")} ${str} NOT NULL,
825
+ ${sidCol("expiresAt")} bigint NOT NULL,
826
+ PRIMARY KEY (${sidCol("token")}, ${sidCol("signalId")})
827
+ )`
828
+ ],
782
829
  // Persisted schedules. `next_fire_at` is epoch ms as a bigint (exact, so the compare-and-set on
783
830
  // it survives MySQL's DATETIME millisecond truncation); `spec`/`state` are engine-owned JSON.
784
831
  [
@@ -998,6 +1045,12 @@ async function ensureTypeOrmDurableSchema(dataSource, options = {}) {
998
1045
  `${bufferedEvents} (${bevCol("name")}, ${bevCol("publishedAt")})`,
999
1046
  "durable_buffered_events"
1000
1047
  ],
1048
+ // The expired-signal-id sweep (`expires_at <= now`).
1049
+ [
1050
+ "durable_signal_ids_expires_at_idx",
1051
+ `${signalIds} (${sidCol("expiresAt")})`,
1052
+ "durable_signal_ids"
1053
+ ],
1001
1054
  // Search-attribute pushdown: equality + range predicates probe by (key, value). Two composite
1002
1055
  // indexes — one per typed column — so a numeric range or a string equality is an index scan.
1003
1056
  // Persisted schedules: the poll's "due by T" scan (`paused = false AND next_fire_at <= T`).
@@ -1090,6 +1143,9 @@ __name(existingColumns, "existingColumns");
1090
1143
  // src/typeorm-state-store.ts
1091
1144
  var import_nestjs_durable_core = require("@dudousxd/nestjs-durable-core");
1092
1145
  var import_typeorm2 = require("typeorm");
1146
+ var RETENTION_EXCLUDED = [
1147
+ import_nestjs_durable_core.LIFECYCLE_REGISTRY_WORKFLOW
1148
+ ];
1093
1149
  var CHECKPOINT_NAME_KIND_SQL = "CASE WHEN cp.name LIKE 'signal:child:%' THEN 'child' WHEN cp.name LIKE 'fan:%' THEN 'folded' WHEN cp.name LIKE 'spawn:%' THEN 'spawn' ELSE 'step' END";
1094
1150
  var sqliteSingletonClaims = /* @__PURE__ */ new WeakMap();
1095
1151
  var TypeOrmStateStore = class {
@@ -1098,9 +1154,25 @@ var TypeOrmStateStore = class {
1098
1154
  }
1099
1155
  dataSource;
1100
1156
  options;
1157
+ // The round-trip-saving optional methods, bound per dialect where they save a statement (see
1158
+ // `TypeOrmRoundTrips`); absent on SQLite, where the engine's read-then-write calls are used.
1159
+ settleResultsIfPending;
1160
+ insertCheckpointIfAbsent;
1161
+ tryLockRunAndGet;
1162
+ claimPendingRuns;
1101
1163
  constructor(dataSource, options = {}) {
1102
1164
  this.dataSource = dataSource;
1103
1165
  this.options = options;
1166
+ const type = String(dataSource.options.type);
1167
+ const dialect = type === "postgres" ? "postgres" : type === "mysql" ? "mysql" : void 0;
1168
+ if (!dialect) return;
1169
+ const fast = new TypeOrmRoundTrips(dataSource, dialect, this);
1170
+ this.insertCheckpointIfAbsent = (cp) => fast.insertCheckpointIfAbsent(cp);
1171
+ this.settleResultsIfPending = (results) => fast.settleResultsIfPending(results);
1172
+ this.claimPendingRuns = (limit, owner, until, now, ns) => fast.claimPendingRuns(limit, owner, until, now, ns);
1173
+ if (dialect === "postgres") {
1174
+ this.tryLockRunAndGet = (runId, owner, until, now) => fast.tryLockRunAndGet(runId, owner, until, now);
1175
+ }
1104
1176
  }
1105
1177
  async ensureSchema() {
1106
1178
  await ensureTypeOrmDurableSchema(this.dataSource, {
@@ -1123,6 +1195,9 @@ var TypeOrmStateStore = class {
1123
1195
  bufferedEvents() {
1124
1196
  return this.dataSource.getRepository(BufferedEventEntity);
1125
1197
  }
1198
+ signalIds() {
1199
+ return this.dataSource.getRepository(SignalIdEntity);
1200
+ }
1126
1201
  attributes() {
1127
1202
  return this.dataSource.getRepository(RunAttributeEntity);
1128
1203
  }
@@ -1297,21 +1372,22 @@ var TypeOrmStateStore = class {
1297
1372
  if (policy.maxAge == null && policy.maxCount == null) return 0;
1298
1373
  const scoped = /* @__PURE__ */ __name(() => this.runQueryBuilder({
1299
1374
  ...policy.scope ?? {},
1300
- statuses
1301
- }).select("r.id", "id"), "scoped");
1375
+ statuses,
1376
+ excludeWorkflows: RETENTION_EXCLUDED
1377
+ }).select("r.id", "id").addSelect("r.tags", "tags"), "scoped");
1302
1378
  const ids = /* @__PURE__ */ new Set();
1303
1379
  if (policy.maxAge != null) {
1304
1380
  const cutoff = new Date(nowMs - (0, import_nestjs_durable_core.parseDuration)(policy.maxAge));
1305
1381
  const rows = await scoped().andWhere("r.updatedAt < :cutoff", {
1306
1382
  cutoff
1307
1383
  }).orderBy("r.updatedAt", "ASC").limit(limit).getRawMany();
1308
- for (const r of rows) ids.add(r.id);
1384
+ for (const r of rows) if (!(0, import_nestjs_durable_core.retentionKeeps)(r.tags)) ids.add(r.id);
1309
1385
  }
1310
1386
  if (policy.maxCount != null && ids.size < limit) {
1311
1387
  const rows = await scoped().orderBy("r.updatedAt", "DESC").addOrderBy("r.id", "DESC").limit(limit).offset(policy.maxCount).getRawMany();
1312
1388
  for (const r of rows) {
1313
1389
  if (ids.size >= limit) break;
1314
- ids.add(r.id);
1390
+ if (!(0, import_nestjs_durable_core.retentionKeeps)(r.tags)) ids.add(r.id);
1315
1391
  }
1316
1392
  }
1317
1393
  const doomed = [
@@ -1404,6 +1480,24 @@ var TypeOrmStateStore = class {
1404
1480
  }
1405
1481
  async settleCheckpointsIfPending(settlements) {
1406
1482
  if (settlements.length === 0) return [];
1483
+ if (settlements.length === 1) {
1484
+ const s = settlements[0];
1485
+ const result = await this.checkpoints().createQueryBuilder().update().set({
1486
+ status: s.status,
1487
+ output: s.output ?? null,
1488
+ error: s.error ?? null,
1489
+ events: s.events ?? null,
1490
+ startedAt: s.startedAt,
1491
+ finishedAt: s.finishedAt
1492
+ }).where({
1493
+ runId: s.runId,
1494
+ seq: s.seq,
1495
+ status: "pending"
1496
+ }).execute();
1497
+ return [
1498
+ result.affected === 1
1499
+ ];
1500
+ }
1407
1501
  return this.dataSource.transaction(async (em) => {
1408
1502
  const repo = em.getRepository(StepCheckpointEntity);
1409
1503
  const pending = await this.lockPendingCheckpoints(repo, settlements, [
@@ -1629,6 +1723,14 @@ var TypeOrmStateStore = class {
1629
1723
  if (query.workflows) qb.andWhere(query.workflows.length ? "r.workflow IN (:...workflows)" : "1 = 0", query.workflows.length ? {
1630
1724
  workflows: query.workflows
1631
1725
  } : {});
1726
+ if (query.excludeWorkflows?.length) qb.andWhere("r.workflow NOT IN (:...excludeWorkflows)", {
1727
+ excludeWorkflows: query.excludeWorkflows
1728
+ });
1729
+ if (query.updatedAfter !== void 0) {
1730
+ qb.andWhere("r.updatedAt >= :updatedAfter", {
1731
+ updatedAfter: new Date(query.updatedAfter)
1732
+ });
1733
+ }
1632
1734
  if (query.namespace !== void 0) qb.andWhere("r.namespace = :namespace", {
1633
1735
  namespace: query.namespace
1634
1736
  });
@@ -2086,6 +2188,87 @@ var TypeOrmStateStore = class {
2086
2188
  payload: e.payload ?? void 0
2087
2189
  };
2088
2190
  }
2191
+ async takeOldestBufferedSignal(tokens) {
2192
+ if (tokens.length === 0) return null;
2193
+ const unique = [
2194
+ ...new Set(tokens)
2195
+ ];
2196
+ for (; ; ) {
2197
+ const e = await this.buffered().findOne({
2198
+ where: {
2199
+ token: (0, import_typeorm2.In)(unique)
2200
+ },
2201
+ order: {
2202
+ id: "ASC"
2203
+ }
2204
+ });
2205
+ if (!e) return null;
2206
+ const deleted = await this.buffered().delete({
2207
+ id: e.id
2208
+ });
2209
+ if (deleted.affected === 1) return {
2210
+ token: e.token,
2211
+ payload: e.payload ?? void 0
2212
+ };
2213
+ }
2214
+ }
2215
+ async claimSignalId(token, id, expiresAt, nowMs) {
2216
+ for (; ; ) {
2217
+ try {
2218
+ await this.signalIds().insert({
2219
+ token,
2220
+ signalId: id,
2221
+ expiresAt
2222
+ });
2223
+ return true;
2224
+ } catch (err) {
2225
+ if (!isUniqueViolation(err)) throw err;
2226
+ }
2227
+ const taken = await this.signalIds().update({
2228
+ token,
2229
+ signalId: id,
2230
+ expiresAt: (0, import_typeorm2.LessThanOrEqual)(nowMs)
2231
+ }, {
2232
+ expiresAt
2233
+ });
2234
+ if (taken.affected === 1) return true;
2235
+ if (await this.signalIds().exists({
2236
+ where: {
2237
+ token,
2238
+ signalId: id
2239
+ }
2240
+ })) return false;
2241
+ }
2242
+ }
2243
+ async releaseSignalId(token, id) {
2244
+ await this.signalIds().delete({
2245
+ token,
2246
+ signalId: id
2247
+ });
2248
+ }
2249
+ async deleteExpiredSignalIds(nowMs, limit) {
2250
+ const rows = await this.signalIds().find({
2251
+ select: {
2252
+ token: true,
2253
+ signalId: true
2254
+ },
2255
+ where: {
2256
+ expiresAt: (0, import_typeorm2.LessThanOrEqual)(nowMs)
2257
+ },
2258
+ take: limit
2259
+ });
2260
+ if (rows.length === 0) return 0;
2261
+ const result = await this.signalIds().delete({
2262
+ token: (0, import_typeorm2.In)([
2263
+ ...new Set(rows.map((r) => r.token))
2264
+ ]),
2265
+ signalId: (0, import_typeorm2.In)([
2266
+ ...new Set(rows.map((r) => r.signalId))
2267
+ ]),
2268
+ expiresAt: (0, import_typeorm2.LessThanOrEqual)(nowMs)
2269
+ });
2270
+ return result.affected ?? rows.length;
2271
+ }
2089
2272
  async listBufferedSignals(tokenPrefix, afterId, limit) {
2090
2273
  const rows = await this.buffered().find({
2091
2274
  where: {
@@ -2371,6 +2554,251 @@ async function updateIfPending(repo, checkpoint) {
2371
2554
  return (result.affected ?? 0) > 0;
2372
2555
  }
2373
2556
  __name(updateIfPending, "updateIfPending");
2557
+ function isUniqueViolation(err) {
2558
+ const driverError = err?.driverError ?? err;
2559
+ const e = driverError;
2560
+ const code = String(e?.code ?? "");
2561
+ return code === "23505" || code === "ER_DUP_ENTRY" || e?.errno === 1062 || code.startsWith("SQLITE_CONSTRAINT");
2562
+ }
2563
+ __name(isUniqueViolation, "isUniqueViolation");
2564
+ var TypeOrmRoundTrips = class TypeOrmRoundTrips2 {
2565
+ static {
2566
+ __name(this, "TypeOrmRoundTrips");
2567
+ }
2568
+ ds;
2569
+ store;
2570
+ pg;
2571
+ /** The checkpoint columns' actual Postgres types (what the schema created), read once. */
2572
+ cpTypes;
2573
+ constructor(ds, dialect, store) {
2574
+ this.ds = ds;
2575
+ this.store = store;
2576
+ this.pg = dialect === "postgres";
2577
+ }
2578
+ get runs() {
2579
+ return this.ds.getMetadata(WorkflowRunEntity);
2580
+ }
2581
+ get cps() {
2582
+ return this.ds.getMetadata(StepCheckpointEntity);
2583
+ }
2584
+ /**
2585
+ * The Postgres type to cast a `VALUES` parameter of checkpoint `property` to: the column's actual
2586
+ * type in the database (a `timestamptz` the schema helper created, or a `timestamp` from
2587
+ * `synchronize`), so the value converts exactly as the repository's own writes convert it.
2588
+ */
2589
+ async cpType(property) {
2590
+ this.cpTypes ??= this.ds.query("SELECT column_name AS name, data_type AS type FROM information_schema.columns WHERE table_name = $1 AND table_schema = current_schema()", [
2591
+ this.cps.tableName
2592
+ ]).then((rows) => new Map(rows.map((r) => [
2593
+ r.name,
2594
+ r.type
2595
+ ])));
2596
+ const column = this.col(this.cps, property);
2597
+ return (await this.cpTypes).get(column.databaseName) ?? this.ds.driver.normalizeType(column);
2598
+ }
2599
+ q(name) {
2600
+ return this.pg ? `"${name.replace(/"/g, '""')}"` : `\`${name.replace(/`/g, "``")}\``;
2601
+ }
2602
+ p(i) {
2603
+ return this.pg ? `$${i}` : "?";
2604
+ }
2605
+ col(meta, property) {
2606
+ const column = meta.findColumnWithPropertyName(property);
2607
+ if (!column) throw new Error(`no column for ${property}`);
2608
+ return column;
2609
+ }
2610
+ c(meta, property) {
2611
+ return this.q(this.col(meta, property).databaseName);
2612
+ }
2613
+ table(meta) {
2614
+ return this.q(meta.tableName);
2615
+ }
2616
+ /** `value` as the driver binds it for `property` (transformers and type conversion included). */
2617
+ bind(meta, property, value) {
2618
+ if (value == null) return null;
2619
+ return this.ds.driver.preparePersistentValue(value, this.col(meta, property));
2620
+ }
2621
+ /** A raw row as the repository would hydrate it; a column the row lacks reads `null`. */
2622
+ hydrate(meta, raw) {
2623
+ const entity = {};
2624
+ for (const column of meta.columns) {
2625
+ const value = raw[column.databaseName];
2626
+ entity[column.propertyName] = value === void 0 ? null : this.ds.driver.prepareHydratedValue(value, column);
2627
+ }
2628
+ return entity;
2629
+ }
2630
+ /** Rows of a statement: Postgres `UPDATE … RETURNING` comes back as `[rows, count]`. */
2631
+ rows(out) {
2632
+ if (Array.isArray(out) && Array.isArray(out[0])) return out[0];
2633
+ return Array.isArray(out) ? out : [];
2634
+ }
2635
+ cpColumns(withInput) {
2636
+ return this.cps.columns.filter((c) => withInput || c.propertyName !== "input");
2637
+ }
2638
+ async tryLockRunAndGet(runId, owner, leaseUntilMs, nowMs) {
2639
+ const r = this.runs;
2640
+ const until = this.c(r, "lockedUntil");
2641
+ const out = await this.ds.query(`UPDATE ${this.table(r)} SET ${this.c(r, "lockedBy")} = $1, ${until} = $2 WHERE ${this.c(r, "id")} = $3 AND (${until} IS NULL OR ${until} <= $4) RETURNING *`, [
2642
+ owner,
2643
+ this.bind(r, "lockedUntil", new Date(leaseUntilMs)),
2644
+ runId,
2645
+ this.bind(r, "lockedUntil", new Date(nowMs))
2646
+ ]);
2647
+ const [row] = this.rows(out);
2648
+ return row ? fromRunEntity(this.hydrate(r, row)) : null;
2649
+ }
2650
+ async insertCheckpointIfAbsent(checkpoint) {
2651
+ const entity = toCheckpointEntity(checkpoint);
2652
+ const columns = this.cps.columns;
2653
+ const names = columns.map((c) => this.q(c.databaseName)).join(", ");
2654
+ const values = columns.map((c) => this.bind(this.cps, c.propertyName, entity[c.propertyName]));
2655
+ const marks = columns.map((_, i) => this.p(i + 1)).join(", ");
2656
+ const insert = `INSERT INTO ${this.table(this.cps)} (${names}) VALUES (${marks})`;
2657
+ if (this.pg) {
2658
+ const key = `${this.c(this.cps, "runId")}, ${this.c(this.cps, "seq")}`;
2659
+ const out = await this.ds.query(`${insert} ON CONFLICT (${key}) DO NOTHING RETURNING ${this.c(this.cps, "seq")}`, values);
2660
+ return this.rows(out).length === 1;
2661
+ }
2662
+ try {
2663
+ await this.ds.query(insert, values);
2664
+ return true;
2665
+ } catch (err) {
2666
+ const driver = err.driverError;
2667
+ if (driver?.errno === 1062 || driver?.code === "ER_DUP_ENTRY") return false;
2668
+ throw err;
2669
+ }
2670
+ }
2671
+ async settleResultsIfPending(results) {
2672
+ const out = results.map(() => null);
2673
+ const seen = /* @__PURE__ */ new Set();
2674
+ const sent = results.flatMap((r2, i) => {
2675
+ const key = checkpointKey(r2.runId, r2.seq);
2676
+ if (seen.has(key)) return [];
2677
+ seen.add(key);
2678
+ return [
2679
+ i
2680
+ ];
2681
+ });
2682
+ if (sent.length === 0) return out;
2683
+ if (!this.pg) {
2684
+ if (sent.length > 1) return (0, import_nestjs_durable_core.settleResultsWithPlainCalls)(this.store, results);
2685
+ const only = sent[0];
2686
+ out[only] = await this.settleOneMysql(results[only]);
2687
+ return out;
2688
+ }
2689
+ const k = this.cps;
2690
+ const r = this.runs;
2691
+ const props = [
2692
+ "runId",
2693
+ "seq",
2694
+ "status",
2695
+ "output",
2696
+ "error",
2697
+ "events",
2698
+ "startedAt",
2699
+ "finishedAt"
2700
+ ];
2701
+ const types = new Map(await Promise.all(props.map(async (prop) => [
2702
+ prop,
2703
+ await this.cpType(prop)
2704
+ ])));
2705
+ const type = /* @__PURE__ */ __name((property) => types.get(property), "type");
2706
+ for (let at = 0; at < sent.length; at += 500) {
2707
+ const chunk = sent.slice(at, at + 500);
2708
+ const values = [];
2709
+ const rows = chunk.map((i) => {
2710
+ const s = results[i];
2711
+ const n = values.length;
2712
+ values.push(s.runId, s.seq, s.status, this.bind(k, "output", s.status === "completed" ? s.output : null), this.bind(k, "error", s.error), this.bind(k, "events", s.events), this.bind(k, "startedAt", s.startedAt), this.bind(k, "finishedAt", s.finishedAt));
2713
+ const p = /* @__PURE__ */ __name((j) => `$${n + j}`, "p");
2714
+ return `(${p(1)}::${type("runId")}, ${p(2)}::${type("seq")}, ${p(3)}::${type("status")}, ${p(4)}::${type("output")}, ${p(5)}::${type("error")}, ${p(6)}::${type("events")}, ${p(7)}::${type("startedAt")}, ${p(8)}::${type("finishedAt")})`;
2715
+ });
2716
+ const c = /* @__PURE__ */ __name((prop) => this.c(k, prop), "c");
2717
+ const returning = this.cpColumns(false).map((col) => `c.${this.q(col.databaseName)}`).join(", ");
2718
+ const settled = this.rows(await this.ds.query(`UPDATE ${this.table(k)} AS c SET ${c("status")} = v.status, ${c("output")} = CASE WHEN v.status = 'completed' THEN v.output ELSE c.${c("output")} END, ${c("error")} = v.error, ${c("events")} = COALESCE(v.events, c.${c("events")}), ${c("startedAt")} = COALESCE(v.started_at, c.${c("startedAt")}), ${c("finishedAt")} = v.finished_at FROM (VALUES ${rows.join(", ")}) AS v(run_id, seq, status, output, error, events, started_at, finished_at) WHERE c.${c("runId")} = v.run_id AND c.${c("seq")} = v.seq AND c.${c("status")} = 'pending' AND NOT EXISTS (SELECT 1 FROM ${this.table(r)} AS r WHERE r.${this.c(r, "id")} = c.${c("runId")} AND r.${this.c(r, "status")} IN ('cancelled', 'completed')) RETURNING ${returning}`, values));
2719
+ const byKey = new Map(chunk.map((i) => {
2720
+ const s = results[i];
2721
+ return [
2722
+ checkpointKey(s.runId, s.seq),
2723
+ i
2724
+ ];
2725
+ }));
2726
+ for (const raw of settled) {
2727
+ const cp = fromCheckpointEntity(this.hydrate(k, raw));
2728
+ const i = byKey.get(checkpointKey(cp.runId, cp.seq));
2729
+ if (i !== void 0) out[i] = cp;
2730
+ }
2731
+ }
2732
+ return out;
2733
+ }
2734
+ async settleOneMysql(s) {
2735
+ const k = this.cps;
2736
+ const r = this.runs;
2737
+ const c = /* @__PURE__ */ __name((prop) => `c.${this.c(k, prop)}`, "c");
2738
+ const done = s.status === "completed";
2739
+ const result = await this.ds.query(`UPDATE ${this.table(k)} AS c SET ${c("status")} = ?, ${c("output")} = ${done ? "?" : c("output")}, ${c("error")} = ?, ${c("events")} = COALESCE(?, ${c("events")}), ${c("startedAt")} = COALESCE(?, ${c("startedAt")}), ${c("finishedAt")} = ? WHERE ${c("runId")} = ? AND ${c("seq")} = ? AND ${c("status")} = 'pending' AND NOT EXISTS (SELECT 1 FROM ${this.table(r)} AS r WHERE r.${this.c(r, "id")} = ${c("runId")} AND r.${this.c(r, "status")} IN ('cancelled', 'completed'))`, [
2740
+ s.status,
2741
+ ...done ? [
2742
+ this.bind(k, "output", s.output)
2743
+ ] : [],
2744
+ this.bind(k, "error", s.error),
2745
+ this.bind(k, "events", s.events),
2746
+ this.bind(k, "startedAt", s.startedAt),
2747
+ this.bind(k, "finishedAt", s.finishedAt),
2748
+ s.runId,
2749
+ s.seq
2750
+ ]);
2751
+ if (!result?.affectedRows) return null;
2752
+ const list = this.cpColumns(false).map((col) => this.q(col.databaseName)).join(", ");
2753
+ const [row] = this.rows(await this.ds.query(`SELECT ${list} FROM ${this.table(k)} WHERE ${this.c(k, "runId")} = ? AND ${this.c(k, "seq")} = ?`, [
2754
+ s.runId,
2755
+ s.seq
2756
+ ]));
2757
+ return row ? fromCheckpointEntity(this.hydrate(k, row)) : null;
2758
+ }
2759
+ async claimPendingRuns(limit, owner, leaseUntilMs, nowMs, namespace) {
2760
+ if (limit <= 0) return [];
2761
+ const r = this.runs;
2762
+ const c = /* @__PURE__ */ __name((prop) => this.c(r, prop), "c");
2763
+ const until = this.bind(r, "lockedUntil", new Date(leaseUntilMs));
2764
+ const now = this.bind(r, "lockedUntil", new Date(nowMs));
2765
+ const sort = /* @__PURE__ */ __name((runs) => runs.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime() || a.id.localeCompare(b.id)), "sort");
2766
+ if (this.pg) {
2767
+ const ns = namespace !== void 0 ? ` AND ${c("namespace")} = $5` : "";
2768
+ const out = await this.ds.query(`WITH claimed AS MATERIALIZED (SELECT ${c("id")} AS claimed_id FROM ${this.table(r)} WHERE ${c("status")} = 'pending' AND (${c("lockedUntil")} IS NULL OR ${c("lockedUntil")} <= $3)${ns} ORDER BY ${c("createdAt")}, ${c("id")} LIMIT $4 FOR UPDATE SKIP LOCKED) UPDATE ${this.table(r)} AS r SET ${c("lockedBy")} = $1, ${c("lockedUntil")} = $2 FROM claimed WHERE r.${c("id")} = claimed.claimed_id RETURNING r.*`, [
2769
+ owner,
2770
+ until,
2771
+ now,
2772
+ limit,
2773
+ ...namespace !== void 0 ? [
2774
+ namespace
2775
+ ] : []
2776
+ ]);
2777
+ return sort(this.rows(out).map((raw) => fromRunEntity(this.hydrate(r, raw))));
2778
+ }
2779
+ return this.ds.transaction(async (em) => {
2780
+ const ns = namespace !== void 0 ? ` AND ${c("namespace")} = ?` : "";
2781
+ const picked = this.rows(await em.query(`SELECT * FROM ${this.table(r)} WHERE ${c("status")} = 'pending' AND (${c("lockedUntil")} IS NULL OR ${c("lockedUntil")} <= ?)${ns} ORDER BY ${c("createdAt")}, ${c("id")} LIMIT ? FOR UPDATE SKIP LOCKED`, [
2782
+ now,
2783
+ ...namespace !== void 0 ? [
2784
+ namespace
2785
+ ] : [],
2786
+ limit
2787
+ ])).map((raw) => fromRunEntity(this.hydrate(r, raw)));
2788
+ if (picked.length === 0) return [];
2789
+ await em.query(`UPDATE ${this.table(r)} SET ${c("lockedBy")} = ?, ${c("lockedUntil")} = ? WHERE ${c("id")} IN (${picked.map(() => "?").join(", ")})`, [
2790
+ owner,
2791
+ until,
2792
+ ...picked.map((run) => run.id)
2793
+ ]);
2794
+ return sort(picked.map((run) => ({
2795
+ ...run,
2796
+ lockedBy: owner,
2797
+ lockedUntil: leaseUntilMs
2798
+ })));
2799
+ });
2800
+ }
2801
+ };
2374
2802
  // Annotate the CommonJS export names for ESM import in node:
2375
2803
  0 && (module.exports = {
2376
2804
  BufferedEventEntity,
@@ -2381,6 +2809,7 @@ __name(updateIfPending, "updateIfPending");
2381
2809
  MYSQL_DATETIME_TYPE,
2382
2810
  RunAttributeEntity,
2383
2811
  ScheduleEntity,
2812
+ SignalIdEntity,
2384
2813
  SignalWaiterEntity,
2385
2814
  StepCheckpointEntity,
2386
2815
  TypeOrmStateStore,