@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.js
CHANGED
|
@@ -254,6 +254,23 @@ var PrismaStateStore = class {
|
|
|
254
254
|
});
|
|
255
255
|
return row ? fromCheckpointRow(row) : null;
|
|
256
256
|
}
|
|
257
|
+
async getCheckpoints(runId, seqs) {
|
|
258
|
+
const unique = [
|
|
259
|
+
...new Set(seqs)
|
|
260
|
+
];
|
|
261
|
+
const rows = [];
|
|
262
|
+
for (let at = 0; at < unique.length; at += 500) {
|
|
263
|
+
rows.push(...await this.db.durableStepCheckpoint.findMany({
|
|
264
|
+
where: {
|
|
265
|
+
runId,
|
|
266
|
+
seq: {
|
|
267
|
+
in: unique.slice(at, at + 500)
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}));
|
|
271
|
+
}
|
|
272
|
+
return rows.sort((a, b) => a.seq - b.seq).map(fromCheckpointRow);
|
|
273
|
+
}
|
|
257
274
|
async saveCheckpoint(checkpoint) {
|
|
258
275
|
const data = toCheckpointData(checkpoint);
|
|
259
276
|
await this.db.durableStepCheckpoint.upsert({
|
|
@@ -282,22 +299,55 @@ var PrismaStateStore = class {
|
|
|
282
299
|
async saveCheckpointsIfPending(checkpoints) {
|
|
283
300
|
if (checkpoints.length === 0) return [];
|
|
284
301
|
return this.db.$transaction(async (tx) => {
|
|
285
|
-
const
|
|
286
|
-
for (const
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
302
|
+
const pending = /* @__PURE__ */ new Set();
|
|
303
|
+
for (const [runId, seqs] of seqsByRun(checkpoints)) {
|
|
304
|
+
for (let at = 0; at < seqs.length; at += 500) {
|
|
305
|
+
const rows = await tx.durableStepCheckpoint.findMany({
|
|
306
|
+
where: {
|
|
307
|
+
runId,
|
|
308
|
+
seq: {
|
|
309
|
+
in: seqs.slice(at, at + 500)
|
|
310
|
+
},
|
|
311
|
+
status: "pending"
|
|
312
|
+
},
|
|
313
|
+
select: {
|
|
314
|
+
runId: true,
|
|
315
|
+
seq: true
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
for (const row of rows) pending.add(checkpointKey(row.runId, row.seq));
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
const { wrote, writes } = decideIfPending(checkpoints, pending);
|
|
322
|
+
for (const [runId, seqs] of seqsByRun(writes)) {
|
|
323
|
+
for (let at = 0; at < seqs.length; at += 500) {
|
|
324
|
+
const chunk = seqs.slice(at, at + 500);
|
|
325
|
+
const { count } = await tx.durableStepCheckpoint.deleteMany({
|
|
326
|
+
where: {
|
|
327
|
+
runId,
|
|
328
|
+
seq: {
|
|
329
|
+
in: chunk
|
|
330
|
+
},
|
|
331
|
+
status: "pending"
|
|
332
|
+
}
|
|
333
|
+
});
|
|
334
|
+
if (count !== chunk.length) {
|
|
335
|
+
throw new Error("a checkpoint of the batch was settled while the batch was written");
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
for (let at = 0; at < writes.length; at += 500) {
|
|
340
|
+
await tx.durableStepCheckpoint.createMany({
|
|
341
|
+
data: writes.slice(at, at + 500).map(toCheckpointData)
|
|
295
342
|
});
|
|
296
|
-
wrote.push(count > 0);
|
|
297
343
|
}
|
|
298
344
|
return wrote;
|
|
299
345
|
});
|
|
300
346
|
}
|
|
347
|
+
async saveCheckpoints(checkpoints) {
|
|
348
|
+
if (checkpoints.length === 0) return;
|
|
349
|
+
await this.db.$transaction((tx) => replaceCheckpoints(tx, checkpoints));
|
|
350
|
+
}
|
|
301
351
|
async transaction(work) {
|
|
302
352
|
return this.db.$transaction(async (tx) => work({
|
|
303
353
|
raw: tx,
|
|
@@ -891,36 +941,25 @@ var PrismaStateStore = class {
|
|
|
891
941
|
async registerChildWaits(entries) {
|
|
892
942
|
if (entries.length === 0) return;
|
|
893
943
|
await this.db.$transaction(async (tx) => {
|
|
894
|
-
for (
|
|
895
|
-
const
|
|
896
|
-
|
|
897
|
-
seq: waiter.seq,
|
|
898
|
-
parallelGroup: waiter.parallelGroup ?? null
|
|
899
|
-
};
|
|
900
|
-
await tx.durableSignalWaiter.upsert({
|
|
901
|
-
where: {
|
|
902
|
-
token: waiter.token
|
|
903
|
-
},
|
|
904
|
-
create: {
|
|
905
|
-
token: waiter.token,
|
|
906
|
-
...data
|
|
907
|
-
},
|
|
908
|
-
update: data
|
|
909
|
-
});
|
|
910
|
-
}
|
|
911
|
-
for (const { checkpoint } of entries) {
|
|
912
|
-
const data = toCheckpointData(checkpoint);
|
|
913
|
-
await tx.durableStepCheckpoint.upsert({
|
|
944
|
+
for (let at = 0; at < entries.length; at += 500) {
|
|
945
|
+
const chunk = entries.slice(at, at + 500);
|
|
946
|
+
await tx.durableSignalWaiter.deleteMany({
|
|
914
947
|
where: {
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
seq: checkpoint.seq
|
|
948
|
+
token: {
|
|
949
|
+
in: chunk.map(({ waiter }) => waiter.token)
|
|
918
950
|
}
|
|
919
|
-
}
|
|
920
|
-
|
|
921
|
-
|
|
951
|
+
}
|
|
952
|
+
});
|
|
953
|
+
await tx.durableSignalWaiter.createMany({
|
|
954
|
+
data: chunk.map(({ waiter }) => ({
|
|
955
|
+
token: waiter.token,
|
|
956
|
+
runId: waiter.runId,
|
|
957
|
+
seq: waiter.seq,
|
|
958
|
+
parallelGroup: waiter.parallelGroup ?? null
|
|
959
|
+
}))
|
|
922
960
|
});
|
|
923
961
|
}
|
|
962
|
+
await replaceCheckpoints(tx, entries.map(({ checkpoint }) => checkpoint));
|
|
924
963
|
});
|
|
925
964
|
}
|
|
926
965
|
async compactCheckpoints(runId, fromSeq, toSeq, into) {
|
|
@@ -1022,6 +1061,39 @@ var PrismaStateStore = class {
|
|
|
1022
1061
|
payload: row.payload ?? void 0
|
|
1023
1062
|
};
|
|
1024
1063
|
}
|
|
1064
|
+
async listBufferedSignals(tokenPrefix, afterId, limit) {
|
|
1065
|
+
const rows = await this.db.durableBufferedSignal.findMany({
|
|
1066
|
+
where: {
|
|
1067
|
+
token: {
|
|
1068
|
+
startsWith: tokenPrefix
|
|
1069
|
+
},
|
|
1070
|
+
...afterId !== void 0 ? {
|
|
1071
|
+
id: {
|
|
1072
|
+
gt: Number(afterId)
|
|
1073
|
+
}
|
|
1074
|
+
} : {}
|
|
1075
|
+
},
|
|
1076
|
+
orderBy: {
|
|
1077
|
+
id: "asc"
|
|
1078
|
+
},
|
|
1079
|
+
take: limit
|
|
1080
|
+
});
|
|
1081
|
+
return rows.filter((r) => r.token.startsWith(tokenPrefix)).map((r) => ({
|
|
1082
|
+
id: String(r.id),
|
|
1083
|
+
token: r.token,
|
|
1084
|
+
payload: r.payload ?? void 0
|
|
1085
|
+
}));
|
|
1086
|
+
}
|
|
1087
|
+
async deleteBufferedSignals(ids) {
|
|
1088
|
+
if (ids.length === 0) return;
|
|
1089
|
+
await this.db.durableBufferedSignal.deleteMany({
|
|
1090
|
+
where: {
|
|
1091
|
+
id: {
|
|
1092
|
+
in: ids.map(Number)
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
});
|
|
1096
|
+
}
|
|
1025
1097
|
async bufferEvent(input) {
|
|
1026
1098
|
await this.db.durableBufferedEvent.create({
|
|
1027
1099
|
data: {
|
|
@@ -1234,6 +1306,65 @@ function toCheckpointStat(nameKind, r, unenqueued) {
|
|
|
1234
1306
|
};
|
|
1235
1307
|
}
|
|
1236
1308
|
__name(toCheckpointStat, "toCheckpointStat");
|
|
1309
|
+
function seqsByRun(checkpoints) {
|
|
1310
|
+
const byRun = /* @__PURE__ */ new Map();
|
|
1311
|
+
for (const { runId, seq } of checkpoints) {
|
|
1312
|
+
const seqs = byRun.get(runId) ?? /* @__PURE__ */ new Set();
|
|
1313
|
+
seqs.add(seq);
|
|
1314
|
+
byRun.set(runId, seqs);
|
|
1315
|
+
}
|
|
1316
|
+
return new Map([
|
|
1317
|
+
...byRun
|
|
1318
|
+
].map(([runId, seqs]) => [
|
|
1319
|
+
runId,
|
|
1320
|
+
[
|
|
1321
|
+
...seqs
|
|
1322
|
+
]
|
|
1323
|
+
]));
|
|
1324
|
+
}
|
|
1325
|
+
__name(seqsByRun, "seqsByRun");
|
|
1326
|
+
var checkpointKey = /* @__PURE__ */ __name((runId, seq) => `${runId}\0${seq}`, "checkpointKey");
|
|
1327
|
+
function decideIfPending(checkpoints, pending) {
|
|
1328
|
+
const wrote = [];
|
|
1329
|
+
const writes = /* @__PURE__ */ new Map();
|
|
1330
|
+
for (const checkpoint of checkpoints) {
|
|
1331
|
+
const key = checkpointKey(checkpoint.runId, checkpoint.seq);
|
|
1332
|
+
if (!pending.has(key)) {
|
|
1333
|
+
wrote.push(false);
|
|
1334
|
+
continue;
|
|
1335
|
+
}
|
|
1336
|
+
wrote.push(true);
|
|
1337
|
+
writes.set(key, checkpoint);
|
|
1338
|
+
if (checkpoint.status !== "pending") pending.delete(key);
|
|
1339
|
+
}
|
|
1340
|
+
return {
|
|
1341
|
+
wrote,
|
|
1342
|
+
writes: [
|
|
1343
|
+
...writes.values()
|
|
1344
|
+
]
|
|
1345
|
+
};
|
|
1346
|
+
}
|
|
1347
|
+
__name(decideIfPending, "decideIfPending");
|
|
1348
|
+
async function replaceCheckpoints(tx, checkpoints) {
|
|
1349
|
+
for (const [runId, seqs] of seqsByRun(checkpoints)) {
|
|
1350
|
+
for (let at = 0; at < seqs.length; at += 500) {
|
|
1351
|
+
await tx.durableStepCheckpoint.deleteMany({
|
|
1352
|
+
where: {
|
|
1353
|
+
runId,
|
|
1354
|
+
seq: {
|
|
1355
|
+
in: seqs.slice(at, at + 500)
|
|
1356
|
+
}
|
|
1357
|
+
}
|
|
1358
|
+
});
|
|
1359
|
+
}
|
|
1360
|
+
}
|
|
1361
|
+
for (let at = 0; at < checkpoints.length; at += 500) {
|
|
1362
|
+
await tx.durableStepCheckpoint.createMany({
|
|
1363
|
+
data: checkpoints.slice(at, at + 500).map(toCheckpointData)
|
|
1364
|
+
});
|
|
1365
|
+
}
|
|
1366
|
+
}
|
|
1367
|
+
__name(replaceCheckpoints, "replaceCheckpoints");
|
|
1237
1368
|
function toCheckpointData(cp) {
|
|
1238
1369
|
return {
|
|
1239
1370
|
runId: cp.runId,
|