@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/index.cjs CHANGED
@@ -397,6 +397,20 @@ var DrizzleStateStore = class {
397
397
  }
398
398
  return rows.sort((a, b) => a.seq - b.seq).map(fromCheckpointRow);
399
399
  }
400
+ async getCheckpointsForSettle(runId, seqs) {
401
+ const unique = [
402
+ ...new Set(seqs)
403
+ ];
404
+ const { input: _input, ...columns } = (0, import_drizzle_orm2.getTableColumns)(stepCheckpoints);
405
+ const rows = [];
406
+ for (let at = 0; at < unique.length; at += 500) {
407
+ rows.push(...await this.db.select(columns).from(stepCheckpoints).where((0, import_drizzle_orm2.and)((0, import_drizzle_orm2.eq)(stepCheckpoints.runId, runId), (0, import_drizzle_orm2.inArray)(stepCheckpoints.seq, unique.slice(at, at + 500)))));
408
+ }
409
+ return rows.sort((a, b) => a.seq - b.seq).map((row) => fromCheckpointRow({
410
+ ...row,
411
+ input: null
412
+ }));
413
+ }
400
414
  async saveCheckpoint(checkpoint) {
401
415
  const row = toCheckpointRow(checkpoint);
402
416
  await this.db.insert(stepCheckpoints).values(row).onConflictDoUpdate({
@@ -471,6 +485,68 @@ var DrizzleStateStore = class {
471
485
  });
472
486
  return wrote;
473
487
  }
488
+ async settleCheckpointsIfPending(settlements) {
489
+ if (settlements.length === 0) return [];
490
+ const wrote = [];
491
+ await this.db.transaction((tx) => {
492
+ const pending = /* @__PURE__ */ new Map();
493
+ const steps = [];
494
+ for (const [runId, seqs] of seqsByRun(settlements)) {
495
+ for (let at = 0; at < seqs.length; at += 400) {
496
+ const chunk = seqs.slice(at, at + 400);
497
+ steps.push(() => {
498
+ const rows = tx.select({
499
+ runId: stepCheckpoints.runId,
500
+ seq: stepCheckpoints.seq,
501
+ name: stepCheckpoints.name,
502
+ kind: stepCheckpoints.kind,
503
+ stepId: stepCheckpoints.stepId,
504
+ attempts: stepCheckpoints.attempts
505
+ }).from(stepCheckpoints).where((0, import_drizzle_orm2.and)((0, import_drizzle_orm2.eq)(stepCheckpoints.runId, runId), (0, import_drizzle_orm2.inArray)(stepCheckpoints.seq, chunk), (0, import_drizzle_orm2.eq)(stepCheckpoints.status, "pending"))).all();
506
+ const add = /* @__PURE__ */ __name((found) => {
507
+ for (const row of found) pending.set(checkpointKey(row.runId, row.seq), row);
508
+ }, "add");
509
+ return rows instanceof Promise ? rows.then(add) : add(rows);
510
+ });
511
+ }
512
+ }
513
+ steps.push(() => {
514
+ const rows = [];
515
+ for (const s of settlements) {
516
+ const key = checkpointKey(s.runId, s.seq);
517
+ const row = pending.get(key);
518
+ wrote.push(row !== void 0);
519
+ if (!row) continue;
520
+ pending.delete(key);
521
+ rows.push({
522
+ ...row,
523
+ status: s.status,
524
+ output: s.output ?? null,
525
+ error: s.error ?? null,
526
+ events: s.events ?? null,
527
+ startedAt: s.startedAt.getTime(),
528
+ finishedAt: s.finishedAt.getTime()
529
+ });
530
+ }
531
+ const writes = [];
532
+ for (let at = 0; at < rows.length; at += 100) {
533
+ const chunk = rows.slice(at, at + 100);
534
+ writes.push(() => tx.insert(stepCheckpoints).values(chunk).onConflictDoUpdate({
535
+ target: [
536
+ stepCheckpoints.runId,
537
+ stepCheckpoints.seq
538
+ ],
539
+ set: excludedSet(stepCheckpoints, [
540
+ ...SETTLED_FIELDS
541
+ ])
542
+ }).run());
543
+ }
544
+ return inSequence(writes);
545
+ });
546
+ return inSequence(steps);
547
+ });
548
+ return wrote;
549
+ }
474
550
  async transaction(work) {
475
551
  return this.db.transaction(async (tx) => work({
476
552
  raw: tx,
@@ -672,6 +748,23 @@ var DrizzleStateStore = class {
672
748
  const rows = await this.db.select().from(stepCheckpoints).where((0, import_drizzle_orm2.eq)(stepCheckpoints.runId, runId)).orderBy((0, import_drizzle_orm2.asc)(stepCheckpoints.seq));
673
749
  return rows.map(fromCheckpointRow);
674
750
  }
751
+ async listCheckpointsForReplay(runId) {
752
+ const { input: _input, ...columns } = (0, import_drizzle_orm2.getTableColumns)(stepCheckpoints);
753
+ const rows = await this.db.select(columns).from(stepCheckpoints).where((0, import_drizzle_orm2.eq)(stepCheckpoints.runId, runId)).orderBy((0, import_drizzle_orm2.asc)(stepCheckpoints.seq));
754
+ const open = rows.filter((row) => row.status !== "completed").map((row) => row.seq);
755
+ const inputs = /* @__PURE__ */ new Map();
756
+ for (let at = 0; at < open.length; at += 500) {
757
+ const found = await this.db.select({
758
+ seq: stepCheckpoints.seq,
759
+ input: stepCheckpoints.input
760
+ }).from(stepCheckpoints).where((0, import_drizzle_orm2.and)((0, import_drizzle_orm2.eq)(stepCheckpoints.runId, runId), (0, import_drizzle_orm2.inArray)(stepCheckpoints.seq, open.slice(at, at + 500))));
761
+ for (const row of found) inputs.set(row.seq, row.input);
762
+ }
763
+ return rows.map((row) => fromCheckpointRow({
764
+ ...row,
765
+ input: inputs.get(row.seq) ?? null
766
+ }));
767
+ }
675
768
  async getLatestCheckpointByName(runId, name) {
676
769
  const rows = await this.db.select().from(stepCheckpoints).where((0, import_drizzle_orm2.and)((0, import_drizzle_orm2.eq)(stepCheckpoints.runId, runId), (0, import_drizzle_orm2.eq)(stepCheckpoints.name, name))).orderBy((0, import_drizzle_orm2.desc)(stepCheckpoints.seq)).limit(1);
677
770
  return rows[0] ? fromCheckpointRow(rows[0]) : void 0;
@@ -1071,6 +1164,14 @@ function fromScheduleRow(row) {
1071
1164
  };
1072
1165
  }
1073
1166
  __name(fromScheduleRow, "fromScheduleRow");
1167
+ var SETTLED_FIELDS = [
1168
+ "status",
1169
+ "output",
1170
+ "error",
1171
+ "events",
1172
+ "startedAt",
1173
+ "finishedAt"
1174
+ ];
1074
1175
  function inSequence(steps, from = 0) {
1075
1176
  for (let i = from; i < steps.length; i += 1) {
1076
1177
  const done = steps[i]();