@dudousxd/nestjs-durable-store-mikro-orm 0.33.3 → 0.33.5

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
@@ -1334,11 +1334,52 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1334
1334
  });
1335
1335
  return entity ? fromCheckpointEntity(entity) : null;
1336
1336
  }
1337
+ async getCheckpoints(runId, seqs) {
1338
+ if (seqs.length === 0) return [];
1339
+ const em = this.fork();
1340
+ const unique = [
1341
+ ...new Set(seqs)
1342
+ ];
1343
+ const rows = [];
1344
+ for (let at = 0; at < unique.length; at += 500) {
1345
+ const seq = {
1346
+ $in: unique.slice(at, at + 500)
1347
+ };
1348
+ rows.push(...await selectCheckpointRows(em, {
1349
+ runId,
1350
+ seq
1351
+ }));
1352
+ }
1353
+ return rows.sort((a, b) => a.seq - b.seq).map(fromCheckpointEntity);
1354
+ }
1355
+ async getCheckpointsForSettle(runId, seqs) {
1356
+ if (seqs.length === 0) return [];
1357
+ const em = this.fork();
1358
+ const fields = checkpointFieldsWithoutInput(em);
1359
+ const unique = [
1360
+ ...new Set(seqs)
1361
+ ];
1362
+ const rows = [];
1363
+ for (let at = 0; at < unique.length; at += 500) {
1364
+ const seq = {
1365
+ $in: unique.slice(at, at + 500)
1366
+ };
1367
+ rows.push(...await selectCheckpointRows(em, {
1368
+ runId,
1369
+ seq
1370
+ }, fields));
1371
+ }
1372
+ return rows.sort((a, b) => a.seq - b.seq).map(fromCheckpointEntity);
1373
+ }
1337
1374
  async saveCheckpoint(checkpoint) {
1338
1375
  const em = this.fork();
1339
1376
  await em.upsert(StepCheckpointEntity, toCheckpointEntity(checkpoint));
1340
1377
  await em.flush();
1341
1378
  }
