@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.js
CHANGED
|
@@ -3,7 +3,7 @@ var __name = (target, value) => __defProp(target, "name", { value, configurable:
|
|
|
3
3
|
|
|
4
4
|
// src/drizzle-state-store.ts
|
|
5
5
|
import { RUN_VALUE_FACET_SCAN, SINGLETON_ACTIVE_STATUSES, TERMINAL_RUN_STATUSES, attributePredicateOperands, axisIsRunColumn, clearTerminalSingletonAdmission, mergeRunFacetRows, mergeRunValueFacetRows, normalizeAttributeRows, parseDuration, runValueFacetsFromRuns, singletonAdmissionDenialPatch, singletonAdmissionTags } from "@dudousxd/nestjs-durable-core";
|
|
6
|
-
import { and, asc, desc, eq, exists, getTableColumns, gte, inArray, isNotNull, isNull, like, lt, lte, notInArray, or, sql } from "drizzle-orm";
|
|
6
|
+
import { and, asc, desc, eq, exists, getTableColumns, gt, gte, inArray, isNotNull, isNull, like, lt, lte, notInArray, or, sql } from "drizzle-orm";
|
|
7
7
|
|
|
8
8
|
// src/schema.ts
|
|
9
9
|
import { getTableName } from "drizzle-orm";
|
|
@@ -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
|
-
|
|
401
|
+
let wrote = [];
|
|
375
402
|
await this.db.transaction((tx) => {
|
|
376
|
-
const
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
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
|
-
|
|
396
|
-
|
|
397
|
-
|
|
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
|
}
|
|
@@ -796,6 +835,18 @@ var DrizzleStateStore = class {
|
|
|
796
835
|
payload: row.payload ?? void 0
|
|
797
836
|
};
|
|
798
837
|
}
|
|
838
|
+
async listBufferedSignals(tokenPrefix, afterId, limit) {
|
|
839
|
+
const rows = await this.db.select().from(bufferedSignals).where(and(like(bufferedSignals.token, `${tokenPrefix}%`), afterId !== void 0 ? gt(bufferedSignals.id, Number(afterId)) : void 0)).orderBy(asc(bufferedSignals.id)).limit(limit);
|
|
840
|
+
return rows.filter((r) => r.token.startsWith(tokenPrefix)).map((r) => ({
|
|
841
|
+
id: String(r.id),
|
|
842
|
+
token: r.token,
|
|
843
|
+
payload: r.payload ?? void 0
|
|
844
|
+
}));
|
|
845
|
+
}
|
|
846
|
+
async deleteBufferedSignals(ids) {
|
|
847
|
+
if (ids.length === 0) return;
|
|
848
|
+
await this.db.delete(bufferedSignals).where(inArray(bufferedSignals.id, ids.map(Number)));
|
|
849
|
+
}
|
|
799
850
|
async bufferEvent(input) {
|
|
800
851
|
await this.db.insert(bufferedEvents).values({
|
|
801
852
|
id: input.id,
|
|
@@ -986,6 +1037,53 @@ function fromScheduleRow(row) {
|
|
|
986
1037
|
};
|
|
987
1038
|
}
|
|
988
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");
|
|
989
1087
|
function excludedSet(table, only) {
|
|
990
1088
|
return Object.fromEntries(Object.entries(getTableColumns(table)).filter(([key]) => !only || only.includes(key)).map(([key, column]) => [
|
|
991
1089
|
key,
|