@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/index.js CHANGED
@@ -353,6 +353,30 @@ var DrizzleStateStore = class {
353
353
  const rows = await this.db.select().from(stepCheckpoints).where(and(eq(stepCheckpoints.runId, runId), eq(stepCheckpoints.seq, seq))).limit(1);
354
354
  return rows[0] ? fromCheckpointRow(rows[0]) : null;
355
355
  }
356
+ async getCheckpoints(runId, seqs) {
357
+ const unique = [
358
+ ...new Set(seqs)
359
+ ];
360
+ const rows = [];
361
+ for (let at = 0; at < unique.length; at += 500) {
362
+ rows.push(...await this.db.select().from(stepCheckpoints).where(and(eq(stepCheckpoints.runId, runId), inArray(stepCheckpoints.seq, unique.slice(at, at + 500)))));
363
+ }
364
+ return rows.sort((a, b) => a.seq - b.seq).map(fromCheckpointRow);
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
+ }
356
380
  async saveCheckpoint(checkpoint) {
357
381
  const row = toCheckpointRow(checkpoint);
358
382
  await this.db.insert(stepCheckpoints).values(row).onConflictDoUpdate({
@@ -363,6 +387,23 @@ var DrizzleStateStore = class {
363
387
  set: row
364
388
  });
365
389
  }
390
+ async saveCheckpoints(checkpoints) {
391
+ if (checkpoints.length === 0) return;
392
+ await this.db.transaction((tx) => {
393
+ const runFrom = /* @__PURE__ */ __name((at) => {
394
+ if (at >= checkpoints.length) return void 0;
395
+ const done = tx.insert(stepCheckpoints).values(checkpoints.slice(at, at + 100).map(toCheckpointRow)).onConflictDoUpdate({
396
+ target: [
397
+ stepCheckpoints.runId,
398
+ stepCheckpoints.seq
399
+ ],
400
+ set: excludedSet(stepCheckpoints)
401
+ }).run();
402
+ return done instanceof Promise ? done.then(() => runFrom(at + 100)) : runFrom(at + 100);
403
+ }, "runFrom");
404
+ return runFrom(0);
405
+ });
406
+ }
366
407
  async saveCheckpointIfPending(checkpoint) {
367
408
  const { runId, seq, ...set } = toCheckpointRow(checkpoint);
368
409
  const result = await this.db.update(stepCheckpoints).set(set).where(and(eq(stepCheckpoints.runId, runId), eq(stepCheckpoints.seq, seq), eq(stepCheckpoints.status, "pending")));
@@ -371,30 +412,104 @@ var DrizzleStateStore = class {
371
412
  }
372
413
  async saveCheckpointsIfPending(checkpoints) {
373
414
  if (checkpoints.length === 0) return [];
415
+ let wrote = [];
416
+ await this.db.transaction((tx) => {
417
+ const pending = /* @__PURE__ */ new Set();
418
+ const steps = [];
419
+ for (const [runId, seqs] of seqsByRun(checkpoints)) {
420
+ for (let at = 0; at < seqs.length; at += 400) {
421
+ const chunk = seqs.slice(at, at + 400);
422
+ steps.push(() => {
423
+ const rows = tx.select({
424
+ runId: stepCheckpoints.runId,
425
+ seq: stepCheckpoints.seq
426
+ }).from(stepCheckpoints).where(and(eq(stepCheckpoints.runId, runId), inArray(stepCheckpoints.seq, chunk), eq(stepCheckpoints.status, "pending"))).all();
427
+ const add = /* @__PURE__ */ __name((found) => {
428
+ for (const row of found) pending.add(checkpointKey(row.runId, row.seq));
429
+ }, "add");
430
+ return rows instanceof Promise ? rows.then(add) : add(rows);
431
+ });
432
+ }
433
+ }
434
+ steps.push(() => {
435
+ const decided = decideIfPending(checkpoints, pending);
436
+ wrote = decided.wrote;
437
+ const writes = [];
438
+ for (let at = 0; at < decided.writes.length; at += 100) {
439
+ const chunk = decided.writes.slice(at, at + 100);
440
+ writes.push(() => tx.insert(stepCheckpoints).values(chunk.map(toCheckpointRow)).onConflictDoUpdate({
441
+ target: [
442
+ stepCheckpoints.runId,
443
+ stepCheckpoints.seq
444
+ ],
445
+ set: excludedSet(stepCheckpoints)
446
+ }).run());
447
+ }
448
+ return inSequence(writes);
449
+ });
450
+ return inSequence(steps);
451
+ });
452
+ return wrote;
453
+ }
454
+ async settleCheckpointsIfPending(settlements) {
455
+ if (settlements.length === 0) return [];
374
456
  const wrote = [];
375
457
  await this.db.transaction((tx) => {
376
- const update = /* @__PURE__ */ __name((checkpoint) => {
377
- const { runId, seq, ...set } = toCheckpointRow(checkpoint);
378
- return tx.update(stepCheckpoints).set(set).where(and(eq(stepCheckpoints.runId, runId), eq(stepCheckpoints.seq, seq), eq(stepCheckpoints.status, "pending"))).run();
379
- }, "update");
380
- const record = /* @__PURE__ */ __name((result) => {
381
- const { changes, rowsAffected } = result;
382
- wrote.push((changes ?? rowsAffected ?? 0) > 0);
383
- }, "record");
384
- const runFrom = /* @__PURE__ */ __name((from) => {
385
- for (let i = from; i < checkpoints.length; i += 1) {
386
- const result = update(checkpoints[i]);
387
- if (result instanceof Promise) {
388
- return result.then((r) => {
389
- record(r);
390
- return runFrom(i + 1);
391
- });
392
- }
393
- record(result);
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
+ });
394
477
  }
395
- return void 0;
396
- }, "runFrom");
397
- return runFrom(0);
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);
398
513
  });
