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

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
@@ -271,6 +271,27 @@ var PrismaStateStore = class {
271
271
  }
272
272
  return rows.sort((a, b) => a.seq - b.seq).map(fromCheckpointRow);
273
273
  }
274
+ async getCheckpointsForSettle(runId, seqs) {
275
+ const unique = [
276
+ ...new Set(seqs)
277
+ ];
278
+ const rows = [];
279
+ for (let at = 0; at < unique.length; at += 500) {
280
+ rows.push(...await this.db.durableStepCheckpoint.findMany({
281
+ where: {
282
+ runId,
283
+ seq: {
284
+ in: unique.slice(at, at + 500)
285
+ }
286
+ },
287
+ select: CHECKPOINT_SELECT_WITHOUT_INPUT
288
+ }));
289
+ }
290
+ return rows.sort((a, b) => a.seq - b.seq).map((row) => fromCheckpointRow({
291
+ ...row,
292
+ input: null
293
+ }));
294
+ }
274
295
  async saveCheckpoint(checkpoint) {
275
296
  const data = toCheckpointData(checkpoint);
276
297
  await this.db.durableStepCheckpoint.upsert({
@@ -344,6 +365,57 @@ var PrismaStateStore = class {
344
365
  return wrote;
345
366
  });
346
367
  }
368
+ async settleCheckpointsIfPending(settlements) {
369
+ const wrote = settlements.map(() => false);
370
+ if (settlements.length === 0) return wrote;
371
+ const seen = /* @__PURE__ */ new Set();
372
+ const firsts = [];
373
+ settlements.forEach((s, i) => {
374
+ const key = checkpointKey(s.runId, s.seq);
375
+ if (seen.has(key)) return;
376
+ seen.add(key);
377
+ firsts.push(i);
378
+ });
379
+ const dialect = rawSettleDialect(this.db);
380
+ if (!dialect) {
381
+ const settle = /* @__PURE__ */ __name(async (db) => {
382
+ for (const i of firsts) {
383
+ const s = settlements[i];
384
+ const { count } = await db.durableStepCheckpoint.updateMany({
385
+ where: {
386
+ runId: s.runId,
387
+ seq: s.seq,
388
+ status: "pending"
389
+ },
390
+ data: {
391
+ status: s.status,
392
+ output: jsonOrNull(s.output),
393
+ error: jsonOrNull(s.error),
394
+ events: jsonOrNull(s.events),
395
+ startedAt: s.startedAt,
396
+ finishedAt: s.finishedAt
397
+ }
398
+ });
399
+ wrote[i] = count > 0;
400
+ }
401
+ }, "settle");
402
+ if (firsts.length === 1) await settle(this.db);
403
+ else await this.db.$transaction((tx) => settle(tx));
404
+ return wrote;
405
+ }
406
+ await this.db.$transaction(async (tx) => {
407
+ const raw = tx;
408
+ for (let at = 0; at < firsts.length; at += 500) {
409
+ const chunk = firsts.slice(at, at + 500);
410
+ const won = await dialect(raw, chunk.map((i) => settlements[i]));
411
+ for (const i of chunk) {
412
+ const s = settlements[i];
413
+ wrote[i] = won.has(checkpointKey(s.runId, s.seq));
414
+ }
415
+ }
416
+ });
417
+ return wrote;
418
+ }
347
419
  async saveCheckpoints(checkpoints) {
348
420
  if (checkpoints.length === 0) return;
349
421
  await this.db.$transaction((tx) => replaceCheckpoints(tx, checkpoints));
@@ -711,6 +783,38 @@ var PrismaStateStore = class {
711
783
  });
712
784
  return rows.map(fromCheckpointRow);
713
785
  }
