@dudousxd/nestjs-durable-store-typeorm 0.27.0 → 0.28.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
@@ -98,6 +98,8 @@ var WorkflowRunEntity = class {
98
98
  priority;
99
99
  namespace;
100
100
  origin;
101
+ tenantId;
102
+ tenantKind;
101
103
  createdAt;
102
104
  updatedAt;
103
105
  };
@@ -181,6 +183,8 @@ var ScheduleEntity = class {
181
183
  tags;
182
184
  spec;
183
185
  state;
186
+ tenantId;
187
+ tenantKind;
184
188
  createdAt;
185
189
  updatedAt;
186
190
  };
@@ -298,6 +302,18 @@ function durableEntities(options = {}) {
298
302
  nullable: true,
299
303
  name: col("origin")
300
304
  },
305
+ // The CUSTOMER (tenancy) the run belongs to — not the worker-pool `namespace`. Nullable, no
306
+ // default: NULL = no tenant. `ensureTypeOrmDurableSchema` adds the columns in place.
307
+ tenantId: {
308
+ type: "text",
309
+ nullable: true,
310
+ name: col("tenantId")
311
+ },
312
+ tenantKind: {
313
+ type: "text",
314
+ nullable: true,
315
+ name: col("tenantKind")
316
+ },
301
317
  createdAt: {
302
318
  type: Date,
303
319
  name: col("createdAt")
@@ -599,6 +615,17 @@ function durableEntities(options = {}) {
599
615
  name: col("state"),
600
616
  transformer: jsonColumnTransformer("schedules.state")
601
617
  },
618
+ // The customer the schedule (and every run it starts) belongs to. NULL = none.
619
+ tenantId: {
620
+ type: "text",
621
+ nullable: true,
622
+ name: col("tenantId")
623
+ },
624
+ tenantKind: {
625
+ type: "text",
626
+ nullable: true,
627
+ name: col("tenantKind")
628
+ },
602
629
  createdAt: {
603
630
  type: Date,
604
631
  name: col("createdAt")
@@ -769,6 +796,7 @@ async function ensureTypeOrmDurableSchema(dataSource, options = {}) {
769
796
  ${runsCol("awaitingDecisionTaskId")} ${str},
770
797
  ${runsCol("recoveryAttempts")} ${int}, ${runsCol("tags")} ${txt}, ${runsCol("searchAttributes")} ${txt},
771
798
  ${runsCol("priority")} ${int}, ${runsCol("namespace")} ${nsCol}, ${runsCol("origin")} ${str},
799
+ ${runsCol("tenantId")} ${str}, ${runsCol("tenantKind")} ${str},
772
800
  ${runsCol("createdAt")} ${ts} NOT NULL, ${runsCol("updatedAt")} ${ts} NOT NULL
773
801
  )`
774
802
  ],
@@ -836,6 +864,7 @@ async function ensureTypeOrmDurableSchema(dataSource, options = {}) {
836
864
  ${schCol("paused")} boolean NOT NULL DEFAULT ${isPg ? "false" : "0"},
837
865
  ${schCol("nextFireAt")} bigint, ${schCol("tags")} ${txt},
838
866
  ${schCol("spec")} ${txt} NOT NULL, ${schCol("state")} ${txt} NOT NULL,
867
+ ${schCol("tenantId")} ${str}, ${schCol("tenantKind")} ${str},
839
868
  ${schCol("createdAt")} ${ts} NOT NULL, ${schCol("updatedAt")} ${ts} NOT NULL
840
869
  )`
841
870
  ]
@@ -889,6 +918,15 @@ async function ensureTypeOrmDurableSchema(dataSource, options = {}) {
889
918
  [
890
919
  "origin",
891
920
  str
921
+ ],
922
+ // The customer (tenancy). Nullable, never back-filled: a run written before it has no tenant.
923
+ [
924
+ "tenantId",
925
+ str
926
+ ],
927
+ [
928
+ "tenantKind",
929
+ str
892
930
  ]
893
931
  ],
894
932
  durable_step_checkpoints: [
@@ -928,6 +966,16 @@ async function ensureTypeOrmDurableSchema(dataSource, options = {}) {
928
966
  "parallelGroup",
929
967
  str
930
968
  ]
969
+ ],
970
+ durable_schedules: [
971
+ [
972
+ "tenantId",
973
+ str
974
+ ],
975
+ [
976
+ "tenantKind",
977
+ str
978
+ ]
931
979
  ]
932
980
  };
