@dudousxd/nestjs-durable-store-prisma 0.24.5 → 0.24.7

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,7 @@ 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 { RUN_VALUE_FACET_SCAN, SINGLETON_ACTIVE_STATUSES, SINGLETON_LOCK_CHUNK, TERMINAL_RUN_STATUSES, attributePredicateOperands, axisIsRunColumn, clearTerminalSingletonAdmission, mergeRunFacetRows, mergeRunValueFacetRows, normalizeAttributeRows, parseDuration, runValueFacetsFromRuns, singletonAdmissionDenialPatch, singletonAdmissionTags, singletonClaimantIds } from "@dudousxd/nestjs-durable-core";
6
6
  function namespaceWhere(namespace) {
7
7
  if (namespace === void 0) return {};
8
8
  return {
@@ -19,19 +19,40 @@ var PrismaStateStore = class {
19
19
  this.db = db;
20
20
  }
21
21
  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({
22
+ const where = {
23
+ workflow,
24
+ status: {
25
+ in: SINGLETON_ACTIVE_STATUSES
26
+ }
27
+ };
28
+ const lockRows = rowLockDialect(this.db);
29
+ const claimants = lockRows ? singletonClaimantIds(
30
+ // The primary, under the read-replicas extension: a lagging replica would leave rows out.
31
+ await primaryOf(this.db).durableWorkflowRun.findMany({
30
32
  where,
31
- data: {
32
- workflow
33
+ select: {
34
+ id: true,
35
+ tags: true
36
+ },
37
+ orderBy: {
38
+ id: "asc"
33
39
  }
34
- });
40
+ }),
41
+ tag
42
+ ) : [];
43
+ return this.db.$transaction(async (tx) => {
44
+ if (lockRows) {
45
+ for (let at = 0; at < claimants.length; at += SINGLETON_LOCK_CHUNK) {
46
+ await lockRows(tx, claimants.slice(at, at + SINGLETON_LOCK_CHUNK));
47
+ }
48
+ } else {
49
+ await tx.durableWorkflowRun.updateMany({
50
+ where,
51
+ data: {
52
+ workflow
53
+ }
54
+ });
55
+ }
35
56
  const rows = await tx.durableWorkflowRun.findMany({
36
57
  where
37
58
  });
@@ -59,19 +80,28 @@ var PrismaStateStore = class {
59
80
  });
60
81
  }
61
82
  async createRun(run) {
62
- await this.db.durableWorkflowRun.create({
63
- data: toRunData(run)
83
+ await this.db.durableWorkflowRun.createMany({
84
+ data: [
85
+ toRunData(run)
86
+ ]
64
87
  });
65
88
  await this.reindexAttributes(run.id, run.searchAttributes);
66
89
  }
67
90
  async updateRun(runId, inputPatch) {
68
91
  const patch = inputPatch.status && TERMINAL_RUN_STATUSES.includes(inputPatch.status) ? await clearTerminalSingletonAdmission(runId, inputPatch, (id) => this.getRun(id)) : inputPatch;
69
- await this.db.durableWorkflowRun.update({
92
+ const { count } = await this.db.durableWorkflowRun.updateMany({
70
93
  where: {
71
94
  id: runId
72
95
  },
73
96
  data: toRunPatch(patch)
74
97
  });
98
+ if (count === 0 && !await this.db.durableWorkflowRun.findUnique({
99
+ where: {
100
+ id: runId
101
+ }
102
+ })) {
103
+ throw new Error(`run ${runId} not found`);
104
+ }
75
105
  if ("searchAttributes" in patch) await this.reindexAttributes(runId, patch.searchAttributes);
76
106
  }
77
107
  async updateRunIf(runId, expectedStatuses, inputPatch) {
@@ -1510,6 +1540,23 @@ var CHECKPOINT_SELECT_WITHOUT_INPUT = {
1510
1540
  finishedAt: true
1511
1541
  };
1512
1542
  var jsonText = /* @__PURE__ */ __name((v) => v == null ? null : JSON.stringify(v), "jsonText");
1543
+ function primaryOf(db) {
1544
+ const client = db;
1545
+ return typeof client.$primary === "function" ? client.$primary() : db;
1546
+ }
1547
+ __name(primaryOf, "primaryOf");
1548
+ function rowLockDialect(db) {
1549
+ const client = db;
1550
+ if (typeof client.$queryRawUnsafe !== "function") return void 0;
1551
+ if (client._activeProvider === "postgresql") {
1552
+ 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);
1553
+ }
1554
+ if (client._activeProvider === "mysql") {
1555
+ return (tx, ids) => tx.$queryRawUnsafe(`SELECT id FROM durable_workflow_runs WHERE id IN (${ids.map(() => "?").join(", ")}) ORDER BY id FOR UPDATE`, ...ids);
1556
+ }
1557
+ return void 0;
1558
+ }
1559
+ __name(rowLockDialect, "rowLockDialect");
1513
1560
  function rawSettleDialect(db) {
1514
1561
  const client = db;
1515
1562
  if (typeof client.$queryRawUnsafe !== "function") return void 0;