786
+ async listCheckpointsForReplay(runId) {
787
+ const rows = await this.db.durableStepCheckpoint.findMany({
788
+ where: {
789
+ runId
790
+ },
791
+ orderBy: {
792
+ seq: "asc"
793
+ },
794
+ select: CHECKPOINT_SELECT_WITHOUT_INPUT
795
+ });
796
+ const open = rows.filter((row) => row.status !== "completed").map((row) => row.seq);
797
+ const inputs = /* @__PURE__ */ new Map();
798
+ for (let at = 0; at < open.length; at += 500) {
799
+ const found = await this.db.durableStepCheckpoint.findMany({
800
+ where: {
801
+ runId,
802
+ seq: {
803
+ in: open.slice(at, at + 500)
804
+ }
805
+ },
806
+ select: {
807
+ seq: true,
808
+ input: true
809
+ }
810
+ });
811
+ for (const row of found) inputs.set(row.seq, row.input);
812
+ }
813
+ return rows.map((row) => fromCheckpointRow({
814
+ ...row,
815
+ input: inputs.get(row.seq) ?? null
816
+ }));
817
+ }
714
818
  async getLatestCheckpointByName(runId, name) {
715
819
  const row = await this.db.durableStepCheckpoint.findFirst({
716
820
  where: {
@@ -723,6 +827,28 @@ var PrismaStateStore = class {
723
827
  });
724
828
  return row ? fromCheckpointRow(row) : void 0;
725
829
  }
830
+ async hasCheckpoints(runId, fromSeq, toSeq, statuses) {
831
+ if (statuses.length === 0) return false;
832
+ const row = await this.db.durableStepCheckpoint.findFirst({
833
+ where: {
834
+ runId,
835
+ seq: {
836
+ gte: fromSeq,
837
+ lte: toSeq
838
+ },
839
+ status: {
840
+ in: statuses
841
+ }
842
+ },
843
+ select: {
844
+ seq: true
845
+ },
846
+ orderBy: {
847
+ seq: "desc"
848
+ }
849
+ });
850
+ return row !== null;
851
+ }
726
852
  async countCheckpoints(runId, fromSeq, toSeq, statuses) {
727
853
  if (statuses.length === 0) return 0;
728
854
  return this.db.durableStepCheckpoint.count({
@@ -1365,6 +1491,89 @@ async function replaceCheckpoints(tx, checkpoints) {
1365
1491
  }
1366
1492
  }
1367
1493
  __name(replaceCheckpoints, "replaceCheckpoints");
1494
+ var CHECKPOINT_SELECT_WITHOUT_INPUT = {
1495
+ runId: true,
1496
+ seq: true,
1497
+ name: true,
1498
+ kind: true,
1499
+ stepId: true,
1500
+ status: true,
1501
+ output: true,
1502
+ error: true,
1503
+ events: true,
1504
+ attempts: true,
1505
+ workerGroup: true,
1506
+ parallelGroup: true,
1507
+ wakeAt: true,
1508
+ enqueuedAt: true,
1509
+ startedAt: true,
1510
+ finishedAt: true
1511
+ };
1512
+ var jsonText = /* @__PURE__ */ __name((v) => v == null ? null : JSON.stringify(v), "jsonText");
1513
+ function rawSettleDialect(db) {
1514
+ const client = db;
1515
+ if (typeof client.$queryRawUnsafe !== "function") return void 0;
1516
+ if (client._activeProvider === "postgresql") return settleRowsPostgres;
1517
+ if (client._activeProvider === "mysql") return settleRowsMysql;
1518
+ if (client._activeProvider === "sqlite") return settleRowsSqlite;
1519
+ return void 0;
1520
+ }
1521
+ __name(rawSettleDialect, "rawSettleDialect");
1522
+ var settleRowsPostgres = /* @__PURE__ */ __name(async (db, settlements) => {
1523
+ const values = [];
1524
+ const rows = settlements.map((s) => {
1525
+ const n = values.length;
1526
+ values.push(s.runId, s.seq, s.status, jsonText(s.output), jsonText(s.error), jsonText(s.events), s.startedAt.toISOString(), s.finishedAt.toISOString());
1527
+ const p = /* @__PURE__ */ __name((k) => `$${n + k}`, "p");
1528
+ return `(${p(1)}::text, ${p(2)}::integer, ${p(3)}::text, ${p(4)}::jsonb, ${p(5)}::jsonb, ${p(6)}::jsonb, (${p(7)}::timestamptz AT TIME ZONE 'UTC'), (${p(8)}::timestamptz AT TIME ZONE 'UTC'))`;
1529
+ });
1530
+ const written = await db.$queryRawUnsafe(`UPDATE "durable_step_checkpoints" AS c SET status = v.status, output = v.output,
1531
+ error = v.error, events = v.events, started_at = v.started_at, finished_at = v.finished_at
1532
+ FROM (VALUES ${rows.join(", ")}) AS v(run_id, seq, status, output, error, events, started_at,
1533
+ finished_at)
1534
+ WHERE c.run_id = v.run_id AND c.seq = v.seq AND c.status = 'pending'
1535
+ RETURNING c.run_id, c.seq`, ...values);
1536
+ return new Set(written.map((row) => checkpointKey(row.run_id, Number(row.seq))));
1537
+ }, "settleRowsPostgres");
1538
+ var settleRowsMysql = /* @__PURE__ */ __name(async (db, settlements) => {
1539
+ const pending = /* @__PURE__ */ new Set();
1540
+ for (const [runId, seqs] of seqsByRun(settlements)) {
1541
+ const rows2 = await db.$queryRawUnsafe(`SELECT run_id, seq FROM durable_step_checkpoints WHERE run_id = ? AND seq IN (${seqs.map(() => "?").join(", ")}) AND status = 'pending' FOR UPDATE`, runId, ...seqs);
1542
+ for (const row of rows2) pending.add(checkpointKey(row.run_id, Number(row.seq)));
1543
+ }
1544
+ const won = settlements.filter((s) => pending.has(checkpointKey(s.runId, s.seq)));
1545
+ if (won.length === 0) return pending;
1546
+ const at = /* @__PURE__ */ __name((d) => d.toISOString().replace("T", " ").replace("Z", ""), "at");
1547
+ const values = [];
1548
+ const rows = won.map((s, i) => {
1549
+ values.push(s.runId, s.seq, s.status, jsonText(s.output), jsonText(s.error), jsonText(s.events), at(s.startedAt), at(s.finishedAt));
1550
+ return i === 0 ? "SELECT ? AS run_id, ? AS seq, ? AS status, ? AS output, ? AS error, ? AS events, ? AS started_at, ? AS finished_at" : "SELECT ?, ?, ?, ?, ?, ?, ?, ?";
1551
+ });
1552
+ const updated = await db.$executeRawUnsafe(`UPDATE durable_step_checkpoints AS c JOIN (${rows.join(" UNION ALL ")}) AS v
1553
+ ON c.run_id = v.run_id AND c.seq = v.seq
1554
+ SET c.status = v.status, c.output = CAST(v.output AS JSON), c.error = CAST(v.error AS JSON),
1555
+ c.events = CAST(v.events AS JSON), c.started_at = v.started_at, c.finished_at = v.finished_at
1556
+ WHERE c.status = 'pending'`, ...values);
1557
+ if (updated !== won.length) {
1558
+ throw new Error("a checkpoint of the batch was settled while the batch was written");
1559
+ }
1560
+ return pending;
1561
+ }, "settleRowsMysql");
1562
+ var settleRowsSqlite = /* @__PURE__ */ __name(async (db, settlements) => {
1563
+ const values = [];
1564
+ const rows = settlements.map((s) => {
1565
+ values.push(s.runId, s.seq, s.status, jsonText(s.output), jsonText(s.error), jsonText(s.events), s.startedAt.getTime(), s.finishedAt.getTime());
1566
+ return "(?, ?, ?, ?, ?, ?, ?, ?)";
1567
+ });
1568
+ const written = await db.$queryRawUnsafe(`WITH v(run_id, seq, status, output, error, events, started_at, finished_at) AS (VALUES ${rows.join(", ")})
1569
+ UPDATE durable_step_checkpoints SET status = v.status, output = v.output, error = v.error,
1570
+ events = v.events, started_at = v.started_at, finished_at = v.finished_at
1571
+ FROM v
1572
+ WHERE durable_step_checkpoints.run_id = v.run_id AND durable_step_checkpoints.seq = v.seq
1573
+ AND durable_step_checkpoints.status = 'pending'
1574
+ RETURNING run_id, seq`, ...values);
1575
+ return new Set(written.map((row) => checkpointKey(row.run_id, Number(row.seq))));
1576
+ }, "settleRowsSqlite");
1368
1577
  function toCheckpointData(cp) {
1369
1578
  return {
1370
1579
  runId: cp.runId,