@dudousxd/nestjs-durable-store-mikro-orm 0.35.0 → 0.36.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.d.cts CHANGED
@@ -73,6 +73,8 @@ declare class WorkflowRunEntity {
73
73
  priority?: number | null;
74
74
  namespace: string;
75
75
  origin?: string | null;
76
+ tenantId?: string | null;
77
+ tenantKind?: string | null;
76
78
  createdAt: Date;
77
79
  updatedAt: Date;
78
80
  }
@@ -153,6 +155,8 @@ declare class ScheduleEntity {
153
155
  tags?: string[] | null;
154
156
  spec: Record<string, unknown>;
155
157
  state: Record<string, unknown>;
158
+ tenantId?: string | null;
159
+ tenantKind?: string | null;
156
160
  createdAt: Date;
157
161
  updatedAt: Date;
158
162
  }
package/dist/index.d.ts CHANGED
@@ -73,6 +73,8 @@ declare class WorkflowRunEntity {
73
73
  priority?: number | null;
74
74
  namespace: string;
75
75
  origin?: string | null;
76
+ tenantId?: string | null;
77
+ tenantKind?: string | null;
76
78
  createdAt: Date;
77
79
  updatedAt: Date;
78
80
  }
@@ -153,6 +155,8 @@ declare class ScheduleEntity {
153
155
  tags?: string[] | null;
154
156
  spec: Record<string, unknown>;
155
157
  state: Record<string, unknown>;
158
+ tenantId?: string | null;
159
+ tenantKind?: string | null;
156
160
  createdAt: Date;
157
161
  updatedAt: Date;
158
162
  }
package/dist/index.js CHANGED
@@ -81,6 +81,8 @@ var WorkflowRunEntity = class {
81
81
  priority;
82
82
  namespace;
83
83
  origin;
84
+ tenantId;
85
+ tenantKind;
84
86
  createdAt;
85
87
  updatedAt;
86
88
  };
@@ -171,6 +173,8 @@ var ScheduleEntity = class {
171
173
  tags;
172
174
  spec;
173
175
  state;
176
+ tenantId;
177
+ tenantKind;
174
178
  createdAt;
175
179
  updatedAt;
176
180
  };
@@ -232,6 +236,14 @@ function durableEntities(options = {}) {
232
236
  "status",
233
237
  "createdAt"
234
238
  ]
239
+ },
240
+ // App-facing reads with tenancy on: `tenant_id = ? ORDER BY created_at DESC`.
241
+ {
242
+ name: "durable_workflow_runs_tenant_idx",
243
+ properties: [
244
+ "tenantId",
245
+ "createdAt"
246
+ ]
235
247
  }
236
248
  ],
237
249
  properties: {
@@ -341,6 +353,19 @@ function durableEntities(options = {}) {
341
353
  nullable: true,
342
354
  fieldName: col("origin")
343
355
  },
356
+ // The CUSTOMER (tenancy) the run belongs to — not the worker-pool `namespace`. Nullable, no
357
+ // default: NULL = no tenant, which is every run while tenancy is off. Added in place by the
358
+ // boot-time schema heal.
359
+ tenantId: {
360
+ type: "string",
361
+ nullable: true,
362
+ fieldName: col("tenantId")
363
+ },
364
+ tenantKind: {
365
+ type: "string",
366
+ nullable: true,
367
+ fieldName: col("tenantKind")
368
+ },
344
369
  createdAt: {
345
370
  type: "Date",
346
371
  length: DATETIME_PRECISION,
@@ -646,6 +671,12 @@ function durableEntities(options = {}) {
646
671
  "paused",
647
672
  "nextFireAt"
648
673
  ]
674
+ },
675
+ {
676
+ name: "durable_schedules_tenant_idx",
677
+ properties: [
678
+ "tenantId"
679
+ ]
649
680
  }
650
681
  ],
