@dudousxd/nestjs-durable-store-drizzle 0.24.7 → 0.26.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
@@ -29,6 +29,7 @@ __export(src_exports, {
29
29
  durableSchedules: () => durableSchedules,
30
30
  durableSchema: () => durableSchema,
31
31
  runAttributes: () => runAttributes,
32
+ signalIds: () => signalIds,
32
33
  signalWaiters: () => signalWaiters,
33
34
  stepCheckpoints: () => stepCheckpoints,
34
35
  workflowRuns: () => workflowRuns
@@ -208,6 +209,20 @@ var bufferedEvents = (0, import_sqlite_core.sqliteTable)("durable_buffered_event
208
209
  }),
209
210
  publishedAt: (0, import_sqlite_core.integer)("published_at").notNull()
210
211
  });
212
+ var signalIds = (0, import_sqlite_core.sqliteTable)("durable_signal_ids", {
213
+ token: (0, import_sqlite_core.text)("token").notNull(),
214
+ signalId: (0, import_sqlite_core.text)("signal_id").notNull(),
215
+ expiresAt: (0, import_sqlite_core.integer)("expires_at").notNull()
216
+ }, (t) => [
217
+ (0, import_sqlite_core.primaryKey)({
218
+ columns: [
219
+ t.token,
220
+ t.signalId
221
+ ]
222
+ }),
223
+ // The sweep's "expired by T" scan.
224
+ (0, import_sqlite_core.index)("durable_signal_ids_expires_at_idx").on(t.expiresAt)
225
+ ]);
211
226
  var durableSchedules = (0, import_sqlite_core.sqliteTable)(
212
227
  "durable_schedules",
213
228
  {
@@ -242,6 +257,7 @@ var durableSchema = {
242
257
  signalWaiters,
243
258
  bufferedSignals,
244
259
  bufferedEvents,
260
+ signalIds,
245
261
  durableSchedules
246
262
  };
247
263
  function durableManagedTables() {
@@ -250,6 +266,9 @@ function durableManagedTables() {
250
266
  __name(durableManagedTables, "durableManagedTables");
251
267
 
252
268
  // src/drizzle-state-store.ts
269
+ var RETENTION_EXCLUDED = [
270
+ import_nestjs_durable_core.LIFECYCLE_REGISTRY_WORKFLOW
271
+ ];
253
272
  function anyOf(conditions) {
254
273
  const present = conditions.filter((c) => c !== void 0);
255
274
  return present.length ? (0, import_drizzle_orm2.or)(...present) : import_drizzle_orm2.sql`1 = 0`;
@@ -358,23 +377,26 @@ var DrizzleStateStore = class {
358
377
  if (policy.maxAge == null && policy.maxCount == null) return 0;
359
378
  const scoped = this.runFilters({
360
379
  ...policy.scope ?? {},
361
- statuses
380
+ statuses,
381
+ excludeWorkflows: RETENTION_EXCLUDED
362
382
  });
363
383
  const ids = /* @__PURE__ */ new Set();
364
384
  if (policy.maxAge != null) {
365
385
  const cutoff = nowMs - (0, import_nestjs_durable_core.parseDuration)(policy.maxAge);
366
386
  const rows = await this.db.select({
367
- id: workflowRuns.id
387
+ id: workflowRuns.id,
388
+ tags: workflowRuns.tags
368
389
  }).from(workflowRuns).where((0, import_drizzle_orm2.and)(...scoped, (0, import_drizzle_orm2.lt)(workflowRuns.updatedAt, cutoff))).orderBy((0, import_drizzle_orm2.asc)(workflowRuns.updatedAt)).limit(limit);
369
- for (const r of rows) ids.add(r.id);
390
+ for (const r of rows) if (!(0, import_nestjs_durable_core.retentionKeeps)(r.tags)) ids.add(r.id);
370
391
  }
371
392
  if (policy.maxCount != null && ids.size < limit) {
372
393
  const rows = await this.db.select({
373
- id: workflowRuns.id
394
+ id: workflowRuns.id,
395
+ tags: workflowRuns.tags
374
396
  }).from(workflowRuns).where((0, import_drizzle_orm2.and)(...scoped)).orderBy((0, import_drizzle_orm2.desc)(workflowRuns.updatedAt), (0, import_drizzle_orm2.desc)(workflowRuns.id)).limit(limit).offset(policy.maxCount);
375
397
  for (const r of rows) {
376
398
  if (ids.size >= limit) break;
377
- ids.add(r.id);
399
+ if (!(0, import_nestjs_durable_core.retentionKeeps)(r.tags)) ids.add(r.id);
378
400
  }
379
401
  }
380
402
  const doomed = [
@@ -623,6 +645,8 @@ var DrizzleStateStore = class {
623
645
  // The plural form of each axis below: ORed within itself, ANDed with everything else, and an
624
646
  // empty set matches nothing — the same contract `statuses` already had.
625
647
  query.workflows ? anyOf(query.workflows.map((w) => (0, import_drizzle_orm2.eq)(workflowRuns.workflow, w))) : void 0,
648
+ query.excludeWorkflows?.length ? (0, import_drizzle_orm2.notInArray)(workflowRuns.workflow, query.excludeWorkflows) : void 0,
649
+ query.updatedAfter !== void 0 ? (0, import_drizzle_orm2.gte)(workflowRuns.updatedAt, query.updatedAfter) : void 0,
626
650
  // The tenant boundary, ANDed with everything else. This is not only a dashboard facet: it is
627
651
  // the fourth namespace-scoped worker path, because `engine.sweepTimeouts` cancels runs past
628
652
  // their `executionTimeout` via `listRuns({ workflow, status, namespace })` — unscoped here, a
@@ -969,6 +993,51 @@ var DrizzleStateStore = class {
969
993
  payload: row.payload ?? void 0
970
994
  };
971
995
  }
996
+ async takeOldestBufferedSignal(tokens) {
997
+ if (tokens.length === 0) return null;
998
+ const oldest = this.db.select({
999
+ id: bufferedSignals.id
1000
+ }).from(bufferedSignals).where((0, import_drizzle_orm2.inArray)(bufferedSignals.token, [
1001
+ ...new Set(tokens)
1002
+ ])).orderBy((0, import_drizzle_orm2.asc)(bufferedSignals.id)).limit(1);
1003
+ const rows = await this.db.delete(bufferedSignals).where((0, import_drizzle_orm2.inArray)(bufferedSignals.id, oldest)).returning({
1004
+ token: bufferedSignals.token,
1005
+ payload: bufferedSignals.payload
1006
+ });
1007
+ const row = rows[0];
1008
+ return row ? {
1009
+ token: row.token,
1010
+ payload: row.payload ?? void 0
1011
+ } : null;
1012
+ }
1013
+ async claimSignalId(token, id, expiresAt, nowMs) {
1014
+ const rows = await this.db.insert(signalIds).values({
1015
+ token,
1016
+ signalId: id,
1017
+ expiresAt
1018
+ }).onConflictDoUpdate({
1019
+ target: [
1020
+ signalIds.token,
1021
+ signalIds.signalId
1022
+ ],
1023
+ set: {
1024
+ expiresAt
1025
+ },
1026
+ setWhere: (0, import_drizzle_orm2.lte)(signalIds.expiresAt, nowMs)
1027
+ }).returning({
1028
+ token: signalIds.token
1029
+ });
1030
+ return rows.length > 0;
1031
+ }
1032
+ async releaseSignalId(token, id) {
1033
+ await this.db.delete(signalIds).where((0, import_drizzle_orm2.and)((0, import_drizzle_orm2.eq)(signalIds.token, token), (0, import_drizzle_orm2.eq)(signalIds.signalId, id)));
1034
+ }
1035
+ async deleteExpiredSignalIds(nowMs, limit) {
1036
+ const rows = await this.db.delete(signalIds).where(import_drizzle_orm2.sql`(${signalIds.token}, ${signalIds.signalId}) in (select ${signalIds.token}, ${signalIds.signalId} from ${signalIds} where ${signalIds.expiresAt} <= ${nowMs} limit ${limit})`).returning({
1037
+ token: signalIds.token
1038
+ });
1039
+ return rows.length;
1040
+ }
972
1041
  async listBufferedSignals(tokenPrefix, afterId, limit) {
973
1042
  const rows = await this.db.select().from(bufferedSignals).where((0, import_drizzle_orm2.and)((0, import_drizzle_orm2.like)(bufferedSignals.token, `${tokenPrefix}%`), afterId !== void 0 ? (0, import_drizzle_orm2.gt)(bufferedSignals.id, Number(afterId)) : void 0)).orderBy((0, import_drizzle_orm2.asc)(bufferedSignals.id)).limit(limit);
974
1043
  return rows.filter((r) => r.token.startsWith(tokenPrefix)).map((r) => ({
@@ -1243,6 +1312,7 @@ __name(excludedSet, "excludedSet");
1243
1312
  durableSchedules,
1244
1313
  durableSchema,
1245
1314
  runAttributes,
1315
+ signalIds,
1246
1316
  signalWaiters,
1247
1317
  stepCheckpoints,
1248
1318
  workflowRuns