@dudousxd/nestjs-durable-store-drizzle 0.25.0 → 0.27.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.js CHANGED
@@ -73,9 +73,23 @@ var workflowRuns = sqliteTable("durable_workflow_runs", {
73
73
  // declared here, so until that runs, EVERY run query fails with "no such column: origin" — the same
74
74
  // step `priority` needed before it.
75
75
  origin: text("origin"),
76
+ // The CUSTOMER (organization, clinic…) the run belongs to — tenancy, not the worker-pool
77
+ // `namespace`. Nullable: NULL = no tenant, which is every run while tenancy is off.
78
+ //
79
+ // MIGRATION (you own this adapter's migrations; until it runs, every run query fails with
80
+ // "no such column: tenant_id"):
81
+ //
82
+ // ALTER TABLE durable_workflow_runs ADD COLUMN tenant_id TEXT;
83
+ // ALTER TABLE durable_workflow_runs ADD COLUMN tenant_kind TEXT;
84
+ // CREATE INDEX IF NOT EXISTS durable_workflow_runs_tenant_idx
85
+ // ON durable_workflow_runs (tenant_id, created_at);
86
+ tenantId: text("tenant_id"),
87
+ tenantKind: text("tenant_kind"),
76
88
  createdAt: integer("created_at").notNull(),
77
89
  updatedAt: integer("updated_at").notNull()
78
90
  }, (t) => [
91
+ // App-facing reads with tenancy on filter by tenant and page newest first.
92
+ index("durable_workflow_runs_tenant_idx").on(t.tenantId, t.createdAt),
79
93
  // `namespace` sits on EVERY poll tick's predicate — the pending (FIFO by `created_at`),
80
94
  // incomplete and due-timer scans all filter by it and then by `status` — unlike `origin`, which
81
95
  // only the dashboard's listing touches. Mirrors the MikroORM adapter's
@@ -174,6 +188,20 @@ var bufferedEvents = sqliteTable("durable_buffered_events", {
174
188
  }),
175
189
  publishedAt: integer("published_at").notNull()
176
190
  });
