@dudousxd/nestjs-durable-store-typeorm 0.25.3 → 0.25.4

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,19 @@ 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
+ }
1304
1317
  async saveCheckpoint(checkpoint) {
1305
1318
  const entity = toCheckpointEntity(checkpoint);
1306
1319
  await this.checkpoints().upsert(entity, [
@@ -1308,15 +1321,57 @@ var TypeOrmStateStore = class {
1308
1321
  "seq"
1309
1322
  ]);
1310
1323
  }
1324
+ async saveCheckpoints(checkpoints) {
1325
+ if (checkpoints.length === 0) return;
1326
+ await this.dataSource.transaction(async (em) => {
1327
+ const repo = em.getRepository(StepCheckpointEntity);
1328
+ for (let at = 0; at < checkpoints.length; at += 500) {
1329
+ await repo.upsert(checkpoints.slice(at, at + 500).map((cp) => toCheckpointEntity(cp)), [
1330
+ "runId",
1331
+ "seq"
1332
+ ]);
1333
+ }
1334
+ });
1335
+ }
1311
1336
  async saveCheckpointIfPending(checkpoint) {
1312
1337
  return updateIfPending(this.checkpoints(), checkpoint);
1313
1338
  }
1314
1339
  async saveCheckpointsIfPending(checkpoints) {
1315
1340
  if (checkpoints.length === 0) return [];
1341
+ const lock = ![
1342
+ "sqlite",
1343
+ "better-sqlite3",
1344
+ "sqljs"
1345
+ ].includes(this.dataSource.options.type);
1316
1346
  return this.dataSource.transaction(async (em) => {
1317
1347
  const repo = em.getRepository(StepCheckpointEntity);
1318
- const wrote = [];
1319
- for (const checkpoint of checkpoints) wrote.push(await updateIfPending(repo, checkpoint));
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 { wrote, writes } = decideIfPending(checkpoints, pending);
1366
+ for (let at = 0; at < writes.length; at += 500) {
1367
+ await repo.upsert(writes.slice(at, at + 500).map((cp) => ({
1368
+ ...toCheckpointEntity(cp),
1369
+ wakeAt: cp.wakeAt != null ? new Date(cp.wakeAt) : null
1370
+ })), [
1371
+ "runId",
1372
+ "seq"
1373
+ ]);
1374
+ }
1320
1375
  return wrote;
1321
1376
  });
1322
1377
  }
@@ -2109,6 +2164,45 @@ function runUpdateSet(patch) {
2109
2164
  return update;
2110
2165
  }
2111
2166
  __name(runUpdateSet, "runUpdateSet");
2167
+ function seqsByRun(checkpoints) {
2168
+ const byRun = /* @__PURE__ */ new Map();
2169
+ for (const { runId, seq } of checkpoints) {
2170
+ const seqs = byRun.get(runId) ?? /* @__PURE__ */ new Set();
2171
+ seqs.add(seq);
2172
+ byRun.set(runId, seqs);
2173
+ }
2174
+ return new Map([
2175
+ ...byRun
2176
+ ].map(([runId, seqs]) => [
2177
+ runId,
2178
+ [
2179
+ ...seqs
2180
+ ]
2181
+ ]));
2182
+ }
2183
+ __name(seqsByRun, "seqsByRun");
2184
+ var checkpointKey = /* @__PURE__ */ __name((runId, seq) => `${runId}\0${seq}`, "checkpointKey");
2185
+ function decideIfPending(checkpoints, pending) {
2186
+ const wrote = [];
2187
+ const writes = /* @__PURE__ */ new Map();
2188
+ for (const checkpoint of checkpoints) {
2189
+ const key = checkpointKey(checkpoint.runId, checkpoint.seq);
2190
+ if (!pending.has(key)) {
2191
+ wrote.push(false);
2192
+ continue;
2193
+ }
2194
+ wrote.push(true);
2195
+ writes.set(key, checkpoint);
2196
+ if (checkpoint.status !== "pending") pending.delete(key);
2197
+ }
2198
+ return {
2199
+ wrote,
2200
+ writes: [
2201
+ ...writes.values()
2202
+ ]
2203
+ };
2204
+ }
2205
+ __name(decideIfPending, "decideIfPending");
2112
2206
  async function updateIfPending(repo, checkpoint) {
2113
2207
  const { runId, seq, ...data } = toCheckpointEntity(checkpoint);
2114
2208
  const set = {