@dudousxd/nestjs-durable-store-drizzle 0.24.3 → 0.24.4

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/pg/index.cjs CHANGED
@@ -589,6 +589,20 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
589
589
  }
590
590
  return rows.sort((a, b) => a.seq - b.seq).map(fromCheckpointRow);
591
591
  }
592
+ async getCheckpointsForSettle(runId, seqs) {
593
+ const unique = [
594
+ ...new Set(seqs)
595
+ ];
596
+ const { input: _input, ...columns } = (0, import_drizzle_orm4.getTableColumns)(durableStepCheckpoints);
597
+ const rows = [];
598
+ for (let at = 0; at < unique.length; at += 500) {
599
+ rows.push(...await this.db.select(columns).from(durableStepCheckpoints).where((0, import_drizzle_orm4.and)((0, import_drizzle_orm4.eq)(durableStepCheckpoints.runId, runId), (0, import_drizzle_orm4.inArray)(durableStepCheckpoints.seq, unique.slice(at, at + 500)))));
600
+ }
601
+ return rows.sort((a, b) => a.seq - b.seq).map((row) => fromCheckpointRow({
602
+ ...row,
603
+ input: null
604
+ }));
605
+ }
592
606
  async saveCheckpoint(checkpoint) {
593
607
  await upsertCheckpoint(this.db, checkpoint);
594
608
  }
@@ -637,6 +651,30 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
637
651
  });
638
652
  return wrote;
639
653
  }
654
+ async settleCheckpointsIfPending(settlements) {
655
+ const wrote = settlements.map(() => false);
656
+ if (settlements.length === 0) return wrote;
657
+ await this.db.transaction(async (tx) => {
658
+ for (const round of distinctRounds(settlements)) {
659
+ for (let at = 0; at < round.length; at += CHECKPOINT_BATCH_ROWS) {
660
+ const chunk = round.slice(at, at + CHECKPOINT_BATCH_ROWS);
661
+ const updated = rowsOf(await tx.execute(settleIfPendingFrom(chunk.map((i) => settlements[i]))));
662
+ const byKey = new Map(chunk.map((i) => [
663
+ rowKey(settlements[i]),
664
+ i
665
+ ]));
666
+ for (const row of updated) {
667
+ const i = byKey.get(rowKey({
668
+ runId: row.run_id,
669
+ seq: Number(row.seq)
670
+ }));
671
+ if (i !== void 0) wrote[i] = true;
672
+ }
673
+ }
674
+ }
675
+ });
676
+ return wrote;
677
+ }
640
678
  async transaction(work) {
641
679
  return this.db.transaction(async (tx) => work({
642
680
  raw: tx,
@@ -816,6 +854,23 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
816
854
  const rows = await this.db.select().from(durableStepCheckpoints).where((0, import_drizzle_orm4.eq)(durableStepCheckpoints.runId, runId)).orderBy((0, import_drizzle_orm4.asc)(durableStepCheckpoints.seq));
817
855
  return rows.map(fromCheckpointRow);
818
856
  }
857
+ async listCheckpointsForReplay(runId) {
858
+ const { input: _input, ...columns } = (0, import_drizzle_orm4.getTableColumns)(durableStepCheckpoints);
859
+ const rows = await this.db.select(columns).from(durableStepCheckpoints).where((0, import_drizzle_orm4.eq)(durableStepCheckpoints.runId, runId)).orderBy((0, import_drizzle_orm4.asc)(durableStepCheckpoints.seq));
860
+ const open = rows.filter((row) => row.status !== "completed").map((row) => row.seq);
861
+ const inputs = /* @__PURE__ */ new Map();
862
+ for (let at = 0; at < open.length; at += 500) {
863
+ const found = await this.db.select({
864
+ seq: durableStepCheckpoints.seq,
865
+ input: durableStepCheckpoints.input
866
+ }).from(durableStepCheckpoints).where((0, import_drizzle_orm4.and)((0, import_drizzle_orm4.eq)(durableStepCheckpoints.runId, runId), (0, import_drizzle_orm4.inArray)(durableStepCheckpoints.seq, open.slice(at, at + 500))));
867
+ for (const row of found) inputs.set(row.seq, row.input);
868
+ }
869
+ return rows.map((row) => fromCheckpointRow({
870
+ ...row,
871
+ input: inputs.get(row.seq) ?? null
872
+ }));
873
+ }
819
874
  async getLatestCheckpointByName(runId, name) {
820
875
  const rows = await this.db.select().from(durableStepCheckpoints).where((0, import_drizzle_orm4.and)((0, import_drizzle_orm4.eq)(durableStepCheckpoints.runId, runId), (0, import_drizzle_orm4.eq)(durableStepCheckpoints.name, name))).orderBy((0, import_drizzle_orm4.desc)(durableStepCheckpoints.seq)).limit(1);
821
876
  return rows[0] ? fromCheckpointRow(rows[0]) : void 0;
@@ -1041,6 +1096,18 @@ function updateIfPendingFrom(checkpoints) {
1041
1096
  RETURNING c.run_id, c.seq`;
1042
1097
  }
1043
1098
  __name(updateIfPendingFrom, "updateIfPendingFrom");
1099
+ function settleIfPendingFrom(settlements) {
1100
+ const json = /* @__PURE__ */ __name((v) => v == null ? null : JSON.stringify(v), "json");
1101
+ const values = settlements.map((s) => import_drizzle_orm4.sql`(${s.runId}::text, ${s.seq}::integer, ${s.status}::text, ${json(s.output)}::jsonb, ${json(s.error)}::jsonb, ${json(s.events)}::jsonb, ${s.startedAt.toISOString()}::timestamptz, ${s.finishedAt.toISOString()}::timestamptz)`);
1102
+ return import_drizzle_orm4.sql`UPDATE ${durableStepCheckpoints} AS c SET
1103
+ status = v.status, output = v.output, error = v.error, events = v.events,
1104
+ started_at = v.started_at, finished_at = v.finished_at
1105
+ FROM (VALUES ${import_drizzle_orm4.sql.join(values, import_drizzle_orm4.sql`, `)}) AS v(run_id, seq, status, output, error, events,
1106
+ started_at, finished_at)
1107
+ WHERE c.run_id = v.run_id AND c.seq = v.seq AND c.status = 'pending'
1108
+ RETURNING c.run_id, c.seq`;
1109
+ }
1110
+ __name(settleIfPendingFrom, "settleIfPendingFrom");
1044
1111
  async function upsertCheckpoint(db, cp) {
1045
1112
  const row = toCheckpointRow(cp);
1046
1113
  await db.insert(durableStepCheckpoints).values(row).onConflictDoUpdate({