933
981
  const repairs = [];
@@ -1059,6 +1107,17 @@ async function ensureTypeOrmDurableSchema(dataSource, options = {}) {
1059
1107
  `${schedules} (${schCol("paused")}, ${schCol("nextFireAt")})`,
1060
1108
  "durable_schedules"
1061
1109
  ],
1110
+ // Tenancy: app-facing reads filter by the customer and page newest first.
1111
+ [
1112
+ "durable_workflow_runs_tenant_idx",
1113
+ `${runs} (${runsCol("tenantId")}, ${runsCol("createdAt")})`,
1114
+ "durable_workflow_runs"
1115
+ ],
1116
+ [
1117
+ "durable_schedules_tenant_idx",
1118
+ `${schedules} (${schCol("tenantId")})`,
1119
+ "durable_schedules"
1120
+ ],
1062
1121
  [
1063
1122
  "durable_run_attributes_num_idx",
1064
1123
  `${runAttributes} (${attrCol("key")}, ${attrCol("numValue")})`,
@@ -1750,6 +1809,7 @@ var TypeOrmStateStore = class {
1750
1809
  origins: named
1751
1810
  } : {});
1752
1811
  }
1812
+ applyTenantWhere(qb, "r", query);
1753
1813
  if (query.status) qb.andWhere("r.status = :status", {
1754
1814
  status: query.status
1755
1815
  });
