@dudousxd/nestjs-durable-store-mikro-orm 0.33.4 → 0.33.6

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
@@ -1345,13 +1345,32 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1345
1345
  const seq = {
1346
1346
  $in: unique.slice(at, at + 500)
1347
1347
  };
1348
- rows.push(...await em.find(StepCheckpointEntity, {
1348
+ rows.push(...await selectCheckpointRows(em, {
1349
1349
  runId,
1350
1350
  seq
1351
1351
  }));
1352
1352
  }
1353
1353
  return rows.sort((a, b) => a.seq - b.seq).map(fromCheckpointEntity);
1354
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
+ }
1355
1374
  async saveCheckpoint(checkpoint) {
1356
1375
  const em = this.fork();
1357
1376
  await em.upsert(StepCheckpointEntity, toCheckpointEntity(checkpoint));
@@ -1359,11 +1378,7 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1359
1378
  }
1360
1379
  async saveCheckpoints(checkpoints) {
1361
1380
  if (checkpoints.length === 0) return;
1362
- await this.fork().transactional(async (em) => {
1363
- for (let at = 0; at < checkpoints.length; at += 500) {
1364
- await em.upsertMany(StepCheckpointEntity, checkpoints.slice(at, at + 500).map(toCheckpointRow));
1365
- }
1366
- });
1381
+ await this.fork().transactional((em) => upsertCheckpointRows(em, checkpoints.map(toCheckpointRow)));
1367
1382
  }
1368
1383
  async saveCheckpointIfPending(checkpoint) {
1369
1384
  const { runId: _runId, seq: _seq, ...data } = toCheckpointEntity(checkpoint);
@@ -1380,40 +1395,14 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1380
1395
  async saveCheckpointsIfPending(checkpoints) {
1381
1396
  if (checkpoints.length === 0) return [];
1382
1397
  return this.fork().transactional(async (em) => {
1383
- const key = /* @__PURE__ */ __name((runId, seq) => `${runId}\0${seq}`, "key");
1384
- const seqsByRun = /* @__PURE__ */ new Map();
1385
- for (const { runId, seq } of checkpoints) {
1386
- const seqs = seqsByRun.get(runId) ?? /* @__PURE__ */ new Set();
1387
- seqs.add(seq);
1388
- seqsByRun.set(runId, seqs);
1389
- }
1390
- const pending = /* @__PURE__ */ new Set();
1391
- for (const [runId, seqSet] of seqsByRun) {
1392
- const seqs = [
1393
- ...seqSet
1394
- ];
1395
- for (let at = 0; at < seqs.length; at += 500) {
1396
- const rows2 = await em.find(StepCheckpointEntity, {
1397
- runId,
1398
- seq: {
1399
- $in: seqs.slice(at, at + 500)
1400
- },
1401
- status: "pending"
1402
- }, {
1403
- fields: [
1404
- "runId",
1405
- "seq"
1406
- ],
1407
- lockMode: import_core3.LockMode.PESSIMISTIC_WRITE,
1408
- disableIdentityMap: true
1409
- });
1410
- for (const row of rows2) pending.add(key(row.runId, row.seq));
1411
- }
1412
- }
1398
+ const pending = await lockPendingCheckpoints(em, checkpoints, [
1399
+ "runId",
1400
+ "seq"
1401
+ ]);
1413
1402
  const wrote = [];
1414
1403
  const writes = /* @__PURE__ */ new Map();
1415
1404
  for (const checkpoint of checkpoints) {
1416
- const k = key(checkpoint.runId, checkpoint.seq);
1405
+ const k = checkpointKey(checkpoint.runId, checkpoint.seq);
1417
1406
  if (!pending.has(k)) {
1418
1407
  wrote.push(false);
1419
1408
  continue;
@@ -1422,11 +1411,53 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1422
1411
  writes.set(k, checkpoint);
1423
1412
  if (checkpoint.status !== "pending") pending.delete(k);
1424
1413
  }
1425
- const rows = [
1414
+ await upsertCheckpointRows(em, [
1426
1415
  ...writes.values()
1427
- ].map(toCheckpointRow);
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
1452
+ });
1453
+ }
1428
1454
  for (let at = 0; at < rows.length; at += 500) {
1429
- await em.upsertMany(StepCheckpointEntity, rows.slice(at, 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();
1430
1461
  }
1431
1462
  return wrote;
1432
1463
  });
@@ -1944,14 +1975,33 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1944
1975
  return (await qb.getResultList()).map(fromScheduleEntity);
1945
1976
  }
1946
1977
  async listCheckpoints(runId) {
1978
+ const rows = await selectCheckpointRows(this.fork(), {
1979
+ runId
1980
+ });
1981
+ return rows.map(fromCheckpointEntity);
1982
+ }
1983
+ async listCheckpointsForReplay(runId) {
1947
1984
  const em = this.fork();
1948
- const rows = await em.find(StepCheckpointEntity, {
1985
+ const rows = await selectCheckpointRows(em, {
1949
1986
  runId
1950
- }, {
1951
- orderBy: {
1952
- 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);
1953
2002
  }
1954
- });
2003
+ for (const row of rows) if (inputs.has(row.seq)) row.input = inputs.get(row.seq);
2004
+ }
1955
2005
  return rows.map(fromCheckpointEntity);
1956
2006
  }
1957
2007
  async getLatestCheckpointByName(runId, name) {
@@ -1966,6 +2016,27 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1966
2016
  });
1967
2017
  return entity ? fromCheckpointEntity(entity) : void 0;
1968
2018
  }
2019
+ async hasCheckpoints(runId, fromSeq, toSeq, statuses) {
2020
+ if (statuses.length === 0) return false;
2021
+ const row = await this.fork().findOne(StepCheckpointEntity, {
2022
+ runId,
2023
+ seq: {
2024
+ $gte: fromSeq,
2025
+ $lte: toSeq
2026
+ },
2027
+ status: {
2028
+ $in: statuses
2029
+ }
2030
+ }, {
2031
+ fields: [
2032
+ "seq"
2033
+ ],
2034
+ orderBy: {
2035
+ seq: "desc"
2036
+ }
2037
+ });
2038
+ return row !== null;
2039
+ }
1969
2040
  async countCheckpoints(runId, fromSeq, toSeq, statuses) {
1970
2041
  if (statuses.length === 0) return 0;
1971
2042
  return this.fork().count(StepCheckpointEntity, {
@@ -2376,6 +2447,77 @@ function aggregateDateReader(em) {
2376
2447
  };
2377
2448
  }
2378
2449
  __name(aggregateDateReader, "aggregateDateReader");
2450
+ function checkpointFieldsWithoutInput(em) {
2451
+ return Object.keys(em.getMetadata().get(StepCheckpointEntity).properties).filter((name) => name !== "input");
2452
+ }
2453
+ __name(checkpointFieldsWithoutInput, "checkpointFieldsWithoutInput");
2454
+ async function selectCheckpointRows(em, where, fields) {
2455
+ const query = em.createQueryBuilder(StepCheckpointEntity);
2456
+ if (fields) query.select(fields);
2457
+ const rows = await query.where(where).orderBy({
2458
+ seq: "asc"
2459
+ }).execute("all", true);
2460
+ const platform = em.getPlatform();
2461
+ const custom = Object.values(em.getMetadata().get(StepCheckpointEntity).properties).filter((prop) => prop.customType);
2462
+ for (const row of rows) {
2463
+ for (const prop of custom) {
2464
+ const value = row[prop.name];
2465
+ if (value != null) row[prop.name] = prop.customType?.convertToJSValue(value, platform);
2466
+ }
2467
+ }
2468
+ return rows;
2469
+ }
2470
+ __name(selectCheckpointRows, "selectCheckpointRows");
2471
+ var SETTLED_FIELDS = [
2472
+ "status",
2473
+ "output",
2474
+ "error",
2475
+ "events",
2476
+ "startedAt",
2477
+ "finishedAt"
2478
+ ];
2479
+ var checkpointKey = /* @__PURE__ */ __name((runId, seq) => `${runId}\0${seq}`, "checkpointKey");
2480
+ async function lockPendingCheckpoints(em, entries, fields) {
2481
+ const seqsByRun = /* @__PURE__ */ new Map();
2482
+ for (const { runId, seq } of entries) {
2483
+ const seqs = seqsByRun.get(runId) ?? /* @__PURE__ */ new Set();
2484
+ seqs.add(seq);
2485
+ seqsByRun.set(runId, seqs);
2486
+ }
2487
+ const pending = /* @__PURE__ */ new Map();
2488
+ for (const [runId, seqSet] of seqsByRun) {
2489
+ const seqs = [
2490
+ ...seqSet
2491
+ ];
2492
+ for (let at = 0; at < seqs.length; at += 500) {
2493
+ const rows = await em.find(StepCheckpointEntity, {
2494
+ runId,
2495
+ seq: {
2496
+ $in: seqs.slice(at, at + 500)
2497
+ },
2498
+ status: "pending"
2499
+ }, {
2500
+ fields,
2501
+ lockMode: import_core3.LockMode.PESSIMISTIC_WRITE,
2502
+ disableIdentityMap: true
2503
+ });
2504
+ for (const row of rows) pending.set(checkpointKey(row.runId, row.seq), row);
2505
+ }
2506
+ }
2507
+ return pending;
2508
+ }
2509
+ __name(lockPendingCheckpoints, "lockPendingCheckpoints");
2510
+ async function upsertCheckpointRows(em, rows) {
2511
+ for (let at = 0; at < rows.length; at += 500) {
2512
+ await em.createQueryBuilder(StepCheckpointEntity).insert(rows.slice(at, at + 500).map((row) => ({
2513
+ ...row
2514
+ }))).onConflict([
2515
+ "runId",
2516
+ "seq"
2517
+ ]).merge().execute();
2518
+ }
2519
+ }
2520
+ __name(upsertCheckpointRows, "upsertCheckpointRows");
2379
2521
  function toCheckpointRow(cp) {
2380
2522
  const row = toCheckpointEntity(cp);
2381
2523
  for (const key of [