@dudousxd/nestjs-durable-store-mikro-orm 0.33.3 → 0.33.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.d.cts CHANGED
@@ -311,7 +311,9 @@ declare class MikroOrmStateStore implements StateStore {
311
311
  deleteRuns(runIds: string[]): Promise<void>;
312
312
  pruneTerminalRuns(policy: RetentionPolicy, nowMs: number, limit: number): Promise<number>;
313
313
  getCheckpoint(runId: string, seq: number): Promise<StepCheckpoint | null>;
314
+ getCheckpoints(runId: string, seqs: number[]): Promise<StepCheckpoint[]>;
314
315
  saveCheckpoint(checkpoint: StepCheckpoint): Promise<void>;
316
+ saveCheckpoints(checkpoints: StepCheckpoint[]): Promise<void>;
315
317
  saveCheckpointIfPending(checkpoint: StepCheckpoint): Promise<boolean>;
316
318
  saveCheckpointsIfPending(checkpoints: StepCheckpoint[]): Promise<boolean[]>;
317
319
  transaction<T>(work: (tx: {
package/dist/index.d.ts CHANGED
@@ -311,7 +311,9 @@ declare class MikroOrmStateStore implements StateStore {
311
311
  deleteRuns(runIds: string[]): Promise<void>;
312
312
  pruneTerminalRuns(policy: RetentionPolicy, nowMs: number, limit: number): Promise<number>;
313
313
  getCheckpoint(runId: string, seq: number): Promise<StepCheckpoint | null>;
314
+ getCheckpoints(runId: string, seqs: number[]): Promise<StepCheckpoint[]>;
314
315
  saveCheckpoint(checkpoint: StepCheckpoint): Promise<void>;
316
+ saveCheckpoints(checkpoints: StepCheckpoint[]): Promise<void>;
315
317
  saveCheckpointIfPending(checkpoint: StepCheckpoint): Promise<boolean>;
316
318
  saveCheckpointsIfPending(checkpoints: StepCheckpoint[]): Promise<boolean[]>;
317
319
  transaction<T>(work: (tx: {
package/dist/index.js CHANGED
@@ -668,7 +668,7 @@ var ENTITIES = durableEntities();
668
668
 
669
669
  // src/mikro-orm-state-store.ts
670
670
  import { ENGINE_MINTED_TAG_PREFIXES, RUN_VALUE_FACET_LIMIT, SINGLETON_ACTIVE_STATUSES, TERMINAL_RUN_STATUSES, attributePredicateOperands, axisIsRunColumn, clearTerminalSingletonAdmission, facetOrigin, mergeRunFacetRows, normalizeAttributeRows, parseDuration, singletonAdmissionDenialPatch, singletonAdmissionTags } from "@dudousxd/nestjs-durable-core";
671
- import { raw } from "@mikro-orm/core";
671
+ import { LockMode, raw } from "@mikro-orm/core";
672
672
 
673
673
  // src/schema.ts
674
674
  import { createHash } from "node:crypto";
@@ -1288,11 +1288,37 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1288
1288
  });
1289
1289
  return entity ? fromCheckpointEntity(entity) : null;
1290
1290
  }
1291
+ async getCheckpoints(runId, seqs) {
1292
+ if (seqs.length === 0) return [];
1293
+ const em = this.fork();
1294
+ const unique = [
1295
+ ...new Set(seqs)
1296
+ ];
1297
+ const rows = [];
1298
+ for (let at = 0; at < unique.length; at += 500) {
1299
+ const seq = {
1300
+ $in: unique.slice(at, at + 500)
1301
+ };
1302
+ rows.push(...await em.find(StepCheckpointEntity, {
1303
+ runId,
1304
+ seq
1305
+ }));
1306
+ }
1307
+ return rows.sort((a, b) => a.seq - b.seq).map(fromCheckpointEntity);
1308
+ }
1291
1309
  async saveCheckpoint(checkpoint) {
1292
1310
  const em = this.fork();
1293
1311
  await em.upsert(StepCheckpointEntity, toCheckpointEntity(checkpoint));
1294
1312
  await em.flush();
1295
1313
  }
1314
+ async saveCheckpoints(checkpoints) {
1315
+ if (checkpoints.length === 0) return;
1316
+ await this.fork().transactional(async (em) => {
1317
+ for (let at = 0; at < checkpoints.length; at += 500) {
1318
+ await em.upsertMany(StepCheckpointEntity, checkpoints.slice(at, at + 500).map(toCheckpointRow));
1319
+ }
1320
+ });
1321
+ }
1296
1322
  async saveCheckpointIfPending(checkpoint) {
1297
1323
  const { runId: _runId, seq: _seq, ...data } = toCheckpointEntity(checkpoint);
1298
1324
  const affected = await this.fork().nativeUpdate(StepCheckpointEntity, {
@@ -1308,18 +1334,53 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1308
1334
  async saveCheckpointsIfPending(checkpoints) {
1309
1335
  if (checkpoints.length === 0) return [];
1310
1336
  return this.fork().transactional(async (em) => {
1337
+ const key = /* @__PURE__ */ __name((runId, seq) => `${runId}\0${seq}`, "key");
1338
+ const seqsByRun = /* @__PURE__ */ new Map();
1339
+ for (const { runId, seq } of checkpoints) {
1340
+ const seqs = seqsByRun.get(runId) ?? /* @__PURE__ */ new Set();
1341
+ seqs.add(seq);
1342
+ seqsByRun.set(runId, seqs);
1343
+ }
1344
+ const pending = /* @__PURE__ */ new Set();
1345
+ for (const [runId, seqSet] of seqsByRun) {
1346
+ const seqs = [
1347
+ ...seqSet
1348
+ ];
1349
+ for (let at = 0; at < seqs.length; at += 500) {
1350
+ const rows2 = await em.find(StepCheckpointEntity, {
1351
+ runId,
1352
+ seq: {
1353
+ $in: seqs.slice(at, at + 500)
1354
+ },
1355
+ status: "pending"
1356
+ }, {
1357
+ fields: [
1358
+ "runId",
1359
+ "seq"
1360
+ ],
1361
+ lockMode: LockMode.PESSIMISTIC_WRITE,
1362
+ disableIdentityMap: true
1363
+ });
1364
+ for (const row of rows2) pending.add(key(row.runId, row.seq));
1365
+ }
1366
+ }
1311
1367
  const wrote = [];
1368
+ const writes = /* @__PURE__ */ new Map();
1312
1369
  for (const checkpoint of checkpoints) {
1313
- const { runId: _runId, seq: _seq, ...data } = toCheckpointEntity(checkpoint);
1314
- const affected = await em.nativeUpdate(StepCheckpointEntity, {
1315
- runId: checkpoint.runId,
1316
- seq: checkpoint.seq,
1317
- status: "pending"
1318
- }, {
1319
- ...data,
1320
- wakeAt: checkpoint.wakeAt != null ? new Date(checkpoint.wakeAt) : null
1321
- });
1322
- wrote.push(affected > 0);
1370
+ const k = key(checkpoint.runId, checkpoint.seq);
1371
+ if (!pending.has(k)) {
1372
+ wrote.push(false);
1373
+ continue;
1374
+ }
1375
+ wrote.push(true);
1376
+ writes.set(k, checkpoint);
1377
+ if (checkpoint.status !== "pending") pending.delete(k);
1378
+ }
1379
+ const rows = [
1380
+ ...writes.values()
1381
+ ].map(toCheckpointRow);
1382
+ for (let at = 0; at < rows.length; at += 500) {
1383
+ await em.upsertMany(StepCheckpointEntity, rows.slice(at, at + 500));
1323
1384
  }
1324
1385
  return wrote;
1325
1386
  });
@@ -2269,6 +2330,19 @@ function aggregateDateReader(em) {
2269
2330
  };
2270
2331
  }
2271
2332
  __name(aggregateDateReader, "aggregateDateReader");
2333
+ function toCheckpointRow(cp) {
2334
+ const row = toCheckpointEntity(cp);
2335
+ for (const key of [
2336
+ "workerGroup",
2337
+ "parallelGroup",
2338
+ "wakeAt",
2339
+ "enqueuedAt"
2340
+ ]) {
2341
+ if (row[key] === void 0) row[key] = null;
2342
+ }
2343
+ return row;
2344
+ }
2345
+ __name(toCheckpointRow, "toCheckpointRow");
2272
2346
  function toCheckpointEntity(cp) {
2273
2347
  const e = new StepCheckpointEntity();
2274
2348
  e.runId = cp.runId;