@dudousxd/nestjs-durable-store-mikro-orm 0.33.8 → 0.35.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
@@ -33,6 +33,8 @@ __export(index_exports, {
33
33
  RunAttributeRepository: () => RunAttributeRepository,
34
34
  ScheduleEntity: () => ScheduleEntity,
35
35
  ScheduleRepository: () => ScheduleRepository,
36
+ SignalIdEntity: () => SignalIdEntity,
37
+ SignalIdRepository: () => SignalIdRepository,
36
38
  SignalWaiterEntity: () => SignalWaiterEntity,
37
39
  SignalWaiterRepository: () => SignalWaiterRepository,
38
40
  StepCheckpointEntity: () => StepCheckpointEntity,
@@ -87,6 +89,11 @@ var ScheduleRepository = class extends import_core.EntityRepository {
87
89
  __name(this, "ScheduleRepository");
88
90
  }
89
91
  };
92
+ var SignalIdRepository = class extends import_core.EntityRepository {
93
+ static {
94
+ __name(this, "SignalIdRepository");
95
+ }
96
+ };
90
97
 
91
98
  // src/entities.ts
92
99
  function snakeCase(property) {
@@ -187,6 +194,16 @@ var BufferedEventEntity = class {
187
194
  payload;
188
195
  publishedAt;
189
196
  };
197
+ var SignalIdEntity = class {
198
+ static {
199
+ __name(this, "SignalIdEntity");
200
+ }
201
+ [import_core2.EntityRepositoryType];
202
+ token;
203
+ signalId;
204
+ /** Epoch ms as a `bigint` column — exact, unlike a DATETIME. Drivers may hand it back as a string. */
205
+ expiresAt;
206
+ };
190
207
  var ScheduleEntity = class {
191
208
  static {
192
209
  __name(this, "ScheduleEntity");
@@ -635,6 +652,36 @@ function durableEntities(options = {}) {
635
652
  }
636
653
  }
637
654
  });
655
+ const signalIds = new import_core2.EntitySchema({
656
+ class: SignalIdEntity,
657
+ tableName: "durable_signal_ids",
658
+ repository: /* @__PURE__ */ __name(() => SignalIdRepository, "repository"),
659
+ // The expired-id sweep (`expires_at <= now`).
660
+ indexes: [
661
+ {
662
+ name: "durable_signal_ids_expires_at_idx",
663
+ properties: [
664
+ "expiresAt"
665
+ ]
666
+ }
667
+ ],
668
+ properties: {
669
+ token: {
670
+ type: "string",
671
+ primary: true,
672
+ fieldName: col("token")
673
+ },
674
+ signalId: {
675
+ type: "string",
676
+ primary: true,
677
+ fieldName: col("signalId")
678
+ },
679
+ expiresAt: {
680
+ type: "bigint",
681
+ fieldName: col("expiresAt")
682
+ }
683
+ }
684
+ });
638
685
  const schedules = new import_core2.EntitySchema({
639
686
  class: ScheduleEntity,
640
687
  tableName: "durable_schedules",
@@ -706,6 +753,7 @@ function durableEntities(options = {}) {
706
753
  signalWaiters,
707
754
  bufferedSignals,
708
755
  bufferedEvents,
756
+ signalIds,
709
757
  schedules
710
758
  ];
711
759
  }
@@ -725,6 +773,7 @@ var DURABLE_TABLE_NAMES = /* @__PURE__ */ new Set([
725
773
  "durable_signal_waiters",
726
774
  "durable_buffered_signals",
727
775
  "durable_buffered_events",
776
+ "durable_signal_ids",
728
777
  "durable_schedules"
729
778
  ]);
730
779
  function durableManagedTables() {
@@ -1095,6 +1144,9 @@ async function alignDurableCollation(orm) {
1095
1144
  __name(alignDurableCollation, "alignDurableCollation");
1096
1145
 
1097
1146
  // src/mikro-orm-state-store.ts
1147
+ var RETENTION_EXCLUDED = [
1148
+ import_nestjs_durable_core.LIFECYCLE_REGISTRY_WORKFLOW
1149
+ ];
1098
1150
  var MikroOrmStateStore = class _MikroOrmStateStore {
1099
1151
  static {
1100
1152
  __name(this, "MikroOrmStateStore");
@@ -1102,10 +1154,31 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1102
1154
  orm;
1103
1155
  opts;
1104
1156
  scopeNamespace;
1157
+ // The round-trip-saving optional methods, bound on Postgres and MySQL (see
1158
+ // `MikroOrmRoundTrips`); absent on SQLite, where the engine's read-then-write calls are used.
1159
+ settleResultsIfPending;
1160
+ insertCheckpointIfAbsent;
1161
+ tryLockRunAndGet;
1162
+ claimPendingRuns;
1105
1163
  constructor(orm, opts = {}) {
1106
1164
  this.orm = orm;
1107
1165
  this.opts = opts;
1108
1166
  this.scopeNamespace = opts.scope?.namespace;
1167
+ let platform = "";
1168
+ try {
1169
+ platform = String(orm.em.getPlatform().constructor.name).toLowerCase();
1170
+ } catch {
1171
+ return;
1172
+ }
1173
+ const dialect = platform.includes("postgre") ? "postgres" : platform.includes("mysql") ? "mysql" : void 0;
1174
+ if (!dialect) return;
1175
+ const fast = new MikroOrmRoundTrips(() => this.orm.em.fork(), dialect, this.scopeNamespace, this);
1176
+ this.insertCheckpointIfAbsent = (cp) => fast.insertCheckpointIfAbsent(cp);
1177
+ this.settleResultsIfPending = (results) => fast.settleResultsIfPending(results);
1178
+ this.claimPendingRuns = (limit, owner, until, now, ns) => fast.claimPendingRuns(limit, owner, until, now, ns);
1179
+ if (dialect === "postgres") {
1180
+ this.tryLockRunAndGet = (runId, owner, until, now) => fast.tryLockRunAndGet(runId, owner, until, now);
1181
+ }
1109
1182
  }
1110
1183
  /**
1111
1184
  * Derive a store confined to a single tenant `scope.namespace`, sharing this store's ORM (no new
@@ -1322,13 +1395,16 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1322
1395
  const idCol = meta.properties.id?.fieldNames?.[0] ?? "id";
1323
1396
  const query = {
1324
1397
  ...policy.scope ?? {},
1325
- statuses
1398
+ statuses,
1399
+ excludeWorkflows: RETENTION_EXCLUDED
1326
1400
  };
1401
+ const tagsCol = meta.properties.tags?.fieldNames?.[0] ?? "tags";
1327
1402
  const scoped = /* @__PURE__ */ __name((extra) => this.runQueryBuilder(em, query, {
1328
1403
  ...this.runWhere(query),
1329
1404
  ...extra ?? {}
1330
1405
  }).select([
1331
- `r.${idCol}`
1406
+ `r.${idCol}`,
1407
+ `r.${tagsCol}`
1332
1408
  ]), "scoped");
1333
1409
  const ids = /* @__PURE__ */ new Set();
1334
1410
  if (policy.maxAge != null) {
@@ -1340,7 +1416,9 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1340
1416
  }).orderBy({
1341
1417
  "r.updatedAt": "asc"
1342
1418
  }).limit(limit).execute();
1343
- for (const r of rows) ids.add(String(r[idCol] ?? r.id));
1419
+ for (const r of rows) {
1420
+ if (!(0, import_nestjs_durable_core.retentionKeeps)(r[tagsCol] ?? r.tags)) ids.add(String(r[idCol] ?? r.id));
1421
+ }
1344
1422
  }
1345
1423
  if (policy.maxCount != null && ids.size < limit) {
1346
1424
  const rows = await scoped().orderBy({
@@ -1349,7 +1427,7 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1349
1427
  }).limit(limit).offset(policy.maxCount).execute();
1350
1428
  for (const r of rows) {
1351
1429
  if (ids.size >= limit) break;
1352
- ids.add(String(r[idCol] ?? r.id));
1430
+ if (!(0, import_nestjs_durable_core.retentionKeeps)(r[tagsCol] ?? r.tags)) ids.add(String(r[idCol] ?? r.id));
1353
1431
  }
1354
1432
  }
1355
1433
  const idList = [
@@ -1451,6 +1529,24 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1451
1529
  }
1452
1530
  async settleCheckpointsIfPending(settlements) {
1453
1531
  if (settlements.length === 0) return [];
1532
+ if (settlements.length === 1) {
1533
+ const s = settlements[0];
1534
+ const changed = await this.fork().nativeUpdate(StepCheckpointEntity, {
1535
+ runId: s.runId,
1536
+ seq: s.seq,
1537
+ status: "pending"
1538
+ }, {
1539
+ status: s.status,
1540
+ output: s.output ?? null,
1541
+ error: s.error ?? null,
1542
+ events: s.events ?? null,
1543
+ startedAt: s.startedAt,
1544
+ finishedAt: s.finishedAt
1545
+ });
1546
+ return [
1547
+ changed === 1
1548
+ ];
1549
+ }
1454
1550
  return this.fork().transactional(async (em) => {
1455
1551
  const pending = await lockPendingCheckpoints(em, settlements, [
1456
1552
  "runId",
@@ -1647,6 +1743,18 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1647
1743
  $in: query.workflows
1648
1744
  }
1649
1745
  });
1746
+ if (query.excludeWorkflows?.length) and.push({
1747
+ workflow: {
1748
+ $nin: query.excludeWorkflows
1749
+ }
1750
+ });
1751
+ if (query.updatedAfter !== void 0) {
1752
+ and.push({
1753
+ updatedAt: {
1754
+ $gte: new Date(query.updatedAfter)
1755
+ }
1756
+ });
1757
+ }
1650
1758
  if (query.namespace !== void 0) where.namespace = query.namespace;
1651
1759
  if (query.namespaces) and.push({
1652
1760
  namespace: {
@@ -2327,6 +2435,92 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
2327
2435
  payload
2328
2436
  };
2329
2437
  }
2438
+ async takeOldestBufferedSignal(tokens) {
2439
+ if (tokens.length === 0) return null;
2440
+ const unique = [
2441
+ ...new Set(tokens)
2442
+ ];
2443
+ for (; ; ) {
2444
+ const em = this.fork();
2445
+ const entity = await em.findOne(BufferedSignalEntity, {
2446
+ token: {
2447
+ $in: unique
2448
+ }
2449
+ }, {
2450
+ orderBy: {
2451
+ id: "asc"
2452
+ }
2453
+ });
2454
+ if (!entity) return null;
2455
+ if (await em.nativeDelete(BufferedSignalEntity, {
2456
+ id: entity.id
2457
+ }) === 1) {
2458
+ return {
2459
+ token: entity.token,
2460
+ payload: entity.payload ?? void 0
2461
+ };
2462
+ }
2463
+ }
2464
+ }
2465
+ async claimSignalId(token, id, expiresAt, nowMs) {
2466
+ for (; ; ) {
2467
+ const em = this.fork();
2468
+ try {
2469
+ await em.insert(SignalIdEntity, {
2470
+ token,
2471
+ signalId: id,
2472
+ expiresAt
2473
+ });
2474
+ return true;
2475
+ } catch (err) {
2476
+ if (!(err instanceof import_core3.UniqueConstraintViolationException)) throw err;
2477
+ }
2478
+ const taken = await em.nativeUpdate(SignalIdEntity, {
2479
+ token,
2480
+ signalId: id,
2481
+ expiresAt: {
2482
+ $lte: nowMs
2483
+ }
2484
+ }, {
2485
+ expiresAt
2486
+ });
2487
+ if (taken === 1) return true;
2488
+ if (await em.count(SignalIdEntity, {
2489
+ token,
2490
+ signalId: id
2491
+ }) > 0) return false;
2492
+ }
2493
+ }
2494
+ async releaseSignalId(token, id) {
2495
+ await this.fork().nativeDelete(SignalIdEntity, {
2496
+ token,
2497
+ signalId: id
2498
+ });
2499
+ }
2500
+ async deleteExpiredSignalIds(nowMs, limit) {
2501
+ const em = this.fork();
2502
+ const rows = await em.find(SignalIdEntity, {
2503
+ expiresAt: {
2504
+ $lte: nowMs
2505
+ }
2506
+ }, {
2507
+ fields: [
2508
+ "token",
2509
+ "signalId"
2510
+ ],
2511
+ limit
2512
+ });
2513
+ if (rows.length === 0) return 0;
2514
+ return em.nativeDelete(SignalIdEntity, {
2515
+ $or: rows.map((r) => ({
2516
+ token: r.token,
2517
+ signalId: r.signalId
2518
+ })),
2519
+ expiresAt: {
2520
+ $lte: nowMs
2521
+ }
2522
+ });
2523
+ }
2330
2524
  async listBufferedSignals(tokenPrefix, afterId, limit) {
2331
2525
  const em = this.fork();
2332
2526
  const rows = await em.find(BufferedSignalEntity, {
@@ -2639,6 +2833,240 @@ function fromScheduleEntity(e) {
2639
2833
  };
2640
2834
  }
2641
2835
  __name(fromScheduleEntity, "fromScheduleEntity");
2836
+ var MikroOrmRoundTrips = class MikroOrmRoundTrips2 {
2837
+ static {
2838
+ __name(this, "MikroOrmRoundTrips");
2839
+ }
2840
+ fork;
2841
+ scope;
2842
+ store;
2843
+ pg;
2844
+ cpTypes;
2845
+ constructor(fork, dialect, scope, store) {
2846
+ this.fork = fork;
2847
+ this.scope = scope;
2848
+ this.store = store;
2849
+ this.pg = dialect === "postgres";
2850
+ }
2851
+ q(em, name) {
2852
+ return em.getPlatform().quoteIdentifier(name);
2853
+ }
2854
+ meta(em, entity) {
2855
+ return em.getMetadata().get(entity);
2856
+ }
2857
+ field(em, entity, property) {
2858
+ const prop = this.meta(em, entity).properties[property];
2859
+ const name = prop?.fieldNames[0];
2860
+ if (!name) throw new Error(`no column for ${property}`);
2861
+ return name;
2862
+ }
2863
+ c(em, entity, property) {
2864
+ return this.q(em, this.field(em, entity, property));
2865
+ }
2866
+ table(em, entity) {
2867
+ return this.q(em, this.meta(em, entity).tableName);
2868
+ }
2869
+ /** `value` as MikroORM binds it for `property` (its custom type's conversion, if any). */
2870
+ bind(em, entity, property, value) {
2871
+ if (value == null) return null;
2872
+ const type = this.meta(em, entity).properties[property]?.customType;
2873
+ return type ? type.convertToDatabaseValue(value, em.getPlatform(), {
2874
+ mode: "query"
2875
+ }) : value;
2876
+ }
2877
+ async run(em, sql, params, method = "all") {
2878
+ const connection = em.getConnection();
2879
+ return connection.execute(sql, params, method, em.getTransactionContext());
2880
+ }
2881
+ rows(out) {
2882
+ if (Array.isArray(out)) return out;
2883
+ const rows = out?.rows;
2884
+ return Array.isArray(rows) ? rows : [];
2885
+ }
2886
+ affected(out) {
2887
+ return Number(out?.affectedRows ?? 0);
2888
+ }
2889
+ runFromRow(em, raw2) {
2890
+ return fromRunEntity(em.map(WorkflowRunEntity, raw2));
2891
+ }
2892
+ checkpointFromRow(em, raw2) {
2893
+ return fromCheckpointEntity(em.map(StepCheckpointEntity, raw2));
2894
+ }
2895
+ /** The checkpoint columns but `input`, quoted, each prefixed with `prefix`. */
2896
+ settledColumns(em, prefix) {
2897
+ return Object.keys(this.meta(em, StepCheckpointEntity).properties).filter((p) => p !== "input").map((p) => `${prefix}${this.c(em, StepCheckpointEntity, p)}`).join(", ");
2898
+ }
2899
+ async tryLockRunAndGet(runId, owner, leaseUntilMs, nowMs) {
2900
+ const em = this.fork();
2901
+ const R = WorkflowRunEntity;
2902
+ const until = this.c(em, R, "lockedUntil");
2903
+ const scope = this.scope !== void 0 ? ` AND ${this.c(em, R, "namespace")} = ?` : "";
2904
+ const out = await this.run(em, `UPDATE ${this.table(em, R)} SET ${this.c(em, R, "lockedBy")} = ?, ${until} = ? WHERE ${this.c(em, R, "id")} = ? AND (${until} IS NULL OR ${until} <= ?)${scope} RETURNING *`, [
2905
+ owner,
2906
+ this.bind(em, R, "lockedUntil", new Date(leaseUntilMs)),
2907
+ runId,
2908
+ this.bind(em, R, "lockedUntil", new Date(nowMs)),
2909
+ ...this.scope !== void 0 ? [
2910
+ this.scope
2911
+ ] : []
2912
+ ]);
2913
+ const [row] = this.rows(out);
2914
+ return row ? this.runFromRow(em, row) : null;
2915
+ }
2916
+ async insertCheckpointIfAbsent(checkpoint) {
2917
+ try {
2918
+ await this.fork().insert(StepCheckpointEntity, toCheckpointEntity(checkpoint));
2919
+ return true;
2920
+ } catch (err) {
2921
+ if (err instanceof import_core3.UniqueConstraintViolationException) return false;
2922
+ throw err;
2923
+ }
2924
+ }
2925
+ async cpType(em, property) {
2926
+ const field = this.field(em, StepCheckpointEntity, property);
2927
+ this.cpTypes ??= this.run(em, "SELECT column_name AS name, data_type AS type FROM information_schema.columns WHERE table_name = ? AND table_schema = current_schema()", [
2928
+ this.meta(em, StepCheckpointEntity).tableName
2929
+ ]).then((out) => new Map(this.rows(out).map((r) => [
2930
+ String(r.name),
2931
+ String(r.type)
2932
+ ])));
2933
+ return (await this.cpTypes).get(field) ?? "text";
2934
+ }
2935
+ async settleResultsIfPending(results) {
2936
+ const out = results.map(() => null);
2937
+ const seen = /* @__PURE__ */ new Set();
2938
+ const sent = results.flatMap((r, i) => {
2939
+ const key = checkpointKey(r.runId, r.seq);
2940
+ if (seen.has(key)) return [];
2941
+ seen.add(key);
2942
+ return [
2943
+ i
2944
+ ];
2945
+ });
2946
+ if (sent.length === 0) return out;
2947
+ const em = this.fork();
2948
+ const K = StepCheckpointEntity;
2949
+ const R = WorkflowRunEntity;
2950
+ const c = /* @__PURE__ */ __name((prop) => this.c(em, K, prop), "c");
2951
+ const finished = `NOT EXISTS (SELECT 1 FROM ${this.table(em, R)} r WHERE r.${this.c(em, R, "id")} = c.${c("runId")} AND r.${this.c(em, R, "status")} IN ('cancelled', 'completed'))`;
2952
+ if (!this.pg && sent.length > 1) {
2953
+ return (0, import_nestjs_durable_core.settleResultsWithPlainCalls)(this.store, results);
2954
+ }
2955
+ if (!this.pg) {
2956
+ for (const i of sent) {
2957
+ const s = results[i];
2958
+ const done = s.status === "completed";
2959
+ const wrote = this.affected(await this.run(em, `UPDATE ${this.table(em, K)} c SET c.${c("status")} = ?, c.${c("output")} = ${done ? "?" : `c.${c("output")}`}, c.${c("error")} = ?, c.${c("events")} = COALESCE(?, c.${c("events")}), c.${c("startedAt")} = COALESCE(?, c.${c("startedAt")}), c.${c("finishedAt")} = ? WHERE c.${c("runId")} = ? AND c.${c("seq")} = ? AND c.${c("status")} = 'pending' AND ${finished}`, [
2960
+ s.status,
2961
+ ...done ? [
2962
+ this.bind(em, K, "output", s.output)
2963
+ ] : [],
2964
+ this.bind(em, K, "error", s.error),
2965
+ this.bind(em, K, "events", s.events),
2966
+ this.bind(em, K, "startedAt", s.startedAt),
2967
+ this.bind(em, K, "finishedAt", s.finishedAt),
2968
+ s.runId,
2969
+ s.seq
2970
+ ], "run"));
2971
+ if (!wrote) continue;
2972
+ const [row] = this.rows(await this.run(em, `SELECT ${this.settledColumns(em, "")} FROM ${this.table(em, K)} WHERE ${c("runId")} = ? AND ${c("seq")} = ?`, [
2973
+ s.runId,
2974
+ s.seq
2975
+ ]));
2976
+ out[i] = row ? this.checkpointFromRow(em, row) : null;
2977
+ }
2978
+ return out;
2979
+ }
2980
+ const props = [
2981
+ "runId",
2982
+ "seq",
2983
+ "status",
2984
+ "output",
2985
+ "error",
2986
+ "events",
2987
+ "startedAt",
2988
+ "finishedAt"
2989
+ ];
2990
+ const types = new Map(await Promise.all(props.map(async (p) => [
2991
+ p,
2992
+ await this.cpType(em, p)
2993
+ ])));
2994
+ for (let at = 0; at < sent.length; at += 500) {
2995
+ const chunk = sent.slice(at, at + 500);
2996
+ const params = [];
2997
+ const rows = chunk.map((i) => {
2998
+ const s = results[i];
2999
+ params.push(s.runId, s.seq, s.status, this.bind(em, K, "output", s.status === "completed" ? s.output : null), this.bind(em, K, "error", s.error), this.bind(em, K, "events", s.events), this.bind(em, K, "startedAt", s.startedAt), this.bind(em, K, "finishedAt", s.finishedAt));
3000
+ return `(${props.map((p) => `?::${types.get(p)}`).join(", ")})`;
3001
+ });
3002
+ const settled = this.rows(await this.run(em, `UPDATE ${this.table(em, 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 ${finished} RETURNING ${this.settledColumns(em, "c.")}`, params));
3003
+ const byKey = new Map(chunk.map((i) => {
3004
+ const s = results[i];
3005
+ return [
3006
+ checkpointKey(s.runId, s.seq),
3007
+ i
3008
+ ];
3009
+ }));
3010
+ for (const raw2 of settled) {
3011
+ const cp = this.checkpointFromRow(em, raw2);
3012
+ const i = byKey.get(checkpointKey(cp.runId, cp.seq));
3013
+ if (i !== void 0) out[i] = cp;
3014
+ }
3015
+ }
3016
+ return out;
3017
+ }
3018
+ async claimPendingRuns(limit, owner, leaseUntilMs, nowMs, namespace) {
3019
+ if (limit <= 0) return [];
3020
+ const em = this.fork();
3021
+ const R = WorkflowRunEntity;
3022
+ const c = /* @__PURE__ */ __name((prop) => this.c(em, R, prop), "c");
3023
+ const until = this.bind(em, R, "lockedUntil", new Date(leaseUntilMs));
3024
+ const now = this.bind(em, R, "lockedUntil", new Date(nowMs));
3025
+ const filters = [
3026
+ ...namespace !== void 0 ? [
3027
+ namespace
3028
+ ] : [],
3029
+ ...this.scope !== void 0 ? [
3030
+ this.scope
3031
+ ] : []
3032
+ ];
3033
+ const where = `${c("status")} = 'pending' AND (${c("lockedUntil")} IS NULL OR ${c("lockedUntil")} <= ?)${filters.map(() => ` AND ${c("namespace")} = ?`).join("")}`;
3034
+ const sort = /* @__PURE__ */ __name((runs) => runs.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime() || a.id.localeCompare(b.id)), "sort");
3035
+ if (this.pg) {
3036
+ const out = await this.run(
3037
+ em,
3038
+ `WITH claimed AS MATERIALIZED (SELECT ${c("id")} AS claimed_id FROM ${this.table(em, R)} WHERE ${where} ORDER BY ${c("createdAt")}, ${c("id")} LIMIT ? FOR UPDATE SKIP LOCKED) UPDATE ${this.table(em, R)} AS r SET ${c("lockedBy")} = ?, ${c("lockedUntil")} = ? FROM claimed WHERE r.${c("id")} = claimed.claimed_id RETURNING r.*`,
3039
+ // `?` placeholders bind in order: the CTE's (now, namespace filters, limit) come first.
3040
+ [
3041
+ now,
3042
+ ...filters,
3043
+ limit,
3044
+ owner,
3045
+ until
3046
+ ]
3047
+ );
3048
+ return sort(this.rows(out).map((raw2) => this.runFromRow(em, raw2)));
3049
+ }
3050
+ return em.transactional(async (tx) => {
3051
+ const picked = this.rows(await this.run(tx, `SELECT * FROM ${this.table(tx, R)} WHERE ${where} ORDER BY ${c("createdAt")}, ${c("id")} LIMIT ? FOR UPDATE SKIP LOCKED`, [
3052
+ now,
3053
+ ...filters,
3054
+ limit
3055
+ ])).map((raw2) => this.runFromRow(tx, raw2));
3056
+ if (picked.length === 0) return [];
3057
+ await this.run(tx, `UPDATE ${this.table(tx, R)} SET ${c("lockedBy")} = ?, ${c("lockedUntil")} = ? WHERE ${c("id")} IN (${picked.map(() => "?").join(", ")})`, [
3058
+ owner,
3059
+ until,
3060
+ ...picked.map((run) => run.id)
3061
+ ], "run");
3062
+ return sort(picked.map((run) => ({
3063
+ ...run,
3064
+ lockedBy: owner,
3065
+ lockedUntil: leaseUntilMs
3066
+ })));
3067
+ });
3068
+ }
3069
+ };
2642
3070
  // Annotate the CommonJS export names for ESM import in node:
2643
3071
  0 && (module.exports = {
2644
3072
  BufferedEventEntity,
@@ -2653,6 +3081,8 @@ __name(fromScheduleEntity, "fromScheduleEntity");
2653
3081
  RunAttributeRepository,
2654
3082
  ScheduleEntity,
2655
3083
  ScheduleRepository,
3084
+ SignalIdEntity,
3085
+ SignalIdRepository,
2656
3086
  SignalWaiterEntity,
2657
3087
  SignalWaiterRepository,
2658
3088
  StepCheckpointEntity,