191
+ var signalIds = sqliteTable("durable_signal_ids", {
192
+ token: text("token").notNull(),
193
+ signalId: text("signal_id").notNull(),
194
+ expiresAt: integer("expires_at").notNull()
195
+ }, (t) => [
196
+ primaryKey({
197
+ columns: [
198
+ t.token,
199
+ t.signalId
200
+ ]
201
+ }),
202
+ // The sweep's "expired by T" scan.
203
+ index("durable_signal_ids_expires_at_idx").on(t.expiresAt)
204
+ ]);
177
205
  var durableSchedules = sqliteTable(
178
206
  "durable_schedules",
179
207
  {
@@ -193,12 +221,19 @@ var durableSchedules = sqliteTable(
193
221
  state: text("state", {
194
222
  mode: "json"
195
223
  }).$type().notNull(),
224
+ // The customer the schedule (and every run it starts) belongs to. MIGRATION:
225
+ // ALTER TABLE durable_schedules ADD COLUMN tenant_id TEXT;
226
+ // ALTER TABLE durable_schedules ADD COLUMN tenant_kind TEXT;
227
+ // CREATE INDEX IF NOT EXISTS durable_schedules_tenant_idx ON durable_schedules (tenant_id);
228
+ tenantId: text("tenant_id"),
229
+ tenantKind: text("tenant_kind"),
196
230
  createdAt: integer("created_at").notNull(),
197
231
  updatedAt: integer("updated_at").notNull()
198
232
  },
199
233
  // The poll's "due by T" scan: `paused = 0 AND next_fire_at <= T ORDER BY next_fire_at`.
200
234
  (t) => [
201
- index("durable_schedules_due_idx").on(t.paused, t.nextFireAt)
235
+ index("durable_schedules_due_idx").on(t.paused, t.nextFireAt),
236
+ index("durable_schedules_tenant_idx").on(t.tenantId)
202
237
  ]
203
238
  );
204
239
  var durableSchema = {
@@ -208,6 +243,7 @@ var durableSchema = {
208
243
  signalWaiters,
209
244
  bufferedSignals,
210
245
  bufferedEvents,
246
+ signalIds,
211
247
  durableSchedules
212
248
  };
213
249
  function durableManagedTables() {
@@ -215,6 +251,74 @@ function durableManagedTables() {
215
251
  }
216
252
  __name(durableManagedTables, "durableManagedTables");
217
253
 
254
+ // src/tenant-columns.ts
255
+ var PG_STATEMENTS = [
256
+ 'ALTER TABLE "durable_workflow_runs" ADD COLUMN IF NOT EXISTS "tenant_id" text;',
257
+ 'ALTER TABLE "durable_workflow_runs" ADD COLUMN IF NOT EXISTS "tenant_kind" text;',
258
+ 'ALTER TABLE "durable_schedules" ADD COLUMN IF NOT EXISTS "tenant_id" text;',
259
+ 'ALTER TABLE "durable_schedules" ADD COLUMN IF NOT EXISTS "tenant_kind" text;',
260
+ 'CREATE INDEX IF NOT EXISTS "durable_workflow_runs_tenant_idx" ON "durable_workflow_runs" USING btree ("tenant_id","created_at");',
261
+ 'CREATE INDEX IF NOT EXISTS "durable_schedules_tenant_idx" ON "durable_schedules" USING btree ("tenant_id");'
262
+ ];
263
+ var SQLITE_STATEMENTS = [
264
+ "ALTER TABLE durable_workflow_runs ADD COLUMN tenant_id TEXT;",
265
+ "ALTER TABLE durable_workflow_runs ADD COLUMN tenant_kind TEXT;",
266
+ "ALTER TABLE durable_schedules ADD COLUMN tenant_id TEXT;",
267
+ "ALTER TABLE durable_schedules ADD COLUMN tenant_kind TEXT;",
268
+ "CREATE INDEX IF NOT EXISTS durable_workflow_runs_tenant_idx ON durable_workflow_runs (tenant_id, created_at);",
269
+ "CREATE INDEX IF NOT EXISTS durable_schedules_tenant_idx ON durable_schedules (tenant_id);"
270
+ ];
271
+ var TENANT_COLUMN = /tenant_(id|kind)/;
272
+ function isMissingTenantColumn(err) {
273
+ let cur = err;
274
+ for (let depth = 0; cur && depth < 5; depth += 1) {
275
+ const e = cur;
276
+ const message = typeof e.message === "string" ? e.message : "";
277
+ if (TENANT_COLUMN.test(message)) {
278
+ if (e.code === "42703" || /does not exist/.test(message) || /no such column/i.test(message)) {
279
+ return true;
280
+ }
281
+ }
282
+ cur = e.cause;
283
+ }
284
+ return false;
285
+ }
286
+ __name(isMissingTenantColumn, "isMissingTenantColumn");
287
+ var MissingTenantColumnsError = class extends Error {
288
+ static {
289
+ __name(this, "MissingTenantColumnsError");
290
+ }
291
+ constructor(dialect, cause) {
292
+ const statements = dialect === "pg" ? PG_STATEMENTS : SQLITE_STATEMENTS;
293
+ const fix = dialect === "pg" ? "Generate a migration with drizzle-kit (`drizzle-kit generate`) from the store schema and apply it, or turn on the module's `autoSchema`, which adds them on boot." : "Generate a migration with drizzle-kit (`drizzle-kit generate`) from the store schema and apply it.";
294
+ super([
295
+ "[nestjs-durable] The durable tables are missing the tenancy columns (tenant_id, tenant_kind).",
296
+ "This version of the Drizzle store reads them on every run and schedule query, even with tenancy off.",
297
+ fix,
298
+ "The migration must contain:",
299
+ ...statements.map((s) => ` ${s}`)
300
+ ].join("\n"), {
301
+ cause
302
+ });
303
+ this.name = "MissingTenantColumnsError";
304
+ }
305
+ };
306
+ function tenantColumnsHint(dialect) {
307
+ return (err) => {
308
+ if (isMissingTenantColumn(err)) throw new MissingTenantColumnsError(dialect, err);
309
+ throw err;
310
+ };
311
+ }
312
+ __name(tenantColumnsHint, "tenantColumnsHint");
313
+ async function withTenantColumnsHint(dialect, query) {
314
+ try {
315
+ return await query;
316
+ } catch (err) {
317
+ return tenantColumnsHint(dialect)(err);
318
+ }
319
+ }
320
+ __name(withTenantColumnsHint, "withTenantColumnsHint");
321
+
218
322
  // src/drizzle-state-store.ts
219
323
  var RETENTION_EXCLUDED = [
220
324
  LIFECYCLE_REGISTRY_WORKFLOW
@@ -224,6 +328,14 @@ function anyOf(conditions) {
224
328
  return present.length ? or(...present) : sql`1 = 0`;
225
329
  }
226
330
  __name(anyOf, "anyOf");
331
+ function tenantFilters(column, query) {
332
+ const one = /* @__PURE__ */ __name((id) => id === null ? isNull(column) : eq(column, id), "one");
333
+ return [
334
+ query.tenantId !== void 0 ? one(query.tenantId) : void 0,
335
+ query.tenantIds ? anyOf(query.tenantIds.map(one)) : void 0
336
+ ];
337
+ }
338
+ __name(tenantFilters, "tenantFilters");
227
339
  function namespaceFilter(namespace) {
228
340
  if (namespace === void 0) return void 0;
229
341
  if (namespace === DEFAULT_NAMESPACE || namespace === "") return or(eq(workflowRuns.namespace, DEFAULT_NAMESPACE), eq(workflowRuns.namespace, ""), isNull(workflowRuns.namespace));
@@ -537,15 +649,15 @@ var DrizzleStateStore = class {
537
649
  /** Recovery on boot. Namespace-scoped: a worker re-drives only its own tenant's crashed runs.
538
650
  * `and(...)` drops an `undefined` predicate, so an unscoped caller recovers every namespace. */
539
651
  async listIncompleteRuns(namespace) {
540
- const rows = await this.db.select().from(workflowRuns).where(and(inArray(workflowRuns.status, [
652
+ const rows = await withTenantColumnsHint("sqlite", this.db.select().from(workflowRuns).where(and(inArray(workflowRuns.status, [
541
653
  "running",
542
654
  "cancelling"
543
- ]), namespaceFilter(namespace)));
655
+ ]), namespaceFilter(namespace))));
544
656
  return rows.map(fromRunRow);
545
657
  }
546
658
  async listOrphanedRuns(nowMs, limit, namespace, ownedBy) {
547
659
  if (limit <= 0) return [];
548
- const rows = await this.db.select().from(workflowRuns).where(and(inArray(workflowRuns.status, [
660
+ const rows = await withTenantColumnsHint("sqlite", this.db.select().from(workflowRuns).where(and(inArray(workflowRuns.status, [
549
661
  "running",
550
662
  "cancelling"
551
663
  ]), or(
@@ -553,19 +665,19 @@ var DrizzleStateStore = class {
553
665
  lte(workflowRuns.lockedUntil, nowMs),
554
666
  // A live lease left by a previous process with the caller's own instance id.
555
667
  ownedBy ? and(eq(workflowRuns.lockedBy, ownedBy.owner), lt(workflowRuns.lockedUntil, ownedBy.lockedBefore)) : void 0
556
- ), namespaceFilter(namespace))).orderBy(asc(workflowRuns.createdAt)).limit(limit);
668
+ ), namespaceFilter(namespace))).orderBy(asc(workflowRuns.createdAt)).limit(limit));
557
669
  return rows.map(fromRunRow);
558
670
  }
559
671
  /** Dispatch pick-up. Namespace-scoped — without it a worker serving one tenant drains another
560
672
  * tenant's queue, and the `limit` makes it worse: foreign runs consume the FIFO budget. */
561
673
  async listPendingRuns(limit, namespace) {
562
- const rows = await this.db.select().from(workflowRuns).where(and(eq(workflowRuns.status, "pending"), namespaceFilter(namespace))).orderBy(asc(workflowRuns.createdAt)).limit(limit);
674
+ const rows = await withTenantColumnsHint("sqlite", this.db.select().from(workflowRuns).where(and(eq(workflowRuns.status, "pending"), namespaceFilter(namespace))).orderBy(asc(workflowRuns.createdAt)).limit(limit));
563
675
  return rows.map(fromRunRow);
564
676
  }
565
677
  /** Due durable timers. Namespace-scoped: a worker resumes only its own tenant's suspended runs. */
566
678
  async listDueTimers(nowMs, namespace, limit) {
567
679
  const query = this.db.select().from(workflowRuns).where(and(eq(workflowRuns.status, "suspended"), isNotNull(workflowRuns.wakeAt), lte(workflowRuns.wakeAt, nowMs), namespaceFilter(namespace))).orderBy(asc(workflowRuns.wakeAt));
568
- const rows = limit !== void 0 ? await query.limit(limit) : await query;
680
+ const rows = await withTenantColumnsHint("sqlite", limit !== void 0 ? query.limit(limit) : query);
569
681
  return rows.map(fromRunRow);
570
682
  }
571
683
  async tryLockRun(runId, owner, leaseUntilMs, nowMs) {
@@ -615,6 +727,8 @@ var DrizzleStateStore = class {
615
727
  // A set of origins may include `null` (the unattributed bucket), which `IN` cannot carry —
616
728
  // `IN (NULL)` is never true — so it becomes its own ORed `IS NULL` branch.
617
729
  query.origins ? anyOf(query.origins.map((o) => o === null ? isNull(workflowRuns.origin) : eq(workflowRuns.origin, o))) : void 0,
730
+ // The customer (tenancy matching rule): `null` = runs with no tenant.
731
+ ...tenantFilters(workflowRuns.tenantId, query),
618
732
  query.status ? eq(workflowRuns.status, query.status) : void 0,
619
733
  // `status IN (...)`; an empty set matches nothing (mirrors the in-memory store).
620
734
  query.statuses ? query.statuses.length ? inArray(workflowRuns.status, query.statuses) : sql`1 = 0` : void 0,
@@ -715,7 +829,7 @@ var DrizzleStateStore = class {
715
829
  return rows.length > 0;
716
830
  }
717
831
  async listSchedules(query) {
718
- const rows = await this.db.select().from(durableSchedules).where(and(query.namespace !== void 0 ? eq(durableSchedules.namespace, query.namespace) : void 0, query.workflow !== void 0 ? eq(durableSchedules.workflow, query.workflow) : void 0, query.paused !== void 0 ? eq(durableSchedules.paused, query.paused) : void 0, query.tag !== void 0 ? like(durableSchedules.tags, `%"${query.tag}"%`) : void 0, query.dueBy !== void 0 ? and(eq(durableSchedules.paused, false), lte(durableSchedules.nextFireAt, query.dueBy)) : void 0)).orderBy(sql`${durableSchedules.nextFireAt} IS NULL`, asc(durableSchedules.nextFireAt), asc(durableSchedules.id)).limit(query.limit ?? -1).offset(query.offset ?? 0);
832
+ const rows = await withTenantColumnsHint("sqlite", this.db.select().from(durableSchedules).where(and(query.namespace !== void 0 ? eq(durableSchedules.namespace, query.namespace) : void 0, query.workflow !== void 0 ? eq(durableSchedules.workflow, query.workflow) : void 0, query.paused !== void 0 ? eq(durableSchedules.paused, query.paused) : void 0, query.tag !== void 0 ? like(durableSchedules.tags, `%"${query.tag}"%`) : void 0, ...tenantFilters(durableSchedules.tenantId, query), query.dueBy !== void 0 ? and(eq(durableSchedules.paused, false), lte(durableSchedules.nextFireAt, query.dueBy)) : void 0)).orderBy(sql`${durableSchedules.nextFireAt} IS NULL`, asc(durableSchedules.nextFireAt), asc(durableSchedules.id)).limit(query.limit ?? -1).offset(query.offset ?? 0));
719
833
  return rows.map(fromScheduleRow);
720
834
  }
721
835
  async listCheckpoints(runId) {
@@ -943,6 +1057,51 @@ var DrizzleStateStore = class {
943
1057
  payload: row.payload ?? void 0
944
1058
  };
945
1059
  }
1060
+ async takeOldestBufferedSignal(tokens) {
1061
+ if (tokens.length === 0) return null;
1062
+ const oldest = this.db.select({
1063
+ id: bufferedSignals.id
1064
+ }).from(bufferedSignals).where(inArray(bufferedSignals.token, [
1065
+ ...new Set(tokens)
1066
+ ])).orderBy(asc(bufferedSignals.id)).limit(1);
1067
+ const rows = await this.db.delete(bufferedSignals).where(inArray(bufferedSignals.id, oldest)).returning({
1068
+ token: bufferedSignals.token,
1069
+ payload: bufferedSignals.payload
1070
+ });
1071
+ const row = rows[0];
1072
+ return row ? {
1073
+ token: row.token,
1074
+ payload: row.payload ?? void 0
1075
+ } : null;
1076
+ }
1077
+ async claimSignalId(token, id, expiresAt, nowMs) {
1078
+ const rows = await this.db.insert(signalIds).values({
1079
+ token,
1080
+ signalId: id,
1081
+ expiresAt
1082
+ }).onConflictDoUpdate({
1083
+ target: [
1084
+ signalIds.token,
1085
+ signalIds.signalId
1086
+ ],
1087
+ set: {
1088
+ expiresAt
1089
+ },
1090
+ setWhere: lte(signalIds.expiresAt, nowMs)
1091
+ }).returning({
1092
+ token: signalIds.token
1093
+ });
1094
+ return rows.length > 0;
1095
+ }
1096
+ async releaseSignalId(token, id) {
1097
+ await this.db.delete(signalIds).where(and(eq(signalIds.token, token), eq(signalIds.signalId, id)));
1098
+ }
1099
+ async deleteExpiredSignalIds(nowMs, limit) {
1100
+ const rows = await this.db.delete(signalIds).where(sql`(${signalIds.token}, ${signalIds.signalId}) in (select ${signalIds.token}, ${signalIds.signalId} from ${signalIds} where ${signalIds.expiresAt} <= ${nowMs} limit ${limit})`).returning({
1101
+ token: signalIds.token
1102
+ });
1103
+ return rows.length;
1104
+ }
946
1105
  async listBufferedSignals(tokenPrefix, afterId, limit) {
947
1106
  const rows = await this.db.select().from(bufferedSignals).where(and(like(bufferedSignals.token, `${tokenPrefix}%`), afterId !== void 0 ? gt(bufferedSignals.id, Number(afterId)) : void 0)).orderBy(asc(bufferedSignals.id)).limit(limit);
948
1107
  return rows.filter((r) => r.token.startsWith(tokenPrefix)).map((r) => ({
@@ -999,6 +1158,8 @@ function toRunRow(run) {
999
1158
  namespace: run.namespace || DEFAULT_NAMESPACE,
1000
1159
  // Absent origin stays SQL NULL — "unknown", never coerced into a real-looking library name.
1001
1160
  origin: run.origin ?? null,
1161
+ tenantId: run.tenantId ?? null,
1162
+ tenantKind: run.tenantKind ?? null,
1002
1163
  createdAt: run.createdAt.getTime(),
1003
1164
  updatedAt: run.updatedAt.getTime()
1004
1165
  };
@@ -1022,6 +1183,8 @@ function toRunPatch(patch) {
1022
1183
  if ("priority" in patch) row.priority = patch.priority ?? null;
1023
1184
  if ("namespace" in patch) row.namespace = patch.namespace || DEFAULT_NAMESPACE;
1024
1185
  if ("origin" in patch) row.origin = patch.origin ?? null;
1186
+ if ("tenantId" in patch) row.tenantId = patch.tenantId ?? null;
1187
+ if ("tenantKind" in patch) row.tenantKind = patch.tenantKind ?? null;
1025
1188
  if (patch.createdAt != null) row.createdAt = patch.createdAt.getTime();
1026
1189
  if (patch.updatedAt != null) row.updatedAt = patch.updatedAt.getTime();
1027
1190
  return row;
@@ -1051,6 +1214,13 @@ function fromRunRow(row) {
1051
1214
  namespace: row.namespace || DEFAULT_NAMESPACE,
1052
1215
  // NULL (a row written before the column existed) surfaces as `undefined` = unknown origin.
1053
1216
  origin: row.origin ?? void 0,
1217
+ // NULL = no tenant; left off the object entirely so a tenancy-off run reads back unchanged.
1218
+ ...row.tenantId != null ? {
1219
+ tenantId: row.tenantId
1220
+ } : {},
1221
+ ...row.tenantKind != null ? {
1222
+ tenantKind: row.tenantKind
1223
+ } : {},
1054
1224
  createdAt: new Date(row.createdAt),
1055
1225
  updatedAt: new Date(row.updatedAt)
1056
1226
  };
@@ -1110,6 +1280,8 @@ function toScheduleRow(r) {
1110
1280
  tags: r.tags ?? null,
1111
1281
  spec: r.spec,
1112
1282
  state: r.state,
1283
+ tenantId: r.tenantId ?? null,
1284
+ tenantKind: r.tenantKind ?? null,
1113
1285
  createdAt: r.createdAt.getTime(),
1114
1286
  updatedAt: r.updatedAt.getTime()
1115
1287
  };
@@ -1124,6 +1296,8 @@ function toSchedulePatch(patch) {
1124
1296
  if ("tags" in patch) set.tags = patch.tags ?? null;
1125
1297
  if ("spec" in patch && patch.spec !== void 0) set.spec = patch.spec;
1126
1298
  if ("state" in patch && patch.state !== void 0) set.state = patch.state;
1299
+ if ("tenantId" in patch) set.tenantId = patch.tenantId ?? null;
1300
+ if ("tenantKind" in patch) set.tenantKind = patch.tenantKind ?? null;
1127
1301
  if ("updatedAt" in patch && patch.updatedAt !== void 0) set.updatedAt = patch.updatedAt.getTime();
1128
1302
  return set;
1129
1303
  }
@@ -1140,6 +1314,12 @@ function fromScheduleRow(row) {
1140
1314
  } : {},
1141
1315
  spec: row.spec ?? {},
1142
1316
  state: row.state ?? {},
1317
+ ...row.tenantId != null ? {
1318
+ tenantId: row.tenantId
1319
+ } : {},
1320
+ ...row.tenantKind != null ? {
1321
+ tenantKind: row.tenantKind
1322
+ } : {},
1143
1323
  createdAt: new Date(row.createdAt),
1144
1324
  updatedAt: new Date(row.updatedAt)
1145
1325
  };
@@ -1216,6 +1396,7 @@ export {
1216
1396
  durableSchedules,
1217
1397
  durableSchema,
1218
1398
  runAttributes,
1399
+ signalIds,
1219
1400
  signalWaiters,
1220
1401
  stepCheckpoints,
1221
1402
  workflowRuns