@dudousxd/nestjs-durable-store-typeorm 0.25.3 → 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
@@ -1301,6 +1301,41 @@ var TypeOrmStateStore = class {
1301
1301
  });
1302
1302
  return e ? fromCheckpointEntity(e) : null;
1303
1303
  }
1304
+ async getCheckpoints(runId, seqs) {
1305
+ const unique = [
1306
+ ...new Set(seqs)
1307
+ ];
1308
+ const rows = [];
1309
+ for (let at = 0; at < unique.length; at += 500) {
1310
+ rows.push(...await this.checkpoints().findBy({
1311
+ runId,
1312
+ seq: (0, import_typeorm2.In)(unique.slice(at, at + 500))
1313
+ }));
1314
+ }
1315
+ return rows.sort((a, b) => a.seq - b.seq).map(fromCheckpointEntity);
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
+ }
1304
1339
  async saveCheckpoint(checkpoint) {
1305
1340
  const entity = toCheckpointEntity(checkpoint);
1306
1341
  await this.checkpoints().upsert(entity, [
@@ -1308,6 +1343,18 @@ var TypeOrmStateStore = class {
1308
1343
  "seq"
1309
1344
  ]);
1310
1345
  }
1346
+ async saveCheckpoints(checkpoints) {
1347
+ if (checkpoints.length === 0) return;
1348
+ await this.dataSource.transaction(async (em) => {
1349
+ const repo = em.getRepository(StepCheckpointEntity);
1350
+ for (let at = 0; at < checkpoints.length; at += 500) {
1351
+ await repo.upsert(checkpoints.slice(at, at + 500).map((cp) => toCheckpointEntity(cp)), [
1352
+ "runId",
1353
+ "seq"
1354
+ ]);
1355
+ }
1356
+ });
1357
+ }
1311
1358
  async saveCheckpointIfPending(checkpoint) {
1312
1359
  return updateIfPending(this.checkpoints(), checkpoint);
1313
1360
  }
@@ -1315,11 +1362,96 @@ var TypeOrmStateStore = class {
1315
1362
  if (checkpoints.length === 0) return [];
1316
1363
  return this.dataSource.transaction(async (em) => {
1317
1364
  const repo = em.getRepository(StepCheckpointEntity);
1365
+ const pending = new Set((await this.lockPendingCheckpoints(repo, checkpoints, [])).keys());
1366
+ const { wrote, writes } = decideIfPending(checkpoints, pending);
1367
+ for (let at = 0; at < writes.length; at += 500) {
1368
+ await repo.upsert(writes.slice(at, at + 500).map((cp) => ({
1369
+ ...toCheckpointEntity(cp),
1370
+ wakeAt: cp.wakeAt != null ? new Date(cp.wakeAt) : null
1371
+ })), [
1372
+ "runId",
1373
+ "seq"
1374
+ ]);
1375
+ }
1376
+ return wrote;
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
+ ]);
1318
1389
  const wrote = [];
1319
- for (const checkpoint of checkpoints) wrote.push(await updateIfPending(repo, checkpoint));
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
+ }
1320
1419
  return wrote;
1321
1420
  });
1322
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
+ }
1323
1455
  async transaction(work) {
1324
1456
  return this.dataSource.transaction(async (em) => work({
1325
1457
  raw: em,
@@ -1658,6 +1790,37 @@ var TypeOrmStateStore = class {
1658
1790
  });
1659
1791
  return rows.map(fromCheckpointEntity);
1660
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
+ }
1661
1824
  async getLatestCheckpointByName(runId, name) {
1662
1825
  const e = await this.checkpoints().findOne({
1663
1826
  where: {
@@ -2109,6 +2272,53 @@ function runUpdateSet(patch) {
2109
2272
  return update;
2110
2273
  }
2111
2274
  __name(runUpdateSet, "runUpdateSet");
2275
+ var SETTLED_FIELDS = [
2276
+ "status",
2277
+ "output",
2278
+ "error",
2279
+ "events",
2280
+ "startedAt",
2281
+ "finishedAt"
2282
+ ];
2283
+ function seqsByRun(checkpoints) {
2284
+ const byRun = /* @__PURE__ */ new Map();
2285
+ for (const { runId, seq } of checkpoints) {
2286
+ const seqs = byRun.get(runId) ?? /* @__PURE__ */ new Set();
2287
+ seqs.add(seq);
2288
+ byRun.set(runId, seqs);
2289
+ }
2290
+ return new Map([
2291
+ ...byRun
2292
+ ].map(([runId, seqs]) => [
2293
+ runId,
2294
+ [
2295
+ ...seqs
2296
+ ]
2297
+ ]));
2298
+ }
2299
+ __name(seqsByRun, "seqsByRun");
2300
+ var checkpointKey = /* @__PURE__ */ __name((runId, seq) => `${runId}\0${seq}`, "checkpointKey");
2301
+ function decideIfPending(checkpoints, pending) {
2302
+ const wrote = [];
2303
+ const writes = /* @__PURE__ */ new Map();
2304
+ for (const checkpoint of checkpoints) {
2305
+ const key = checkpointKey(checkpoint.runId, checkpoint.seq);
2306
+ if (!pending.has(key)) {
2307
+ wrote.push(false);
2308
+ continue;
2309
+ }
2310
+ wrote.push(true);
2311
+ writes.set(key, checkpoint);
2312
+ if (checkpoint.status !== "pending") pending.delete(key);
2313
+ }
2314
+ return {
2315
+ wrote,
2316
+ writes: [
2317
+ ...writes.values()
2318
+ ]
2319
+ };
2320
+ }
2321
+ __name(decideIfPending, "decideIfPending");
2112
2322
  async function updateIfPending(repo, checkpoint) {
2113
2323
  const { runId, seq, ...data } = toCheckpointEntity(checkpoint);
2114
2324
  const set = {