@dudousxd/nestjs-durable-store-drizzle 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.
@@ -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
  /**
@@ -121,10 +121,12 @@ declare class DrizzlePgStateStore implements StateStore {
121
121
  pruneTerminalRuns(policy: RetentionPolicy, nowMs: number, limit: number): Promise<number>;
122
122
  getCheckpoint(runId: string, seq: number): Promise<StepCheckpoint | null>;
123
123
  getCheckpoints(runId: string, seqs: number[]): Promise<StepCheckpoint[]>;
124
+ getCheckpointsForSettle(runId: string, seqs: number[]): Promise<StepCheckpoint[]>;
124
125
  saveCheckpoint(checkpoint: StepCheckpoint): Promise<void>;
125
126
  saveCheckpoints(checkpoints: StepCheckpoint[]): Promise<void>;
126
127
  saveCheckpointIfPending(checkpoint: StepCheckpoint): Promise<boolean>;
127
128
  saveCheckpointsIfPending(checkpoints: StepCheckpoint[]): Promise<boolean[]>;
129
+ settleCheckpointsIfPending(settlements: StepSettlement[]): Promise<boolean[]>;
128
130
  transaction<T>(work: (tx: StoreTransaction) => Promise<T>): Promise<T>;
129
131
  /** Crash recovery. Namespace-scoped: a worker re-drives only its own tenant's runs. */
130
132
  listIncompleteRuns(namespace?: string): Promise<WorkflowRun[]>;
