@dudousxd/nestjs-durable-store-drizzle 0.24.2 → 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
@@ -579,9 +579,47 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
579
579
  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.seq, seq))).limit(1);
580
580
  return rows[0] ? fromCheckpointRow(rows[0]) : null;
581
581
  }
582
+ async getCheckpoints(runId, seqs) {
583
+ const unique = [
584
+ ...new Set(seqs)
585
+ ];
586
+ const rows = [];
587
+ for (let at = 0; at < unique.length; at += 500) {
588
+ rows.push(...await this.db.select().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)))));
589
+ }
590
+ return rows.sort((a, b) => a.seq - b.seq).map(fromCheckpointRow);
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
+ }
582
606
  async saveCheckpoint(checkpoint) {
583
607
  await upsertCheckpoint(this.db, checkpoint);
584
608
  }
609
+ async saveCheckpoints(checkpoints) {
610
+ if (checkpoints.length === 0) return;
611
+ await this.db.transaction(async (tx) => {
612
+ for (let at = 0; at < checkpoints.length; at += CHECKPOINT_BATCH_ROWS) {
613
+ await tx.insert(durableStepCheckpoints).values(checkpoints.slice(at, at + CHECKPOINT_BATCH_ROWS).map(toCheckpointRow)).onConflictDoUpdate({
614
+ target: [
615
+ durableStepCheckpoints.runId,
616
+ durableStepCheckpoints.seq
617
+ ],
618
+ set: excludedSet(durableStepCheckpoints)
619
+ });
620
+ }
621
+ });
622
+ }
585
623
  async saveCheckpointIfPending(checkpoint) {
586
624
  const { runId, seq, ...set } = toCheckpointRow(checkpoint);
587
625
  const rows = await this.db.update(durableStepCheckpoints).set(set).where((0, import_drizzle_orm4.and)((0, import_drizzle_orm4.eq)(durableStepCheckpoints.runId, runId), (0, import_drizzle_orm4.eq)(durableStepCheckpoints.seq, seq), (0, import_drizzle_orm4.eq)(durableStepCheckpoints.status, "pending"))).returning({
@@ -613,6 +651,30 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
613
651
  });
614
652
  return wrote;
615
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
+ }
616
678
  async transaction(work) {
617
679
  return this.db.transaction(async (tx) => work({
618
680
  raw: tx,
@@ -792,6 +854,23 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
792
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));
793
855
  return rows.map(fromCheckpointRow);
794
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
+ }
795
874
  async getLatestCheckpointByName(runId, name) {
796
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);
797
876
  return rows[0] ? fromCheckpointRow(rows[0]) : void 0;
@@ -1017,6 +1096,18 @@ function updateIfPendingFrom(checkpoints) {
1017
1096
  RETURNING c.run_id, c.seq`;
1018
1097
  }
1019
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");
1020
1111
  async function upsertCheckpoint(db, cp) {
1021
1112
  const row = toCheckpointRow(cp);
1022
1113
  await db.insert(durableStepCheckpoints).values(row).onConflictDoUpdate({