@dudousxd/nestjs-durable-store-typeorm 0.23.0 → 0.24.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
@@ -998,6 +998,7 @@ var TypeOrmStateStore = class {
998
998
  }, {
999
999
  status: "suspended",
1000
1000
  wakeAt: new Date(patch.wakeAt),
1001
+ tags: patch.tags,
1001
1002
  updatedAt: patch.updatedAt
1002
1003
  });
1003
1004
  return false;
@@ -1029,11 +1030,13 @@ var TypeOrmStateStore = class {
1029
1030
  async updateRunIf(runId, expectedStatuses, inputPatch) {
1030
1031
  if (expectedStatuses.length === 0) return false;
1031
1032
  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.getRun(id)) : inputPatch;
1032
- const result = await this.runs().createQueryBuilder().update().set(runUpdateSet(patch)).where({
1033
+ const set = runUpdateSet(patch);
1034
+ const where = {
1033
1035
  id: runId,
1034
1036
  status: (0, import_typeorm2.In)(expectedStatuses)
1035
- }).execute();
1036
- if (!result.affected) return false;
1037
+ };
1038
+ const matched = Object.keys(set).length ? (await this.runs().createQueryBuilder().update().set(set).where(where).execute()).affected ?? 0 : await this.runs().countBy(where);
1039
+ if (!matched) return false;
1037
1040
  if ("searchAttributes" in patch) await this.reindexAttributes(runId, patch.searchAttributes);
1038
1041
  return true;
1039
1042
  }
@@ -1156,7 +1159,7 @@ var TypeOrmStateStore = class {
1156
1159
  });
1157
1160
  return rows.map(fromRunEntity);
1158
1161
  }
1159
- async listOrphanedRuns(nowMs, limit, namespace) {
1162
+ async listOrphanedRuns(nowMs, limit, namespace, ownedBy) {
1160
1163
  if (limit <= 0) return [];
1161
1164
  const now = new Date(nowMs);
1162
1165
  const rows = await this.runs().find({
@@ -1180,7 +1183,21 @@ var TypeOrmStateStore = class {
1180
1183
  ...namespace !== void 0 ? {
1181
1184
  namespace
1182
1185
  } : {}
1183
- }
1186
+ },
1187
+ // A live lease left by a previous process with the caller's own instance id.
1188
+ ...ownedBy ? [
1189
+ {
1190
+ status: (0, import_typeorm2.In)([
1191
+ "running",
1192
+ "cancelling"
1193
+ ]),
1194
+ lockedBy: ownedBy.owner,
1195
+ lockedUntil: (0, import_typeorm2.LessThan)(new Date(ownedBy.lockedBefore)),
1196
+ ...namespace !== void 0 ? {
1197
+ namespace
1198
+ } : {}
1199
+ }
1200
+ ] : []
1184
1201
  ],
1185
1202
  order: {
1186
1203
  createdAt: "ASC"
@@ -1737,7 +1754,7 @@ function toRunEntity(run) {
1737
1754
  // An absent namespace is persisted as `'default'` (what the core `WorkflowRun.namespace` docblock
1738
1755
  // promises), so the column is always a value a worker can match on — the opposite of `origin`
1739
1756
  // below, where NULL is the only honest answer.
1740
- namespace: run.namespace ?? "default",
1757
+ namespace: run.namespace || "default",
1741
1758
  // Absent origin stays SQL NULL — "unknown", never coerced into a real-looking library name.
1742
1759
  origin: run.origin ?? null,
1743
1760
  createdAt: run.createdAt,
@@ -1869,7 +1886,7 @@ function runUpdateSet(patch) {
1869
1886
  if ("tags" in patch) update.tags = patch.tags ?? null;
1870
1887
  if ("searchAttributes" in patch) update.searchAttributes = patch.searchAttributes ?? null;
1871
1888
  if ("priority" in patch) update.priority = patch.priority ?? null;
1872
- if ("namespace" in patch) update.namespace = patch.namespace ?? "default";
1889
+ if ("namespace" in patch) update.namespace = patch.namespace || "default";
1873
1890
  if ("origin" in patch) update.origin = patch.origin ?? null;
1874
1891
  if ("createdAt" in patch) update.createdAt = patch.createdAt;
1875
1892
  if ("updatedAt" in patch) update.updatedAt = patch.updatedAt;