@dudousxd/nestjs-durable-store-mikro-orm 0.24.0 → 0.26.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.d.ts CHANGED
@@ -203,11 +203,40 @@ declare class MikroOrmStateStore implements StateStore {
203
203
  * console can show whole-set counts next to a bounded page instead of downloading every run to
204
204
  * count them in the browser. */
205
205
  runFacets(query: RunFacetQuery): Promise<RunFacetRow[]>;
206
- /** Distinct values of one filter axis over the same predicates {@link listRuns} pages. A run-table
207
- * column is a `GROUP BY` like {@link runFacets}, exact over the whole matching set; the tag and
208
- * attribute axes live outside the row (json array / side table) and are counted from a bounded
209
- * page of runs instead — see {@link RunValueFacetOptions.scan}. */
206
+ /**
207
+ * Distinct values of one filter axis over the same predicates {@link listRuns} pages, with counts —
208
+ * what a console's value pickers list.
209
+ *
210
+ * All three axis families are answered by ONE grouped query against the filtered run set, so the
211
+ * counts are exact over that whole set (not a sample), the ordering is stable, and a picker can
212
+ * page and search server-side. That last part is what makes a picker usable on the axis that needs
213
+ * it most: tag cardinality grows with the data, so the values an operator is looking for are
214
+ * routinely outside any top slice, and a search that only filtered the fetched page could never
215
+ * find them.
216
+ *
217
+ * The predicates come from {@link runQueryBuilder} as a subquery rather than being rewritten here,
218
+ * so a picker and the list it filters can never disagree about which runs they are talking about.
219
+ */
210
220
  runValueFacets(axis: RunValueAxis, query: RunFacetQuery, opts?: RunValueFacetOptions): Promise<RunValueFacetRow[]>;
221
+ /** The filtered run set as a derived table, carrying only the columns the axis needs. One source
222
+ * of truth with `listRuns`: the same builder, the same predicates. */
223
+ private runSubquery;
224
+ /** Where the values of an axis come from, and how to name one — a column of the filtered runs, an
225
+ * element of their tags array, or a row of the attribute side table. */
226
+ private valueSource;
227
+ /**
228
+ * The dialect's way of turning a JSON array column into one row per element — the only portable
229
+ * thing about it being that all three have one. Without it a tag picker cannot be grouped in the
230
+ * database at all, and falls back to deduping a bounded page of runs in memory: counts that
231
+ * describe a sample, and an order that cannot be paged.
232
+ */
233
+ private jsonArrayJoin;
234
+ /** `lower(value) like '%needle%'` — the picker's search box, applied BEFORE the page is cut so a
235
+ * match outside the top slice is still reachable. */
236
+ private searchCondition;
237
+ /** A grouped value as the string a picker shows. Drivers return a JSON element or a numeric
238
+ * attribute as something other than a string; `null` stays null (the absent bucket). */
239
+ private text;
211
240
  /** One attribute predicate as a raw EXISTS subquery on the side-table, correlated to the outer run
212
241
  * alias. `<>` (ne) also excludes runs where the attribute is absent (missing-key-never-matches):
213
242
  * EXISTS already requires a present row, so EXISTS(... <> ...) is exactly ne-with-present. Numeric
package/dist/index.js CHANGED
@@ -547,7 +547,7 @@ __name(durableEntities, "durableEntities");
547
547
  var ENTITIES = durableEntities();
548
548
 
549
549
  // src/mikro-orm-state-store.ts
550
- import { RUN_VALUE_FACET_SCAN, attributePredicateOperands, axisIsRunColumn, mergeRunFacetRows, mergeRunValueFacetRows, normalizeAttributeRows, parseDuration, runValueFacetsFromRuns } from "@dudousxd/nestjs-durable-core";
550
+ import { ENGINE_MINTED_TAG_PREFIXES, RUN_VALUE_FACET_LIMIT, attributePredicateOperands, axisIsRunColumn, facetOrigin, mergeRunFacetRows, normalizeAttributeRows, parseDuration } from "@dudousxd/nestjs-durable-core";
551
551
  import { raw } from "@mikro-orm/core";
552
552
 
553
553
  // src/schema.ts
@@ -1151,30 +1151,118 @@ var MikroOrmStateStore = class _MikroOrmStateStore {
1151
1151
  count: Number(r.count)
1152
1152
  })));
1153
1153
  }
1154
- /** Distinct values of one filter axis over the same predicates {@link listRuns} pages. A run-table
1155
- * column is a `GROUP BY` like {@link runFacets}, exact over the whole matching set; the tag and
1156
- * attribute axes live outside the row (json array / side table) and are counted from a bounded
1157
- * page of runs instead — see {@link RunValueFacetOptions.scan}. */
1154
+ /**
1155
+ * Distinct values of one filter axis over the same predicates {@link listRuns} pages, with counts —
1156
+ * what a console's value pickers list.
1157
+ *
1158
+ * All three axis families are answered by ONE grouped query against the filtered run set, so the
1159
+ * counts are exact over that whole set (not a sample), the ordering is stable, and a picker can
1160
+ * page and search server-side. That last part is what makes a picker usable on the axis that needs
1161
+ * it most: tag cardinality grows with the data, so the values an operator is looking for are
1162
+ * routinely outside any top slice, and a search that only filtered the fetched page could never
1163
+ * find them.
1164
+ *
1165
+ * The predicates come from {@link runQueryBuilder} as a subquery rather than being rewritten here,
1166
+ * so a picker and the list it filters can never disagree about which runs they are talking about.
1167
+ */
1158
1168
  async runValueFacets(axis, query, opts) {
1159
- if (!axisIsRunColumn(axis)) {
1160
- const runs = await this.listRuns({
1161
- ...query,
1162
- limit: opts?.scan ?? RUN_VALUE_FACET_SCAN
1163
- });
1164
- return runValueFacetsFromRuns(runs, axis, opts);
1165
- }
1166
1169
  const em = this.fork();
1167
- const meta = em.getMetadata().get(WorkflowRunEntity);
1168
- const col = meta.properties[axis.field]?.fieldNames?.[0] ?? axis.field;
1169
- const rows = await this.runQueryBuilder(em, query, this.runWhere(query)).select([
1170
- `r.${col}`
1171
- ]).addSelect(raw("count(*) as count")).groupBy([
1172
- `r.${col}`
1173
- ]).execute();
1174
- return mergeRunValueFacetRows(rows.map((row) => ({
1175
- value: row[axis.field] ?? null,
1170
+ const quote = this.idQuote(em);
1171
+ const limit = Math.max(0, opts?.limit ?? RUN_VALUE_FACET_LIMIT);
1172
+ const offset = Math.max(0, opts?.offset ?? 0);
1173
+ if (limit === 0) return [];
1174
+ const runs = this.runSubquery(em, query, axis);
1175
+ const { source, valueExpr } = this.valueSource(em, axis, runs, quote);
1176
+ const conditions = [
1177
+ this.searchCondition(valueExpr, opts?.search)
1178
+ ].filter((part) => part !== void 0);
1179
+ const where = conditions.length ? ` where ${conditions.join(" and ")}` : "";
1180
+ const engineLast = axis.field === "tag" ? `case when ${valueExpr} like '${ENGINE_MINTED_TAG_PREFIXES[0]}%' then 1 else 0 end, ` : "";
1181
+ const sql = `select ${valueExpr} as value, count(*) as count from ${source}${where} group by ${valueExpr} order by ${engineLast}count(*) desc, ${valueExpr} asc limit ${limit} offset ${offset}`;
1182
+ const rows = await em.getConnection().execute(sql);
1183
+ return rows.map((row) => ({
1184
+ // Blank and absent are the same thing to an operator on the origin axis — see facetOrigin.
1185
+ value: axis.field === "origin" ? facetOrigin(row.value) : this.text(row.value),
1176
1186
  count: Number(row.count)
1177
- })), opts);
1187
+ }));
1188
+ }
1189
+ /** The filtered run set as a derived table, carrying only the columns the axis needs. One source
1190
+ * of truth with `listRuns`: the same builder, the same predicates. */
1191
+ runSubquery(em, query, axis) {
1192
+ const meta = em.getMetadata().get(WorkflowRunEntity);
1193
+ const idCol = meta.properties.id?.fieldNames?.[0] ?? "id";
1194
+ const columns = axis.field === "tag" ? [
1195
+ `r.${idCol}`,
1196
+ `r.${this.tagsColumn(em)}`
1197
+ ] : axisIsRunColumn(axis) ? [
1198
+ `r.${meta.properties[axis.field]?.fieldNames?.[0] ?? axis.field}`
1199
+ ] : [
1200
+ `r.${idCol}`
1201
+ ];
1202
+ return this.runQueryBuilder(em, query, this.runWhere(query)).select(columns).getFormattedQuery();
1203
+ }
1204
+ /** Where the values of an axis come from, and how to name one — a column of the filtered runs, an
1205
+ * element of their tags array, or a row of the attribute side table. */
1206
+ valueSource(em, axis, runs, quote) {
1207
+ const meta = em.getMetadata().get(WorkflowRunEntity);
1208
+ if (axisIsRunColumn(axis)) {
1209
+ const col = meta.properties[axis.field]?.fieldNames?.[0] ?? axis.field;
1210
+ return {
1211
+ source: `(${runs}) as ${quote("r")}`,
1212
+ valueExpr: `${quote("r")}.${quote(col)}`
1213
+ };
1214
+ }
1215
+ if (axis.field === "tag") {
1216
+ return {
1217
+ source: `(${runs}) as ${quote("r")} ${this.jsonArrayJoin(em, `${quote("r")}.${quote(this.tagsColumn(em))}`, quote)}`,
1218
+ valueExpr: `${quote("t")}.${quote("value")}`
1219
+ };
1220
+ }
1221
+ const cols = this.attributeColumns(em);
1222
+ const idCol = meta.properties.id?.fieldNames?.[0] ?? "id";
1223
+ const a = quote("a");
1224
+ const join = `${quote(cols.table)} as ${a} join (${runs}) as ${quote("r")} on ${a}.${quote(cols.runId)} = ${quote("r")}.${quote(idCol)}`;
1225
+ if (axis.field === "attributeKey") {
1226
+ return {
1227
+ source: join,
1228
+ valueExpr: `${a}.${quote(cols.key)}`
1229
+ };
1230
+ }
1231
+ const key = axis.key.replace(/'/g, "''");
1232
+ return {
1233
+ source: `${join} and ${a}.${quote(cols.key)} = '${key}'`,
1234
+ valueExpr: `coalesce(${a}.${quote(cols.strValue)}, cast(${a}.${quote(cols.numValue)} as char))`
1235
+ };
1236
+ }
1237
+ /**
1238
+ * The dialect's way of turning a JSON array column into one row per element — the only portable
1239
+ * thing about it being that all three have one. Without it a tag picker cannot be grouped in the
1240
+ * database at all, and falls back to deduping a bounded page of runs in memory: counts that
1241
+ * describe a sample, and an order that cannot be paged.
1242
+ */
1243
+ jsonArrayJoin(em, colRef, quote) {
1244
+ const platform = String(em.getPlatform().constructor.name).toLowerCase();
1245
+ const t = quote("t");
1246
+ if (platform.includes("postgre")) {
1247
+ return `cross join lateral jsonb_array_elements_text(${colRef}::jsonb) as ${t}(${quote("value")})`;
1248
+ }
1249
+ if (platform.includes("mysql") || platform.includes("mariadb")) {
1250
+ return `join json_table(cast(${colRef} as json), '$[*]' columns (${quote("value")} varchar(512) path '$')) as ${t}`;
1251
+ }
1252
+ return `join json_each(${colRef}) as ${t}`;
1253
+ }
1254
+ /** `lower(value) like '%needle%'` — the picker's search box, applied BEFORE the page is cut so a
1255
+ * match outside the top slice is still reachable. */
1256
+ searchCondition(valueExpr, search) {
1257
+ const needle = search?.trim().toLowerCase();
1258
+ if (!needle) return void 0;
1259
+ const escaped = needle.replace(/'/g, "''").replace(/[%_]/g, (c) => `\\${c}`);
1260
+ return `lower(${valueExpr}) like '%${escaped}%'`;
1261
+ }
1262
+ /** A grouped value as the string a picker shows. Drivers return a JSON element or a numeric
1263
+ * attribute as something other than a string; `null` stays null (the absent bucket). */
1264
+ text(value) {
1265
+ return value === null || value === void 0 ? null : String(value);
1178
1266
  }
1179
1267
  /** One attribute predicate as a raw EXISTS subquery on the side-table, correlated to the outer run
1180
1268
  * alias. `<>` (ne) also excludes runs where the attribute is absent (missing-key-never-matches):