399
514
  return wrote;
400
515
  }
@@ -599,6 +714,23 @@ var DrizzleStateStore = class {
599
714
  const rows = await this.db.select().from(stepCheckpoints).where(eq(stepCheckpoints.runId, runId)).orderBy(asc(stepCheckpoints.seq));
600
715
  return rows.map(fromCheckpointRow);
601
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
+ }
602
734
  async getLatestCheckpointByName(runId, name) {
603
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);
604
736
  return rows[0] ? fromCheckpointRow(rows[0]) : void 0;
@@ -998,6 +1130,61 @@ function fromScheduleRow(row) {
998
1130
  };
999
1131
  }
1000
1132
  __name(fromScheduleRow, "fromScheduleRow");
1133
+ var SETTLED_FIELDS = [
1134
+ "status",
1135
+ "output",
1136
+ "error",
1137
+ "events",
1138
+ "startedAt",
1139
+ "finishedAt"
1140
+ ];
1141
+ function inSequence(steps, from = 0) {
1142
+ for (let i = from; i < steps.length; i += 1) {
1143
+ const done = steps[i]();
1144
+ if (done instanceof Promise) return done.then(() => inSequence(steps, i + 1));
1145
+ }
1146
+ return void 0;
1147
+ }
1148
+ __name(inSequence, "inSequence");
1149
+ function seqsByRun(checkpoints) {
1150
+ const byRun = /* @__PURE__ */ new Map();
1151
+ for (const { runId, seq } of checkpoints) {
1152
+ const seqs = byRun.get(runId) ?? /* @__PURE__ */ new Set();
1153
+ seqs.add(seq);
1154
+ byRun.set(runId, seqs);
1155
+ }
1156
+ return new Map([
1157
+ ...byRun
1158
+ ].map(([runId, seqs]) => [
1159
+ runId,
1160
+ [
1161
+ ...seqs
1162
+ ]
1163
+ ]));
1164
+ }
1165
+ __name(seqsByRun, "seqsByRun");
1166
+ var checkpointKey = /* @__PURE__ */ __name((runId, seq) => `${runId}\0${seq}`, "checkpointKey");
1167
+ function decideIfPending(checkpoints, pending) {
1168
+ const wrote = [];
1169
+ const writes = /* @__PURE__ */ new Map();
1170
+ for (const checkpoint of checkpoints) {
1171
+ const key = checkpointKey(checkpoint.runId, checkpoint.seq);
1172
+ if (!pending.has(key)) {
1173
+ wrote.push(false);
1174
+ continue;
1175
+ }
1176
+ wrote.push(true);
1177
+ writes.set(key, checkpoint);
1178
+ if (checkpoint.status !== "pending") pending.delete(key);
1179
+ }
1180
+ return {
1181
+ wrote,
1182
+ writes: [
1183
+ ...writes.values()
1184
+ ]
1185
+ };
1186
+ }
1187
+ __name(decideIfPending, "decideIfPending");
1001
1188
  function excludedSet(table, only) {
1002
1189
  return Object.fromEntries(Object.entries(getTableColumns(table)).filter(([key]) => !only || only.includes(key)).map(([key, column]) => [
1003
1190
  key,