@dudousxd/nestjs-durable-store-prisma 0.24.2 → 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 +135 -37
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +135 -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({
|
|
944
|
+
for (let at = 0; at < entries.length; at += 500) {
|
|
945
|
+
const chunk = entries.slice(at, at + 500);
|
|
946
|
+
await tx.durableSignalWaiter.deleteMany({
|
|
901
947
|
where: {
|
|
902
|
-
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({
|
|
914
|
-
where: {
|
|
915
|
-
runId_seq: {
|
|
916
|
-
runId: checkpoint.runId,
|
|
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) {
|
|
@@ -1267,6 +1306,65 @@ function toCheckpointStat(nameKind, r, unenqueued) {
|
|
|
1267
1306
|
};
|
|
1268
1307
|
}
|
|
1269
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");
|
|
1270
1368
|
function toCheckpointData(cp) {
|
|
1271
1369
|
return {
|
|
1272
1370
|
runId: cp.runId,
|