@@ -155,7 +157,9 @@ declare class DrizzlePgStateStore implements StateStore {
155
157
  deleteSchedule(id: string): Promise<boolean>;
156
158
  listSchedules(query: ScheduleQuery): Promise<ScheduleRecord[]>;
157
159
  listCheckpoints(runId: string): Promise<StepCheckpoint[]>;
160
+ listCheckpointsForReplay(runId: string): Promise<StepCheckpoint[]>;
158
161
  getLatestCheckpointByName(runId: string, name: string): Promise<StepCheckpoint | undefined>;
162
+ hasCheckpoints(runId: string, fromSeq: number, toSeq: number, statuses: StepCheckpoint['status'][]): Promise<boolean>;
159
163
  countCheckpoints(runId: string, fromSeq: number, toSeq: number, statuses: StepCheckpoint['status'][]): Promise<number>;
160
164
  listCheckpointsByNamePrefix(runId: string, prefixes: string[]): Promise<StepCheckpoint[]>;
161
165
  /** {@link StateStore.checkpointStats}: one `GROUP BY parallel_group, status, name_kind` over the
@@ -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
  /**
@@ -121,10 +121,12 @@ declare class DrizzlePgStateStore implements StateStore {
121
121
  pruneTerminalRuns(policy: RetentionPolicy, nowMs: number, limit: number): Promise<number>;
122
122
  getCheckpoint(runId: string, seq: number): Promise<StepCheckpoint | null>;
123
123
  getCheckpoints(runId: string, seqs: number[]): Promise<StepCheckpoint[]>;
124
+ getCheckpointsForSettle(runId: string, seqs: number[]): Promise<StepCheckpoint[]>;
124
125
  saveCheckpoint(checkpoint: StepCheckpoint): Promise<void>;
125
126
  saveCheckpoints(checkpoints: StepCheckpoint[]): Promise<void>;
126
127
  saveCheckpointIfPending(checkpoint: StepCheckpoint): Promise<boolean>;
127
128
  saveCheckpointsIfPending(checkpoints: StepCheckpoint[]): Promise<boolean[]>;
129
+ settleCheckpointsIfPending(settlements: StepSettlement[]): Promise<boolean[]>;
128
130
  transaction<T>(work: (tx: StoreTransaction) => Promise<T>): Promise<T>;
129
131
  /** Crash recovery. Namespace-scoped: a worker re-drives only its own tenant's runs. */
130
132
  listIncompleteRuns(namespace?: string): Promise<WorkflowRun[]>;
@@ -155,7 +157,9 @@ declare class DrizzlePgStateStore implements StateStore {
155
157
  deleteSchedule(id: string): Promise<boolean>;
156
158
  listSchedules(query: ScheduleQuery): Promise<ScheduleRecord[]>;
157
159
  listCheckpoints(runId: string): Promise<StepCheckpoint[]>;
160
+ listCheckpointsForReplay(runId: string): Promise<StepCheckpoint[]>;
158
161
  getLatestCheckpointByName(runId: string, name: string): Promise<StepCheckpoint | undefined>;
162
+ hasCheckpoints(runId: string, fromSeq: number, toSeq: number, statuses: StepCheckpoint['status'][]): Promise<boolean>;
159
163
  countCheckpoints(runId: string, fromSeq: number, toSeq: number, statuses: StepCheckpoint['status'][]): Promise<number>;
160
164
  listCheckpointsByNamePrefix(runId: string, prefixes: string[]): Promise<StepCheckpoint[]>;
161
165
  /** {@link StateStore.checkpointStats}: one `GROUP BY parallel_group, status, name_kind` over the
package/dist/pg/index.js CHANGED
@@ -549,6 +549,20 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
549
549
  }
550
550
  return rows.sort((a, b) => a.seq - b.seq).map(fromCheckpointRow);
551
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
+ }
552
566
  async saveCheckpoint(checkpoint) {
553
567
  await upsertCheckpoint(this.db, checkpoint);
554
568
  }
@@ -597,6 +611,30 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
597
611
  });
598
612
  return wrote;
599
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
+ }
600
638
  async transaction(work) {
601
639
  return this.db.transaction(async (tx) => work({
602
640
  raw: tx,
@@ -776,10 +814,34 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
776
814
  const rows = await this.db.select().from(durableStepCheckpoints).where(eq(durableStepCheckpoints.runId, runId)).orderBy(asc(durableStepCheckpoints.seq));
777
815
  return rows.map(fromCheckpointRow);
778
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
+ }
779
834
  async getLatestCheckpointByName(runId, name) {
780
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);
781
836
  return rows[0] ? fromCheckpointRow(rows[0]) : void 0;
782
837
  }
838
+ async hasCheckpoints(runId, fromSeq, toSeq, statuses) {
839
+ if (statuses.length === 0) return false;
840
+ const rows = await this.db.select({
841
+ seq: durableStepCheckpoints.seq
842
+ }).from(durableStepCheckpoints).where(and(eq(durableStepCheckpoints.runId, runId), gte(durableStepCheckpoints.seq, fromSeq), lte(durableStepCheckpoints.seq, toSeq), inArray(durableStepCheckpoints.status, statuses))).orderBy(desc(durableStepCheckpoints.seq)).limit(1);
843
+ return rows.length > 0;
844
+ }
783
845
  async countCheckpoints(runId, fromSeq, toSeq, statuses) {
784
846
  if (statuses.length === 0) return 0;
785
847
  const rows = await this.db.select({
@@ -1001,6 +1063,18 @@ function updateIfPendingFrom(checkpoints) {
1001
1063
  RETURNING c.run_id, c.seq`;
1002
1064
  }
1003
1065
  __name(updateIfPendingFrom, "updateIfPendingFrom");
1066
+ function settleIfPendingFrom(settlements) {
1067
+ const json = /* @__PURE__ */ __name((v) => v == null ? null : JSON.stringify(v), "json");
1068
+ 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)`);
1069
+ return sql3`UPDATE ${durableStepCheckpoints} AS c SET
1070
+ status = v.status, output = v.output, error = v.error, events = v.events,
1071
+ started_at = v.started_at, finished_at = v.finished_at
1072
+ FROM (VALUES ${sql3.join(values, sql3`, `)}) AS v(run_id, seq, status, output, error, events,
1073
+ started_at, finished_at)
1074
+ WHERE c.run_id = v.run_id AND c.seq = v.seq AND c.status = 'pending'
1075
+ RETURNING c.run_id, c.seq`;
1076
+ }
1077
+ __name(settleIfPendingFrom, "settleIfPendingFrom");
1004
1078
  async function upsertCheckpoint(db, cp) {
1005
1079
  const row = toCheckpointRow(cp);
1006
1080
  await db.insert(durableStepCheckpoints).values(row).onConflictDoUpdate({