1379
+ async saveCheckpoints(checkpoints) {
1380
+ if (checkpoints.length === 0) return;
1381
+ await this.fork().transactional((em) => upsertCheckpointRows(em, checkpoints.map(toCheckpointRow)));
1382
+ }
1342
1383
  async saveCheckpointIfPending(checkpoint) {
1343
1384
  const { runId: _runId, seq: _seq, ...data } = toCheckpointEntity(checkpoint);
1344
1385
  const affected = await this.fork().nativeUpdate(StepCheckpointEntity, {
@@ -1354,18 +1395,69 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1354
1395
  async saveCheckpointsIfPending(checkpoints) {
1355
1396
  if (checkpoints.length === 0) return [];
1356
1397
  return this.fork().transactional(async (em) => {
1398
+ const pending = await lockPendingCheckpoints(em, checkpoints, [
1399
+ "runId",
1400
+ "seq"
1401
+ ]);
1357
1402
  const wrote = [];
1403
+ const writes = /* @__PURE__ */ new Map();
1358
1404
  for (const checkpoint of checkpoints) {
1359
- const { runId: _runId, seq: _seq, ...data } = toCheckpointEntity(checkpoint);
1360
- const affected = await em.nativeUpdate(StepCheckpointEntity, {
1361
- runId: checkpoint.runId,
1362
- seq: checkpoint.seq,
1363
- status: "pending"
1364
- }, {
1365
- ...data,
1366
- wakeAt: checkpoint.wakeAt != null ? new Date(checkpoint.wakeAt) : null
1405
+ const k = checkpointKey(checkpoint.runId, checkpoint.seq);
1406
+ if (!pending.has(k)) {
1407
+ wrote.push(false);
1408
+ continue;
1409
+ }
1410
+ wrote.push(true);
1411
+ writes.set(k, checkpoint);
1412
+ if (checkpoint.status !== "pending") pending.delete(k);
1413
+ }
1414
+ await upsertCheckpointRows(em, [
1415
+ ...writes.values()
1416
+ ].map(toCheckpointRow));
1417
+ return wrote;
1418
+ });
1419
+ }
1420
+ async settleCheckpointsIfPending(settlements) {
1421
+ if (settlements.length === 0) return [];
1422
+ return this.fork().transactional(async (em) => {
1423
+ const pending = await lockPendingCheckpoints(em, settlements, [
1424
+ "runId",
1425
+ "seq",
1426
+ "name",
1427
+ "kind",
1428
+ "stepId",
1429
+ "attempts"
1430
+ ]);
1431
+ const wrote = [];
1432
+ const rows = [];
1433
+ for (const s of settlements) {
1434
+ const k = checkpointKey(s.runId, s.seq);
1435
+ const row = pending.get(k);
1436
+ wrote.push(row !== void 0);
1437
+ if (!row) continue;
1438
+ pending.delete(k);
1439
+ rows.push({
1440
+ runId: row.runId,
1441
+ seq: row.seq,
1442
+ name: row.name,
1443
+ kind: row.kind,
1444
+ stepId: row.stepId,
1445
+ attempts: row.attempts,
1446
+ status: s.status,
1447
+ output: s.output ?? null,
1448
+ error: s.error ?? null,
1449
+ events: s.events ?? null,
1450
+ startedAt: s.startedAt,
1451
+ finishedAt: s.finishedAt
1367
1452
  });
1368
- wrote.push(affected > 0);
1453
+ }
1454
+ for (let at = 0; at < rows.length; at += 500) {
1455
+ await em.createQueryBuilder(StepCheckpointEntity).insert(rows.slice(at, at + 500)).onConflict([
1456
+ "runId",
1457
+ "seq"
1458
+ ]).merge([
1459
+ ...SETTLED_FIELDS
1460
+ ]).execute();
1369
1461
  }
1370
1462
  return wrote;
1371
1463
  });
@@ -1883,14 +1975,33 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1883
1975
  return (await qb.getResultList()).map(fromScheduleEntity);
1884
1976
  }
1885
1977
  async listCheckpoints(runId) {
1978
+ const rows = await selectCheckpointRows(this.fork(), {
1979
+ runId
1980
+ });
1981
+ return rows.map(fromCheckpointEntity);
1982
+ }
1983
+ async listCheckpointsForReplay(runId) {
1886
1984
  const em = this.fork();
1887
- const rows = await em.find(StepCheckpointEntity, {
1985
+ const rows = await selectCheckpointRows(em, {
1888
1986
  runId
1889
- }, {
1890
- orderBy: {
1891
- seq: "asc"
1987
+ }, checkpointFieldsWithoutInput(em));
1988
+ const open = rows.filter((row) => row.status !== "completed").map((row) => row.seq);
1989
+ if (open.length > 0) {
1990
+ const inputs = /* @__PURE__ */ new Map();
1991
+ for (let at = 0; at < open.length; at += 500) {
1992
+ const found = await selectCheckpointRows(em, {
1993
+ runId,
1994
+ seq: {
1995
+ $in: open.slice(at, at + 500)
1996
+ }
1997
+ }, [
1998
+ "seq",
1999
+ "input"
2000
+ ]);
2001
+ for (const row of found) inputs.set(row.seq, row.input);
1892
2002
  }
1893
- });
2003
+ for (const row of rows) if (inputs.has(row.seq)) row.input = inputs.get(row.seq);
2004
+ }
1894
2005
  return rows.map(fromCheckpointEntity);
1895
2006
  }
1896
2007
  async getLatestCheckpointByName(runId, name) {
@@ -2315,6 +2426,90 @@ function aggregateDateReader(em) {
2315
2426
  };
2316
2427
  }
2317
2428
  __name(aggregateDateReader, "aggregateDateReader");
2429
+ function checkpointFieldsWithoutInput(em) {
2430
+ return Object.keys(em.getMetadata().get(StepCheckpointEntity).properties).filter((name) => name !== "input");
2431
+ }
2432
+ __name(checkpointFieldsWithoutInput, "checkpointFieldsWithoutInput");
2433
+ async function selectCheckpointRows(em, where, fields) {
2434
+ const query = em.createQueryBuilder(StepCheckpointEntity);
2435
+ if (fields) query.select(fields);
2436
+ const rows = await query.where(where).orderBy({
2437
+ seq: "asc"
2438
+ }).execute("all", true);
2439
+ const platform = em.getPlatform();
2440
+ const custom = Object.values(em.getMetadata().get(StepCheckpointEntity).properties).filter((prop) => prop.customType);
2441
+ for (const row of rows) {
2442
+ for (const prop of custom) {
2443
+ const value = row[prop.name];
2444
+ if (value != null) row[prop.name] = prop.customType?.convertToJSValue(value, platform);
2445
+ }
2446
+ }
2447
+ return rows;
2448
+ }
2449
+ __name(selectCheckpointRows, "selectCheckpointRows");
2450
+ var SETTLED_FIELDS = [
2451
+ "status",
2452
+ "output",
2453
+ "error",
2454
+ "events",
2455
+ "startedAt",
2456
+ "finishedAt"
2457
+ ];
2458
+ var checkpointKey = /* @__PURE__ */ __name((runId, seq) => `${runId}\0${seq}`, "checkpointKey");
2459
+ async function lockPendingCheckpoints(em, entries, fields) {
2460
+ const seqsByRun = /* @__PURE__ */ new Map();
2461
+ for (const { runId, seq } of entries) {
2462
+ const seqs = seqsByRun.get(runId) ?? /* @__PURE__ */ new Set();
2463
+ seqs.add(seq);
2464
+ seqsByRun.set(runId, seqs);
2465
+ }
2466
+ const pending = /* @__PURE__ */ new Map();
2467
+ for (const [runId, seqSet] of seqsByRun) {
2468
+ const seqs = [
2469
+ ...seqSet
2470
+ ];
2471
+ for (let at = 0; at < seqs.length; at += 500) {
2472
+ const rows = await em.find(StepCheckpointEntity, {
2473
+ runId,
2474
+ seq: {
2475
+ $in: seqs.slice(at, at + 500)
2476
+ },
2477
+ status: "pending"
2478
+ }, {
2479
+ fields,
2480
+ lockMode: import_core3.LockMode.PESSIMISTIC_WRITE,
2481
+ disableIdentityMap: true
2482
+ });
2483
+ for (const row of rows) pending.set(checkpointKey(row.runId, row.seq), row);
2484
+ }
2485
+ }
2486
+ return pending;
2487
+ }
2488
+ __name(lockPendingCheckpoints, "lockPendingCheckpoints");
2489
+ async function upsertCheckpointRows(em, rows) {
2490
+ for (let at = 0; at < rows.length; at += 500) {
2491
+ await em.createQueryBuilder(StepCheckpointEntity).insert(rows.slice(at, at + 500).map((row) => ({
2492
+ ...row
2493
+ }))).onConflict([
2494
+ "runId",
2495
+ "seq"
2496
+ ]).merge().execute();
2497
+ }
2498
+ }
2499
+ __name(upsertCheckpointRows, "upsertCheckpointRows");
2500
+ function toCheckpointRow(cp) {
2501
+ const row = toCheckpointEntity(cp);
2502
+ for (const key of [
2503
+ "workerGroup",
2504
+ "parallelGroup",
2505
+ "wakeAt",
2506
+ "enqueuedAt"
2507
+ ]) {
2508
+ if (row[key] === void 0) row[key] = null;
2509
+ }
2510
+ return row;
2511
+ }
2512
+ __name(toCheckpointRow, "toCheckpointRow");
2318
2513
  function toCheckpointEntity(cp) {
2319
2514
  const e = new StepCheckpointEntity();
2320
2515
  e.runId = cp.runId;