@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.cjs +208 -21
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +6 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.js +208 -21
- package/dist/index.js.map +1 -1
- package/dist/pg/index.cjs +91 -0
- package/dist/pg/index.cjs.map +1 -1
- package/dist/pg/index.d.cts +6 -1
- package/dist/pg/index.d.ts +6 -1
- package/dist/pg/index.js +91 -0
- package/dist/pg/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -387,6 +387,30 @@ var DrizzleStateStore = class {
|
|
|
387
387
|
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.seq, seq))).limit(1);
|
|
388
388
|
return rows[0] ? fromCheckpointRow(rows[0]) : null;
|
|
389
389
|
}
|
|
390
|
+
async getCheckpoints(runId, seqs) {
|
|
391
|
+
const unique = [
|
|
392
|
+
...new Set(seqs)
|
|
393
|
+
];
|
|
394
|
+
const rows = [];
|
|
395
|
+
for (let at = 0; at < unique.length; at += 500) {
|
|
396
|
+
rows.push(...await this.db.select().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)))));
|
|
397
|
+
}
|
|
398
|
+
return rows.sort((a, b) => a.seq - b.seq).map(fromCheckpointRow);
|
|
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
|
+
}
|
|
390
414
|
async saveCheckpoint(checkpoint) {
|
|
391
415
|
const row = toCheckpointRow(checkpoint);
|
|
392
416
|
await this.db.insert(stepCheckpoints).values(row).onConflictDoUpdate({
|
|
@@ -397,6 +421,23 @@ var DrizzleStateStore = class {
|
|
|
397
421
|
set: row
|
|
398
422
|
});
|
|
399
423
|
}
|
|
424
|
+
async saveCheckpoints(checkpoints) {
|
|
425
|
+
if (checkpoints.length === 0) return;
|
|
426
|
+
await this.db.transaction((tx) => {
|
|
427
|
+
const runFrom = /* @__PURE__ */ __name((at) => {
|
|
428
|
+
if (at >= checkpoints.length) return void 0;
|
|
429
|
+
const done = tx.insert(stepCheckpoints).values(checkpoints.slice(at, at + 100).map(toCheckpointRow)).onConflictDoUpdate({
|
|
430
|
+
target: [
|
|
431
|
+
stepCheckpoints.runId,
|
|
432
|
+
stepCheckpoints.seq
|
|
433
|
+
],
|
|
434
|
+
set: excludedSet(stepCheckpoints)
|
|
435
|
+
}).run();
|
|
436
|
+
return done instanceof Promise ? done.then(() => runFrom(at + 100)) : runFrom(at + 100);
|
|
437
|
+
}, "runFrom");
|
|
438
|
+
return runFrom(0);
|
|
439
|
+
});
|
|
440
|
+
}
|
|
400
441
|
async saveCheckpointIfPending(checkpoint) {
|
|
401
442
|
const { runId, seq, ...set } = toCheckpointRow(checkpoint);
|
|
402
443
|
const result = await this.db.update(stepCheckpoints).set(set).where((0, import_drizzle_orm2.and)((0, import_drizzle_orm2.eq)(stepCheckpoints.runId, runId), (0, import_drizzle_orm2.eq)(stepCheckpoints.seq, seq), (0, import_drizzle_orm2.eq)(stepCheckpoints.status, "pending")));
|
|
@@ -405,30 +446,104 @@ var DrizzleStateStore = class {
|
|
|
405
446
|
}
|
|
406
447
|
async saveCheckpointsIfPending(checkpoints) {
|
|
407
448
|
if (checkpoints.length === 0) return [];
|
|
449
|
+
let wrote = [];
|
|
450
|
+
await this.db.transaction((tx) => {
|
|
451
|
+
const pending = /* @__PURE__ */ new Set();
|
|
452
|
+
const steps = [];
|
|
453
|
+
for (const [runId, seqs] of seqsByRun(checkpoints)) {
|
|
454
|
+
for (let at = 0; at < seqs.length; at += 400) {
|
|
455
|
+
const chunk = seqs.slice(at, at + 400);
|
|
456
|
+
steps.push(() => {
|
|
457
|
+
const rows = tx.select({
|
|
458
|
+
runId: stepCheckpoints.runId,
|
|
459
|
+
seq: stepCheckpoints.seq
|
|
460
|
+
}).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();
|
|
461
|
+
const add = /* @__PURE__ */ __name((found) => {
|
|
462
|
+
for (const row of found) pending.add(checkpointKey(row.runId, row.seq));
|
|
463
|
+
}, "add");
|
|
464
|
+
return rows instanceof Promise ? rows.then(add) : add(rows);
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
steps.push(() => {
|
|
469
|
+
const decided = decideIfPending(checkpoints, pending);
|
|
470
|
+
wrote = decided.wrote;
|
|
471
|
+
const writes = [];
|
|
472
|
+
for (let at = 0; at < decided.writes.length; at += 100) {
|
|
473
|
+
const chunk = decided.writes.slice(at, at + 100);
|
|
474
|
+
writes.push(() => tx.insert(stepCheckpoints).values(chunk.map(toCheckpointRow)).onConflictDoUpdate({
|
|
475
|
+
target: [
|
|
476
|
+
stepCheckpoints.runId,
|
|
477
|
+
stepCheckpoints.seq
|
|
478
|
+
],
|
|
479
|
+
set: excludedSet(stepCheckpoints)
|
|
480
|
+
}).run());
|
|
481
|
+
}
|
|
482
|
+
return inSequence(writes);
|
|
483
|
+
});
|
|
484
|
+
return inSequence(steps);
|
|
485
|
+
});
|
|
486
|
+
return wrote;
|
|
487
|
+
}
|
|
488
|
+
async settleCheckpointsIfPending(settlements) {
|
|
489
|
+
if (settlements.length === 0) return [];
|
|
408
490
|
const wrote = [];
|
|
409
491
|
await this.db.transaction((tx) => {
|
|
410
|
-
const
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
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
|
+
});
|
|
428
511
|
}
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
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);
|
|
432
547
|
});
|
|
433
548
|
return wrote;
|
|
434
549
|
}
|
|
@@ -633,6 +748,23 @@ var DrizzleStateStore = class {
|
|
|
633
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));
|
|
634
749
|
return rows.map(fromCheckpointRow);
|
|
635
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
|
+
}
|
|
636
768
|
async getLatestCheckpointByName(runId, name) {
|
|
637
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);
|
|
638
770
|
return rows[0] ? fromCheckpointRow(rows[0]) : void 0;
|
|
@@ -1032,6 +1164,61 @@ function fromScheduleRow(row) {
|
|
|
1032
1164
|
};
|
|
1033
1165
|
}
|
|
1034
1166
|
__name(fromScheduleRow, "fromScheduleRow");
|
|
1167
|
+
var SETTLED_FIELDS = [
|
|
1168
|
+
"status",
|
|
1169
|
+
"output",
|
|
1170
|
+
"error",
|
|
1171
|
+
"events",
|
|
1172
|
+
"startedAt",
|
|
1173
|
+
"finishedAt"
|
|
1174
|
+
];
|
|
1175
|
+
function inSequence(steps, from = 0) {
|
|
1176
|
+
for (let i = from; i < steps.length; i += 1) {
|
|
1177
|
+
const done = steps[i]();
|
|
1178
|
+
if (done instanceof Promise) return done.then(() => inSequence(steps, i + 1));
|
|
1179
|
+
}
|
|
1180
|
+
return void 0;
|
|
1181
|
+
}
|
|
1182
|
+
__name(inSequence, "inSequence");
|
|
1183
|
+
function seqsByRun(checkpoints) {
|
|
1184
|
+
const byRun = /* @__PURE__ */ new Map();
|
|
1185
|
+
for (const { runId, seq } of checkpoints) {
|
|
1186
|
+
const seqs = byRun.get(runId) ?? /* @__PURE__ */ new Set();
|
|
1187
|
+
seqs.add(seq);
|
|
1188
|
+
byRun.set(runId, seqs);
|
|
1189
|
+
}
|
|
1190
|
+
return new Map([
|
|
1191
|
+
...byRun
|
|
1192
|
+
].map(([runId, seqs]) => [
|
|
1193
|
+
runId,
|
|
1194
|
+
[
|
|
1195
|
+
...seqs
|
|
1196
|
+
]
|
|
1197
|
+
]));
|
|
1198
|
+
}
|
|
1199
|
+
__name(seqsByRun, "seqsByRun");
|
|
1200
|
+
var checkpointKey = /* @__PURE__ */ __name((runId, seq) => `${runId}\0${seq}`, "checkpointKey");
|
|
1201
|
+
function decideIfPending(checkpoints, pending) {
|
|
1202
|
+
const wrote = [];
|
|
1203
|
+
const writes = /* @__PURE__ */ new Map();
|
|
1204
|
+
for (const checkpoint of checkpoints) {
|
|
1205
|
+
const key = checkpointKey(checkpoint.runId, checkpoint.seq);
|
|
1206
|
+
if (!pending.has(key)) {
|
|
1207
|
+
wrote.push(false);
|
|
1208
|
+
continue;
|
|
1209
|
+
}
|
|
1210
|
+
wrote.push(true);
|
|
1211
|
+
writes.set(key, checkpoint);
|
|
1212
|
+
if (checkpoint.status !== "pending") pending.delete(key);
|
|
1213
|
+
}
|
|
1214
|
+
return {
|
|
1215
|
+
wrote,
|
|
1216
|
+
writes: [
|
|
1217
|
+
...writes.values()
|
|
1218
|
+
]
|
|
1219
|
+
};
|
|
1220
|
+
}
|
|
1221
|
+
__name(decideIfPending, "decideIfPending");
|
|
1035
1222
|
function excludedSet(table, only) {
|
|
1036
1223
|
return Object.fromEntries(Object.entries((0, import_drizzle_orm2.getTableColumns)(table)).filter(([key]) => !only || only.includes(key)).map(([key, column]) => [
|
|
1037
1224
|
key,
|