@dudousxd/nestjs-durable-store-drizzle 0.26.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.d.cts CHANGED
@@ -436,6 +436,44 @@ declare const workflowRuns: drizzle_orm_sqlite_core.SQLiteTableWithColumns<{
436
436
  }, {}, {
437
437
  length: number | undefined;
438
438
  }>;
439
+ tenantId: drizzle_orm_sqlite_core.SQLiteColumn<{
440
+ name: "tenant_id";
441
+ tableName: "durable_workflow_runs";
442
+ dataType: "string";
443
+ columnType: "SQLiteText";
444
+ data: string;
445
+ driverParam: string;
446
+ notNull: false;
447
+ hasDefault: false;
448
+ isPrimaryKey: false;
449
+ isAutoincrement: false;
450
+ hasRuntimeDefault: false;
451
+ enumValues: [string, ...string[]];
452
+ baseColumn: never;
453
+ identity: undefined;
454
+ generated: undefined;
455
+ }, {}, {
456
+ length: number | undefined;
457
+ }>;
458
+ tenantKind: drizzle_orm_sqlite_core.SQLiteColumn<{
459
+ name: "tenant_kind";
460
+ tableName: "durable_workflow_runs";
461
+ dataType: "string";
462
+ columnType: "SQLiteText";
463
+ data: string;
464
+ driverParam: string;
465
+ notNull: false;
466
+ hasDefault: false;
467
+ isPrimaryKey: false;
468
+ isAutoincrement: false;
469
+ hasRuntimeDefault: false;
470
+ enumValues: [string, ...string[]];
471
+ baseColumn: never;
472
+ identity: undefined;
473
+ generated: undefined;
474
+ }, {}, {
475
+ length: number | undefined;
476
+ }>;
439
477
  createdAt: drizzle_orm_sqlite_core.SQLiteColumn<{
440
478
  name: "created_at";
441
479
  tableName: "durable_workflow_runs";
@@ -1322,6 +1360,44 @@ declare const durableSchedules: drizzle_orm_sqlite_core.SQLiteTableWithColumns<{
1322
1360
  }, {}, {
1323
1361
  $type: Record<string, unknown>;
1324
1362
  }>;
1363
+ tenantId: drizzle_orm_sqlite_core.SQLiteColumn<{
1364
+ name: "tenant_id";
1365
+ tableName: "durable_schedules";
1366
+ dataType: "string";
1367
+ columnType: "SQLiteText";
1368
+ data: string;
1369
+ driverParam: string;
1370
+ notNull: false;
1371
+ hasDefault: false;
1372
+ isPrimaryKey: false;
1373
+ isAutoincrement: false;
1374
+ hasRuntimeDefault: false;
1375
+ enumValues: [string, ...string[]];
1376
+ baseColumn: never;
1377
+ identity: undefined;
1378
+ generated: undefined;
1379
+ }, {}, {
1380
+ length: number | undefined;
1381
+ }>;
1382
+ tenantKind: drizzle_orm_sqlite_core.SQLiteColumn<{
1383
+ name: "tenant_kind";
1384
+ tableName: "durable_schedules";
1385
+ dataType: "string";
1386
+ columnType: "SQLiteText";
1387
+ data: string;
1388
+ driverParam: string;
1389
+ notNull: false;
1390
+ hasDefault: false;
1391
+ isPrimaryKey: false;
1392
+ isAutoincrement: false;
1393
+ hasRuntimeDefault: false;
1394
+ enumValues: [string, ...string[]];
1395
+ baseColumn: never;
1396
+ identity: undefined;
1397
+ generated: undefined;
1398
+ }, {}, {
1399
+ length: number | undefined;
1400
+ }>;
1325
1401
  createdAt: drizzle_orm_sqlite_core.SQLiteColumn<{
1326
1402
  name: "created_at";
1327
1403
  tableName: "durable_schedules";
@@ -1673,6 +1749,44 @@ declare const durableSchema: {
1673
1749
  }, {}, {
1674
1750
  length: number | undefined;
1675
1751
  }>;
1752
+ tenantId: drizzle_orm_sqlite_core.SQLiteColumn<{
1753
+ name: "tenant_id";
1754
+ tableName: "durable_workflow_runs";
1755
+ dataType: "string";
1756
+ columnType: "SQLiteText";
1757
+ data: string;
1758
+ driverParam: string;
1759
+ notNull: false;
1760
+ hasDefault: false;
1761
+ isPrimaryKey: false;
1762
+ isAutoincrement: false;
1763
+ hasRuntimeDefault: false;
1764
+ enumValues: [string, ...string[]];
1765
+ baseColumn: never;
1766
+ identity: undefined;
1767
+ generated: undefined;
1768
+ }, {}, {
1769
+ length: number | undefined;
1770
+ }>;
1771
+ tenantKind: drizzle_orm_sqlite_core.SQLiteColumn<{
1772
+ name: "tenant_kind";
1773
+ tableName: "durable_workflow_runs";
1774
+ dataType: "string";
1775
+ columnType: "SQLiteText";
1776
+ data: string;
1777
+ driverParam: string;
1778
+ notNull: false;
1779
+ hasDefault: false;
1780
+ isPrimaryKey: false;
1781
+ isAutoincrement: false;
1782
+ hasRuntimeDefault: false;
1783
+ enumValues: [string, ...string[]];
1784
+ baseColumn: never;
1785
+ identity: undefined;
1786
+ generated: undefined;
1787
+ }, {}, {
1788
+ length: number | undefined;
1789
+ }>;
1676
1790
  createdAt: drizzle_orm_sqlite_core.SQLiteColumn<{
1677
1791
  name: "created_at";
1678
1792
  tableName: "durable_workflow_runs";
@@ -2535,6 +2649,44 @@ declare const durableSchema: {
2535
2649
  }, {}, {
2536
2650
  $type: Record<string, unknown>;
2537
2651
  }>;
2652
+ tenantId: drizzle_orm_sqlite_core.SQLiteColumn<{
2653
+ name: "tenant_id";
2654
+ tableName: "durable_schedules";
2655
+ dataType: "string";
2656
+ columnType: "SQLiteText";
2657
+ data: string;
2658
+ driverParam: string;
2659
+ notNull: false;
2660
+ hasDefault: false;
2661
+ isPrimaryKey: false;
2662
+ isAutoincrement: false;
2663
+ hasRuntimeDefault: false;
2664
+ enumValues: [string, ...string[]];
2665
+ baseColumn: never;
2666
+ identity: undefined;
2667
+ generated: undefined;
2668
+ }, {}, {
2669
+ length: number | undefined;
2670
+ }>;
2671
+ tenantKind: drizzle_orm_sqlite_core.SQLiteColumn<{
2672
+ name: "tenant_kind";
2673
+ tableName: "durable_schedules";
2674
+ dataType: "string";
2675
+ columnType: "SQLiteText";
2676
+ data: string;
2677
+ driverParam: string;
2678
+ notNull: false;
2679
+ hasDefault: false;
2680
+ isPrimaryKey: false;
2681
+ isAutoincrement: false;
2682
+ hasRuntimeDefault: false;
2683
+ enumValues: [string, ...string[]];
2684
+ baseColumn: never;
2685
+ identity: undefined;
2686
+ generated: undefined;
2687
+ }, {}, {
2688
+ length: number | undefined;
2689
+ }>;
2538
2690
  createdAt: drizzle_orm_sqlite_core.SQLiteColumn<{
2539
2691
  name: "created_at";
2540
2692
  tableName: "durable_schedules";
package/dist/index.d.ts CHANGED
@@ -436,6 +436,44 @@ declare const workflowRuns: drizzle_orm_sqlite_core.SQLiteTableWithColumns<{
436
436
  }, {}, {
437
437
  length: number | undefined;
438
438
  }>;
439
+ tenantId: drizzle_orm_sqlite_core.SQLiteColumn<{
440
+ name: "tenant_id";
441
+ tableName: "durable_workflow_runs";
442
+ dataType: "string";
443
+ columnType: "SQLiteText";
444
+ data: string;
445
+ driverParam: string;
446
+ notNull: false;
447
+ hasDefault: false;
448
+ isPrimaryKey: false;
449
+ isAutoincrement: false;
450
+ hasRuntimeDefault: false;
451
+ enumValues: [string, ...string[]];
452
+ baseColumn: never;
453
+ identity: undefined;
454
+ generated: undefined;
455
+ }, {}, {
456
+ length: number | undefined;
457
+ }>;
458
+ tenantKind: drizzle_orm_sqlite_core.SQLiteColumn<{
459
+ name: "tenant_kind";
460
+ tableName: "durable_workflow_runs";
461
+ dataType: "string";
462
+ columnType: "SQLiteText";
463
+ data: string;
464
+ driverParam: string;
465
+ notNull: false;
466
+ hasDefault: false;
467
+ isPrimaryKey: false;
468
+ isAutoincrement: false;
469
+ hasRuntimeDefault: false;
470
+ enumValues: [string, ...string[]];
471
+ baseColumn: never;
472
+ identity: undefined;
473
+ generated: undefined;
474
+ }, {}, {
475
+ length: number | undefined;
476
+ }>;
439
477
  createdAt: drizzle_orm_sqlite_core.SQLiteColumn<{
440
478
  name: "created_at";
441
479
  tableName: "durable_workflow_runs";
@@ -1322,6 +1360,44 @@ declare const durableSchedules: drizzle_orm_sqlite_core.SQLiteTableWithColumns<{
1322
1360
  }, {}, {
1323
1361
  $type: Record<string, unknown>;
1324
1362
  }>;
1363
+ tenantId: drizzle_orm_sqlite_core.SQLiteColumn<{
1364
+ name: "tenant_id";
1365
+ tableName: "durable_schedules";
1366
+ dataType: "string";
1367
+ columnType: "SQLiteText";
1368
+ data: string;
1369
+ driverParam: string;
1370
+ notNull: false;
1371
+ hasDefault: false;
1372
+ isPrimaryKey: false;
1373
+ isAutoincrement: false;
1374
+ hasRuntimeDefault: false;
1375
+ enumValues: [string, ...string[]];
1376
+ baseColumn: never;
1377
+ identity: undefined;
1378
+ generated: undefined;
1379
+ }, {}, {
1380
+ length: number | undefined;
1381
+ }>;
1382
+ tenantKind: drizzle_orm_sqlite_core.SQLiteColumn<{
1383
+ name: "tenant_kind";
1384
+ tableName: "durable_schedules";
1385
+ dataType: "string";
1386
+ columnType: "SQLiteText";
1387
+ data: string;
1388
+ driverParam: string;
1389
+ notNull: false;
1390
+ hasDefault: false;
1391
+ isPrimaryKey: false;
1392
+ isAutoincrement: false;
1393
+ hasRuntimeDefault: false;
1394
+ enumValues: [string, ...string[]];
1395
+ baseColumn: never;
1396
+ identity: undefined;
1397
+ generated: undefined;
1398
+ }, {}, {
1399
+ length: number | undefined;
1400
+ }>;
1325
1401
  createdAt: drizzle_orm_sqlite_core.SQLiteColumn<{
1326
1402
  name: "created_at";
1327
1403
  tableName: "durable_schedules";
@@ -1673,6 +1749,44 @@ declare const durableSchema: {
1673
1749
  }, {}, {
1674
1750
  length: number | undefined;
1675
1751
  }>;
1752
+ tenantId: drizzle_orm_sqlite_core.SQLiteColumn<{
1753
+ name: "tenant_id";
1754
+ tableName: "durable_workflow_runs";
1755
+ dataType: "string";
1756
+ columnType: "SQLiteText";
1757
+ data: string;
1758
+ driverParam: string;
1759
+ notNull: false;
1760
+ hasDefault: false;
1761
+ isPrimaryKey: false;
1762
+ isAutoincrement: false;
1763
+ hasRuntimeDefault: false;
1764
+ enumValues: [string, ...string[]];
1765
+ baseColumn: never;
1766
+ identity: undefined;
1767
+ generated: undefined;
1768
+ }, {}, {
1769
+ length: number | undefined;
1770
+ }>;
1771
+ tenantKind: drizzle_orm_sqlite_core.SQLiteColumn<{
1772
+ name: "tenant_kind";
1773
+ tableName: "durable_workflow_runs";
1774
+ dataType: "string";
1775
+ columnType: "SQLiteText";
1776
+ data: string;
1777
+ driverParam: string;
1778
+ notNull: false;
1779
+ hasDefault: false;
1780
+ isPrimaryKey: false;
1781
+ isAutoincrement: false;
1782
+ hasRuntimeDefault: false;
1783
+ enumValues: [string, ...string[]];
1784
+ baseColumn: never;
1785
+ identity: undefined;
1786
+ generated: undefined;
1787
+ }, {}, {
1788
+ length: number | undefined;
1789
+ }>;
1676
1790
  createdAt: drizzle_orm_sqlite_core.SQLiteColumn<{
1677
1791
  name: "created_at";
1678
1792
  tableName: "durable_workflow_runs";
@@ -2535,6 +2649,44 @@ declare const durableSchema: {
2535
2649
  }, {}, {
2536
2650
  $type: Record<string, unknown>;
2537
2651
  }>;
2652
+ tenantId: drizzle_orm_sqlite_core.SQLiteColumn<{
2653
+ name: "tenant_id";
2654
+ tableName: "durable_schedules";
2655
+ dataType: "string";
2656
+ columnType: "SQLiteText";
2657
+ data: string;
2658
+ driverParam: string;
2659
+ notNull: false;
2660
+ hasDefault: false;
2661
+ isPrimaryKey: false;
2662
+ isAutoincrement: false;
2663
+ hasRuntimeDefault: false;
2664
+ enumValues: [string, ...string[]];
2665
+ baseColumn: never;
2666
+ identity: undefined;
2667
+ generated: undefined;
2668
+ }, {}, {
2669
+ length: number | undefined;
2670
+ }>;
2671
+ tenantKind: drizzle_orm_sqlite_core.SQLiteColumn<{
2672
+ name: "tenant_kind";
2673
+ tableName: "durable_schedules";
2674
+ dataType: "string";
2675
+ columnType: "SQLiteText";
2676
+ data: string;
2677
+ driverParam: string;
2678
+ notNull: false;
2679
+ hasDefault: false;
2680
+ isPrimaryKey: false;
2681
+ isAutoincrement: false;
2682
+ hasRuntimeDefault: false;
2683
+ enumValues: [string, ...string[]];
2684
+ baseColumn: never;
2685
+ identity: undefined;
2686
+ generated: undefined;
2687
+ }, {}, {
2688
+ length: number | undefined;
2689
+ }>;
2538
2690
  createdAt: drizzle_orm_sqlite_core.SQLiteColumn<{
2539
2691
  name: "created_at";
2540
2692
  tableName: "durable_schedules";
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
@@ -207,12 +221,19 @@ var durableSchedules = sqliteTable(
207
221
  state: text("state", {
208
222
  mode: "json"
209
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"),
210
230
  createdAt: integer("created_at").notNull(),
211
231
  updatedAt: integer("updated_at").notNull()
212
232
  },
213
233
  // The poll's "due by T" scan: `paused = 0 AND next_fire_at <= T ORDER BY next_fire_at`.
214
234
  (t) => [
215
- 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)
216
237
  ]
217
238
  );
218
239
  var durableSchema = {
@@ -230,6 +251,74 @@ function durableManagedTables() {
230
251
  }
231
252
  __name(durableManagedTables, "durableManagedTables");
232
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
+
233
322
  // src/drizzle-state-store.ts
234
323
  var RETENTION_EXCLUDED = [
235
324
  LIFECYCLE_REGISTRY_WORKFLOW
@@ -239,6 +328,14 @@ function anyOf(conditions) {
239
328
  return present.length ? or(...present) : sql`1 = 0`;
240
329
  }
241
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");
242
339
  function namespaceFilter(namespace) {
243
340
  if (namespace === void 0) return void 0;
244
341
  if (namespace === DEFAULT_NAMESPACE || namespace === "") return or(eq(workflowRuns.namespace, DEFAULT_NAMESPACE), eq(workflowRuns.namespace, ""), isNull(workflowRuns.namespace));
@@ -552,15 +649,15 @@ var DrizzleStateStore = class {
552
649
  /** Recovery on boot. Namespace-scoped: a worker re-drives only its own tenant's crashed runs.
553
650
  * `and(...)` drops an `undefined` predicate, so an unscoped caller recovers every namespace. */
554
651
  async listIncompleteRuns(namespace) {
555
- 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, [
556
653
  "running",
557
654
  "cancelling"
558
- ]), namespaceFilter(namespace)));
655
+ ]), namespaceFilter(namespace))));
559
656
  return rows.map(fromRunRow);
560
657
  }
561
658
  async listOrphanedRuns(nowMs, limit, namespace, ownedBy) {
562
659
  if (limit <= 0) return [];
563
- 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, [
564
661
  "running",
565
662
  "cancelling"
566
663
  ]), or(
@@ -568,19 +665,19 @@ var DrizzleStateStore = class {
568
665
  lte(workflowRuns.lockedUntil, nowMs),
569
666
  // A live lease left by a previous process with the caller's own instance id.
570
667
  ownedBy ? and(eq(workflowRuns.lockedBy, ownedBy.owner), lt(workflowRuns.lockedUntil, ownedBy.lockedBefore)) : void 0
571
- ), namespaceFilter(namespace))).orderBy(asc(workflowRuns.createdAt)).limit(limit);
668
+ ), namespaceFilter(namespace))).orderBy(asc(workflowRuns.createdAt)).limit(limit));
572
669
  return rows.map(fromRunRow);
