@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.cjs CHANGED
@@ -1334,11 +1334,37 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1334
1334
  });
1335
1335
  return entity ? fromCheckpointEntity(entity) : null;
1336
1336
  }
1337
+ async getCheckpoints(runId, seqs) {
1338
+ if (seqs.length === 0) return [];
1339
+ const em = this.fork();
1340
+ const unique = [
1341
+ ...new Set(seqs)
1342
+ ];
1343
+ const rows = [];
1344
+ for (let at = 0; at < unique.length; at += 500) {
1345
+ const seq = {
1346
+ $in: unique.slice(at, at + 500)
1347
+ };
1348
+ rows.push(...await em.find(StepCheckpointEntity, {
1349
+ runId,
1350
+ seq
1351
+ }));
1352
+ }
1353
+ return rows.sort((a, b) => a.seq - b.seq).map(fromCheckpointEntity);
1354
+ }
1337
1355
  async saveCheckpoint(checkpoint) {
1338
1356
  const em = this.fork();
1339
1357
  await em.upsert(StepCheckpointEntity, toCheckpointEntity(checkpoint));
1340
1358
  await em.flush();
1341
1359
  }
1360
+ async saveCheckpoints(checkpoints) {
1361
+ if (checkpoints.length === 0) return;
1362
+ await this.fork().transactional(async (em) => {
1363
+ for (let at = 0; at < checkpoints.length; at += 500) {
1364
+ await em.upsertMany(StepCheckpointEntity, checkpoints.slice(at, at + 500).map(toCheckpointRow));
1365
+ }
1366
+ });
1367
+ }
1342
1368
  async saveCheckpointIfPending(checkpoint) {
1343
1369
  const { runId: _runId, seq: _seq, ...data } = toCheckpointEntity(checkpoint);
1344
1370
  const affected = await this.fork().nativeUpdate(StepCheckpointEntity, {
@@ -1354,18 +1380,53 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1354
1380
  async saveCheckpointsIfPending(checkpoints) {
1355
1381
  if (checkpoints.length === 0) return [];
1356
1382
  return this.fork().transactional(async (em) => {
1383
+ const key = /* @__PURE__ */ __name((runId, seq) => `${runId}\0${seq}`, "key");
1384
+ const seqsByRun = /* @__PURE__ */ new Map();
1385
+ for (const { runId, seq } of checkpoints) {
1386
+ const seqs = seqsByRun.get(runId) ?? /* @__PURE__ */ new Set();
1387
+ seqs.add(seq);
1388
+ seqsByRun.set(runId, seqs);
1389
+ }
1390
+ const pending = /* @__PURE__ */ new Set();
1391
+ for (const [runId, seqSet] of seqsByRun) {
1392
+ const seqs = [
1393
+ ...seqSet
1394
+ ];
1395
+ for (let at = 0; at < seqs.length; at += 500) {
1396
+ const rows2 = await em.find(StepCheckpointEntity, {
1397
+ runId,
1398
+ seq: {
1399
+ $in: seqs.slice(at, at + 500)
1400
+ },
1401
+ status: "pending"
1402
+ }, {
1403
+ fields: [
1404
+ "runId",
1405
+ "seq"
1406
+ ],
1407
+ lockMode: import_core3.LockMode.PESSIMISTIC_WRITE,
1408
+ disableIdentityMap: true
1409
+ });
1410
+ for (const row of rows2) pending.add(key(row.runId, row.seq));
1411
+ }
1412
+ }
1357
1413
  const wrote = [];
1414
+ const writes = /* @__PURE__ */ new Map();
1358
1415
  for (const checkpoint of checkpoints) {
1359
- const { runId: _runId, seq: _seq, ...data } = toCheckpointEntity(checkpoint);
1360
- const affected = await em.nativeUpdate(StepCheckpointEntity, {
1361
- runId: checkpoint.runId,
1362
- seq: checkpoint.seq,
1363
- status: "pending"
1364
- }, {
1365
- ...data,
1366
- wakeAt: checkpoint.wakeAt != null ? new Date(checkpoint.wakeAt) : null
1367
- });
1368
- wrote.push(affected > 0);
1416
+ const k = key(checkpoint.runId, checkpoint.seq);
1417
+ if (!pending.has(k)) {
1418
+ wrote.push(false);
1419
+ continue;
1420
+ }
1421
+ wrote.push(true);
1422
+ writes.set(k, checkpoint);
1423
+ if (checkpoint.status !== "pending") pending.delete(k);
1424
+ }
1425
+ const rows = [
1426
+ ...writes.values()
1427
+ ].map(toCheckpointRow);
1428
+ for (let at = 0; at < rows.length; at += 500) {
1429
+ await em.upsertMany(StepCheckpointEntity, rows.slice(at, at + 500));
1369
1430
  }
1370
1431
  return wrote;
1371
1432
  });
@@ -2163,6 +2224,37 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
2163
2224
  payload
2164
2225
  };
2165
2226
  }
2227
+ async listBufferedSignals(tokenPrefix, afterId, limit) {
2228
+ const em = this.fork();
2229
+ const rows = await em.find(BufferedSignalEntity, {
2230
+ token: {
2231
+ $like: `${tokenPrefix}%`
2232
+ },
2233
+ ...afterId !== void 0 ? {
2234
+ id: {
2235
+ $gt: Number(afterId)
2236
+ }
2237
+ } : {}
2238
+ }, {
2239
+ orderBy: {
2240
+ id: "asc"
2241
+ },
2242
+ limit
2243
+ });
2244
+ return rows.filter((r) => r.token.startsWith(tokenPrefix)).map((r) => ({
2245
+ id: String(r.id),
2246
+ token: r.token,
2247
+ payload: r.payload ?? void 0
2248
+ }));
2249
+ }
2250
+ async deleteBufferedSignals(ids) {
2251
+ if (ids.length === 0) return;
2252
+ await this.fork().nativeDelete(BufferedSignalEntity, {
2253
+ id: {
2254
+ $in: ids.map(Number)
2255
+ }
2256
+ });
2257
+ }
2166
2258
  async bufferEvent(input) {
2167
2259
  const em = this.fork();
2168
2260
  const e = new BufferedEventEntity();
@@ -2284,6 +2376,19 @@ function aggregateDateReader(em) {
2284
2376
  };
2285
2377
  }
2286
2378
  __name(aggregateDateReader, "aggregateDateReader");
2379
+ function toCheckpointRow(cp) {
2380
+ const row = toCheckpointEntity(cp);
2381
+ for (const key of [
2382
+ "workerGroup",
2383
+ "parallelGroup",
2384
+ "wakeAt",
2385
+ "enqueuedAt"
2386
+ ]) {
2387
+ if (row[key] === void 0) row[key] = null;
2388
+ }
2389
+ return row;
2390
+ }
2391
+ __name(toCheckpointRow, "toCheckpointRow");
2287
2392
  function toCheckpointEntity(cp) {
2288
2393
  const e = new StepCheckpointEntity();
2289
2394
  e.runId = cp.runId;