@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.cjs CHANGED
@@ -1095,6 +1095,9 @@ async function alignDurableCollation(orm) {
1095
1095
  __name(alignDurableCollation, "alignDurableCollation");
1096
1096
 
1097
1097
  // src/mikro-orm-state-store.ts
1098
+ var RETENTION_EXCLUDED = [
1099
+ import_nestjs_durable_core.LIFECYCLE_REGISTRY_WORKFLOW
1100
+ ];
1098
1101
  var MikroOrmStateStore = class _MikroOrmStateStore {
1099
1102
  static {
1100
1103
  __name(this, "MikroOrmStateStore");
@@ -1139,19 +1142,48 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1139
1142
  });
1140
1143
  }
1141
1144
  async tryAdmitSingleton(runId, tag, workflow, limit, retryWakeAt) {
1145
+ const where = {
1146
+ workflow,
1147
+ status: {
1148
+ $in: import_nestjs_durable_core.SINGLETON_ACTIVE_STATUSES
1149
+ },
1150
+ ...this.scopeNamespace !== void 0 ? {
1151
+ namespace: this.scopeNamespace
1152
+ } : {}
1153
+ };
1154
+ const reader = this.fork();
1155
+ const sqlite = /sqlite|libsql/i.test(reader.getPlatform().constructor.name);
1156
+ const claimants = sqlite ? [] : (0, import_nestjs_durable_core.singletonClaimantIds)(await reader.find(WorkflowRunEntity, where, {
1157
+ fields: [
1158
+ "id",
1159
+ "tags"
1160
+ ],
1161
+ orderBy: {
1162
+ id: "ASC"
1163
+ },
1164
+ // The primary: a lagging replica would leave rows out of the lock set.
1165
+ connectionType: "write"
1166
+ }), tag);
1142
1167
  return this.fork().transactional(async (em) => {
1143
- const where = {
1144
- workflow,
1145
- status: {
1146
- $in: import_nestjs_durable_core.SINGLETON_ACTIVE_STATUSES
1147
- },
1148
- ...this.scopeNamespace !== void 0 ? {
1149
- namespace: this.scopeNamespace
1150
- } : {}
1151
- };
1152
- await em.nativeUpdate(WorkflowRunEntity, where, {
1168
+ if (sqlite) await em.nativeUpdate(WorkflowRunEntity, where, {
1153
1169
  workflow
1154
1170
  });
1171
+ for (let at = 0; at < claimants.length; at += import_nestjs_durable_core.SINGLETON_LOCK_CHUNK) {
1172
+ await em.find(WorkflowRunEntity, {
1173
+ id: {
1174
+ $in: claimants.slice(at, at + import_nestjs_durable_core.SINGLETON_LOCK_CHUNK)
1175
+ }
1176
+ }, {
1177
+ fields: [
1178
+ "id"
1179
+ ],
1180
+ orderBy: {
1181
+ id: "ASC"
1182
+ },
1183
+ lockMode: import_core3.LockMode.PESSIMISTIC_WRITE
1184
+ });
1185
+ }
1186
+ em.clear();
1155
1187
  const rows = await em.find(WorkflowRunEntity, where);
1156
1188
  const runs = rows.map(fromRunEntity);
1157
1189
  const tags = (0, import_nestjs_durable_core.singletonAdmissionTags)(runs, runId, tag, workflow, limit);
@@ -1293,13 +1325,16 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1293
1325
  const idCol = meta.properties.id?.fieldNames?.[0] ?? "id";
1294
1326
  const query = {
1295
1327
  ...policy.scope ?? {},
1296
- statuses
1328
+ statuses,
1329
+ excludeWorkflows: RETENTION_EXCLUDED
1297
1330
  };
1331
+ const tagsCol = meta.properties.tags?.fieldNames?.[0] ?? "tags";
1298
1332
  const scoped = /* @__PURE__ */ __name((extra) => this.runQueryBuilder(em, query, {
1299
1333
  ...this.runWhere(query),
1300
1334
  ...extra ?? {}
1301
1335
  }).select([
1302
- `r.${idCol}`
1336
+ `r.${idCol}`,
1337
+ `r.${tagsCol}`
1303
1338
  ]), "scoped");
1304
1339
  const ids = /* @__PURE__ */ new Set();
1305
1340
  if (policy.maxAge != null) {
@@ -1311,7 +1346,9 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1311
1346
  }).orderBy({
1312
1347
  "r.updatedAt": "asc"
1313
1348
  }).limit(limit).execute();
1314
- for (const r of rows) ids.add(String(r[idCol] ?? r.id));
1349
+ for (const r of rows) {
1350
+ if (!(0, import_nestjs_durable_core.retentionKeeps)(r[tagsCol] ?? r.tags)) ids.add(String(r[idCol] ?? r.id));
1351
+ }
1315
1352
  }
1316
1353
  if (policy.maxCount != null && ids.size < limit) {
1317
1354
  const rows = await scoped().orderBy({
@@ -1320,7 +1357,7 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1320
1357
  }).limit(limit).offset(policy.maxCount).execute();
1321
1358
  for (const r of rows) {
1322
1359
  if (ids.size >= limit) break;
1323
- ids.add(String(r[idCol] ?? r.id));
1360
+ if (!(0, import_nestjs_durable_core.retentionKeeps)(r[tagsCol] ?? r.tags)) ids.add(String(r[idCol] ?? r.id));
1324
1361
  }
1325
1362
  }
1326
1363
  const idList = [
@@ -1618,6 +1655,18 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1618
1655
  $in: query.workflows
1619
1656
  }
1620
1657
  });
1658
+ if (query.excludeWorkflows?.length) and.push({
1659
+ workflow: {
1660
+ $nin: query.excludeWorkflows
1661
+ }
1662
+ });
1663
+ if (query.updatedAfter !== void 0) {
1664
+ and.push({
1665
+ updatedAt: {
1666
+ $gte: new Date(query.updatedAfter)
1667
+ }
1668
+ });
1669
+ }
1621
1670
  if (query.namespace !== void 0) where.namespace = query.namespace;
1622
1671
  if (query.namespaces) and.push({
1623
1672
  namespace: {