@dudousxd/nestjs-durable-store-mikro-orm 0.33.3 → 0.33.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.d.cts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as _mikro_orm_core from '@mikro-orm/core';
2
2
  import { EntityRepository, EntityRepositoryType, EntitySchema, MikroORM } from '@mikro-orm/core';
3
- import { StepKind, RunStatus, StateStore, WorkflowRun, RetentionPolicy, StepCheckpoint, OwnedLeaseReclaim, RunQuery, RunFacetQuery, RunFacetRow, RunValueAxis, RunValueFacetOptions, RunValueFacetRow, ScheduleRecord, ScheduleQuery, CheckpointStat, CheckpointQuery, SignalWaiter, ChildWait, BufferedSignalRow } from '@dudousxd/nestjs-durable-core';
3
+ import { StepKind, RunStatus, StateStore, WorkflowRun, RetentionPolicy, StepCheckpoint, StepSettlement, OwnedLeaseReclaim, RunQuery, RunFacetQuery, RunFacetRow, RunValueAxis, RunValueFacetOptions, RunValueFacetRow, ScheduleRecord, ScheduleQuery, CheckpointStat, CheckpointQuery, SignalWaiter, ChildWait, BufferedSignalRow } from '@dudousxd/nestjs-durable-core';
4
4
 
