@dudousxd/nestjs-durable-store-typeorm 0.25.4 → 0.25.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
@@ -1314,6 +1314,28 @@ var TypeOrmStateStore = class {
1314
1314
  }
1315
1315
  return rows.sort((a, b) => a.seq - b.seq).map(fromCheckpointEntity);
1316
1316
  }
1317
+ async getCheckpointsForSettle(runId, seqs) {
1318
+ const repo = this.checkpoints();
1319
+ const select = this.checkpointColumnsWithoutInput();
1320
+ const unique = [
1321
+ ...new Set(seqs)
1322
+ ];
1323
+ const rows = [];
1324
+ for (let at = 0; at < unique.length; at += 500) {
1325
+ rows.push(...await repo.find({
1326
+ select,
1327
+ where: {
1328
+ runId,
1329
+ seq: (0, import_typeorm2.In)(unique.slice(at, at + 500))
1330
+ }
1331
+ }));
1332
+ }
1333
+ return rows.sort((a, b) => a.seq - b.seq).map(fromCheckpointEntity);
1334
+ }
1335
+ /** Every checkpoint property but `input`, for a read that leaves the column out. */
1336
+ checkpointColumnsWithoutInput() {
1337
+ return this.checkpoints().metadata.columns.map((column) => column.propertyName).filter((name) => name !== "input");
1338
+ }
1317
1339
  async saveCheckpoint(checkpoint) {
1318
1340
  const entity = toCheckpointEntity(checkpoint);
1319
1341
  await this.checkpoints().upsert(entity, [
@@ -1338,30 +1360,9 @@ var TypeOrmStateStore = class {
1338
1360
  }
1339
1361
  async saveCheckpointsIfPending(checkpoints) {
1340
1362
  if (checkpoints.length === 0) return [];
1341
- const lock = ![
1342
- "sqlite",
1343
- "better-sqlite3",
1344
- "sqljs"
1345
- ].includes(this.dataSource.options.type);
1346
1363
  return this.dataSource.transaction(async (em) => {
1347
1364
  const repo = em.getRepository(StepCheckpointEntity);
1348
- const pending = /* @__PURE__ */ new Set();
1349
- for (const [runId, seqs] of seqsByRun(checkpoints)) {
1350
- for (let at = 0; at < seqs.length; at += 500) {
1351
- const query = repo.createQueryBuilder("c").select([
1352
- "c.runId",
1353
- "c.seq"
1354
- ]).where("c.runId = :runId", {
1355
- runId
1356
- }).andWhere("c.seq IN (:...seqs)", {
1357
- seqs: seqs.slice(at, at + 500)
1358
- }).andWhere("c.status = :status", {
1359
- status: "pending"
1360
- });
1361
- if (lock) query.setLock("pessimistic_write");
1362
- for (const row of await query.getMany()) pending.add(checkpointKey(row.runId, row.seq));
1363
- }
1364
- }
1365
+ const pending = new Set((await this.lockPendingCheckpoints(repo, checkpoints, [])).keys());
1365
1366
  const { wrote, writes } = decideIfPending(checkpoints, pending);
1366
1367
  for (let at = 0; at < writes.length; at += 500) {
1367
1368
  await repo.upsert(writes.slice(at, at + 500).map((cp) => ({
@@ -1375,6 +1376,82 @@ var TypeOrmStateStore = class {
1375
1376
  return wrote;
1376
1377
  });
1377
1378
  }
1379
+ async settleCheckpointsIfPending(settlements) {
1380
+ if (settlements.length === 0) return [];
1381
+ return this.dataSource.transaction(async (em) => {
1382
+ const repo = em.getRepository(StepCheckpointEntity);
1383
+ const pending = await this.lockPendingCheckpoints(repo, settlements, [
1384
+ "name",
1385
+ "kind",
1386
+ "stepId",
1387
+ "attempts"
1388
+ ]);
1389
+ const wrote = [];
1390
+ const rows = [];
1391
+ for (const s of settlements) {
1392
+ const key = checkpointKey(s.runId, s.seq);
1393
+ const row = pending.get(key);
1394
+ wrote.push(row !== void 0);
1395
+ if (!row) continue;
1396
+ pending.delete(key);
1397
+ rows.push({
1398
+ runId: row.runId,
1399
+ seq: row.seq,
1400
+ name: row.name,
1401
+ kind: row.kind,
1402
+ stepId: row.stepId,
1403
+ attempts: row.attempts,
1404
+ status: s.status,
1405
+ output: s.output ?? null,
1406
+ error: s.error ?? null,
1407
+ events: s.events ?? null,
1408
+ startedAt: s.startedAt,
1409
+ finishedAt: s.finishedAt
1410
+ });
1411
+ }
1412
+ const column = /* @__PURE__ */ __name((property) => repo.metadata.findColumnWithPropertyName(property)?.databaseName ?? property, "column");
1413
+ for (let at = 0; at < rows.length; at += 500) {
1414
+ await repo.createQueryBuilder().insert().values(rows.slice(at, at + 500)).orUpdate(SETTLED_FIELDS.map(column), [
1415
+ "runId",
1416
+ "seq"
1417
+ ].map(column)).execute();
1418
+ }
1419
+ return wrote;
1420
+ });
1421
+ }
1422
+ /**
1423
+ * Lock the rows of `entries` that are still `pending` (`SELECT … FOR UPDATE` in chunks, except on
1424
+ * SQLite, whose write transaction already excludes other writers), reading their key and `fields`,
1425
+ * keyed by {@link checkpointKey}. Runs inside the caller's transaction.
1426
+ */
1427
+ async lockPendingCheckpoints(repo, entries, fields) {
1428
+ const lock = ![
1429
+ "sqlite",
1430
+ "better-sqlite3",
1431
+ "sqljs"
1432
+ ].includes(this.dataSource.options.type);
1433
+ const pending = /* @__PURE__ */ new Map();
1434
+ for (const [runId, seqs] of seqsByRun(entries)) {
1435
+ for (let at = 0; at < seqs.length; at += 500) {
1436
+ const query = repo.createQueryBuilder("c").select([
1437
+ "c.runId",
1438
+ "c.seq",
1439
+ ...fields.map((f) => `c.${f}`)
1440
+ ]).where("c.runId = :runId", {
1441
+ runId
1442
+ }).andWhere("c.seq IN (:...seqs)", {
1443
+ seqs: seqs.slice(at, at + 500)
1444
+ }).andWhere("c.status = :status", {
1445
+ status: "pending"
1446
+ });
1447
+ if (lock) query.setLock("pessimistic_write");
1448
+ for (const row of await query.getMany()) {
1449
+ pending.set(checkpointKey(row.runId, row.seq), row);
1450
+ }
1451
+ }
1452
+ }
1453
+ return pending;
1454
+ }
1378
1455
  async transaction(work) {
1379
1456
  return this.dataSource.transaction(async (em) => work({
1380
1457
  raw: em,
@@ -1713,6 +1790,37 @@ var TypeOrmStateStore = class {
1713
1790
  });
1714
1791
  return rows.map(fromCheckpointEntity);
1715
1792
  }
1793
+ async listCheckpointsForReplay(runId) {
1794
+ const repo = this.checkpoints();
1795
+ const rows = await repo.find({
1796
+ select: this.checkpointColumnsWithoutInput(),
1797
+ where: {
1798
+ runId
1799
+ },
1800
+ order: {
1801
+ seq: "ASC"
1802
+ }
1803
+ });
1804
+ const open = rows.filter((row) => row.status !== "completed").map((row) => row.seq);
1805
+ const inputs = /* @__PURE__ */ new Map();
1806
+ for (let at = 0; at < open.length; at += 500) {
1807
+ const found = await repo.find({
1808
+ select: [
1809
+ "seq",
1810
+ "input"
1811
+ ],
1812
+ where: {
1813
+ runId,
1814
+ seq: (0, import_typeorm2.In)(open.slice(at, at + 500))
1815
+ }
1816
+ });
1817
+ for (const row of found) inputs.set(row.seq, row.input);
1818
+ }
1819
+ return rows.map((row) => fromCheckpointEntity({
1820
+ ...row,
1821
+ input: inputs.get(row.seq) ?? null
1822
+ }));
1823
+ }
1716
1824
  async getLatestCheckpointByName(runId, name) {
1717
1825
  const e = await this.checkpoints().findOne({
1718
1826
  where: {
@@ -2164,6 +2272,14 @@ function runUpdateSet(patch) {
2164
2272
  return update;
2165
2273
  }
2166
2274
  __name(runUpdateSet, "runUpdateSet");
2275
+ var SETTLED_FIELDS = [
2276
+ "status",
2277
+ "output",
2278
+ "error",
2279
+ "events",
2280
+ "startedAt",
2281
+ "finishedAt"
2282
+ ];
2167
2283
  function seqsByRun(checkpoints) {
2168
2284
  const byRun = /* @__PURE__ */ new Map();
2169
2285
  for (const { runId, seq } of checkpoints) {