@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.cjs CHANGED
@@ -28,6 +28,9 @@ module.exports = __toCommonJS(index_exports);
28
28
 
29
29
  // src/prisma-state-store.ts
30
30
  var import_nestjs_durable_core = require("@dudousxd/nestjs-durable-core");
31
+ var RETENTION_EXCLUDED = [
32
+ import_nestjs_durable_core.LIFECYCLE_REGISTRY_WORKFLOW
33
+ ];
31
34
  function namespaceWhere(namespace) {
32
35
  if (namespace === void 0) return {};
33
36
  return {
@@ -44,19 +47,40 @@ var PrismaStateStore = class {
44
47
  this.db = db;
45
48
  }
46
49
  async tryAdmitSingleton(runId, tag, workflow, limit, retryWakeAt) {
47
- return this.db.$transaction(async (tx) => {
48
- const where = {
49
- workflow,
50
- status: {
51
- in: import_nestjs_durable_core.SINGLETON_ACTIVE_STATUSES
52
- }
53
- };
54
- await tx.durableWorkflowRun.updateMany({
50
+ const where = {
51
+ workflow,
52
+ status: {
53
+ in: import_nestjs_durable_core.SINGLETON_ACTIVE_STATUSES
54
+ }
55
+ };
56
+ const lockRows = rowLockDialect(this.db);
57
+ const claimants = lockRows ? (0, import_nestjs_durable_core.singletonClaimantIds)(
58
+ // The primary, under the read-replicas extension: a lagging replica would leave rows out.
59
+ await primaryOf(this.db).durableWorkflowRun.findMany({
55
60
  where,
56
- data: {
57
- workflow
61
+ select: {
62
+ id: true,
63
+ tags: true
64
+ },
65
+ orderBy: {
66
+ id: "asc"
58
67
  }
59
- });
68
+ }),
69
+ tag
70
+ ) : [];
71
+ return this.db.$transaction(async (tx) => {
72
+ if (lockRows) {
73
+ for (let at = 0; at < claimants.length; at += import_nestjs_durable_core.SINGLETON_LOCK_CHUNK) {
74
+ await lockRows(tx, claimants.slice(at, at + import_nestjs_durable_core.SINGLETON_LOCK_CHUNK));
75
+ }
76
+ } else {
77
+ await tx.durableWorkflowRun.updateMany({
78
+ where,
79
+ data: {
80
+ workflow
81
+ }
82
+ });
83
+ }
60
84
  const rows = await tx.durableWorkflowRun.findMany({
61
85
  where
62
86
  });
@@ -223,7 +247,8 @@ var PrismaStateStore = class {
223
247
  if (policy.maxAge == null && policy.maxCount == null) return 0;
224
248
  const scoped = this.runWhere({
225
249
  ...policy.scope ?? {},
226
- statuses
250
+ statuses,
251
+ excludeWorkflows: RETENTION_EXCLUDED
227
252
  });
228
253
  const ids = /* @__PURE__ */ new Set();
229
254
  if (policy.maxAge != null) {
@@ -240,20 +265,22 @@ var PrismaStateStore = class {
240
265
  ]
241
266
  },
242
267
  select: {
243
- id: true
268
+ id: true,
269
+ tags: true
244
270
  },
245
271
  orderBy: {
246
272
  updatedAt: "asc"
247
273
  },
248
274
  take: limit
249
275
  });
250
- for (const r of rows) ids.add(r.id);
276
+ for (const r of rows) if (!(0, import_nestjs_durable_core.retentionKeeps)(r.tags)) ids.add(r.id);
251
277
  }
252
278
  if (policy.maxCount != null && ids.size < limit) {
253
279
  const rows = await this.db.durableWorkflowRun.findMany({
254
280
  where: scoped,
255
281
  select: {
256
- id: true
282
+ id: true,
283
+ tags: true
257
284
  },
258
285
  orderBy: [
259
286
  {
@@ -268,7 +295,7 @@ var PrismaStateStore = class {
268
295
  });
269
296
  for (const r of rows) {
270
297
  if (ids.size >= limit) break;
271
- ids.add(r.id);
298
+ if (!(0, import_nestjs_durable_core.retentionKeeps)(r.tags)) ids.add(r.id);
272
299
  }
273
300
  }
274
301
  const doomed = [
@@ -614,6 +641,18 @@ var PrismaStateStore = class {
614
641
  if (query.workflows) and.push(anyOf(query.workflows.map((w) => ({
615
642
  workflow: w
616
643
  }))));
644
+ if (query.excludeWorkflows?.length) and.push({
645
+ workflow: {
646
+ notIn: query.excludeWorkflows
647
+ }
648
+ });
649
+ if (query.updatedAfter !== void 0) {
650
+ and.push({
651
+ updatedAt: {
652
+ gte: new Date(query.updatedAfter)
653
+ }
654
+ });
655
+ }
617
656
  if (query.namespaces) and.push(anyOf(query.namespaces.map((n) => ({
618
657
  namespace: n
619
658
  }))));
@@ -1544,6 +1583,23 @@ var CHECKPOINT_SELECT_WITHOUT_INPUT = {
1544
1583
  finishedAt: true
1545
1584
  };
1546
1585
  var jsonText = /* @__PURE__ */ __name((v) => v == null ? null : JSON.stringify(v), "jsonText");
1586
+ function primaryOf(db) {
1587
+ const client = db;
1588
+ return typeof client.$primary === "function" ? client.$primary() : db;
1589
+ }
1590
+ __name(primaryOf, "primaryOf");
1591
+ function rowLockDialect(db) {
1592
+ const client = db;
1593
+ if (typeof client.$queryRawUnsafe !== "function") return void 0;
1594
+ if (client._activeProvider === "postgresql") {
1595
+ 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);
1596
+ }
1597
+ if (client._activeProvider === "mysql") {
1598
+ return (tx, ids) => tx.$queryRawUnsafe(`SELECT id FROM durable_workflow_runs WHERE id IN (${ids.map(() => "?").join(", ")}) ORDER BY id FOR UPDATE`, ...ids);
1599
+ }
1600
+ return void 0;
1601
+ }
1602
+ __name(rowLockDialect, "rowLockDialect");
1547
1603
  function rawSettleDialect(db) {
1548
1604
  const client = db;
1549
1605
  if (typeof client.$queryRawUnsafe !== "function") return void 0;