@dudousxd/nestjs-durable-store-prisma 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/index.js CHANGED
@@ -2,7 +2,10 @@ var __defProp = Object.defineProperty;
2
2
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
3
 
4
4
  // src/prisma-state-store.ts
5
- 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";
5
+ import { LIFECYCLE_REGISTRY_WORKFLOW, RUN_VALUE_FACET_SCAN, SINGLETON_ACTIVE_STATUSES, SINGLETON_LOCK_CHUNK, TERMINAL_RUN_STATUSES, attributePredicateOperands, axisIsRunColumn, clearTerminalSingletonAdmission, mergeRunFacetRows, mergeRunValueFacetRows, normalizeAttributeRows, parseDuration, retentionKeeps, runValueFacetsFromRuns, singletonAdmissionDenialPatch, singletonAdmissionTags, singletonClaimantIds } from "@dudousxd/nestjs-durable-core";
6
+ var RETENTION_EXCLUDED = [
7
+ LIFECYCLE_REGISTRY_WORKFLOW
8
+ ];
6
9
  function namespaceWhere(namespace) {
7
10
  if (namespace === void 0) return {};
8
11
  return {
@@ -19,19 +22,40 @@ var PrismaStateStore = class {
19
22
  this.db = db;
20
23
  }
21
24
  async tryAdmitSingleton(runId, tag, workflow, limit, retryWakeAt) {
22
- return this.db.$transaction(async (tx) => {
23
- const where = {
24
- workflow,
25
- status: {
26
- in: SINGLETON_ACTIVE_STATUSES
27
- }
28
- };
29
- await tx.durableWorkflowRun.updateMany({
25
+ const where = {
26
+ workflow,
27
+ status: {
28
+ in: SINGLETON_ACTIVE_STATUSES
29
+ }
30
+ };
31
+ const lockRows = rowLockDialect(this.db);
32
+ const claimants = lockRows ? singletonClaimantIds(
33
+ // The primary, under the read-replicas extension: a lagging replica would leave rows out.
34
+ await primaryOf(this.db).durableWorkflowRun.findMany({
30
35
  where,
31
- data: {
32
- workflow
36
+ select: {
37
+ id: true,
38
+ tags: true
39
+ },
40
+ orderBy: {
41
+ id: "asc"
33
42
  }
34
- });
43
+ }),
44
+ tag
45
+ ) : [];
46
+ return this.db.$transaction(async (tx) => {
47
+ if (lockRows) {
48
+ for (let at = 0; at < claimants.length; at += SINGLETON_LOCK_CHUNK) {
49
+ await lockRows(tx, claimants.slice(at, at + SINGLETON_LOCK_CHUNK));
50
+ }
51
+ } else {
52
+ await tx.durableWorkflowRun.updateMany({
53
+ where,
54
+ data: {
55
+ workflow
56
+ }
57
+ });
58
+ }
35
59
  const rows = await tx.durableWorkflowRun.findMany({
36
60
  where
37
61
  });
@@ -198,7 +222,8 @@ var PrismaStateStore = class {
198
222
  if (policy.maxAge == null && policy.maxCount == null) return 0;
199
223
  const scoped = this.runWhere({
200
224
  ...policy.scope ?? {},
201
- statuses
225
+ statuses,
226
+ excludeWorkflows: RETENTION_EXCLUDED
202
227
  });
203
228
  const ids = /* @__PURE__ */ new Set();
204
229
  if (policy.maxAge != null) {
@@ -215,20 +240,22 @@ var PrismaStateStore = class {
215
240
  ]
216
241
  },
217
242
  select: {
218
- id: true
243
+ id: true,
244
+ tags: true
219
245
  },
220
246
  orderBy: {
221
247
  updatedAt: "asc"
222
248
  },
223
249
  take: limit
224
250
  });
225
- for (const r of rows) ids.add(r.id);
251
+ for (const r of rows) if (!retentionKeeps(r.tags)) ids.add(r.id);
226
252
  }
227
253
  if (policy.maxCount != null && ids.size < limit) {
228
254
  const rows = await this.db.durableWorkflowRun.findMany({
229
255
  where: scoped,
230
256
  select: {
231
- id: true
257
+ id: true,
258
+ tags: true
232
259
  },
233
260
  orderBy: [
234
261
  {
@@ -243,7 +270,7 @@ var PrismaStateStore = class {
243
270
  });
244
271
  for (const r of rows) {
245
272
  if (ids.size >= limit) break;
246
- ids.add(r.id);
273
+ if (!retentionKeeps(r.tags)) ids.add(r.id);
247
274
  }
248
275
  }
249
276
  const doomed = [
@@ -589,6 +616,18 @@ var PrismaStateStore = class {
589
616
  if (query.workflows) and.push(anyOf(query.workflows.map((w) => ({
590
617
  workflow: w
591
618
  }))));
619
+ if (query.excludeWorkflows?.length) and.push({
620
+ workflow: {
621
+ notIn: query.excludeWorkflows
622
+ }
623
+ });
624
+ if (query.updatedAfter !== void 0) {
625
+ and.push({
626
+ updatedAt: {
627
+ gte: new Date(query.updatedAfter)
628
+ }
629
+ });
630
+ }
592
631
  if (query.namespaces) and.push(anyOf(query.namespaces.map((n) => ({
593
632
  namespace: n
594
633
  }))));
@@ -1519,6 +1558,23 @@ var CHECKPOINT_SELECT_WITHOUT_INPUT = {
1519
1558
  finishedAt: true
1520
1559
  };
1521
1560
  var jsonText = /* @__PURE__ */ __name((v) => v == null ? null : JSON.stringify(v), "jsonText");
1561
+ function primaryOf(db) {
1562
+ const client = db;
1563
+ return typeof client.$primary === "function" ? client.$primary() : db;
1564
+ }
1565
+ __name(primaryOf, "primaryOf");
1566
+ function rowLockDialect(db) {
1567
+ const client = db;
1568
+ if (typeof client.$queryRawUnsafe !== "function") return void 0;
1569
+ if (client._activeProvider === "postgresql") {
1570
+ return (tx, ids) => tx.$queryRawUnsafe(`SELECT id FROM "durable_workflow_runs" WHERE id IN (${ids.map((_, i) => `$${i + 1}`).join(", ")}) ORDER BY id FOR UPDATE`, ...ids);
1571
+ }
1572
+ if (client._activeProvider === "mysql") {
1573
+ return (tx, ids) => tx.$queryRawUnsafe(`SELECT id FROM durable_workflow_runs WHERE id IN (${ids.map(() => "?").join(", ")}) ORDER BY id FOR UPDATE`, ...ids);
1574
+ }
1575
+ return void 0;
1576
+ }
1577
+ __name(rowLockDialect, "rowLockDialect");
1522
1578
  function rawSettleDialect(db) {
1523
1579
  const client = db;
1524
1580
  if (typeof client.$queryRawUnsafe !== "function") return void 0;