@dudousxd/nestjs-durable-store-mikro-orm 0.33.7 → 0.34.0

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.js CHANGED
@@ -667,7 +667,7 @@ __name(durableEntities, "durableEntities");
667
667
  var ENTITIES = durableEntities();
668
668
 
669
669
  // src/mikro-orm-state-store.ts
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";
670
+ import { ENGINE_MINTED_TAG_PREFIXES, LIFECYCLE_REGISTRY_WORKFLOW, RUN_VALUE_FACET_LIMIT, SINGLETON_ACTIVE_STATUSES, SINGLETON_LOCK_CHUNK, TERMINAL_RUN_STATUSES, attributePredicateOperands, axisIsRunColumn, clearTerminalSingletonAdmission, facetOrigin, mergeRunFacetRows, normalizeAttributeRows, parseDuration, retentionKeeps, singletonAdmissionDenialPatch, singletonAdmissionTags, singletonClaimantIds } from "@dudousxd/nestjs-durable-core";
671
671
  import { LockMode, raw } from "@mikro-orm/core";
672
672
 
673
673
  // src/schema.ts
@@ -1049,6 +1049,9 @@ async function alignDurableCollation(orm) {
1049
1049
  __name(alignDurableCollation, "alignDurableCollation");
1050
1050
 
1051
1051
  // src/mikro-orm-state-store.ts
1052
+ var RETENTION_EXCLUDED = [
1053
+ LIFECYCLE_REGISTRY_WORKFLOW
1054
+ ];
1052
1055
  var MikroOrmStateStore = class _MikroOrmStateStore {
1053
1056
  static {
1054
1057
  __name(this, "MikroOrmStateStore");
@@ -1093,19 +1096,48 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1093
1096
  });
1094
1097
  }
1095
1098
  async tryAdmitSingleton(runId, tag, workflow, limit, retryWakeAt) {
1099
+ const where = {
1100
+ workflow,
1101
+ status: {
1102
+ $in: SINGLETON_ACTIVE_STATUSES
1103
+ },
1104
+ ...this.scopeNamespace !== void 0 ? {
1105
+ namespace: this.scopeNamespace
1106
+ } : {}
1107
+ };
1108
+ const reader = this.fork();
1109
+ const sqlite = /sqlite|libsql/i.test(reader.getPlatform().constructor.name);
1110
+ const claimants = sqlite ? [] : singletonClaimantIds(await reader.find(WorkflowRunEntity, where, {
1111
+ fields: [
1112
+ "id",
1113
+ "tags"
1114
+ ],
1115
+ orderBy: {
1116
+ id: "ASC"
1117
+ },
1118
+ // The primary: a lagging replica would leave rows out of the lock set.
1119
+ connectionType: "write"
1120
+ }), tag);
1096
1121
  return this.fork().transactional(async (em) => {
1097
- const where = {
1098
- workflow,
1099
- status: {
1100
- $in: SINGLETON_ACTIVE_STATUSES
1101
- },
1102
- ...this.scopeNamespace !== void 0 ? {
1103
- namespace: this.scopeNamespace
1104
- } : {}
1105
- };
1106
- await em.nativeUpdate(WorkflowRunEntity, where, {
1122
+ if (sqlite) await em.nativeUpdate(WorkflowRunEntity, where, {
1107
1123
  workflow
1108
1124
  });
1125
+ for (let at = 0; at < claimants.length; at += SINGLETON_LOCK_CHUNK) {
1126
+ await em.find(WorkflowRunEntity, {
1127
+ id: {
1128
+ $in: claimants.slice(at, at + SINGLETON_LOCK_CHUNK)
1129
+ }
1130
+ }, {
1131
+ fields: [
1132
+ "id"
1133
+ ],
1134
+ orderBy: {
1135
+ id: "ASC"
1136
+ },
1137
+ lockMode: LockMode.PESSIMISTIC_WRITE
1138
+ });
1139
+ }
1140
+ em.clear();
1109
1141
  const rows = await em.find(WorkflowRunEntity, where);
1110
1142
  const runs = rows.map(fromRunEntity);
1111
1143
  const tags = singletonAdmissionTags(runs, runId, tag, workflow, limit);
@@ -1247,13 +1279,16 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1247
1279
  const idCol = meta.properties.id?.fieldNames?.[0] ?? "id";
1248
1280
  const query = {
1249
1281
  ...policy.scope ?? {},
1250
- statuses
1282
+ statuses,
1283
+ excludeWorkflows: RETENTION_EXCLUDED
1251
1284
  };
1285
+ const tagsCol = meta.properties.tags?.fieldNames?.[0] ?? "tags";
1252
1286
  const scoped = /* @__PURE__ */ __name((extra) => this.runQueryBuilder(em, query, {
1253
1287
  ...this.runWhere(query),
1254
1288
  ...extra ?? {}
1255
1289
  }).select([
1256
- `r.${idCol}`
1290
+ `r.${idCol}`,
1291
+ `r.${tagsCol}`
1257
1292
  ]), "scoped");
1258
1293
  const ids = /* @__PURE__ */ new Set();
1259
1294
  if (policy.maxAge != null) {
@@ -1265,7 +1300,9 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1265
1300
  }).orderBy({
1266
1301
  "r.updatedAt": "asc"
1267
1302
  }).limit(limit).execute();
1268
- for (const r of rows) ids.add(String(r[idCol] ?? r.id));
1303
+ for (const r of rows) {
1304
+ if (!retentionKeeps(r[tagsCol] ?? r.tags)) ids.add(String(r[idCol] ?? r.id));
1305
+ }
1269
1306
  }
1270
1307
  if (policy.maxCount != null && ids.size < limit) {
1271
1308
  const rows = await scoped().orderBy({
@@ -1274,7 +1311,7 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1274
1311
  }).limit(limit).offset(policy.maxCount).execute();
1275
1312
  for (const r of rows) {
1276
1313
  if (ids.size >= limit) break;
1277
- ids.add(String(r[idCol] ?? r.id));
1314
+ if (!retentionKeeps(r[tagsCol] ?? r.tags)) ids.add(String(r[idCol] ?? r.id));
1278
1315
  }
1279
1316
  }
1280
1317
  const idList = [
@@ -1572,6 +1609,18 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1572
1609
  $in: query.workflows
1573
1610
  }
1574
1611
  });
1612
+ if (query.excludeWorkflows?.length) and.push({
1613
+ workflow: {
1614
+ $nin: query.excludeWorkflows
1615
+ }
1616
+ });
1617
+ if (query.updatedAfter !== void 0) {
1618
+ and.push({
1619
+ updatedAt: {
1620
+ $gte: new Date(query.updatedAfter)
1621
+ }
1622
+ });
1623
+ }
1575
1624
  if (query.namespace !== void 0) where.namespace = query.namespace;
1576
1625
  if (query.namespaces) and.push({
1577
1626
  namespace: {