@dudousxd/nestjs-durable-store-prisma 0.24.6 → 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
  });
@@ -1544,6 +1565,23 @@ var CHECKPOINT_SELECT_WITHOUT_INPUT = {
1544
1565
  finishedAt: true
1545
1566
  };
1546
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");
1547
1585
  function rawSettleDialect(db) {
1548
1586
  const client = db;
1549
1587
  if (typeof client.$queryRawUnsafe !== "function") return void 0;