@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.cjs CHANGED
@@ -44,19 +44,40 @@ var PrismaStateStore = class {
44
44
  this.db = db;
45
45
  }
46
46
  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({
47
+ const where = {
48
+ workflow,
49
+ status: {
50
+ in: import_nestjs_durable_core.SINGLETON_ACTIVE_STATUSES
51
+ }
52
+ };
53
+ const lockRows = rowLockDialect(this.db);
54
+ const claimants = lockRows ? (0, import_nestjs_durable_core.singletonClaimantIds)(
55
+ // The primary, under the read-replicas extension: a lagging replica would leave rows out.
56
+ await primaryOf(this.db).durableWorkflowRun.findMany({
55
57
  where,
56
- data: {
57
- workflow
58
+ select: {
59
+ id: true,
60
+ tags: true
61
+ },
62
+ orderBy: {
63
+ id: "asc"
58
64
  }
59
- });
65
+ }),
66
+ tag
67
+ ) : [];
68
+ return this.db.$transaction(async (tx) => {
69
+ if (lockRows) {
70
+ for (let at = 0; at < claimants.length; at += import_nestjs_durable_core.SINGLETON_LOCK_CHUNK) {
71
+ await lockRows(tx, claimants.slice(at, at + import_nestjs_durable_core.SINGLETON_LOCK_CHUNK));
72
+ }
73
+ } else {
74
+ await tx.durableWorkflowRun.updateMany({
75
+ where,
76
+ data: {
77
+ workflow
78
+ }
79
+ });
80
+ }
60
81
  const rows = await tx.durableWorkflowRun.findMany({
61
82
  where
62
83
  });
@@ -84,19 +105,28 @@ var PrismaStateStore = class {
84
105
  });
85
106
  }
86
107
  async createRun(run) {
87
- await this.db.durableWorkflowRun.create({
88
- data: toRunData(run)
108
+ await this.db.durableWorkflowRun.createMany({
109
+ data: [
110
+ toRunData(run)
111
+ ]
89
112
  });
90
113
  await this.reindexAttributes(run.id, run.searchAttributes);
91
114
  }
92
115
  async updateRun(runId, inputPatch) {
93
116
  const patch = inputPatch.status && import_nestjs_durable_core.TERMINAL_RUN_STATUSES.includes(inputPatch.status) ? await (0, import_nestjs_durable_core.clearTerminalSingletonAdmission)(runId, inputPatch, (id) => this.getRun(id)) : inputPatch;
94
- await this.db.durableWorkflowRun.update({
117
+ const { count } = await this.db.durableWorkflowRun.updateMany({
95
118
  where: {
96
119
  id: runId
97
120
  },
98
121
  data: toRunPatch(patch)
99
122
  });
123
+ if (count === 0 && !await this.db.durableWorkflowRun.findUnique({
124
+ where: {
125
+ id: runId
126
+ }
127
+ })) {
128
+ throw new Error(`run ${runId} not found`);
129
+ }
100
130
  if ("searchAttributes" in patch) await this.reindexAttributes(runId, patch.searchAttributes);
101
131
  }
102
132
  async updateRunIf(runId, expectedStatuses, inputPatch) {
@@ -1535,6 +1565,23 @@ var CHECKPOINT_SELECT_WITHOUT_INPUT = {
1535
1565
  finishedAt: true
1536
1566
  };
1537
1567
  var jsonText = /* @__PURE__ */ __name((v) => v == null ? null : JSON.stringify(v), "jsonText");
1568
+ function primaryOf(db) {
1569
+ const client = db;
1570
+ return typeof client.$primary === "function" ? client.$primary() : db;
1571
+ }
1572
+ __name(primaryOf, "primaryOf");
1573
+ function rowLockDialect(db) {
1574
+ const client = db;
1575
+ if (typeof client.$queryRawUnsafe !== "function") return void 0;
1576
+ if (client._activeProvider === "postgresql") {
1577
+ 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);
1578
+ }
1579
+ if (client._activeProvider === "mysql") {
1580
+ return (tx, ids) => tx.$queryRawUnsafe(`SELECT id FROM durable_workflow_runs WHERE id IN (${ids.map(() => "?").join(", ")}) ORDER BY id FOR UPDATE`, ...ids);
1581
+ }
1582
+ return void 0;
1583
+ }
1584
+ __name(rowLockDialect, "rowLockDialect");
1538
1585
  function rawSettleDialect(db) {
1539
1586
  const client = db;
1540
1587
  if (typeof client.$queryRawUnsafe !== "function") return void 0;