5
5
  /**
6
6
  * Custom repositories for the durable entities, so a host app can `@InjectRepository`/inject a
@@ -311,9 +311,13 @@ 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[]>;
315
+ getCheckpointsForSettle(runId: string, seqs: number[]): Promise<StepCheckpoint[]>;
314
316
  saveCheckpoint(checkpoint: StepCheckpoint): Promise<void>;
317
+ saveCheckpoints(checkpoints: StepCheckpoint[]): Promise<void>;
315
318
  saveCheckpointIfPending(checkpoint: StepCheckpoint): Promise<boolean>;
316
319
  saveCheckpointsIfPending(checkpoints: StepCheckpoint[]): Promise<boolean[]>;
320
+ settleCheckpointsIfPending(settlements: StepSettlement[]): Promise<boolean[]>;
317
321
  transaction<T>(work: (tx: {
318
322
  raw: unknown;
319
323
  saveCheckpoint: (cp: StepCheckpoint) => Promise<void>;
@@ -404,6 +408,7 @@ declare class MikroOrmStateStore implements StateStore {
404
408
  deleteSchedule(id: string): Promise<boolean>;
405
409
  listSchedules(query: ScheduleQuery): Promise<ScheduleRecord[]>;
406
410
  listCheckpoints(runId: string): Promise<StepCheckpoint[]>;
411
+ listCheckpointsForReplay(runId: string): Promise<StepCheckpoint[]>;
407
412
  getLatestCheckpointByName(runId: string, name: string): Promise<StepCheckpoint | undefined>;
408
413
  countCheckpoints(runId: string, fromSeq: number, toSeq: number, statuses: StepCheckpoint['status'][]): Promise<number>;
409
414
  listCheckpointsByNamePrefix(runId: string, prefixes: string[]): Promise<StepCheckpoint[]>;
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as _mikro_orm_core from '@mikro-orm/core';
2
2
  import { EntityRepository, EntityRepositoryType, EntitySchema, MikroORM } from '@mikro-orm/core';
3
- import { StepKind, RunStatus, StateStore, WorkflowRun, RetentionPolicy, StepCheckpoint, OwnedLeaseReclaim, RunQuery, RunFacetQuery, RunFacetRow, RunValueAxis, RunValueFacetOptions, RunValueFacetRow, ScheduleRecord, ScheduleQuery, CheckpointStat, CheckpointQuery, SignalWaiter, ChildWait, BufferedSignalRow } from '@dudousxd/nestjs-durable-core';
3
+ import { StepKind, RunStatus, StateStore, WorkflowRun, RetentionPolicy, StepCheckpoint, StepSettlement, OwnedLeaseReclaim, RunQuery, RunFacetQuery, RunFacetRow, RunValueAxis, RunValueFacetOptions, RunValueFacetRow, ScheduleRecord, ScheduleQuery, CheckpointStat, CheckpointQuery, SignalWaiter, ChildWait, BufferedSignalRow } from '@dudousxd/nestjs-durable-core';
4
4
 
5
5
  /**
6
6
  * Custom repositories for the durable entities, so a host app can `@InjectRepository`/inject a
@@ -311,9 +311,13 @@ 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[]>;
315
+ getCheckpointsForSettle(runId: string, seqs: number[]): Promise<StepCheckpoint[]>;
314
316
  saveCheckpoint(checkpoint: StepCheckpoint): Promise<void>;
317
+ saveCheckpoints(checkpoints: StepCheckpoint[]): Promise<void>;
315
318
  saveCheckpointIfPending(checkpoint: StepCheckpoint): Promise<boolean>;
316
319
  saveCheckpointsIfPending(checkpoints: StepCheckpoint[]): Promise<boolean[]>;
320
+ settleCheckpointsIfPending(settlements: StepSettlement[]): Promise<boolean[]>;
317
321
  transaction<T>(work: (tx: {
318
322
  raw: unknown;
319
323
  saveCheckpoint: (cp: StepCheckpoint) => Promise<void>;
@@ -404,6 +408,7 @@ declare class MikroOrmStateStore implements StateStore {
404
408
  deleteSchedule(id: string): Promise<boolean>;
405
409
  listSchedules(query: ScheduleQuery): Promise<ScheduleRecord[]>;
406
410
  listCheckpoints(runId: string): Promise<StepCheckpoint[]>;
411
+ listCheckpointsForReplay(runId: string): Promise<StepCheckpoint[]>;
407
412
  getLatestCheckpointByName(runId: string, name: string): Promise<StepCheckpoint | undefined>;
408
413
  countCheckpoints(runId: string, fromSeq: number, toSeq: number, statuses: StepCheckpoint['status'][]): Promise<number>;
409
414
  listCheckpointsByNamePrefix(runId: string, prefixes: string[]): Promise<StepCheckpoint[]>;
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,52 @@ 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 selectCheckpointRows(em, {
1303
+ runId,
1304
+ seq
1305
+ }));
1306
+ }
1307
+ return rows.sort((a, b) => a.seq - b.seq).map(fromCheckpointEntity);
1308
+ }
1309
+ async getCheckpointsForSettle(runId, seqs) {
1310
+ if (seqs.length === 0) return [];
1311
+ const em = this.fork();
1312
+ const fields = checkpointFieldsWithoutInput(em);
1313
+ const unique = [
1314
+ ...new Set(seqs)
1315
+ ];
1316
+ const rows = [];
1317
+ for (let at = 0; at < unique.length; at += 500) {
1318
+ const seq = {
1319
+ $in: unique.slice(at, at + 500)
1320
+ };
1321
+ rows.push(...await selectCheckpointRows(em, {
1322
+ runId,
1323
+ seq
1324
+ }, fields));
1325
+ }
1326
+ return rows.sort((a, b) => a.seq - b.seq).map(fromCheckpointEntity);
1327
+ }
1291
1328
  async saveCheckpoint(checkpoint) {
1292
1329
  const em = this.fork();
1293
1330
  await em.upsert(StepCheckpointEntity, toCheckpointEntity(checkpoint));
1294
1331
  await em.flush();
1295
1332
  }
1333
+ async saveCheckpoints(checkpoints) {
1334
+ if (checkpoints.length === 0) return;
1335
+ await this.fork().transactional((em) => upsertCheckpointRows(em, checkpoints.map(toCheckpointRow)));
1336
+ }
1296
1337
  async saveCheckpointIfPending(checkpoint) {
1297
1338
  const { runId: _runId, seq: _seq, ...data } = toCheckpointEntity(checkpoint);
1298
1339
  const affected = await this.fork().nativeUpdate(StepCheckpointEntity, {
@@ -1308,18 +1349,69 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1308
1349
  async saveCheckpointsIfPending(checkpoints) {
1309
1350
  if (checkpoints.length === 0) return [];
1310
1351
  return this.fork().transactional(async (em) => {
1352
+ const pending = await lockPendingCheckpoints(em, checkpoints, [
1353
+ "runId",
1354
+ "seq"
1355
+ ]);
1311
1356
  const wrote = [];
1357
+ const writes = /* @__PURE__ */ new Map();
1312
1358
  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
