@dudousxd/nestjs-durable-store-drizzle 0.24.1 → 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.cjs +120 -22
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +5 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.js +121 -23
- package/dist/index.js.map +1 -1
- package/dist/pg/index.cjs +36 -0
- package/dist/pg/index.cjs.map +1 -1
- package/dist/pg/index.d.cts +5 -1
- package/dist/pg/index.d.ts +5 -1
- package/dist/pg/index.js +37 -1
- package/dist/pg/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -387,6 +387,16 @@ 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
|
+
}
|
|
390
400
|
async saveCheckpoint(checkpoint) {
|
|
391
401
|
const row = toCheckpointRow(checkpoint);
|
|
392
402
|
await this.db.insert(stepCheckpoints).values(row).onConflictDoUpdate({
|
|
@@ -397,6 +407,23 @@ var DrizzleStateStore = class {
|
|
|
397
407
|
set: row
|
|
398
408
|
});
|
|
399
409
|
}
|
|
410
|
+
async saveCheckpoints(checkpoints) {
|
|
411
|
+
if (checkpoints.length === 0) return;
|
|
412
|
+
await this.db.transaction((tx) => {
|
|
413
|
+
const runFrom = /* @__PURE__ */ __name((at) => {
|
|
414
|
+
if (at >= checkpoints.length) return void 0;
|
|
415
|
+
const done = tx.insert(stepCheckpoints).values(checkpoints.slice(at, at + 100).map(toCheckpointRow)).onConflictDoUpdate({
|
|
416
|
+
target: [
|
|
417
|
+
stepCheckpoints.runId,
|
|
418
|
+
stepCheckpoints.seq
|
|
419
|
+
],
|
|
420
|
+
set: excludedSet(stepCheckpoints)
|
|
421
|
+
}).run();
|
|
422
|
+
return done instanceof Promise ? done.then(() => runFrom(at + 100)) : runFrom(at + 100);
|
|
423
|
+
}, "runFrom");
|
|
424
|
+
return runFrom(0);
|
|
425
|
+
});
|
|
426
|
+
}
|
|
400
427
|
async saveCheckpointIfPending(checkpoint) {
|
|
401
428
|
const { runId, seq, ...set } = toCheckpointRow(checkpoint);
|
|
402
429
|
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 +432,42 @@ var DrizzleStateStore = class {
|
|
|
405
432
|
}
|
|
406
433
|
async saveCheckpointsIfPending(checkpoints) {
|
|
407
434
|
if (checkpoints.length === 0) return [];
|
|
408
|
-
|
|
435
|
+
let wrote = [];
|
|
409
436
|
await this.db.transaction((tx) => {
|
|
410
|
-
const
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
});
|
|
426
|
-
}
|
|
427
|
-
record(result);
|
|
437
|
+
const pending = /* @__PURE__ */ new Set();
|
|
438
|
+
const steps = [];
|
|
439
|
+
for (const [runId, seqs] of seqsByRun(checkpoints)) {
|
|
440
|
+
for (let at = 0; at < seqs.length; at += 400) {
|
|
441
|
+
const chunk = seqs.slice(at, at + 400);
|
|
442
|
+
steps.push(() => {
|
|
443
|
+
const rows = tx.select({
|
|
444
|
+
runId: stepCheckpoints.runId,
|
|
445
|
+
seq: stepCheckpoints.seq
|
|
446
|
+
}).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();
|
|
447
|
+
const add = /* @__PURE__ */ __name((found) => {
|
|
448
|
+
for (const row of found) pending.add(checkpointKey(row.runId, row.seq));
|
|
449
|
+
}, "add");
|
|
450
|
+
return rows instanceof Promise ? rows.then(add) : add(rows);
|
|
451
|
+
});
|
|
428
452
|
}
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
453
|
+
}
|
|
454
|
+
steps.push(() => {
|
|
455
|
+
const decided = decideIfPending(checkpoints, pending);
|
|
456
|
+
wrote = decided.wrote;
|
|
457
|
+
const writes = [];
|
|
458
|
+
for (let at = 0; at < decided.writes.length; at += 100) {
|
|
459
|
+
const chunk = decided.writes.slice(at, at + 100);
|
|
460
|
+
writes.push(() => tx.insert(stepCheckpoints).values(chunk.map(toCheckpointRow)).onConflictDoUpdate({
|
|
461
|
+
target: [
|
|
462
|
+
stepCheckpoints.runId,
|
|
463
|
+
stepCheckpoints.seq
|
|
464
|
+
],
|
|
465
|
+
set: excludedSet(stepCheckpoints)
|
|
466
|
+
}).run());
|
|
467
|
+
}
|
|
468
|
+
return inSequence(writes);
|
|
469
|
+
});
|
|
470
|
+
return inSequence(steps);
|
|
432
471
|
});
|
|
433
472
|
return wrote;
|
|
434
473
|
}
|
|
@@ -830,6 +869,18 @@ var DrizzleStateStore = class {
|
|
|
830
869
|
payload: row.payload ?? void 0
|
|
831
870
|
};
|
|
832
871
|
}
|
|
872
|
+
async listBufferedSignals(tokenPrefix, afterId, limit) {
|
|
873
|
+
const rows = await this.db.select().from(bufferedSignals).where((0, import_drizzle_orm2.and)((0, import_drizzle_orm2.like)(bufferedSignals.token, `${tokenPrefix}%`), afterId !== void 0 ? (0, import_drizzle_orm2.gt)(bufferedSignals.id, Number(afterId)) : void 0)).orderBy((0, import_drizzle_orm2.asc)(bufferedSignals.id)).limit(limit);
|
|
874
|
+
return rows.filter((r) => r.token.startsWith(tokenPrefix)).map((r) => ({
|
|
875
|
+
id: String(r.id),
|
|
876
|
+
token: r.token,
|
|
877
|
+
payload: r.payload ?? void 0
|
|
878
|
+
}));
|
|
879
|
+
}
|
|
880
|
+
async deleteBufferedSignals(ids) {
|
|
881
|
+
if (ids.length === 0) return;
|
|
882
|
+
await this.db.delete(bufferedSignals).where((0, import_drizzle_orm2.inArray)(bufferedSignals.id, ids.map(Number)));
|
|
883
|
+
}
|
|
833
884
|
async bufferEvent(input) {
|
|
834
885
|
await this.db.insert(bufferedEvents).values({
|
|
835
886
|
id: input.id,
|
|
@@ -1020,6 +1071,53 @@ function fromScheduleRow(row) {
|
|
|
1020
1071
|
};
|
|
1021
1072
|
}
|
|
1022
1073
|
__name(fromScheduleRow, "fromScheduleRow");
|
|
1074
|
+
function inSequence(steps, from = 0) {
|
|
1075
|
+
for (let i = from; i < steps.length; i += 1) {
|
|
1076
|
+
const done = steps[i]();
|
|
1077
|
+
if (done instanceof Promise) return done.then(() => inSequence(steps, i + 1));
|
|
1078
|
+
}
|
|
1079
|
+
return void 0;
|
|
1080
|
+
}
|
|
1081
|
+
__name(inSequence, "inSequence");
|
|
1082
|
+
function seqsByRun(checkpoints) {
|
|
1083
|
+
const byRun = /* @__PURE__ */ new Map();
|
|
1084
|
+
for (const { runId, seq } of checkpoints) {
|
|
1085
|
+
const seqs = byRun.get(runId) ?? /* @__PURE__ */ new Set();
|
|
1086
|
+
seqs.add(seq);
|
|
1087
|
+
byRun.set(runId, seqs);
|
|
1088
|
+
}
|
|
1089
|
+
return new Map([
|
|
1090
|
+
...byRun
|
|
1091
|
+
].map(([runId, seqs]) => [
|
|
1092
|
+
runId,
|
|
1093
|
+
[
|
|
1094
|
+
...seqs
|
|
1095
|
+
]
|
|
1096
|
+
]));
|
|
1097
|
+
}
|
|
1098
|
+
__name(seqsByRun, "seqsByRun");
|
|
1099
|
+
var checkpointKey = /* @__PURE__ */ __name((runId, seq) => `${runId}\0${seq}`, "checkpointKey");
|
|
1100
|
+
function decideIfPending(checkpoints, pending) {
|
|
1101
|
+
const wrote = [];
|
|
1102
|
+
const writes = /* @__PURE__ */ new Map();
|
|
1103
|
+
for (const checkpoint of checkpoints) {
|
|
1104
|
+
const key = checkpointKey(checkpoint.runId, checkpoint.seq);
|
|
1105
|
+
if (!pending.has(key)) {
|
|
1106
|
+
wrote.push(false);
|
|
1107
|
+
continue;
|
|
1108
|
+
}
|
|
1109
|
+
wrote.push(true);
|
|
1110
|
+
writes.set(key, checkpoint);
|
|
1111
|
+
if (checkpoint.status !== "pending") pending.delete(key);
|
|
1112
|
+
}
|
|
1113
|
+
return {
|
|
1114
|
+
wrote,
|
|
1115
|
+
writes: [
|
|
1116
|
+
...writes.values()
|
|
1117
|
+
]
|
|
1118
|
+
};
|
|
1119
|
+
}
|
|
1120
|
+
__name(decideIfPending, "decideIfPending");
|
|
1023
1121
|
function excludedSet(table, only) {
|
|
1024
1122
|
return Object.fromEntries(Object.entries((0, import_drizzle_orm2.getTableColumns)(table)).filter(([key]) => !only || only.includes(key)).map(([key, column]) => [
|
|
1025
1123
|
key,
|