@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.
@@ -1,6 +1,6 @@
1
1
  import * as drizzle_orm_pg_core from 'drizzle-orm/pg-core';
2
2
  import { PgDatabase } from 'drizzle-orm/pg-core';
3
- import { StateStore, WorkflowRun, RunStatus, RetentionPolicy, StepCheckpoint, StoreTransaction, OwnedLeaseReclaim, RunQuery, RunFacetQuery, RunFacetRow, RunValueAxis, RunValueFacetOptions, RunValueFacetRow, ScheduleRecord, ScheduleQuery, CheckpointStat, CheckpointQuery, SignalWaiter, ChildWait, BufferedSignalRow } from '@dudousxd/nestjs-durable-core';
3
+ import { StateStore, WorkflowRun, RunStatus, RetentionPolicy, StepCheckpoint, StepSettlement, StoreTransaction, OwnedLeaseReclaim, RunQuery, RunFacetQuery, RunFacetRow, RunValueAxis, RunValueFacetOptions, RunValueFacetRow, ScheduleRecord, ScheduleQuery, CheckpointStat, CheckpointQuery, SignalWaiter, ChildWait, BufferedSignalRow } from '@dudousxd/nestjs-durable-core';
4
4
  import { SQL } from 'drizzle-orm';
5
5
 
6
6
  /**
@@ -120,9 +120,13 @@ declare class DrizzlePgStateStore implements StateStore {
120
120
  deleteRuns(runIds: string[]): Promise<void>;
121
121
  pruneTerminalRuns(policy: RetentionPolicy, nowMs: number, limit: number): Promise<number>;
122
122
  getCheckpoint(runId: string, seq: number): Promise<StepCheckpoint | null>;
123
+ getCheckpoints(runId: string, seqs: number[]): Promise<StepCheckpoint[]>;
124
+ getCheckpointsForSettle(runId: string, seqs: number[]): Promise<StepCheckpoint[]>;
123
125
  saveCheckpoint(checkpoint: StepCheckpoint): Promise<void>;
126
+ saveCheckpoints(checkpoints: StepCheckpoint[]): Promise<void>;
124
127
  saveCheckpointIfPending(checkpoint: StepCheckpoint): Promise<boolean>;
125
128
  saveCheckpointsIfPending(checkpoints: StepCheckpoint[]): Promise<boolean[]>;
129
+ settleCheckpointsIfPending(settlements: StepSettlement[]): Promise<boolean[]>;
126
130
  transaction<T>(work: (tx: StoreTransaction) => Promise<T>): Promise<T>;
127
131
  /** Crash recovery. Namespace-scoped: a worker re-drives only its own tenant's runs. */
128
132
  listIncompleteRuns(namespace?: string): Promise<WorkflowRun[]>;
