@dudousxd/nestjs-durable-store-drizzle 0.24.2 → 0.24.3

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,16 @@ 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
+ }
356
366
  async saveCheckpoint(checkpoint) {
357
367
  const row = toCheckpointRow(checkpoint);
358
368
  await this.db.insert(stepCheckpoints).values(row).onConflictDoUpdate({
@@ -363,6 +373,23 @@ var DrizzleStateStore = class {
363
373
  set: row
364
374
  });
365
375
  }
376
+ async saveCheckpoints(checkpoints) {
377
+ if (checkpoints.length === 0) return;
378
+ await this.db.transaction((tx) => {
379
+ const runFrom = /* @__PURE__ */ __name((at) => {
380
+ if (at >= checkpoints.length) return void 0;
381
+ const done = tx.insert(stepCheckpoints).values(checkpoints.slice(at, at + 100).map(toCheckpointRow)).onConflictDoUpdate({
382
+ target: [
383
+ stepCheckpoints.runId,
384
+ stepCheckpoints.seq
385
+ ],
386
+ set: excludedSet(stepCheckpoints)
387
+ }).run();
388
+ return done instanceof Promise ? done.then(() => runFrom(at + 100)) : runFrom(at + 100);
389
+ }, "runFrom");
390
+ return runFrom(0);
391
+ });
392
+ }
366
393
  async saveCheckpointIfPending(checkpoint) {
367
394
  const { runId, seq, ...set } = toCheckpointRow(checkpoint);
368
395
  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 +398,42 @@ var DrizzleStateStore = class {
371
398
  }
372
399
  async saveCheckpointsIfPending(checkpoints) {
373
400
  if (checkpoints.length === 0) return [];
374
- const wrote = [];
401
+ let wrote = [];
375
402
  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);
403
+ const pending = /* @__PURE__ */ new Set();
404
+ const steps = [];
405
+ for (const [runId, seqs] of seqsByRun(checkpoints)) {
406
+ for (let at = 0; at < seqs.length; at += 400) {
407
+ const chunk = seqs.slice(at, at + 400);
408
+ steps.push(() => {
409
+ const rows = tx.select({
410
+ runId: stepCheckpoints.runId,
411
+ seq: stepCheckpoints.seq
412
+ }).from(stepCheckpoints).where(and(eq(stepCheckpoints.runId, runId), inArray(stepCheckpoints.seq, chunk), eq(stepCheckpoints.status, "pending"))).all();
413
+ const add = /* @__PURE__ */ __name((found) => {
414
+ for (const row of found) pending.add(checkpointKey(row.runId, row.seq));
415
+ }, "add");
416
+ return rows instanceof Promise ? rows.then(add) : add(rows);
417
+ });
394
418
  }
395
- return void 0;
396
- }, "runFrom");
397
- return runFrom(0);
419
+ }
420
+ steps.push(() => {
421
+ const decided = decideIfPending(checkpoints, pending);
422
+ wrote = decided.wrote;
423
+ const writes = [];
424
+ for (let at = 0; at < decided.writes.length; at += 100) {
425
+ const chunk = decided.writes.slice(at, at + 100);
426
+ writes.push(() => tx.insert(stepCheckpoints).values(chunk.map(toCheckpointRow)).onConflictDoUpdate({
427
+ target: [
428
+ stepCheckpoints.runId,
429
+ stepCheckpoints.seq
430
+ ],
431
+ set: excludedSet(stepCheckpoints)
432
+ }).run());
433
+ }
434
+ return inSequence(writes);
435
+ });
436
+ return inSequence(steps);
398
437
  });
399
438
  return wrote;
400
439
  }
@@ -998,6 +1037,53 @@ function fromScheduleRow(row) {
998
1037
  };
999
1038
  }
1000
1039
  __name(fromScheduleRow, "fromScheduleRow");
1040
+ function inSequence(steps, from = 0) {
1041
+ for (let i = from; i < steps.length; i += 1) {
1042
+ const done = steps[i]();
1043
+ if (done instanceof Promise) return done.then(() => inSequence(steps, i + 1));
1044
+ }
1045
+ return void 0;
1046
+ }
1047
+ __name(inSequence, "inSequence");
1048
+ function seqsByRun(checkpoints) {
1049
+ const byRun = /* @__PURE__ */ new Map();
1050
+ for (const { runId, seq } of checkpoints) {
1051
+ const seqs = byRun.get(runId) ?? /* @__PURE__ */ new Set();
1052
+ seqs.add(seq);
1053
+ byRun.set(runId, seqs);
1054
+ }
1055
+ return new Map([
1056
+ ...byRun
1057
+ ].map(([runId, seqs]) => [
1058
+ runId,
1059
+ [
1060
+ ...seqs
1061
+ ]
1062
+ ]));
1063
+ }
1064
+ __name(seqsByRun, "seqsByRun");
1065
+ var checkpointKey = /* @__PURE__ */ __name((runId, seq) => `${runId}\0${seq}`, "checkpointKey");
1066
+ function decideIfPending(checkpoints, pending) {
1067
+ const wrote = [];
1068
+ const writes = /* @__PURE__ */ new Map();
1069
+ for (const checkpoint of checkpoints) {
1070
+ const key = checkpointKey(checkpoint.runId, checkpoint.seq);
1071
+ if (!pending.has(key)) {
1072
+ wrote.push(false);
1073
+ continue;
1074
+ }
1075
+ wrote.push(true);
1076
+ writes.set(key, checkpoint);
1077
+ if (checkpoint.status !== "pending") pending.delete(key);
1078
+ }
1079
+ return {
1080
+ wrote,
1081
+ writes: [
1082
+ ...writes.values()
1083
+ ]
1084
+ };
1085
+ }
1086
+ __name(decideIfPending, "decideIfPending");
1001
1087
  function excludedSet(table, only) {
1002
1088
  return Object.fromEntries(Object.entries(getTableColumns(table)).filter(([key]) => !only || only.includes(key)).map(([key, column]) => [
1003
1089
  key,