573
670
  }
574
671
  /** Dispatch pick-up. Namespace-scoped — without it a worker serving one tenant drains another
575
672
  * tenant's queue, and the `limit` makes it worse: foreign runs consume the FIFO budget. */
576
673
  async listPendingRuns(limit, namespace) {
577
- 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));
578
675
  return rows.map(fromRunRow);
579
676
  }
580
677
  /** Due durable timers. Namespace-scoped: a worker resumes only its own tenant's suspended runs. */
581
678
  async listDueTimers(nowMs, namespace, limit) {
582
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));
583
- const rows = limit !== void 0 ? await query.limit(limit) : await query;
680
+ const rows = await withTenantColumnsHint("sqlite", limit !== void 0 ? query.limit(limit) : query);
584
681
  return rows.map(fromRunRow);
585
682
  }
586
683
  async tryLockRun(runId, owner, leaseUntilMs, nowMs) {
@@ -630,6 +727,8 @@ var DrizzleStateStore = class {
630
727
  // A set of origins may include `null` (the unattributed bucket), which `IN` cannot carry —
631
728
  // `IN (NULL)` is never true — so it becomes its own ORed `IS NULL` branch.
632
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),
633
732
  query.status ? eq(workflowRuns.status, query.status) : void 0,
634
733
  // `status IN (...)`; an empty set matches nothing (mirrors the in-memory store).
635
734
  query.statuses ? query.statuses.length ? inArray(workflowRuns.status, query.statuses) : sql`1 = 0` : void 0,
@@ -730,7 +829,7 @@ var DrizzleStateStore = class {
730
829
  return rows.length > 0;
731
830
  }
732
831
  async listSchedules(query) {
733
- 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));
734
833
  return rows.map(fromScheduleRow);