@@ -153,6 +157,7 @@ declare class DrizzlePgStateStore implements StateStore {
153
157
  deleteSchedule(id: string): Promise<boolean>;
154
158
  listSchedules(query: ScheduleQuery): Promise<ScheduleRecord[]>;
155
159
  listCheckpoints(runId: string): Promise<StepCheckpoint[]>;
160
+ listCheckpointsForReplay(runId: string): Promise<StepCheckpoint[]>;
156
161
  getLatestCheckpointByName(runId: string, name: string): Promise<StepCheckpoint | undefined>;
157
162
  countCheckpoints(runId: string, fromSeq: number, toSeq: number, statuses: StepCheckpoint['status'][]): Promise<number>;
158
163
  listCheckpointsByNamePrefix(runId: string, prefixes: string[]): Promise<StepCheckpoint[]>;
@@ -1,6 +1,6 @@
1
1
  import * as drizzle_orm_pg_core from 'drizzle-orm/pg-core';
2
2
  import { PgDatabase } from 'drizzle-orm/pg-core';
3
- import { StateStore, WorkflowRun, RunStatus, RetentionPolicy, StepCheckpoint, StoreTransaction, OwnedLeaseReclaim, RunQuery, RunFacetQuery, RunFacetRow, RunValueAxis, RunValueFacetOptions, RunValueFacetRow, ScheduleRecord, ScheduleQuery, CheckpointStat, CheckpointQuery, SignalWaiter, ChildWait, BufferedSignalRow } from '@dudousxd/nestjs-durable-core';
3
+ import { StateStore, WorkflowRun, RunStatus, RetentionPolicy, StepCheckpoint, StepSettlement, StoreTransaction, OwnedLeaseReclaim, RunQuery, RunFacetQuery, RunFacetRow, RunValueAxis, RunValueFacetOptions, RunValueFacetRow, ScheduleRecord, ScheduleQuery, CheckpointStat, CheckpointQuery, SignalWaiter, ChildWait, BufferedSignalRow } from '@dudousxd/nestjs-durable-core';
4
4
  import { SQL } from 'drizzle-orm';
5
5
 
6
6
  /**
@@ -120,9 +120,13 @@ declare class DrizzlePgStateStore implements StateStore {
120
120
  deleteRuns(runIds: string[]): Promise<void>;
121
121
  pruneTerminalRuns(policy: RetentionPolicy, nowMs: number, limit: number): Promise<number>;
122
122
  getCheckpoint(runId: string, seq: number): Promise<StepCheckpoint | null>;
123
+ getCheckpoints(runId: string, seqs: number[]): Promise<StepCheckpoint[]>;
124
+ getCheckpointsForSettle(runId: string, seqs: number[]): Promise<StepCheckpoint[]>;
123
125
  saveCheckpoint(checkpoint: StepCheckpoint): Promise<void>;
126
+ saveCheckpoints(checkpoints: StepCheckpoint[]): Promise<void>;
124
127
  saveCheckpointIfPending(checkpoint: StepCheckpoint): Promise<boolean>;
125
128
  saveCheckpointsIfPending(checkpoints: StepCheckpoint[]): Promise<boolean[]>;
129
+ settleCheckpointsIfPending(settlements: StepSettlement[]): Promise<boolean[]>;
126
130
  transaction<T>(work: (tx: StoreTransaction) => Promise<T>): Promise<T>;
127
131
  /** Crash recovery. Namespace-scoped: a worker re-drives only its own tenant's runs. */
128
132
  listIncompleteRuns(namespace?: string): Promise<WorkflowRun[]>;
@@ -153,6 +157,7 @@ declare class DrizzlePgStateStore implements StateStore {
153
157
  deleteSchedule(id: string): Promise<boolean>;
154
158
  listSchedules(query: ScheduleQuery): Promise<ScheduleRecord[]>;
155
159
  listCheckpoints(runId: string): Promise<StepCheckpoint[]>;
160
+ listCheckpointsForReplay(runId: string): Promise<StepCheckpoint[]>;
156
161
  getLatestCheckpointByName(runId: string, name: string): Promise<StepCheckpoint | undefined>;
157
162
  countCheckpoints(runId: string, fromSeq: number, toSeq: number, statuses: StepCheckpoint['status'][]): Promise<number>;
158
163
  listCheckpointsByNamePrefix(runId: string, prefixes: string[]): Promise<StepCheckpoint[]>;
package/dist/pg/index.js CHANGED
@@ -539,9 +539,47 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
539
539
  const rows = await this.db.select().from(durableStepCheckpoints).where(and(eq(durableStepCheckpoints.runId, runId), eq(durableStepCheckpoints.seq, seq))).limit(1);
540
540
  return rows[0] ? fromCheckpointRow(rows[0]) : null;
541
541
  }
542
+ async getCheckpoints(runId, seqs) {
543
+ const unique = [
544
+ ...new Set(seqs)
545
+ ];
546
+ const rows = [];
547
+ for (let at = 0; at < unique.length; at += 500) {
548
+ rows.push(...await this.db.select().from(durableStepCheckpoints).where(and(eq(durableStepCheckpoints.runId, runId), inArray(durableStepCheckpoints.seq, unique.slice(at, at + 500)))));
549
+ }
550
+ return rows.sort((a, b) => a.seq - b.seq).map(fromCheckpointRow);
551
+ }
552
+ async getCheckpointsForSettle(runId, seqs) {
553
+ const unique = [
554
+ ...new Set(seqs)
555
+ ];
556
+ const { input: _input, ...columns } = getTableColumns(durableStepCheckpoints);
557
+ const rows = [];
558
+ for (let at = 0; at < unique.length; at += 500) {
559
+ rows.push(...await this.db.select(columns).from(durableStepCheckpoints).where(and(eq(durableStepCheckpoints.runId, runId), inArray(durableStepCheckpoints.seq, unique.slice(at, at + 500)))));
560
+ }
561
+ return rows.sort((a, b) => a.seq - b.seq).map((row) => fromCheckpointRow({
562
+ ...row,
563
+ input: null
564
+ }));
565
+ }
542
566
  async saveCheckpoint(checkpoint) {
543
567
  await upsertCheckpoint(this.db, checkpoint);
544
568
  }
569
+ async saveCheckpoints(checkpoints) {
570
+ if (checkpoints.length === 0) return;
571
+ await this.db.transaction(async (tx) => {
572
+ for (let at = 0; at < checkpoints.length; at += CHECKPOINT_BATCH_ROWS) {
573
+ await tx.insert(durableStepCheckpoints).values(checkpoints.slice(at, at + CHECKPOINT_BATCH_ROWS).map(toCheckpointRow)).onConflictDoUpdate({
574
+ target: [
575
+ durableStepCheckpoints.runId,
576
+ durableStepCheckpoints.seq
577
+ ],
578
+ set: excludedSet(durableStepCheckpoints)
579
+ });
580
+ }
581
+ });
582
+ }
545
583
  async saveCheckpointIfPending(checkpoint) {
546
584
  const { runId, seq, ...set } = toCheckpointRow(checkpoint);
547
585
  const rows = await this.db.update(durableStepCheckpoints).set(set).where(and(eq(durableStepCheckpoints.runId, runId), eq(durableStepCheckpoints.seq, seq), eq(durableStepCheckpoints.status, "pending"))).returning({
@@ -573,6 +611,30 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
573
611
  });
574
612
  return wrote;
575
613
  }
614
+ async settleCheckpointsIfPending(settlements) {
615
+ const wrote = settlements.map(() => false);
616
+ if (settlements.length === 0) return wrote;
617
+ await this.db.transaction(async (tx) => {
618
+ for (const round of distinctRounds(settlements)) {
619
+ for (let at = 0; at < round.length; at += CHECKPOINT_BATCH_ROWS) {
620
+ const chunk = round.slice(at, at + CHECKPOINT_BATCH_ROWS);
621
+ const updated = rowsOf(await tx.execute(settleIfPendingFrom(chunk.map((i) => settlements[i]))));
622
+ const byKey = new Map(chunk.map((i) => [
623
+ rowKey(settlements[i]),
624
+ i
625
+ ]));
626
+ for (const row of updated) {
627
+ const i = byKey.get(rowKey({
628
+ runId: row.run_id,
629
+ seq: Number(row.seq)
630
+ }));
631
+ if (i !== void 0) wrote[i] = true;
632
+ }
633
+ }
634
+ }
635
+ });
636
+ return wrote;
637
+ }
576
638
  async transaction(work) {
577
639
  return this.db.transaction(async (tx) => work({
578
640
  raw: tx,
@@ -752,6 +814,23 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
752
814
  const rows = await this.db.select().from(durableStepCheckpoints).where(eq(durableStepCheckpoints.runId, runId)).orderBy(asc(durableStepCheckpoints.seq));
753
815
  return rows.map(fromCheckpointRow);
754
816
  }
817
+ async listCheckpointsForReplay(runId) {
818
+ const { input: _input, ...columns } = getTableColumns(durableStepCheckpoints);
819
+ const rows = await this.db.select(columns).from(durableStepCheckpoints).where(eq(durableStepCheckpoints.runId, runId)).orderBy(asc(durableStepCheckpoints.seq));
820
+ const open = rows.filter((row) => row.status !== "completed").map((row) => row.seq);
821
+ const inputs = /* @__PURE__ */ new Map();
822
+ for (let at = 0; at < open.length; at += 500) {
823
+ const found = await this.db.select({
824
+ seq: durableStepCheckpoints.seq,
825
+ input: durableStepCheckpoints.input
826
+ }).from(durableStepCheckpoints).where(and(eq(durableStepCheckpoints.runId, runId), inArray(durableStepCheckpoints.seq, open.slice(at, at + 500))));
827
+ for (const row of found) inputs.set(row.seq, row.input);
828
+ }
829
+ return rows.map((row) => fromCheckpointRow({
830
+ ...row,
831
+ input: inputs.get(row.seq) ?? null
832
+ }));
833
+ }
755
834
  async getLatestCheckpointByName(runId, name) {
756
835
  const rows = await this.db.select().from(durableStepCheckpoints).where(and(eq(durableStepCheckpoints.runId, runId), eq(durableStepCheckpoints.name, name))).orderBy(desc(durableStepCheckpoints.seq)).limit(1);
757
836
  return rows[0] ? fromCheckpointRow(rows[0]) : void 0;
@@ -977,6 +1056,18 @@ function updateIfPendingFrom(checkpoints) {
977
1056
  RETURNING c.run_id, c.seq`;
978
1057
  }
979
1058
  __name(updateIfPendingFrom, "updateIfPendingFrom");
1059
+ function settleIfPendingFrom(settlements) {
1060
+ const json = /* @__PURE__ */ __name((v) => v == null ? null : JSON.stringify(v), "json");
1061
+ const values = settlements.map((s) => sql3`(${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)`);
1062
+ return sql3`UPDATE ${durableStepCheckpoints} AS c SET
1063
+ status = v.status, output = v.output, error = v.error, events = v.events,
1064
+ started_at = v.started_at, finished_at = v.finished_at
1065
+ FROM (VALUES ${sql3.join(values, sql3`, `)}) AS v(run_id, seq, status, output, error, events,
1066
+ started_at, finished_at)
1067
+ WHERE c.run_id = v.run_id AND c.seq = v.seq AND c.status = 'pending'
1068
+ RETURNING c.run_id, c.seq`;
1069
+ }
1070
+ __name(settleIfPendingFrom, "settleIfPendingFrom");
980
1071
  async function upsertCheckpoint(db, cp) {
981
1072
  const row = toCheckpointRow(cp);
982
1073
  await db.insert(durableStepCheckpoints).values(row).onConflictDoUpdate({