@dudousxd/nestjs-durable-store-mikro-orm 0.34.0 → 0.35.1

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() {
@@ -1105,10 +1154,31 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1105
1154
  orm;
1106
1155
  opts;
1107
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;
1108
1163
  constructor(orm, opts = {}) {
1109
1164
  this.orm = orm;
1110
1165
  this.opts = opts;
1111
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
+ }
1112
1182
  }
1113
1183
  /**
1114
1184
  * Derive a store confined to a single tenant `scope.namespace`, sharing this store's ORM (no new
@@ -1459,6 +1529,24 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1459
1529
  }
1460
1530
  async settleCheckpointsIfPending(settlements) {
1461
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
+ }
1462
1550
  return this.fork().transactional(async (em) => {
1463
1551
  const pending = await lockPendingCheckpoints(em, settlements, [
1464
1552
  "runId",
@@ -2347,6 +2435,92 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
2347
2435
  payload
2348
2436
  };
2349
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
+ }
2350
2524
  async listBufferedSignals(tokenPrefix, afterId, limit) {
2351
2525
  const em = this.fork();
2352
2526
  const rows = await em.find(BufferedSignalEntity, {
@@ -2659,6 +2833,243 @@ function fromScheduleEntity(e) {
2659
2833
  };
2660
2834
  }
2661
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
+ /**
2870
+ * `value` as a raw-SQL parameter for `property` (its custom type's conversion, if any).
2871
+ *
2872
+ * No `mode: 'query'`: in that mode MySQL's JSON conversion returns the value unserialized, for the
2873
+ * query builder to handle, and a raw parameter that is an array is then expanded into a value list.
2874
+ */
2875
+ bind(em, entity, property, value) {
2876
+ if (value == null) return null;
2877
+ const type = this.meta(em, entity).properties[property]?.customType;
2878
+ return type ? type.convertToDatabaseValue(value, em.getPlatform()) : value;
2879
+ }
2880
+ async run(em, sql, params, method = "all") {
2881
+ const connection = em.getConnection();
2882
+ return connection.execute(sql, params, method, em.getTransactionContext());
2883
+ }
2884
+ rows(out) {
2885
+ if (Array.isArray(out)) return out;
2886
+ const rows = out?.rows;
2887
+ return Array.isArray(rows) ? rows : [];
2888
+ }
2889
+ affected(out) {
2890
+ return Number(out?.affectedRows ?? 0);
2891
+ }
2892
+ runFromRow(em, raw2) {
2893
+ return fromRunEntity(em.map(WorkflowRunEntity, raw2));
2894
+ }
2895
+ checkpointFromRow(em, raw2) {
2896
+ return fromCheckpointEntity(em.map(StepCheckpointEntity, raw2));
2897
+ }
2898
+ /** The checkpoint columns but `input`, quoted, each prefixed with `prefix`. */
2899
+ settledColumns(em, prefix) {
2900
+ return Object.keys(this.meta(em, StepCheckpointEntity).properties).filter((p) => p !== "input").map((p) => `${prefix}${this.c(em, StepCheckpointEntity, p)}`).join(", ");
2901
+ }
2902
+ async tryLockRunAndGet(runId, owner, leaseUntilMs, nowMs) {
2903
+ const em = this.fork();
2904
+ const R = WorkflowRunEntity;
2905
+ const until = this.c(em, R, "lockedUntil");
2906
+ const scope = this.scope !== void 0 ? ` AND ${this.c(em, R, "namespace")} = ?` : "";
2907
+ 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 *`, [
2908
+ owner,
2909
+ this.bind(em, R, "lockedUntil", new Date(leaseUntilMs)),
2910
+ runId,
2911
+ this.bind(em, R, "lockedUntil", new Date(nowMs)),
2912
+ ...this.scope !== void 0 ? [
2913
+ this.scope
2914
+ ] : []
2915
+ ]);
2916
+ const [row] = this.rows(out);
2917
+ return row ? this.runFromRow(em, row) : null;
2918
+ }
2919
+ async insertCheckpointIfAbsent(checkpoint) {
2920
+ try {
2921
+ await this.fork().insert(StepCheckpointEntity, toCheckpointEntity(checkpoint));
2922
+ return true;
2923
+ } catch (err) {
2924
+ if (err instanceof import_core3.UniqueConstraintViolationException) return false;
2925
+ throw err;
2926
+ }
2927
+ }
2928
+ async cpType(em, property) {
2929
+ const field = this.field(em, StepCheckpointEntity, property);
2930
+ 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()", [
2931
+ this.meta(em, StepCheckpointEntity).tableName
2932
+ ]).then((out) => new Map(this.rows(out).map((r) => [
2933
+ String(r.name),
2934
+ String(r.type)
2935
+ ])));
2936
+ return (await this.cpTypes).get(field) ?? "text";
2937
+ }
2938
+ async settleResultsIfPending(results) {
2939
+ const out = results.map(() => null);
2940
+ const seen = /* @__PURE__ */ new Set();
2941
+ const sent = results.flatMap((r, i) => {
2942
+ const key = checkpointKey(r.runId, r.seq);
2943
+ if (seen.has(key)) return [];
2944
+ seen.add(key);
2945
+ return [
2946
+ i
2947
+ ];
2948
+ });
2949
+ if (sent.length === 0) return out;
2950
+ const em = this.fork();
2951
+ const K = StepCheckpointEntity;
2952
+ const R = WorkflowRunEntity;
2953
+ const c = /* @__PURE__ */ __name((prop) => this.c(em, K, prop), "c");
2954
+ 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'))`;
2955
+ if (!this.pg && sent.length > 1) {
2956
+ return (0, import_nestjs_durable_core.settleResultsWithPlainCalls)(this.store, results);
2957
+ }
2958
+ if (!this.pg) {
2959
+ for (const i of sent) {
2960
+ const s = results[i];
2961
+ const done = s.status === "completed";
2962
+ 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}`, [
2963
+ s.status,
2964
+ ...done ? [
2965
+ this.bind(em, K, "output", s.output)
2966
+ ] : [],
2967
+ this.bind(em, K, "error", s.error),
2968
+ this.bind(em, K, "events", s.events),
2969
+ this.bind(em, K, "startedAt", s.startedAt),
2970
+ this.bind(em, K, "finishedAt", s.finishedAt),
2971
+ s.runId,
2972
+ s.seq
2973
+ ], "run"));
2974
+ if (!wrote) continue;
2975
+ const [row] = this.rows(await this.run(em, `SELECT ${this.settledColumns(em, "")} FROM ${this.table(em, K)} WHERE ${c("runId")} = ? AND ${c("seq")} = ?`, [
2976
+ s.runId,
2977
+ s.seq
2978
+ ]));
2979
+ out[i] = row ? this.checkpointFromRow(em, row) : null;
2980
+ }
2981
+ return out;
2982
+ }
2983
+ const props = [
2984
+ "runId",
2985
+ "seq",
2986
+ "status",
2987
+ "output",
2988
+ "error",
2989
+ "events",
2990
+ "startedAt",
2991
+ "finishedAt"
2992
+ ];
2993
+ const types = new Map(await Promise.all(props.map(async (p) => [
2994
+ p,
2995
+ await this.cpType(em, p)
2996
+ ])));
2997
+ for (let at = 0; at < sent.length; at += 500) {
2998
+ const chunk = sent.slice(at, at + 500);
2999
+ const params = [];
3000
+ const rows = chunk.map((i) => {
3001
+ const s = results[i];
3002
+ 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));
3003
+ return `(${props.map((p) => `?::${types.get(p)}`).join(", ")})`;
3004
+ });
3005
+ 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));
3006
+ const byKey = new Map(chunk.map((i) => {
3007
+ const s = results[i];
3008
+ return [
3009
+ checkpointKey(s.runId, s.seq),
3010
+ i
3011
+ ];
3012
+ }));
3013
+ for (const raw2 of settled) {
3014
+ const cp = this.checkpointFromRow(em, raw2);
3015
+ const i = byKey.get(checkpointKey(cp.runId, cp.seq));
3016
+ if (i !== void 0) out[i] = cp;
3017
+ }
3018
+ }
3019
+ return out;
3020
+ }
3021
+ async claimPendingRuns(limit, owner, leaseUntilMs, nowMs, namespace) {
3022
+ if (limit <= 0) return [];
3023
+ const em = this.fork();
3024
+ const R = WorkflowRunEntity;
3025
+ const c = /* @__PURE__ */ __name((prop) => this.c(em, R, prop), "c");
3026
+ const until = this.bind(em, R, "lockedUntil", new Date(leaseUntilMs));
3027
+ const now = this.bind(em, R, "lockedUntil", new Date(nowMs));
3028
+ const filters = [
3029
+ ...namespace !== void 0 ? [
3030
+ namespace
3031
+ ] : [],
3032
+ ...this.scope !== void 0 ? [
3033
+ this.scope
3034
+ ] : []
3035
+ ];
3036
+ const where = `${c("status")} = 'pending' AND (${c("lockedUntil")} IS NULL OR ${c("lockedUntil")} <= ?)${filters.map(() => ` AND ${c("namespace")} = ?`).join("")}`;
3037
+ const sort = /* @__PURE__ */ __name((runs) => runs.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime() || a.id.localeCompare(b.id)), "sort");
3038
+ if (this.pg) {
3039
+ const out = await this.run(
3040
+ em,
3041
+ `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.*`,
3042
+ // `?` placeholders bind in order: the CTE's (now, namespace filters, limit) come first.
3043
+ [
3044
+ now,
3045
+ ...filters,
3046
+ limit,
3047
+ owner,
3048
+ until
3049
+ ]
3050
+ );
3051
+ return sort(this.rows(out).map((raw2) => this.runFromRow(em, raw2)));
3052
+ }
3053
+ return em.transactional(async (tx) => {
3054
+ 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`, [
3055
+ now,
3056
+ ...filters,
3057
+ limit
3058
+ ])).map((raw2) => this.runFromRow(tx, raw2));
3059
+ if (picked.length === 0) return [];
3060
+ await this.run(tx, `UPDATE ${this.table(tx, R)} SET ${c("lockedBy")} = ?, ${c("lockedUntil")} = ? WHERE ${c("id")} IN (${picked.map(() => "?").join(", ")})`, [
3061
+ owner,
3062
+ until,
3063
+ ...picked.map((run) => run.id)
3064
+ ], "run");
3065
+ return sort(picked.map((run) => ({
3066
+ ...run,
3067
+ lockedBy: owner,
3068
+ lockedUntil: leaseUntilMs
3069
+ })));
3070
+ });
3071
+ }
3072
+ };
2662
3073
  // Annotate the CommonJS export names for ESM import in node:
2663
3074
  0 && (module.exports = {
2664
3075
  BufferedEventEntity,
@@ -2673,6 +3084,8 @@ __name(fromScheduleEntity, "fromScheduleEntity");
2673
3084
  RunAttributeRepository,
2674
3085
  ScheduleEntity,
2675
3086
  ScheduleRepository,
3087
+ SignalIdEntity,
3088
+ SignalIdRepository,
2676
3089
  SignalWaiterEntity,
2677
3090
  SignalWaiterRepository,
2678
3091
  StepCheckpointEntity,