1359
+ const k = checkpointKey(checkpoint.runId, checkpoint.seq);
1360
+ if (!pending.has(k)) {
1361
+ wrote.push(false);
1362
+ continue;
1363
+ }
1364
+ wrote.push(true);
1365
+ writes.set(k, checkpoint);
1366
+ if (checkpoint.status !== "pending") pending.delete(k);
1367
+ }
1368
+ await upsertCheckpointRows(em, [
1369
+ ...writes.values()
1370
+ ].map(toCheckpointRow));
1371
+ return wrote;
1372
+ });
1373
+ }
1374
+ async settleCheckpointsIfPending(settlements) {
1375
+ if (settlements.length === 0) return [];
1376
+ return this.fork().transactional(async (em) => {
1377
+ const pending = await lockPendingCheckpoints(em, settlements, [
1378
+ "runId",
1379
+ "seq",
1380
+ "name",
1381
+ "kind",
1382
+ "stepId",
1383
+ "attempts"
1384
+ ]);
1385
+ const wrote = [];
1386
+ const rows = [];
1387
+ for (const s of settlements) {
1388
+ const k = checkpointKey(s.runId, s.seq);
1389
+ const row = pending.get(k);
1390
+ wrote.push(row !== void 0);
1391
+ if (!row) continue;
1392
+ pending.delete(k);
1393
+ rows.push({
1394
+ runId: row.runId,
1395
+ seq: row.seq,
1396
+ name: row.name,
1397
+ kind: row.kind,
1398
+ stepId: row.stepId,
1399
+ attempts: row.attempts,
1400
+ status: s.status,
1401
+ output: s.output ?? null,
1402
+ error: s.error ?? null,
1403
+ events: s.events ?? null,
1404
+ startedAt: s.startedAt,
1405
+ finishedAt: s.finishedAt
1321
1406
  });
1322
- wrote.push(affected > 0);
1407
+ }
1408
+ for (let at = 0; at < rows.length; at += 500) {
1409
+ await em.createQueryBuilder(StepCheckpointEntity).insert(rows.slice(at, at + 500)).onConflict([
1410
+ "runId",
1411
+ "seq"
1412
+ ]).merge([
1413
+ ...SETTLED_FIELDS
1414
+ ]).execute();
1323
1415
  }
1324
1416
  return wrote;
1325
1417
  });
@@ -1837,14 +1929,33 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1837
1929
  return (await qb.getResultList()).map(fromScheduleEntity);
1838
1930
  }
