@mikro-orm/sql 7.1.12-dev.17 → 7.1.12-dev.19

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.1.12-dev.17",
3
+ "version": "7.1.12-dev.19",
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.1.11"
54
54
  },
55
55
  "peerDependencies": {
56
- "@mikro-orm/core": "7.1.12-dev.17"
56
+ "@mikro-orm/core": "7.1.12-dev.19"
57
57
  },
58
58
  "engines": {
59
59
  "node": ">= 22.17.0"
@@ -113,7 +113,8 @@ export class ObjectCriteriaNode extends CriteriaNode {
113
113
  }
114
114
  else if (isRawField) {
115
115
  const rawField = RawQueryFragment.getKnownFragment(field);
116
- o[raw(rawField.sql.replaceAll(ALIAS_REPLACEMENT, alias), rawField.params)] = payload;
116
+ qb.ensureTPTJoins();
117
+ o[raw(qb.helper.replaceAliases(rawField.sql, alias), rawField.params)] = payload;
117
118
  }
118
119
  else if (!childNode.validate && !childNode.prop && !field.includes('.') && !operator) {
119
120
  // wrap unknown fields in raw() to prevent alias prefixing (e.g. raw SQL aliases in HAVING)
@@ -14,6 +14,11 @@ export declare class QueryBuilderHelper {
14
14
  * Returns the main alias if not a TPT property or if the property belongs to the main entity.
15
15
  */
16
16
  getTPTAliasForProperty(propName: string, defaultAlias: string): string;
17
+ /**
18
+ * Replaces `ALIAS_REPLACEMENT` placeholders, resolving column-qualified ones via
19
+ * `getTPTAliasForProperty()` so TPT inherited columns map to their owning table.
20
+ */
21
+ replaceAliases(sql: string, alias?: string): string;
17
22
  mapper(field: string | Raw | RawQueryFragmentSymbol, type?: QueryType): string;
18
23
  mapper(field: string | Raw | RawQueryFragmentSymbol, type?: QueryType, value?: any, alias?: string | null, schema?: string): string;
19
24
  processData(data: Dictionary, convertCustomTypes: boolean, multi?: boolean): any;
@@ -1,5 +1,7 @@
1
1
  import { ALIAS_REPLACEMENT, ALIAS_REPLACEMENT_RE, ArrayType, JsonType, inspect, isRaw, LockMode, OptimisticLockError, QueryOperator, QueryOrderNumeric, raw, Raw, QueryHelper, ReferenceKind, Utils, ValidationError, } from '@mikro-orm/core';
2
2
  import { EMBEDDABLE_ARRAY_OPS, JoinType, QueryType } from './enums.js';
3
+ /** Captures the column name that follows the alias placeholder. */
4
+ const QUALIFYING_ALIAS_RE = new RegExp(ALIAS_REPLACEMENT_RE + '(?=["\'`\\]]*\\.["\'`\\[]?([\\w$]+))', 'g');
3
5
  /**
4
6
  * @internal
5
7
  */
@@ -49,6 +51,15 @@ export class QueryBuilderHelper {
49
51
  // Property not found in hierarchy, return default alias
50
52
  return defaultAlias;
51
53
  }
54
+ /**
55
+ * Replaces `ALIAS_REPLACEMENT` placeholders, resolving column-qualified ones via
56
+ * `getTPTAliasForProperty()` so TPT inherited columns map to their owning table.
57
+ */
58
+ replaceAliases(sql, alias = this.#alias) {
59
+ return sql
60
+ .replace(QUALIFYING_ALIAS_RE, (_, column) => this.getTPTAliasForProperty(column, alias))
61
+ .replaceAll(ALIAS_REPLACEMENT, alias);
62
+ }
52
63
  mapper(field, type = QueryType.SELECT, value, alias, schema) {
53
64
  if (isRaw(field)) {
54
65
  return raw(field.sql, field.params);
@@ -470,7 +481,7 @@ export class QueryBuilderHelper {
470
481
  const op = cond[key] === null ? 'is' : '=';
471
482
  if (Raw.isKnownFragmentSymbol(key)) {
472
483
  const raw = Raw.getKnownFragment(key);
473
- const sql = raw.sql.replaceAll(ALIAS_REPLACEMENT, this.#alias);
484
+ const sql = this.replaceAliases(raw.sql);
474
485
  const value = Utils.asArray(cond[key]);
475
486
  params.push(...raw.params);
476
487
  if (value.length > 0) {
@@ -581,7 +592,7 @@ export class QueryBuilderHelper {
581
592
  if (opValueIsRaw || typeof opValue?.toRaw === 'function') {
582
593
  const query = opValueIsRaw ? opValue : opValue.toRaw();
583
594
  const mappedKey = this.mapper(key, type, query, null);
584
- let sql = query.sql.replaceAll(ALIAS_REPLACEMENT, this.#alias);
595
+ let sql = this.replaceAliases(query.sql);
585
596
  if (['$in', '$nin'].includes(op)) {
586
597
  sql = `(${sql})`;
587
598
  }
package/typings.d.ts CHANGED
@@ -277,6 +277,8 @@ export interface IQueryBuilder<T> {
277
277
  with(name: string, query: AnyQueryBuilder | NativeQueryBuilder | RawQueryFragment, options?: CteOptions): this;
278
278
  withRecursive(name: string, query: AnyQueryBuilder | NativeQueryBuilder | RawQueryFragment, options?: CteOptions): this;
279
279
  scheduleFilterCheck(path: string): void;
280
+ /** @internal */
281
+ ensureTPTJoins(): void;
280
282
  withSchema(schema: string): this;
281
283
  }
282
284
  export interface ICriteriaNodeProcessOptions {