@dudousxd/nestjs-durable-store-mikro-orm 0.33.4 → 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
@@ -312,10 +312,12 @@ declare class MikroOrmStateStore implements StateStore {
312
312
  pruneTerminalRuns(policy: RetentionPolicy, nowMs: number, limit: number): Promise<number>;
313
313
  getCheckpoint(runId: string, seq: number): Promise<StepCheckpoint | null>;
314
314
  getCheckpoints(runId: string, seqs: number[]): Promise<StepCheckpoint[]>;
315
+ getCheckpointsForSettle(runId: string, seqs: number[]): Promise<StepCheckpoint[]>;
315
316
  saveCheckpoint(checkpoint: StepCheckpoint): Promise<void>;
316
317
  saveCheckpoints(checkpoints: StepCheckpoint[]): Promise<void>;
317
318
  saveCheckpointIfPending(checkpoint: StepCheckpoint): Promise<boolean>;
318
319
  saveCheckpointsIfPending(checkpoints: StepCheckpoint[]): Promise<boolean[]>;
320
+ settleCheckpointsIfPending(settlements: StepSettlement[]): Promise<boolean[]>;
319
321
  transaction<T>(work: (tx: {
320
322
  raw: unknown;
321
323
  saveCheckpoint: (cp: StepCheckpoint) => Promise<void>;
@@ -406,6 +408,7 @@ declare class MikroOrmStateStore implements StateStore {
406
408
  deleteSchedule(id: string): Promise<boolean>;
407
409
  listSchedules(query: ScheduleQuery): Promise<ScheduleRecord[]>;
408
410
  listCheckpoints(runId: string): Promise<StepCheckpoint[]>;
411
+ listCheckpointsForReplay(runId: string): Promise<StepCheckpoint[]>;
409
412
  getLatestCheckpointByName(runId: string, name: string): Promise<StepCheckpoint | undefined>;
410
413
  countCheckpoints(runId: string, fromSeq: number, toSeq: number, statuses: StepCheckpoint['status'][]): Promise<number>;
411
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
@@ -312,10 +312,12 @@ declare class MikroOrmStateStore implements StateStore {
312
312
  pruneTerminalRuns(policy: RetentionPolicy, nowMs: number, limit: number): Promise<number>;
313
313
  getCheckpoint(runId: string, seq: number): Promise<StepCheckpoint | null>;
314
314
  getCheckpoints(runId: string, seqs: number[]): Promise<StepCheckpoint[]>;
315
+ getCheckpointsForSettle(runId: string, seqs: number[]): Promise<StepCheckpoint[]>;
315
316
  saveCheckpoint(checkpoint: StepCheckpoint): Promise<void>;
316
317
  saveCheckpoints(checkpoints: StepCheckpoint[]): Promise<void>;
317
318
  saveCheckpointIfPending(checkpoint: StepCheckpoint): Promise<boolean>;
318
319
  saveCheckpointsIfPending(checkpoints: StepCheckpoint[]): Promise<boolean[]>;
320
+ settleCheckpointsIfPending(settlements: StepSettlement[]): Promise<boolean[]>;
319
321
  transaction<T>(work: (tx: {
320
322
  raw: unknown;
321
323
  saveCheckpoint: (cp: StepCheckpoint) => Promise<void>;
@@ -406,6 +408,7 @@ declare class MikroOrmStateStore implements StateStore {
406
408
  deleteSchedule(id: string): Promise<boolean>;
407
409
  listSchedules(query: ScheduleQuery): Promise<ScheduleRecord[]>;
408
410
  listCheckpoints(runId: string): Promise<StepCheckpoint[]>;
411
+ listCheckpointsForReplay(runId: string): Promise<StepCheckpoint[]>;
409
412
  getLatestCheckpointByName(runId: string, name: string): Promise<StepCheckpoint | undefined>;
410
413
  countCheckpoints(runId: string, fromSeq: number, toSeq: number, statuses: StepCheckpoint['status'][]): Promise<number>;
411
414
  listCheckpointsByNamePrefix(runId: string, prefixes: string[]): Promise<StepCheckpoint[]>;
package/dist/index.js CHANGED
@@ -1299,13 +1299,32 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1299
1299
  const seq = {
1300
1300
  $in: unique.slice(at, at + 500)
1301
1301
  };
1302
- rows.push(...await em.find(StepCheckpointEntity, {
1302
+ rows.push(...await selectCheckpointRows(em, {
1303
1303
  runId,
1304
1304
  seq
1305
1305
  }));
1306
1306
  }
1307
1307
  return rows.sort((a, b) => a.seq - b.seq).map(fromCheckpointEntity);
1308
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
+ }
1309
1328
  async saveCheckpoint(checkpoint) {
1310
1329
  const em = this.fork();
1311
1330
  await em.upsert(StepCheckpointEntity, toCheckpointEntity(checkpoint));
@@ -1313,11 +1332,7 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1313
1332
  }
1314
1333
  async saveCheckpoints(checkpoints) {
1315
1334
  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
- });
1335
+ await this.fork().transactional((em) => upsertCheckpointRows(em, checkpoints.map(toCheckpointRow)));
1321
1336
  }
1322
1337
  async saveCheckpointIfPending(checkpoint) {
1323
1338
  const { runId: _runId, seq: _seq, ...data } = toCheckpointEntity(checkpoint);
@@ -1334,40 +1349,14 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1334
1349
  async saveCheckpointsIfPending(checkpoints) {
1335
1350
  if (checkpoints.length === 0) return [];
1336
1351
  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
- }
1352
+ const pending = await lockPendingCheckpoints(em, checkpoints, [
1353
+ "runId",
1354
+ "seq"
1355
+ ]);
1367
1356
  const wrote = [];
1368
1357
  const writes = /* @__PURE__ */ new Map();
1369
1358
  for (const checkpoint of checkpoints) {
1370
- const k = key(checkpoint.runId, checkpoint.seq);
1359
+ const k = checkpointKey(checkpoint.runId, checkpoint.seq);
1371
1360
  if (!pending.has(k)) {
1372
1361
  wrote.push(false);
1373
1362
  continue;
@@ -1376,11 +1365,53 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1376
1365
  writes.set(k, checkpoint);
1377
1366
  if (checkpoint.status !== "pending") pending.delete(k);
1378
1367
  }
1379
- const rows = [
1368
+ await upsertCheckpointRows(em, [
1380
1369
  ...writes.values()
1381
- ].map(toCheckpointRow);
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
1406
+ });
1407
+ }
1382
1408
  for (let at = 0; at < rows.length; at += 500) {
1383
- await em.upsertMany(StepCheckpointEntity, rows.slice(at, 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();
1384
1415
  }
1385
1416
  return wrote;
1386
1417
  });
@@ -1898,14 +1929,33 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1898
1929
  return (await qb.getResultList()).map(fromScheduleEntity);
1899
1930
  }
1900
1931
  async listCheckpoints(runId) {
1932
+ const rows = await selectCheckpointRows(this.fork(), {
1933
+ runId
1934
+ });
1935
+ return rows.map(fromCheckpointEntity);
1936
+ }
1937
+ async listCheckpointsForReplay(runId) {
1901
1938
  const em = this.fork();
1902
- const rows = await em.find(StepCheckpointEntity, {
1939
+ const rows = await selectCheckpointRows(em, {
1903
1940
  runId
1904
- }, {
1905
- orderBy: {
1906
- 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);
1907
1956
  }
1908
- });
1957
+ for (const row of rows) if (inputs.has(row.seq)) row.input = inputs.get(row.seq);
1958
+ }
1909
1959
  return rows.map(fromCheckpointEntity);
1910
1960
  }
1911
1961
  async getLatestCheckpointByName(runId, name) {
@@ -2330,6 +2380,77 @@ function aggregateDateReader(em) {
2330
2380
  };
2331
2381
  }
2332
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");
2333
2454
  function toCheckpointRow(cp) {
2334
2455
  const row = toCheckpointEntity(cp);
2335
2456
  for (const key of [