@dudousxd/nestjs-durable-store-prisma 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 +168 -37
- 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 +168 -37
- package/dist/index.js.map +1 -1
- package/package.json +5 -4
package/dist/index.cjs
CHANGED
|
@@ -279,6 +279,23 @@ var PrismaStateStore = class {
|
|
|
279
279
|
});
|
|
280
280
|
return row ? fromCheckpointRow(row) : null;
|
|
281
281
|
}
|
|
282
|
+
async getCheckpoints(runId, seqs) {
|
|
283
|
+
const unique = [
|
|
284
|
+
...new Set(seqs)
|
|
285
|
+
];
|
|
286
|
+
const rows = [];
|
|
287
|
+
for (let at = 0; at < unique.length; at += 500) {
|
|
288
|
+
rows.push(...await this.db.durableStepCheckpoint.findMany({
|
|
289
|
+
where: {
|
|
290
|
+
runId,
|
|
291
|
+
seq: {
|
|
292
|
+
in: unique.slice(at, at + 500)
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}));
|
|
296
|
+
}
|
|
297
|
+
return rows.sort((a, b) => a.seq - b.seq).map(fromCheckpointRow);
|
|
298
|
+
}
|
|
282
299
|
async saveCheckpoint(checkpoint) {
|
|
283
300
|
const data = toCheckpointData(checkpoint);
|
|
284
301
|
await this.db.durableStepCheckpoint.upsert({
|
|
@@ -307,22 +324,55 @@ var PrismaStateStore = class {
|
|
|
307
324
|
async saveCheckpointsIfPending(checkpoints) {
|
|
308
325
|
if (checkpoints.length === 0) return [];
|
|
309
326
|
return this.db.$transaction(async (tx) => {
|
|
310
|
-
const
|
|
311
|
-
for (const
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
327
|
+
const pending = /* @__PURE__ */ new Set();
|
|
328
|
+
for (const [runId, seqs] of seqsByRun(checkpoints)) {
|
|
329
|
+
for (let at = 0; at < seqs.length; at += 500) {
|
|
330
|
+
const rows = await tx.durableStepCheckpoint.findMany({
|
|
331
|
+
where: {
|
|
332
|
+
runId,
|
|
333
|
+
seq: {
|
|
334
|
+
in: seqs.slice(at, at + 500)
|
|
335
|
+
},
|
|
336
|
+
status: "pending"
|
|
337
|
+
},
|
|
338
|
+
select: {
|
|
339
|
+
runId: true,
|
|
340
|
+
seq: true
|
|
341
|
+
}
|
|
342
|
+
});
|
|
343
|
+
for (const row of rows) pending.add(checkpointKey(row.runId, row.seq));
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
const { wrote, writes } = decideIfPending(checkpoints, pending);
|
|
347
|
+
for (const [runId, seqs] of seqsByRun(writes)) {
|
|
348
|
+
for (let at = 0; at < seqs.length; at += 500) {
|
|
349
|
+
const chunk = seqs.slice(at, at + 500);
|
|
350
|
+
const { count } = await tx.durableStepCheckpoint.deleteMany({
|
|
351
|
+
where: {
|
|
352
|
+
runId,
|
|
353
|
+
seq: {
|
|
354
|
+
in: chunk
|
|
355
|
+
},
|
|
356
|
+
status: "pending"
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
if (count !== chunk.length) {
|
|
360
|
+
throw new Error("a checkpoint of the batch was settled while the batch was written");
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
for (let at = 0; at < writes.length; at += 500) {
|
|
365
|
+
await tx.durableStepCheckpoint.createMany({
|
|
366
|
+
data: writes.slice(at, at + 500).map(toCheckpointData)
|
|
320
367
|
});
|
|
321
|
-
wrote.push(count > 0);
|
|
322
368
|
}
|
|
323
369
|
return wrote;
|
|
324
370
|
});
|
|
325
371
|
}
|
|
372
|
+
async saveCheckpoints(checkpoints) {
|
|
373
|
+
if (checkpoints.length === 0) return;
|
|
374
|
+
await this.db.$transaction((tx) => replaceCheckpoints(tx, checkpoints));
|
|
375
|
+
}
|
|
326
376
|
async transaction(work) {
|
|
327
377
|
return this.db.$transaction(async (tx) => work({
|
|
328
378
|
raw: tx,
|
|
@@ -916,36 +966,25 @@ var PrismaStateStore = class {
|
|
|
916
966
|
async registerChildWaits(entries) {
|
|
917
967
|
if (entries.length === 0) return;
|
|
918
968
|
await this.db.$transaction(async (tx) => {
|
|
919
|
-
for (
|
|
920
|
-
const
|
|
921
|
-
|
|
922
|
-
seq: waiter.seq,
|
|
923
|
-
parallelGroup: waiter.parallelGroup ?? null
|
|
924
|
-
};
|
|
925
|
-
await tx.durableSignalWaiter.upsert({
|
|
926
|
-
where: {
|
|
927
|
-
token: waiter.token
|
|
928
|
-
},
|
|
929
|
-
create: {
|
|
930
|
-
token: waiter.token,
|
|
931
|
-
...data
|
|
932
|
-
},
|
|
933
|
-
update: data
|
|
934
|
-
});
|
|
935
|
-
}
|
|
936
|
-
for (const { checkpoint } of entries) {
|
|
937
|
-
const data = toCheckpointData(checkpoint);
|
|
938
|
-
await tx.durableStepCheckpoint.upsert({
|
|
969
|
+
for (let at = 0; at < entries.length; at += 500) {
|
|
970
|
+
const chunk = entries.slice(at, at + 500);
|
|
971
|
+
await tx.durableSignalWaiter.deleteMany({
|
|
939
972
|
where: {
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
seq: checkpoint.seq
|
|
973
|
+
token: {
|
|
974
|
+
in: chunk.map(({ waiter }) => waiter.token)
|
|
943
975
|
}
|
|
944
|
-
}
|
|
945
|
-
|
|
946
|
-
|
|
976
|
+
}
|
|
977
|
+
});
|
|
978
|
+
await tx.durableSignalWaiter.createMany({
|
|
979
|
+
data: chunk.map(({ waiter }) => ({
|
|
980
|
+
token: waiter.token,
|
|
981
|
+
runId: waiter.runId,
|
|
982
|
+
seq: waiter.seq,
|
|
983
|
+
parallelGroup: waiter.parallelGroup ?? null
|
|
984
|
+
}))
|
|
947
985
|
});
|
|
948
986
|
}
|
|
987
|
+
await replaceCheckpoints(tx, entries.map(({ checkpoint }) => checkpoint));
|
|
949
988
|
});
|
|
950
989
|
}
|
|
951
990
|
async compactCheckpoints(runId, fromSeq, toSeq, into) {
|
|
@@ -1047,6 +1086,39 @@ var PrismaStateStore = class {
|
|
|
1047
1086
|
payload: row.payload ?? void 0
|
|
1048
1087
|
};
|
|
1049
1088
|
}
|
|
1089
|
+
async listBufferedSignals(tokenPrefix, afterId, limit) {
|
|
1090
|
+
const rows = await this.db.durableBufferedSignal.findMany({
|
|
1091
|
+
where: {
|
|
1092
|
+
token: {
|
|
1093
|
+
startsWith: tokenPrefix
|
|
1094
|
+
},
|
|
1095
|
+
...afterId !== void 0 ? {
|
|
1096
|
+
id: {
|
|
1097
|
+
gt: Number(afterId)
|
|
1098
|
+
}
|
|
1099
|
+
} : {}
|
|
1100
|
+
},
|
|
1101
|
+
orderBy: {
|
|
1102
|
+
id: "asc"
|
|
1103
|
+
},
|
|
1104
|
+
take: limit
|
|
1105
|
+
});
|
|
1106
|
+
return rows.filter((r) => r.token.startsWith(tokenPrefix)).map((r) => ({
|
|
1107
|
+
id: String(r.id),
|
|
1108
|
+
token: r.token,
|
|
1109
|
+
payload: r.payload ?? void 0
|
|
1110
|
+
}));
|
|
1111
|
+
}
|
|
1112
|
+
async deleteBufferedSignals(ids) {
|
|
1113
|
+
if (ids.length === 0) return;
|
|
1114
|
+
await this.db.durableBufferedSignal.deleteMany({
|
|
1115
|
+
where: {
|
|
1116
|
+
id: {
|
|
1117
|
+
in: ids.map(Number)
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
});
|
|
1121
|
+
}
|
|
1050
1122
|
async bufferEvent(input) {
|
|
1051
1123
|
await this.db.durableBufferedEvent.create({
|
|
1052
1124
|
data: {
|
|
@@ -1259,6 +1331,65 @@ function toCheckpointStat(nameKind, r, unenqueued) {
|
|
|
1259
1331
|
};
|
|
1260
1332
|
}
|
|
1261
1333
|
__name(toCheckpointStat, "toCheckpointStat");
|
|
1334
|
+
function seqsByRun(checkpoints) {
|
|
1335
|
+
const byRun = /* @__PURE__ */ new Map();
|
|
1336
|
+
for (const { runId, seq } of checkpoints) {
|
|
1337
|
+
const seqs = byRun.get(runId) ?? /* @__PURE__ */ new Set();
|
|
1338
|
+
seqs.add(seq);
|
|
1339
|
+
byRun.set(runId, seqs);
|
|
1340
|
+
}
|
|
1341
|
+
return new Map([
|
|
1342
|
+
...byRun
|
|
1343
|
+
].map(([runId, seqs]) => [
|
|
1344
|
+
runId,
|
|
1345
|
+
[
|
|
1346
|
+
...seqs
|
|
1347
|
+
]
|
|
1348
|
+
]));
|
|
1349
|
+
}
|
|
1350
|
+
__name(seqsByRun, "seqsByRun");
|
|
1351
|
+
var checkpointKey = /* @__PURE__ */ __name((runId, seq) => `${runId}\0${seq}`, "checkpointKey");
|
|
1352
|
+
function decideIfPending(checkpoints, pending) {
|
|
1353
|
+
const wrote = [];
|
|
1354
|
+
const writes = /* @__PURE__ */ new Map();
|
|
1355
|
+
for (const checkpoint of checkpoints) {
|
|
1356
|
+
const key = checkpointKey(checkpoint.runId, checkpoint.seq);
|
|
1357
|
+
if (!pending.has(key)) {
|
|
1358
|
+
wrote.push(false);
|
|
1359
|
+
continue;
|
|
1360
|
+
}
|
|
1361
|
+
wrote.push(true);
|
|
1362
|
+
writes.set(key, checkpoint);
|
|
1363
|
+
if (checkpoint.status !== "pending") pending.delete(key);
|
|
1364
|
+
}
|
|
1365
|
+
return {
|
|
1366
|
+
wrote,
|
|
1367
|
+
writes: [
|
|
1368
|
+
...writes.values()
|
|
1369
|
+
]
|
|
1370
|
+
};
|
|
1371
|
+
}
|
|
1372
|
+
__name(decideIfPending, "decideIfPending");
|
|
1373
|
+
async function replaceCheckpoints(tx, checkpoints) {
|
|
1374
|
+
for (const [runId, seqs] of seqsByRun(checkpoints)) {
|
|
1375
|
+
for (let at = 0; at < seqs.length; at += 500) {
|
|
1376
|
+
await tx.durableStepCheckpoint.deleteMany({
|
|
1377
|
+
where: {
|
|
1378
|
+
runId,
|
|
1379
|
+
seq: {
|
|
1380
|
+
in: seqs.slice(at, at + 500)
|
|
1381
|
+
}
|
|
1382
|
+
}
|
|
1383
|
+
});
|
|
1384
|
+
}
|
|
1385
|
+
}
|
|
1386
|
+
for (let at = 0; at < checkpoints.length; at += 500) {
|
|
1387
|
+
await tx.durableStepCheckpoint.createMany({
|
|
1388
|
+
data: checkpoints.slice(at, at + 500).map(toCheckpointData)
|
|
1389
|
+
});
|
|
1390
|
+
}
|
|
1391
|
+
}
|
|
1392
|
+
__name(replaceCheckpoints, "replaceCheckpoints");
|
|
1262
1393
|
function toCheckpointData(cp) {
|
|
1263
1394
|
return {
|
|
1264
1395
|
runId: cp.runId,
|