@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 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 wrote = [];
311
- for (const checkpoint of checkpoints) {
312
- const { runId, seq, ...data } = toCheckpointData(checkpoint);
313
- const { count } = await tx.durableStepCheckpoint.updateMany({
314
- where: {
315
- runId,
316
- seq,
317
- status: "pending"
318
- },
319
- data
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 (const { waiter } of entries) {
920
- const data = {
921
- runId: waiter.runId,
922
- seq: waiter.seq,
923
- parallelGroup: waiter.parallelGroup ?? null
924
- };
925
- await tx.durableSignalWaiter.upsert({
969
+ for (let at = 0; at < entries.length; at += 500) {
970
+ const chunk = entries.slice(at, at + 500);
971
+ await tx.durableSignalWaiter.deleteMany({
926
972
  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({
939
- where: {
940
- runId_seq: {
941
- runId: checkpoint.runId,
942
- seq: checkpoint.seq
973
+ token: {
974
+ in: chunk.map(({ waiter }) => waiter.token)
943
975
  }
944
- },
945
- create: data,
946
- update: data
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) {
@@ -1292,6 +1331,65 @@ function toCheckpointStat(nameKind, r, unenqueued) {
1292
1331
  };
1293
1332
  }
1294
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");
1295
1393
  function toCheckpointData(cp) {
1296
1394
  return {
1297
1395
  runId: cp.runId,