1839
1931
  async listCheckpoints(runId) {
1932
+ const rows = await selectCheckpointRows(this.fork(), {
1933
+ runId
1934
+ });
1935
+ return rows.map(fromCheckpointEntity);
1936
+ }
1937
+ async listCheckpointsForReplay(runId) {
1840
1938
  const em = this.fork();
1841
- const rows = await em.find(StepCheckpointEntity, {
1939
+ const rows = await selectCheckpointRows(em, {
1842
1940
  runId
1843
- }, {
1844
- orderBy: {
1845
- seq: "asc"
1941
+ }, checkpointFieldsWithoutInput(em));
1942
+ const open = rows.filter((row) => row.status !== "completed").map((row) => row.seq);
1943
+ if (open.length > 0) {
1944
+ const inputs = /* @__PURE__ */ new Map();
1945
+ for (let at = 0; at < open.length; at += 500) {
1946
+ const found = await selectCheckpointRows(em, {
1947
+ runId,
1948
+ seq: {
1949
+ $in: open.slice(at, at + 500)
1950
+ }
1951
+ }, [
1952
+ "seq",
1953
+ "input"
1954
+ ]);
1955
+ for (const row of found) inputs.set(row.seq, row.input);
1846
1956
  }
1847
- });
1957
+ for (const row of rows) if (inputs.has(row.seq)) row.input = inputs.get(row.seq);
1958
+ }
1848
1959
  return rows.map(fromCheckpointEntity);
1849
1960
  }
1850
1961
  async getLatestCheckpointByName(runId, name) {
@@ -2269,6 +2380,90 @@ function aggregateDateReader(em) {
2269
2380
  };
2270
2381
  }
2271
2382
  __name(aggregateDateReader, "aggregateDateReader");
2383
+ function checkpointFieldsWithoutInput(em) {
2384
+ return Object.keys(em.getMetadata().get(StepCheckpointEntity).properties).filter((name) => name !== "input");
2385
+ }
2386
+ __name(checkpointFieldsWithoutInput, "checkpointFieldsWithoutInput");
2387
+ async function selectCheckpointRows(em, where, fields) {
2388
+ const query = em.createQueryBuilder(StepCheckpointEntity);
2389
+ if (fields) query.select(fields);
2390
+ const rows = await query.where(where).orderBy({
2391
+ seq: "asc"
2392
+ }).execute("all", true);
2393
+ const platform = em.getPlatform();
2394
+ const custom = Object.values(em.getMetadata().get(StepCheckpointEntity).properties).filter((prop) => prop.customType);
2395
+ for (const row of rows) {
2396
+ for (const prop of custom) {
2397
+ const value = row[prop.name];
2398
+ if (value != null) row[prop.name] = prop.customType?.convertToJSValue(value, platform);
2399
+ }
2400
+ }
2401
+ return rows;
2402
+ }
2403
+ __name(selectCheckpointRows, "selectCheckpointRows");
2404
+ var SETTLED_FIELDS = [
2405
+ "status",
2406
+ "output",
2407
+ "error",
2408
+ "events",
2409
+ "startedAt",
2410
+ "finishedAt"
2411
+ ];
2412
+ var checkpointKey = /* @__PURE__ */ __name((runId, seq) => `${runId}\0${seq}`, "checkpointKey");
2413
+ async function lockPendingCheckpoints(em, entries, fields) {
2414
+ const seqsByRun = /* @__PURE__ */ new Map();
2415
+ for (const { runId, seq } of entries) {
2416
+ const seqs = seqsByRun.get(runId) ?? /* @__PURE__ */ new Set();
2417
+ seqs.add(seq);
2418
+ seqsByRun.set(runId, seqs);
2419
+ }
2420
+ const pending = /* @__PURE__ */ new Map();
2421
+ for (const [runId, seqSet] of seqsByRun) {
2422
+ const seqs = [
2423
+ ...seqSet
2424
+ ];
2425
+ for (let at = 0; at < seqs.length; at += 500) {
2426
+ const rows = await em.find(StepCheckpointEntity, {
2427
+ runId,
2428
+ seq: {
2429
+ $in: seqs.slice(at, at + 500)
2430
+ },
2431
+ status: "pending"
2432
+ }, {
2433
+ fields,
2434
+ lockMode: LockMode.PESSIMISTIC_WRITE,
2435
+ disableIdentityMap: true
2436
+ });
2437
+ for (const row of rows) pending.set(checkpointKey(row.runId, row.seq), row);
2438
+ }
2439
+ }
2440
+ return pending;
2441
+ }
2442
+ __name(lockPendingCheckpoints, "lockPendingCheckpoints");
2443
+ async function upsertCheckpointRows(em, rows) {
2444
+ for (let at = 0; at < rows.length; at += 500) {
2445
+ await em.createQueryBuilder(StepCheckpointEntity).insert(rows.slice(at, at + 500).map((row) => ({
2446
+ ...row
2447
+ }))).onConflict([
2448
+ "runId",
2449
+ "seq"
2450
+ ]).merge().execute();
2451
+ }
2452
+ }
2453
+ __name(upsertCheckpointRows, "upsertCheckpointRows");
2454
+ function toCheckpointRow(cp) {
2455
+ const row = toCheckpointEntity(cp);
2456
+ for (const key of [
2457
+ "workerGroup",
2458
+ "parallelGroup",
2459
+ "wakeAt",
2460
+ "enqueuedAt"
2461
+ ]) {
2462
+ if (row[key] === void 0) row[key] = null;
2463
+ }
2464
+ return row;
2465
+ }
2466
+ __name(toCheckpointRow, "toCheckpointRow");
2272
2467
  function toCheckpointEntity(cp) {
2273
2468
  const e = new StepCheckpointEntity();
2274
2469
  e.runId = cp.runId;