@mikro-orm/sql 7.2.9-dev.24 → 7.2.9-dev.25

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.
@@ -342,6 +342,14 @@ export class AbstractSqlDriver extends DatabaseDriver {
342
342
  if (qb) {
343
343
  // here we map the aliased results (cartesian product) to an object graph
344
344
  this.mapJoinedProps(ret, meta, populate, qb, ret, map);
345
+ // the aliased columns were deleted, which left the row in slow dictionary mode
346
+ if (populate.length > 0) {
347
+ const clean = {};
348
+ for (const key in ret) {
349
+ clean[key] = ret[key];
350
+ }
351
+ return clean;
352
+ }
345
353
  }
346
354
  return ret;
347
355
  }
@@ -1688,9 +1696,10 @@ export class AbstractSqlDriver extends DatabaseDriver {
1688
1696
  if (!Utils.isEmpty(orderBy) || RawQueryFragment.hasObjectFragments(orderBy)) {
1689
1697
  return Utils.asArray(orderBy).map(o => ({ [pivotProp.name]: o }));
1690
1698
  }
1691
- if (prop.kind === ReferenceKind.MANY_TO_MANY && Utils.asArray(parentOrderBy).some(o => o[prop.name])) {
1699
+ if (prop.kind === ReferenceKind.MANY_TO_MANY &&
1700
+ Utils.asArray(parentOrderBy).some(o => !Utils.isEmpty(o[prop.name]))) {
1692
1701
  return Utils.asArray(parentOrderBy)
1693
- .filter(o => o[prop.name])
1702
+ .filter(o => !Utils.isEmpty(o[prop.name]))
1694
1703
  .map(o => ({ [pivotProp.name]: o[prop.name] }));
1695
1704
  }
1696
1705
  if (!Utils.isEmpty(prop.orderBy) || RawQueryFragment.hasObjectFragments(prop.orderBy)) {
@@ -1,4 +1,4 @@
1
- import { isRaw, JsonProperty, Platform, raw, Utils, ValidationError, } from '@mikro-orm/core';
1
+ import { inspect, isRaw, JsonProperty, Platform, raw, Utils, ValidationError, } from '@mikro-orm/core';
2
2
  import { SqlEntityRepository } from './SqlEntityRepository.js';
3
3
  import { SqlSchemaGenerator } from './schema/SqlSchemaGenerator.js';
4
4
  import { NativeQueryBuilder } from './query/NativeQueryBuilder.js';
@@ -169,9 +169,12 @@ export class AbstractSqlPlatform extends Platform {
169
169
  * @internal
170
170
  */
171
171
  validateOrderByDirection(direction) {
172
- const dir = ('' + direction).toLowerCase().trim();
172
+ if (typeof direction !== 'string') {
173
+ throw new ValidationError(`Invalid order direction: ${inspect(direction)}`);
174
+ }
175
+ const dir = direction.toLowerCase().trim();
173
176
  if (!AbstractSqlPlatform.ORDER_BY_DIRECTIONS.has(dir)) {
174
- throw new Error(`Invalid order direction: '${direction}'`);
177
+ throw new ValidationError(`Invalid order direction: '${direction}'`);
175
178
  }
176
179
  return dir;
177
180
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/sql",
3
- "version": "7.2.9-dev.24",
3
+ "version": "7.2.9-dev.25",
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",
@@ -53,7 +53,7 @@
53
53
  "@mikro-orm/core": "^7.2.8"
54
54
  },
55
55
  "peerDependencies": {
56
- "@mikro-orm/core": "7.2.9-dev.24"
56
+ "@mikro-orm/core": "7.2.9-dev.25"
57
57
  },
58
58
  "engines": {
59
59
  "node": ">= 22.17.0"
@@ -1,4 +1,4 @@
1
- import { ALIAS_REPLACEMENT, GroupOperator, QueryFlag, raw, RawQueryFragment, ReferenceKind, Utils, } from '@mikro-orm/core';
1
+ import { ALIAS_REPLACEMENT, GroupOperator, QueryFlag, raw, RawQueryFragment, ReferenceKind, Utils, ValidationError, } from '@mikro-orm/core';
2
2
  import { CriteriaNode } from './CriteriaNode.js';
3
3
  import { JoinType, QueryType } from './enums.js';
4
4
  const COLLECTION_OPERATORS = ['$some', '$none', '$every', '$size', '$all'];
@@ -7,6 +7,9 @@ const COLLECTION_OPERATORS = ['$some', '$none', '$every', '$size', '$all'];
7
7
  */
8
8
  export class ObjectCriteriaNode extends CriteriaNode {
9
9
  process(qb, options) {
10
+ if (options?.type === 'orderBy' && this.prop?.kind === ReferenceKind.SCALAR) {
11
+ throw new ValidationError(`Invalid order direction for ${Utils.className(this.entityName)}.${this.prop.name}`);
12
+ }
10
13
  const matchPopulateJoins = options?.matchPopulateJoins ||
11
14
  (this.prop && [ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(this.prop.kind));
12
15
  const nestedAlias = qb.getAliasForJoinPath(this.getPath(options), { ...options, matchPopulateJoins });
@@ -2020,12 +2020,13 @@ export class QueryBuilder {
2020
2020
  if (meta && (this.#state.fields?.includes('*') || this.#state.fields?.includes(`${this.mainAlias.aliasName}.*`))) {
2021
2021
  const schema = this.getSchema(this.mainAlias);
2022
2022
  // Per-column entries are unquoted (meant for `quote`), while `toString()` yields the quoted alias for plain template literals
2023
- // For TPT, use helper to resolve correct alias per property (inherited props use parent alias)
2024
2023
  const quotedMainAlias = this.platform.quoteIdentifier(this.mainAlias.aliasName).toString();
2025
- const columns = meta.createColumnMappingObject(prop => this.helper.getTPTAliasForProperty(prop.name, this.mainAlias.aliasName), quotedMainAlias);
2024
+ let columns;
2026
2025
  meta.props
2027
2026
  .filter(prop => prop.formula && (!prop.lazy || this.#state.flags.has(QueryFlag.INCLUDE_LAZY_FORMULAS)))
2028
2027
  .map(prop => {
2028
+ // For TPT, use helper to resolve correct alias per property (inherited props use parent alias)
2029
+ columns ??= meta.createColumnMappingObject(p => this.helper.getTPTAliasForProperty(p.name, this.mainAlias.aliasName), quotedMainAlias);
2029
2030
  const aliased = this.platform.quoteIdentifier(prop.fieldNames[0]);
2030
2031
  const table = this.helper.createFormulaTable(quotedMainAlias, meta, schema);
2031
2032
  return `${this.driver.evaluateFormula(prop.formula, columns, table)} as ${aliased}`;
@@ -767,33 +767,29 @@ export class QueryBuilderHelper {
767
767
  if (isRaw(colPart)) {
768
768
  colPart = this.#platform.formatQuery(colPart.sql, colPart.params);
769
769
  }
770
- if (Array.isArray(order)) {
771
- order.forEach(part => ret.push(...this.getQueryOrderFromObject(type, part, populate, collation)));
772
- }
773
- else {
774
- ret.push(...this.#platform.getOrderByExpression(colPart, order, collation));
775
- }
770
+ ret.push(...this.#platform.getOrderByExpression(colPart, order, collation));
776
771
  }
777
772
  }
778
773
  return ret;
779
774
  }
780
775
  splitField(field, greedyAlias = false) {
781
- const parts = field.split('.');
782
- const ref = parts[parts.length - 1].split(':')[1];
783
- if (ref) {
784
- parts[parts.length - 1] = parts[parts.length - 1].substring(0, parts[parts.length - 1].indexOf(':'));
785
- }
786
- if (parts.length === 1) {
787
- return [this.#alias, parts[0], ref];
788
- }
789
- if (greedyAlias) {
790
- const fromField = parts.pop();
791
- const fromAlias = parts.join('.');
792
- return [fromAlias, fromField, ref];
793
- }
794
- const fromAlias = parts.shift();
795
- const fromField = parts.join('.');
796
- return [fromAlias, fromField, ref];
776
+ const lastDot = field.lastIndexOf('.');
777
+ const colon = field.indexOf(':', lastDot + 1);
778
+ let end = field.length;
779
+ let ref;
780
+ // only the segment between the first and second colon of the last path part is the ref
781
+ if (colon !== -1) {
782
+ const nextColon = field.indexOf(':', colon + 1);
783
+ ref = field.substring(colon + 1, nextColon === -1 ? end : nextColon);
784
+ if (ref) {
785
+ end = colon;
786
+ }
787
+ }
788
+ if (lastDot === -1) {
789
+ return [this.#alias, field.substring(0, end), ref];
790
+ }
791
+ const aliasEnd = greedyAlias ? lastDot : field.indexOf('.');
792
+ return [field.substring(0, aliasEnd), field.substring(aliasEnd + 1, end), ref];
797
793
  }
798
794
  getLockSQL(qb, lockMode, lockTables = [], joinsMap) {
799
795
  const meta = this.#metadata.find(this.#entityName);