@@ -1864,6 +1924,8 @@ var TypeOrmStateStore = class {
1864
1924
  if ("tags" in patch) set.tags = patch.tags ?? null;
1865
1925
  if (patch.spec !== void 0) set.spec = patch.spec;
1866
1926
  if (patch.state !== void 0) set.state = patch.state;
1927
+ if ("tenantId" in patch) set.tenantId = patch.tenantId ?? null;
1928
+ if ("tenantKind" in patch) set.tenantKind = patch.tenantKind ?? null;
1867
1929
  if (patch.updatedAt !== void 0) set.updatedAt = patch.updatedAt;
1868
1930
  if (!Object.keys(set).length) return await this.getSchedule(id) !== null;
1869
1931
  const qb = this.schedules().createQueryBuilder().update().set(set).where({
@@ -1898,6 +1960,7 @@ var TypeOrmStateStore = class {
1898
1960
  if (query.tag !== void 0) qb.andWhere("s.tags LIKE :tag", {
1899
1961
  tag: `%"${query.tag}"%`
1900
1962
  });
1963
+ applyTenantWhere(qb, "s", query);
1901
1964
  if (query.dueBy !== void 0) qb.andWhere("s.paused = :notPaused AND s.nextFireAt <= :dueBy", {
1902
1965
  notPaused: false,
1903
1966
  dueBy: query.dueBy
@@ -2354,6 +2417,8 @@ function toRunEntity(run) {
2354
2417
  namespace: run.namespace || "default",
2355
2418
  // Absent origin stays SQL NULL — "unknown", never coerced into a real-looking library name.
2356
2419
  origin: run.origin ?? null,
2420
+ tenantId: run.tenantId ?? null,
2421
+ tenantKind: run.tenantKind ?? null,
2357
2422
  createdAt: run.createdAt,
2358
2423
  updatedAt: run.updatedAt
2359
2424
  };
@@ -2382,6 +2447,13 @@ function fromRunEntity(e) {
2382
2447
  namespace: e.namespace,
2383
2448
  // NULL (a row written before the column existed) surfaces as `undefined` = unknown origin.
2384
2449
  origin: e.origin ?? void 0,
2450
+ // NULL = no tenant; left off entirely so a tenancy-off run reads back unchanged.
2451
+ ...e.tenantId != null ? {
2452
+ tenantId: e.tenantId
2453
+ } : {},
2454
+ ...e.tenantKind != null ? {
2455
+ tenantKind: e.tenantKind
2456
+ } : {},
2385
2457
  createdAt: e.createdAt,
2386
2458
  updatedAt: e.updatedAt
2387
2459
  };
@@ -2434,6 +2506,22 @@ function fromCheckpointEntity(e) {
2434
2506
  };
2435
2507
  }
2436
2508
  __name(fromCheckpointEntity, "fromCheckpointEntity");
2509
+ function applyTenantWhere(qb, alias, query) {
2510
+ if (query.tenantId === null) qb.andWhere(`${alias}.tenantId IS NULL`);
2511
+ else if (query.tenantId !== void 0) qb.andWhere(`${alias}.tenantId = :tenantId`, {
2512
+ tenantId: query.tenantId
2513
+ });
2514
+ if (query.tenantIds) {
2515
+ const named = query.tenantIds.filter((t) => t !== null);
2516
+ const branches = [];
2517
+ if (named.length) branches.push(`${alias}.tenantId IN (:...tenantIds)`);
2518
+ if (query.tenantIds.some((t) => t === null)) branches.push(`${alias}.tenantId IS NULL`);
2519
+ qb.andWhere(branches.length ? `(${branches.join(" OR ")})` : "1 = 0", named.length ? {
2520
+ tenantIds: named
2521
+ } : {});
2522
+ }
2523
+ }
2524
+ __name(applyTenantWhere, "applyTenantWhere");
2437
2525
  function toScheduleEntity(r) {
2438
2526
  const e = new ScheduleEntity();
2439
2527
  e.id = r.id;
@@ -2444,6 +2532,8 @@ function toScheduleEntity(r) {
2444
2532
  e.tags = r.tags ?? null;
2445
2533
  e.spec = r.spec;
2446
2534
  e.state = r.state;
2535
+ e.tenantId = r.tenantId ?? null;
2536
+ e.tenantKind = r.tenantKind ?? null;
2447
2537
  e.createdAt = r.createdAt;
2448
2538
  e.updatedAt = r.updatedAt;
2449
2539
  return e;
@@ -2462,6 +2552,12 @@ function fromScheduleEntity(e) {
2462
2552
  } : {},
2463
2553
  spec: e.spec ?? {},
2464
2554
  state: e.state ?? {},
2555
+ ...e.tenantId != null ? {
2556
+ tenantId: e.tenantId
2557
+ } : {},
2558
+ ...e.tenantKind != null ? {
2559
+ tenantKind: e.tenantKind
2560
+ } : {},
2465
2561
  createdAt: e.createdAt,
2466
2562
  updatedAt: e.updatedAt
2467
2563
  };
@@ -2485,6 +2581,8 @@ function runUpdateSet(patch) {
2485
2581
  if ("priority" in patch) update.priority = patch.priority ?? null;
2486
2582
  if ("namespace" in patch) update.namespace = patch.namespace || "default";
2487
2583
  if ("origin" in patch) update.origin = patch.origin ?? null;
2584
+ if ("tenantId" in patch) update.tenantId = patch.tenantId ?? null;
2585
+ if ("tenantKind" in patch) update.tenantKind = patch.tenantKind ?? null;
2488
2586
  if ("createdAt" in patch) update.createdAt = patch.createdAt;
2489
2587
  if ("updatedAt" in patch) update.updatedAt = patch.updatedAt;
2490
2588
  return update;