@dudousxd/nestjs-durable-store-mikro-orm 0.30.0 → 0.32.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
@@ -980,6 +980,7 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
980
980
  }, {
981
981
  status: "suspended",
982
982
  wakeAt: new Date(patch.wakeAt),
983
+ tags: patch.tags,
983
984
  updatedAt: patch.updatedAt
984
985
  });
985
986
  return false;
@@ -1011,6 +1012,30 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1011
1012
  await em.flush();
1012
1013
  if ("searchAttributes" in patch) await this.reindexAttributes(runId, patch.searchAttributes);
1013
1014
  }
1015
+ async updateRunIf(runId, expectedStatuses, inputPatch) {
1016
+ if (expectedStatuses.length === 0) return false;
1017
+ const patch = inputPatch.status && import_nestjs_durable_core.TERMINAL_RUN_STATUSES.includes(inputPatch.status) ? await (0, import_nestjs_durable_core.clearTerminalSingletonAdmission)(runId, inputPatch, (id) => this.withScope({}).getRun(id)) : inputPatch;
1018
+ const data = runUpdateData(patch);
1019
+ const em = this.fork();
1020
+ let applied;
1021
+ if (Object.keys(data).length) {
1022
+ applied = await em.nativeUpdate(WorkflowRunEntity, {
1023
+ id: runId,
1024
+ status: {
1025
+ $in: expectedStatuses
1026
+ }
1027
+ }, data) > 0;
1028
+ } else {
1029
+ applied = await em.count(WorkflowRunEntity, {
1030
+ id: runId,
1031
+ status: {
1032
+ $in: expectedStatuses
1033
+ }
1034
+ }) > 0;
1035
+ }
1036
+ if (applied && "searchAttributes" in patch) await this.reindexAttributes(runId, patch.searchAttributes);
1037
+ return applied;
1038
+ }
1014
1039
  /** Rewrite a run's normalized attribute rows: delete the old set, insert the current one. Mirrors
1015
1040
  * the in-memory store's reindex so the side-table always reflects the run's live searchAttributes. */
1016
1041
  async reindexAttributes(runId, attributes, forked) {
@@ -1175,6 +1200,46 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1175
1200
  });
1176
1201
  return rows.map(fromRunEntity);
1177
1202
  }
1203
+ async listOrphanedRuns(nowMs, limit, namespace, ownedBy) {
1204
+ if (limit <= 0) return [];
1205
+ const em = this.fork();
1206
+ const rows = await em.find(WorkflowRunEntity, {
1207
+ status: {
1208
+ $in: [
1209
+ "running",
1210
+ "cancelling"
1211
+ ]
1212
+ },
1213
+ $or: [
1214
+ {
1215
+ lockedUntil: null
1216
+ },
1217
+ {
1218
+ lockedUntil: {
1219
+ $lte: new Date(nowMs)
1220
+ }
1221
+ },
1222
+ // A live lease left by a previous process with the caller's own instance id.
1223
+ ...ownedBy ? [
1224
+ {
1225
+ lockedBy: ownedBy.owner,
1226
+ lockedUntil: {
1227
+ $lt: new Date(ownedBy.lockedBefore)
1228
+ }
1229
+ }
1230
+ ] : []
1231
+ ],
1232
+ ...namespace !== void 0 ? {
1233
+ namespace
1234
+ } : {}
1235
+ }, {
1236
+ orderBy: {
1237
+ createdAt: "ASC"
1238
+ },
1239
+ limit
1240
+ });
1241
+ return rows.map(fromRunEntity);
1242
+ }
1178
1243
  async listPendingRuns(limit, namespace) {
1179
1244
  const em = this.fork();
1180
1245
  const rows = await em.find(WorkflowRunEntity, {
@@ -1566,7 +1631,7 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1566
1631
  * single statement. */
1567
1632
  async updateSchedule(id, patch, expectedNextFireAt) {
1568
1633
  const data = {};
1569
- if (patch.namespace !== void 0) data.namespace = patch.namespace;
1634
+ if (patch.namespace !== void 0) data.namespace = patch.namespace || "default";
1570
1635
  if (patch.workflow !== void 0) data.workflow = patch.workflow;
1571
1636
  if (patch.paused !== void 0) data.paused = patch.paused;
1572
1637
  if ("nextFireAt" in patch) data.nextFireAt = patch.nextFireAt ?? null;
@@ -1936,6 +2001,29 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1936
2001
  return affected > 0;
1937
2002
  }
1938
2003
  };
2004
+ function runUpdateData(patch) {
2005
+ const data = {};
2006
+ if ("workflow" in patch) data.workflow = patch.workflow;
2007
+ if ("workflowVersion" in patch) data.workflowVersion = patch.workflowVersion;
2008
+ if ("status" in patch) data.status = patch.status;
2009
+ if ("input" in patch) data.input = patch.input ?? null;
2010
+ if ("output" in patch) data.output = patch.output ?? null;
2011
+ if ("error" in patch) data.error = patch.error ?? null;
2012
+ if ("wakeAt" in patch) data.wakeAt = patch.wakeAt == null ? null : new Date(patch.wakeAt);
2013
+ if ("lockedBy" in patch) data.lockedBy = patch.lockedBy ?? null;
2014
+ if ("lockedUntil" in patch) data.lockedUntil = patch.lockedUntil == null ? null : new Date(patch.lockedUntil);
2015
+ if ("awaitingDecisionTaskId" in patch) data.awaitingDecisionTaskId = patch.awaitingDecisionTaskId ?? null;
2016
+ if ("recoveryAttempts" in patch) data.recoveryAttempts = patch.recoveryAttempts ?? null;
2017
+ if ("tags" in patch) data.tags = patch.tags ?? null;
2018
+ if ("searchAttributes" in patch) data.searchAttributes = patch.searchAttributes ?? null;
2019
+ if ("priority" in patch) data.priority = patch.priority ?? null;
2020
+ if ("namespace" in patch) data.namespace = patch.namespace || "default";
2021
+ if ("origin" in patch) data.origin = patch.origin ?? null;
2022
+ if (patch.createdAt != null) data.createdAt = patch.createdAt;
2023
+ if (patch.updatedAt != null) data.updatedAt = patch.updatedAt;
2024
+ return data;
2025
+ }
2026
+ __name(runUpdateData, "runUpdateData");
1939
2027
  function toRunEntity(run) {
1940
2028
  const e = new WorkflowRunEntity();
1941
2029
  e.id = run.id;
@@ -1953,7 +2041,7 @@ function toRunEntity(run) {
1953
2041
  e.tags = run.tags ?? null;
1954
2042
  e.searchAttributes = run.searchAttributes ?? null;
1955
2043
  e.priority = run.priority ?? null;
1956
- e.namespace = run.namespace ?? "default";
2044
+ e.namespace = run.namespace || "default";
1957
2045
  e.origin = run.origin ?? null;
1958
2046
  e.createdAt = run.createdAt;
1959
2047
  e.updatedAt = run.updatedAt;