@dudousxd/nestjs-durable-store-mikro-orm 0.33.2 → 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
@@ -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 } from '@dudousxd/nestjs-durable-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';
4
4
 
5
5
  /**
6
6
  * Custom repositories for the durable entities, so a host app can `@InjectRepository`/inject a
@@ -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: {
@@ -432,6 +434,8 @@ declare class MikroOrmStateStore implements StateStore {
432
434
  takeBufferedSignal(token: string): Promise<{
433
435
  payload: unknown;
434
436
  } | null>;
437
+ listBufferedSignals(tokenPrefix: string, afterId: string | undefined, limit: number): Promise<BufferedSignalRow[]>;
438
+ deleteBufferedSignals(ids: string[]): Promise<void>;
435
439
  bufferEvent(input: {
436
440
  name: string;
437
441
  payload: unknown;
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 } from '@dudousxd/nestjs-durable-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';
4
4
 
5
5
  /**
6
6
  * Custom repositories for the durable entities, so a host app can `@InjectRepository`/inject a
@@ -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: {
@@ -432,6 +434,8 @@ declare class MikroOrmStateStore implements StateStore {
432
434
  takeBufferedSignal(token: string): Promise<{
433
435
  payload: unknown;
434
436
  } | null>;
437
+ listBufferedSignals(tokenPrefix: string, afterId: string | undefined, limit: number): Promise<BufferedSignalRow[]>;
438
+ deleteBufferedSignals(ids: string[]): Promise<void>;
435
439
  bufferEvent(input: {
436
440
  name: string;
437
441
  payload: unknown;
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
  });
@@ -2117,6 +2178,37 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
2117
2178
  payload
2118
2179
  };
2119
2180
  }
2181
+ async listBufferedSignals(tokenPrefix, afterId, limit) {
2182
+ const em = this.fork();
2183
+ const rows = await em.find(BufferedSignalEntity, {
2184
+ token: {
2185
+ $like: `${tokenPrefix}%`
2186
+ },
2187
+ ...afterId !== void 0 ? {
2188
+ id: {
2189
+ $gt: Number(afterId)
2190
+ }
2191
+ } : {}
2192
+ }, {
2193
+ orderBy: {
2194
+ id: "asc"
2195
+ },
2196
+ limit
2197
+ });
2198
+ return rows.filter((r) => r.token.startsWith(tokenPrefix)).map((r) => ({
2199
+ id: String(r.id),
2200
+ token: r.token,
2201
+ payload: r.payload ?? void 0
2202
+ }));
2203
+ }
2204
+ async deleteBufferedSignals(ids) {
2205
+ if (ids.length === 0) return;
2206
+ await this.fork().nativeDelete(BufferedSignalEntity, {
2207
+ id: {
2208
+ $in: ids.map(Number)
2209
+ }
2210
+ });
2211
+ }
2120
2212
  async bufferEvent(input) {
2121
2213
  const em = this.fork();
2122
2214
  const e = new BufferedEventEntity();
@@ -2238,6 +2330,19 @@ function aggregateDateReader(em) {
2238
2330
  };
2239
2331
  }
2240
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");
2241
2346
  function toCheckpointEntity(cp) {
2242
2347
  const e = new StepCheckpointEntity();
2243
2348
  e.runId = cp.runId;