@dudousxd/nestjs-durable-store-drizzle 0.24.6 → 0.25.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/pg/index.js CHANGED
@@ -198,7 +198,7 @@ async function ensureDrizzlePgDurableSchema(db, options = {}) {
198
198
  __name(ensureDrizzlePgDurableSchema, "ensureDrizzlePgDurableSchema");
199
199
 
200
200
  // src/pg/drizzle-pg-state-store.ts
201
- import { RUN_VALUE_FACET_SCAN, SINGLETON_ACTIVE_STATUSES, TERMINAL_RUN_STATUSES, attributePredicateOperands, axisIsRunColumn, clearTerminalSingletonAdmission, mergeRunFacetRows, mergeRunValueFacetRows, normalizeAttributeRows, parseDuration, runValueFacetsFromRuns, singletonAdmissionDenialPatch, singletonAdmissionTags } from "@dudousxd/nestjs-durable-core";
201
+ import { LIFECYCLE_REGISTRY_WORKFLOW, RUN_VALUE_FACET_SCAN, SINGLETON_ACTIVE_STATUSES, TERMINAL_RUN_STATUSES, attributePredicateOperands, axisIsRunColumn, clearTerminalSingletonAdmission, mergeRunFacetRows, mergeRunValueFacetRows, normalizeAttributeRows, parseDuration, retentionKeeps, runValueFacetsFromRuns, singletonAdmissionDenialPatch, singletonAdmissionTags } from "@dudousxd/nestjs-durable-core";
202
202
  import { and, asc, desc, eq, exists, getTableColumns, gt, gte, inArray, isNotNull, isNull, like, lt, lte, notInArray, or, sql as sql3 } from "drizzle-orm";
203
203
 
204
204
  // src/pg/schema.ts
@@ -366,6 +366,9 @@ function durablePgManagedTables() {
366
366
  __name(durablePgManagedTables, "durablePgManagedTables");
367
367
 
368
368
  // src/pg/drizzle-pg-state-store.ts
369
+ var RETENTION_EXCLUDED = [
370
+ LIFECYCLE_REGISTRY_WORKFLOW
371
+ ];
369
372
  function anyOf(conditions) {
370
373
  const present = conditions.filter((c) => c !== void 0);
371
374
  return present.length ? or(...present) : sql3`1 = 0`;
@@ -423,10 +426,10 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
423
426
  }
424
427
  async tryAdmitSingleton(runId, tag, workflow, limit, retryWakeAt) {
425
428
  return this.db.transaction(async (tx) => {
426
- const where = and(eq(durableWorkflowRuns.workflow, workflow), inArray(durableWorkflowRuns.status, SINGLETON_ACTIVE_STATUSES), this.scope());
427
- await tx.update(durableWorkflowRuns).set({
428
- workflow
429
- }).where(where);
429
+ const where = and(eq(durableWorkflowRuns.workflow, workflow), inArray(durableWorkflowRuns.status, SINGLETON_ACTIVE_STATUSES), hasTag(tag), this.scope());
430
+ await tx.select({
431
+ id: durableWorkflowRuns.id
432
+ }).from(durableWorkflowRuns).where(where).orderBy(asc(durableWorkflowRuns.id)).for("update");
430
433
  const rows = await tx.select().from(durableWorkflowRuns).where(where);
431
434
  const runs = rows.map(fromRunRow);
432
435
  const tags = singletonAdmissionTags(runs, runId, tag, workflow, limit);
@@ -512,28 +515,31 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
512
515
  if (policy.maxAge == null && policy.maxCount == null) return 0;
513
516
  const scoped = this.runFilters({
514
517
  ...policy.scope ?? {},
515
- statuses
518
+ statuses,
519
+ excludeWorkflows: RETENTION_EXCLUDED
516
520
  });
517
521
  return this.db.transaction(async (tx) => {
518
522
  const ids = /* @__PURE__ */ new Set();
519
523
  if (policy.maxAge != null) {
520
524
  const cutoff = new Date(nowMs - parseDuration(policy.maxAge));
521
525
  const rows = await tx.select({
522
- id: durableWorkflowRuns.id
526
+ id: durableWorkflowRuns.id,
527
+ tags: durableWorkflowRuns.tags
523
528
  }).from(durableWorkflowRuns).where(and(...scoped, lt(durableWorkflowRuns.updatedAt, cutoff))).orderBy(asc(durableWorkflowRuns.updatedAt)).limit(limit).for("update", {
524
529
  skipLocked: true
525
530
  });
526
- for (const r of rows) ids.add(r.id);
531
+ for (const r of rows) if (!retentionKeeps(r.tags)) ids.add(r.id);
527
532
  }
528
533
  if (policy.maxCount != null && ids.size < limit) {
529
534
  const rows = await tx.select({
530
- id: durableWorkflowRuns.id
535
+ id: durableWorkflowRuns.id,
536
+ tags: durableWorkflowRuns.tags
531
537
  }).from(durableWorkflowRuns).where(and(...scoped)).orderBy(desc(durableWorkflowRuns.updatedAt), desc(durableWorkflowRuns.id)).limit(limit).offset(policy.maxCount).for("update", {
532
538
  skipLocked: true
533
539
  });
534
540
  for (const r of rows) {
535
541
  if (ids.size >= limit) break;
536
- ids.add(r.id);
542
+ if (!retentionKeeps(r.tags)) ids.add(r.id);
537
543
  }
538
544
  }
539
545
  if (ids.size === 0) return 0;
@@ -711,6 +717,8 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
711
717
  this.scope(),
712
718
  query.workflow ? eq(durableWorkflowRuns.workflow, query.workflow) : void 0,
713
719
  query.workflows ? query.workflows.length ? inArray(durableWorkflowRuns.workflow, query.workflows) : sql3`1 = 0` : void 0,
720
+ query.excludeWorkflows?.length ? notInArray(durableWorkflowRuns.workflow, query.excludeWorkflows) : void 0,
721
+ query.updatedAfter !== void 0 ? gte(durableWorkflowRuns.updatedAt, new Date(query.updatedAfter)) : void 0,
714
722
  // The tenant boundary. Also the engine's execution-timeout sweep path
715
723
  // (`listRuns({ workflow, status, namespace })`) — unscoped, it would cancel another tenant's runs.
716
724
  namespaceFilter(query.namespace),
@@ -1102,6 +1110,9 @@ async function reindexAttributes(db, runId, attributes) {
1102
1110
  }
1103
1111
  __name(reindexAttributes, "reindexAttributes");
1104
1112
  async function deleteRunsCascade(db, runIds) {
1113
+ await db.select({
1114
+ id: durableWorkflowRuns.id
1115
+ }).from(durableWorkflowRuns).where(inArray(durableWorkflowRuns.id, runIds)).orderBy(asc(durableWorkflowRuns.id)).for("update");
1105
1116
  await db.delete(durableStepCheckpoints).where(inArray(durableStepCheckpoints.runId, runIds));
1106
1117
  await db.delete(durableSignalWaiters).where(inArray(durableSignalWaiters.runId, runIds));
1107
1118
  await db.delete(durableRunAttributes).where(inArray(durableRunAttributes.runId, runIds));