651
682
  properties: {
@@ -686,6 +717,17 @@ function durableEntities(options = {}) {
686
717
  type: "json",
687
718
  fieldName: col("state")
688
719
  },
720
+ // The customer the schedule (and every run it starts) belongs to. NULL = none.
721
+ tenantId: {
722
+ type: "string",
723
+ nullable: true,
724
+ fieldName: col("tenantId")
725
+ },
726
+ tenantKind: {
727
+ type: "string",
728
+ nullable: true,
729
+ fieldName: col("tenantKind")
730
+ },
689
731
  createdAt: {
690
732
  type: "Date",
691
733
  length: DATETIME_PRECISION,
@@ -1733,6 +1775,7 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1733
1775
  }
1734
1776
  });
1735
1777
  }
1778
+ and.push(...tenantWhere(query));
1736
1779
  if (query.status && query.statuses) {
1737
1780
  and.push({
1738
1781
  status: query.status
@@ -2018,6 +2061,8 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
2018
2061
  if ("tags" in patch) data.tags = patch.tags ?? null;
2019
2062
  if (patch.spec !== void 0) data.spec = patch.spec;
2020
2063
  if (patch.state !== void 0) data.state = patch.state;
2064
+ if ("tenantId" in patch) data.tenantId = patch.tenantId ?? null;
2065
+ if ("tenantKind" in patch) data.tenantKind = patch.tenantKind ?? null;
2021
2066
  if (patch.updatedAt !== void 0) data.updatedAt = patch.updatedAt;
2022
2067
  if (!Object.keys(data).length) return await this.getSchedule(id) !== null;
2023
2068
  const where = {
@@ -2038,18 +2083,17 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
2038
2083
  if (query.namespace !== void 0) where.namespace = query.namespace;
2039
2084
  if (query.workflow !== void 0) where.workflow = query.workflow;
2040
2085
  if (query.paused !== void 0) where.paused = query.paused;
2086
+ const scheduleAnd = tenantWhere(query);
2041
2087
  if (query.dueBy !== void 0) {
2042
- where.$and = [
2043
- {
2044
- paused: false
2045
- },
2046
- {
2047
- nextFireAt: {
2048
- $lte: query.dueBy
2049
- }
2088
+ scheduleAnd.push({
2089
+ paused: false
2090
+ }, {
2091
+ nextFireAt: {
2092
+ $lte: query.dueBy
2050
2093
  }
2051
- ];
2094
+ });
2052
2095
  }
2096
+ if (scheduleAnd.length) where.$and = scheduleAnd;
2053
2097
  const quote = this.idQuote(em);
2054
2098
  const meta = em.getMetadata().get(ScheduleEntity);
2055
2099
  const tagsCol = meta.properties.tags?.fieldNames?.[0] ?? "tags";
@@ -2556,6 +2600,8 @@ function runUpdateData(patch) {
2556
2600
  if ("priority" in patch) data.priority = patch.priority ?? null;
2557
2601
  if ("namespace" in patch) data.namespace = patch.namespace || "default";
2558
2602
  if ("origin" in patch) data.origin = patch.origin ?? null;
2603
+ if ("tenantId" in patch) data.tenantId = patch.tenantId ?? null;
2604
+ if ("tenantKind" in patch) data.tenantKind = patch.tenantKind ?? null;
2559
2605
  if (patch.createdAt != null) data.createdAt = patch.createdAt;
2560
2606
  if (patch.updatedAt != null) data.updatedAt = patch.updatedAt;
2561
2607
  return data;
@@ -2580,6 +2626,8 @@ function toRunEntity(run) {
2580
2626
  e.priority = run.priority ?? null;
2581
2627
  e.namespace = run.namespace || "default";
2582
2628
  e.origin = run.origin ?? null;
2629
+ e.tenantId = run.tenantId ?? null;
2630
+ e.tenantKind = run.tenantKind ?? null;
2583
2631
  e.createdAt = run.createdAt;
2584
2632
  e.updatedAt = run.updatedAt;
2585
2633
  return e;
@@ -2605,6 +2653,13 @@ function fromRunEntity(e) {
2605
2653
  namespace: e.namespace,
2606
2654
  // NULL (a row written before the column existed) surfaces as `undefined` = unknown origin.
2607
2655
  origin: e.origin ?? void 0,
2656
+ // NULL = no tenant; left off entirely so a tenancy-off run reads back unchanged.
2657
+ ...e.tenantId != null ? {
2658
+ tenantId: e.tenantId
2659
+ } : {},
2660
+ ...e.tenantKind != null ? {
2661
+ tenantKind: e.tenantKind
2662
+ } : {},
2608
2663
  createdAt: e.createdAt,
2609
2664
  updatedAt: e.updatedAt
2610
2665
  };
@@ -2753,6 +2808,33 @@ function fromCheckpointEntity(e) {
2753
2808
  };
2754
2809
  }
2755
2810
  __name(fromCheckpointEntity, "fromCheckpointEntity");
2811
+ function tenantWhere(query) {
2812
+ const out = [];
2813
+ if (query.tenantId !== void 0) out.push({
2814
+ tenantId: query.tenantId
2815
+ });
2816
+ if (query.tenantIds) {
2817
+ const named = query.tenantIds.filter((t) => t !== null);
2818
+ const branches = [];
2819
+ if (named.length) branches.push({
2820
+ tenantId: {
2821
+ $in: named
2822
+ }
2823
+ });
2824
+ if (query.tenantIds.some((t) => t === null)) branches.push({
2825
+ tenantId: null
2826
+ });
2827
+ out.push(branches.length ? {
2828
+ $or: branches
2829
+ } : {
2830
+ tenantId: {
2831
+ $in: []
2832
+ }
2833
+ });
2834
+ }
2835
+ return out;
2836
+ }
2837
+ __name(tenantWhere, "tenantWhere");
2756
2838
  function toScheduleEntity(r) {
2757
2839
  const e = new ScheduleEntity();
2758
2840
  e.id = r.id;
@@ -2763,6 +2845,8 @@ function toScheduleEntity(r) {
2763
2845
  e.tags = r.tags ?? null;
2764
2846
  e.spec = r.spec;
2765
2847
  e.state = r.state;
2848
+ e.tenantId = r.tenantId ?? null;
2849
+ e.tenantKind = r.tenantKind ?? null;
2766
2850
  e.createdAt = r.createdAt;
2767
2851
  e.updatedAt = r.updatedAt;
2768
2852
  return e;
@@ -2780,6 +2864,12 @@ function fromScheduleEntity(e) {
2780
2864
  } : {},
2781
2865
  spec: e.spec ?? {},
2782
2866
  state: e.state ?? {},
2867
+ ...e.tenantId != null ? {
2868
+ tenantId: e.tenantId
2869
+ } : {},
2870
+ ...e.tenantKind != null ? {
2871
+ tenantKind: e.tenantKind
2872
+ } : {},
2783
2873
  createdAt: e.createdAt,
2784
2874
  updatedAt: e.updatedAt
2785
2875
  };
@@ -2818,13 +2908,16 @@ var MikroOrmRoundTrips = class MikroOrmRoundTrips2 {
2818
2908
  table(em, entity) {
2819
2909
  return this.q(em, this.meta(em, entity).tableName);
2820
2910
  }
2821
- /** `value` as MikroORM binds it for `property` (its custom type's conversion, if any). */
2911
+ /**
2912
+ * `value` as a raw-SQL parameter for `property` (its custom type's conversion, if any).
2913
+ *
2914
+ * No `mode: 'query'`: in that mode MySQL's JSON conversion returns the value unserialized, for the
2915
+ * query builder to handle, and a raw parameter that is an array is then expanded into a value list.
2916
+ */
2822
2917
  bind(em, entity, property, value) {
2823
2918
  if (value == null) return null;
2824
2919
  const type = this.meta(em, entity).properties[property]?.customType;
2825
- return type ? type.convertToDatabaseValue(value, em.getPlatform(), {
2826
- mode: "query"
2827
- }) : value;
2920
+ return type ? type.convertToDatabaseValue(value, em.getPlatform()) : value;
2828
2921
  }
2829
2922
  async run(em, sql, params, method = "all") {
2830
2923
  const connection = em.getConnection();