735
834
  }
736
835
  async listCheckpoints(runId) {
@@ -1059,6 +1158,8 @@ function toRunRow(run) {
1059
1158
  namespace: run.namespace || DEFAULT_NAMESPACE,
1060
1159
  // Absent origin stays SQL NULL — "unknown", never coerced into a real-looking library name.
1061
1160
  origin: run.origin ?? null,
1161
+ tenantId: run.tenantId ?? null,
1162
+ tenantKind: run.tenantKind ?? null,
1062
1163
  createdAt: run.createdAt.getTime(),
1063
1164
  updatedAt: run.updatedAt.getTime()
1064
1165
  };
@@ -1082,6 +1183,8 @@ function toRunPatch(patch) {
1082
1183
  if ("priority" in patch) row.priority = patch.priority ?? null;
1083
1184
  if ("namespace" in patch) row.namespace = patch.namespace || DEFAULT_NAMESPACE;
1084
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;
1085
1188
  if (patch.createdAt != null) row.createdAt = patch.createdAt.getTime();
1086
1189
  if (patch.updatedAt != null) row.updatedAt = patch.updatedAt.getTime();
1087
1190
  return row;
@@ -1111,6 +1214,13 @@ function fromRunRow(row) {
1111
1214
  namespace: row.namespace || DEFAULT_NAMESPACE,
1112
1215
  // NULL (a row written before the column existed) surfaces as `undefined` = unknown origin.
1113
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
+ } : {},
1114
1224
  createdAt: new Date(row.createdAt),
1115
1225
  updatedAt: new Date(row.updatedAt)
1116
1226
  };
@@ -1170,6 +1280,8 @@ function toScheduleRow(r) {
1170
1280
  tags: r.tags ?? null,
1171
1281
  spec: r.spec,
1172
1282
  state: r.state,
1283
+ tenantId: r.tenantId ?? null,
1284
+ tenantKind: r.tenantKind ?? null,
1173
1285
  createdAt: r.createdAt.getTime(),
1174
1286
  updatedAt: r.updatedAt.getTime()
1175
1287
  };
@@ -1184,6 +1296,8 @@ function toSchedulePatch(patch) {
1184
1296
  if ("tags" in patch) set.tags = patch.tags ?? null;
1185
1297
  if ("spec" in patch && patch.spec !== void 0) set.spec = patch.spec;
1186
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;
1187
1301
  if ("updatedAt" in patch && patch.updatedAt !== void 0) set.updatedAt = patch.updatedAt.getTime();
1188
1302
  return set;
1189
1303
  }
@@ -1200,6 +1314,12 @@ function fromScheduleRow(row) {
1200
1314
  } : {},
1201
1315
  spec: row.spec ?? {},
1202
1316
  state: row.state ?? {},
1317
+ ...row.tenantId != null ? {
1318
+ tenantId: row.tenantId
1319
+ } : {},
1320
+ ...row.tenantKind != null ? {
1321
+ tenantKind: row.tenantKind
1322
+ } : {},
1203
1323
  createdAt: new Date(row.createdAt),
1204
1324
  updatedAt: new Date(row.updatedAt)
1205
1325
  };