@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.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { StateStore, WorkflowRun, RunStatus, RetentionPolicy, StepCheckpoint, OwnedLeaseReclaim, RunQuery, RunFacetQuery, RunFacetRow, RunValueAxis, RunValueFacetOptions, RunValueFacetRow, ScheduleRecord, ScheduleQuery, CheckpointStat, CheckpointQuery, SignalWaiter, ChildWait, BufferedSignalRow } from '@dudousxd/nestjs-durable-core';
1
+ import { StateStore, WorkflowRun, RunStatus, RetentionPolicy, StepCheckpoint, StepSettlement, OwnedLeaseReclaim, RunQuery, RunFacetQuery, RunFacetRow, RunValueAxis, RunValueFacetOptions, RunValueFacetRow, ScheduleRecord, ScheduleQuery, CheckpointStat, CheckpointQuery, SignalWaiter, ChildWait, BufferedSignalRow } from '@dudousxd/nestjs-durable-core';
2
2
  import * as drizzle_orm_sqlite_core from 'drizzle-orm/sqlite-core';
3
3
  import { BaseSQLiteDatabase } from 'drizzle-orm/sqlite-core';
4
4
 
@@ -25,10 +25,12 @@ declare class DrizzleStateStore implements StateStore {
25
25
  pruneTerminalRuns(policy: RetentionPolicy, nowMs: number, limit: number): Promise<number>;
26
26
  getCheckpoint(runId: string, seq: number): Promise<StepCheckpoint | null>;
27
27
  getCheckpoints(runId: string, seqs: number[]): Promise<StepCheckpoint[]>;
28
+ getCheckpointsForSettle(runId: string, seqs: number[]): Promise<StepCheckpoint[]>;
28
29
  saveCheckpoint(checkpoint: StepCheckpoint): Promise<void>;
29
30
  saveCheckpoints(checkpoints: StepCheckpoint[]): Promise<void>;
30
31
  saveCheckpointIfPending(checkpoint: StepCheckpoint): Promise<boolean>;
31
32
  saveCheckpointsIfPending(checkpoints: StepCheckpoint[]): Promise<boolean[]>;
33
+ settleCheckpointsIfPending(settlements: StepSettlement[]): Promise<boolean[]>;
32
34
  transaction<T>(work: (tx: {
33
35
  raw: unknown;
34
36
  saveCheckpoint: (cp: StepCheckpoint) => Promise<void>;
@@ -70,7 +72,9 @@ declare class DrizzleStateStore implements StateStore {
70
72
  deleteSchedule(id: string): Promise<boolean>;
71
73
  listSchedules(query: ScheduleQuery): Promise<ScheduleRecord[]>;
72
74
  listCheckpoints(runId: string): Promise<StepCheckpoint[]>;
75
+ listCheckpointsForReplay(runId: string): Promise<StepCheckpoint[]>;
73
76
  getLatestCheckpointByName(runId: string, name: string): Promise<StepCheckpoint | undefined>;
77
+ hasCheckpoints(runId: string, fromSeq: number, toSeq: number, statuses: StepCheckpoint['status'][]): Promise<boolean>;
74
78
  countCheckpoints(runId: string, fromSeq: number, toSeq: number, statuses: StepCheckpoint['status'][]): Promise<number>;
75
79
  listCheckpointsByNamePrefix(runId: string, prefixes: string[]): Promise<StepCheckpoint[]>;
76
80
  /** {@link StateStore.checkpointStats}: one `GROUP BY parallel_group, status, name_kind` over the
package/dist/index.js CHANGED
@@ -363,6 +363,20 @@ var DrizzleStateStore = class {
363
363
  }
364
364
  return rows.sort((a, b) => a.seq - b.seq).map(fromCheckpointRow);
365
365
  }
366
+ async getCheckpointsForSettle(runId, seqs) {
367
+ const unique = [
368
+ ...new Set(seqs)
369
+ ];
370
+ const { input: _input, ...columns } = getTableColumns(stepCheckpoints);
371
+ const rows = [];
372
+ for (let at = 0; at < unique.length; at += 500) {
373
+ rows.push(...await this.db.select(columns).from(stepCheckpoints).where(and(eq(stepCheckpoints.runId, runId), inArray(stepCheckpoints.seq, unique.slice(at, at + 500)))));
374
+ }
375
+ return rows.sort((a, b) => a.seq - b.seq).map((row) => fromCheckpointRow({
376
+ ...row,
377
+ input: null
378
+ }));
379
+ }
366
380
  async saveCheckpoint(checkpoint) {
367
381
  const row = toCheckpointRow(checkpoint);
368
382
  await this.db.insert(stepCheckpoints).values(row).onConflictDoUpdate({
@@ -437,6 +451,68 @@ var DrizzleStateStore = class {
437
451
  });
438
452
  return wrote;
439
453
  }
454
+ async settleCheckpointsIfPending(settlements) {
455
+ if (settlements.length === 0) return [];
456
+ const wrote = [];
457
+ await this.db.transaction((tx) => {
458
+ const pending = /* @__PURE__ */ new Map();
459
+ const steps = [];
460
+ for (const [runId, seqs] of seqsByRun(settlements)) {
461
+ for (let at = 0; at < seqs.length; at += 400) {
462
+ const chunk = seqs.slice(at, at + 400);
463
+ steps.push(() => {
464
+ const rows = tx.select({
465
+ runId: stepCheckpoints.runId,
466
+ seq: stepCheckpoints.seq,
467
+ name: stepCheckpoints.name,
468
+ kind: stepCheckpoints.kind,
469
+ stepId: stepCheckpoints.stepId,
470
+ attempts: stepCheckpoints.attempts
471
+ }).from(stepCheckpoints).where(and(eq(stepCheckpoints.runId, runId), inArray(stepCheckpoints.seq, chunk), eq(stepCheckpoints.status, "pending"))).all();
472
+ const add = /* @__PURE__ */ __name((found) => {
473
+ for (const row of found) pending.set(checkpointKey(row.runId, row.seq), row);
474
+ }, "add");
475
+ return rows instanceof Promise ? rows.then(add) : add(rows);
476
+ });
477
+ }
478
+ }
479
+ steps.push(() => {
480
+ const rows = [];
481
+ for (const s of settlements) {
482
+ const key = checkpointKey(s.runId, s.seq);
483
+ const row = pending.get(key);
484
+ wrote.push(row !== void 0);
485
+ if (!row) continue;
486
+ pending.delete(key);
487
+ rows.push({
488
+ ...row,
489
+ status: s.status,
490
+ output: s.output ?? null,
491
+ error: s.error ?? null,
492
+ events: s.events ?? null,
493
+ startedAt: s.startedAt.getTime(),
494
+ finishedAt: s.finishedAt.getTime()
495
+ });
496
+ }
497
+ const writes = [];
498
+ for (let at = 0; at < rows.length; at += 100) {
499
+ const chunk = rows.slice(at, at + 100);
500
+ writes.push(() => tx.insert(stepCheckpoints).values(chunk).onConflictDoUpdate({
501
+ target: [
502
+ stepCheckpoints.runId,
503
+ stepCheckpoints.seq
504
+ ],
505
+ set: excludedSet(stepCheckpoints, [
506
+ ...SETTLED_FIELDS
507
+ ])
508
+ }).run());
509
+ }
510
+ return inSequence(writes);
511
+ });
512
+ return inSequence(steps);
513
+ });
514
+ return wrote;
515
+ }
440
516
  async transaction(work) {
441
517
  return this.db.transaction(async (tx) => work({
442
518
  raw: tx,
@@ -638,10 +714,34 @@ var DrizzleStateStore = class {
638
714
  const rows = await this.db.select().from(stepCheckpoints).where(eq(stepCheckpoints.runId, runId)).orderBy(asc(stepCheckpoints.seq));
639
715
  return rows.map(fromCheckpointRow);
640
716
  }
717
+ async listCheckpointsForReplay(runId) {
718
+ const { input: _input, ...columns } = getTableColumns(stepCheckpoints);
719
+ const rows = await this.db.select(columns).from(stepCheckpoints).where(eq(stepCheckpoints.runId, runId)).orderBy(asc(stepCheckpoints.seq));
720
+ const open = rows.filter((row) => row.status !== "completed").map((row) => row.seq);
721
+ const inputs = /* @__PURE__ */ new Map();
722
+ for (let at = 0; at < open.length; at += 500) {
723
+ const found = await this.db.select({
724
+ seq: stepCheckpoints.seq,
725
+ input: stepCheckpoints.input
726
+ }).from(stepCheckpoints).where(and(eq(stepCheckpoints.runId, runId), inArray(stepCheckpoints.seq, open.slice(at, at + 500))));
727
+ for (const row of found) inputs.set(row.seq, row.input);
728
+ }
729
+ return rows.map((row) => fromCheckpointRow({
730
+ ...row,
731
+ input: inputs.get(row.seq) ?? null
732
+ }));
733
+ }
641
734
  async getLatestCheckpointByName(runId, name) {
642
735
  const rows = await this.db.select().from(stepCheckpoints).where(and(eq(stepCheckpoints.runId, runId), eq(stepCheckpoints.name, name))).orderBy(desc(stepCheckpoints.seq)).limit(1);
643
736
  return rows[0] ? fromCheckpointRow(rows[0]) : void 0;
644
737
  }
738
+ async hasCheckpoints(runId, fromSeq, toSeq, statuses) {
739
+ if (statuses.length === 0) return false;
740
+ const rows = await this.db.select({
741
+ seq: stepCheckpoints.seq
742
+ }).from(stepCheckpoints).where(and(eq(stepCheckpoints.runId, runId), gte(stepCheckpoints.seq, fromSeq), lte(stepCheckpoints.seq, toSeq), inArray(stepCheckpoints.status, statuses))).orderBy(desc(stepCheckpoints.seq)).limit(1);
743
+ return rows.length > 0;
744
+ }
645
745
  async countCheckpoints(runId, fromSeq, toSeq, statuses) {
646
746
  if (statuses.length === 0) return 0;
647
747
  const rows = await this.db.select({
@@ -1037,6 +1137,14 @@ function fromScheduleRow(row) {
1037
1137
  };
1038
1138
  }
1039
1139
  __name(fromScheduleRow, "fromScheduleRow");
1140
+ var SETTLED_FIELDS = [
1141
+ "status",
1142
+ "output",
1143
+ "error",
1144
+ "events",
1145
+ "startedAt",
1146
+ "finishedAt"
1147
+ ];
1040
1148
  function inSequence(steps, from = 0) {
1041
1149
  for (let i = from; i < steps.length; i += 1) {
1042
1150
  const done = steps[i]();