@mikro-orm/sql 7.0.4-dev.0 → 7.0.4-dev.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/sql",
3
- "version": "7.0.4-dev.0",
3
+ "version": "7.0.4-dev.2",
4
4
  "description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
5
5
  "keywords": [
6
6
  "data-mapper",
@@ -42,7 +42,7 @@
42
42
  },
43
43
  "scripts": {
44
44
  "build": "yarn compile && yarn copy",
45
- "clean": "yarn run -T rimraf ./dist",
45
+ "clean": "yarn run -T rimraf ./dist ./tsconfig.build.tsbuildinfo",
46
46
  "compile": "yarn run -T tsc -p tsconfig.build.json",
47
47
  "copy": "node ../../scripts/copy.mjs"
48
48
  },
@@ -53,7 +53,7 @@
53
53
  "@mikro-orm/core": "^7.0.3"
54
54
  },
55
55
  "peerDependencies": {
56
- "@mikro-orm/core": "7.0.4-dev.0"
56
+ "@mikro-orm/core": "7.0.4-dev.2"
57
57
  },
58
58
  "engines": {
59
59
  "node": ">= 22.17.0"
@@ -923,7 +923,7 @@ export declare class QueryBuilder<Entity extends object = AnyEntity, RootAlias e
923
923
  /**
924
924
  * Removes joins that are not used for population or ordering to improve performance.
925
925
  */
926
- protected pruneJoinsForPagination(meta: EntityMetadata, populatePaths: Set<string>): void;
926
+ protected pruneJoinsForPagination(): void;
927
927
  /**
928
928
  * Transfers WHERE conditions that reference a join alias to the join's ON clause.
929
929
  * This is needed when a join is kept for ORDER BY after pagination wrapping,
@@ -1988,7 +1988,7 @@ export class QueryBuilder {
1988
1988
  const originalCond = this.#state.cond;
1989
1989
  const populatePaths = this.getPopulatePaths();
1990
1990
  if (!this.#state.fields.some(field => isRaw(field))) {
1991
- this.pruneJoinsForPagination(meta, populatePaths);
1991
+ this.pruneJoinsForPagination();
1992
1992
  }
1993
1993
  // Transfer WHERE conditions to ORDER BY joins (GH #6160)
1994
1994
  this.transferConditionsForOrderByJoins(meta, originalCond, populatePaths);
@@ -2043,23 +2043,51 @@ export class QueryBuilder {
2043
2043
  /**
2044
2044
  * Removes joins that are not used for population or ordering to improve performance.
2045
2045
  */
2046
- pruneJoinsForPagination(meta, populatePaths) {
2047
- const orderByAliases = this.#state.orderBy.flatMap(hint => Object.keys(hint)).map(k => k.split('.')[0]);
2046
+ pruneJoinsForPagination() {
2048
2047
  const joins = Object.entries(this.#state.joins);
2049
2048
  const rootAlias = this.alias;
2050
- function addParentAlias(alias) {
2049
+ const keptAliases = new Set();
2050
+ // Collect aliases needed for ORDER BY
2051
+ for (const hint of this.#state.orderBy) {
2052
+ for (const k of Object.keys(hint)) {
2053
+ if (!RawQueryFragment.isKnownFragmentSymbol(k)) {
2054
+ keptAliases.add(k.split('.')[0]);
2055
+ }
2056
+ }
2057
+ }
2058
+ // Collect aliases needed for population (joined strategy):
2059
+ // 1. Joins tracked in populateMap (OneToOne inverse from processPopulateHint)
2060
+ // 2. Joins tracked in joinedProps (manual joinAndSelect calls)
2061
+ // 3. Joins whose alias is referenced in SELECT fields (JOINED strategy populate fields)
2062
+ for (const [key, join] of joins) {
2063
+ if (key in this.#state.populateMap || this.#state.joinedProps.has(join.alias)) {
2064
+ keptAliases.add(join.alias);
2065
+ }
2066
+ }
2067
+ // Check SELECT fields for alias references - keeps joins needed for data loading
2068
+ if (this.#state.fields) {
2069
+ for (const field of this.#state.fields) {
2070
+ const fieldStr = typeof field === 'string' ? field : isRaw(field) ? field.sql : '';
2071
+ for (const [, join] of joins) {
2072
+ if (fieldStr.includes(`${join.alias}.`) || fieldStr.includes(`${join.alias}__`)) {
2073
+ keptAliases.add(join.alias);
2074
+ }
2075
+ }
2076
+ }
2077
+ }
2078
+ // Also keep parent joins of all kept aliases (e.g. pivot tables for M:N)
2079
+ function addParentAliases(alias) {
2051
2080
  const join = joins.find(j => j[1].alias === alias);
2052
- if (join && join[1].ownerAlias !== rootAlias) {
2053
- orderByAliases.push(join[1].ownerAlias);
2054
- addParentAlias(join[1].ownerAlias);
2081
+ if (join && join[1].ownerAlias !== rootAlias && !keptAliases.has(join[1].ownerAlias)) {
2082
+ keptAliases.add(join[1].ownerAlias);
2083
+ addParentAliases(join[1].ownerAlias);
2055
2084
  }
2056
2085
  }
2057
- for (const orderByAlias of orderByAliases) {
2058
- addParentAlias(orderByAlias);
2086
+ for (const alias of [...keptAliases]) {
2087
+ addParentAliases(alias);
2059
2088
  }
2060
2089
  for (const [key, join] of joins) {
2061
- const path = this.normalizeJoinPath(join, meta);
2062
- if (!populatePaths.has(path) && !orderByAliases.includes(join.alias)) {
2090
+ if (!keptAliases.has(join.alias)) {
2063
2091
  delete this.#state.joins[key];
2064
2092
  }
2065
2093
  }