@mikro-orm/sql 7.0.0-rc.0 → 7.0.0-rc.1
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/AbstractSqlDriver.d.ts +7 -0
- package/AbstractSqlDriver.js +58 -25
- package/AbstractSqlPlatform.d.ts +8 -1
- package/AbstractSqlPlatform.js +18 -1
- package/dialects/mysql/BaseMySqlPlatform.d.ts +1 -1
- package/dialects/mysql/BaseMySqlPlatform.js +4 -3
- package/package.json +2 -2
- package/query/ObjectCriteriaNode.js +1 -1
- package/query/QueryBuilder.d.ts +37 -16
- package/query/QueryBuilder.js +80 -15
- package/query/QueryBuilderHelper.d.ts +2 -2
- package/query/QueryBuilderHelper.js +9 -9
- package/schema/SchemaComparator.js +1 -0
- package/tsconfig.build.tsbuildinfo +1 -1
- package/typings.d.ts +4 -2
package/query/QueryBuilder.js
CHANGED
|
@@ -3,6 +3,8 @@ import { JoinType, QueryType } from './enums.js';
|
|
|
3
3
|
import { QueryBuilderHelper } from './QueryBuilderHelper.js';
|
|
4
4
|
import { CriteriaNodeFactory } from './CriteriaNodeFactory.js';
|
|
5
5
|
import { NativeQueryBuilder } from './NativeQueryBuilder.js';
|
|
6
|
+
/** Matches 'path as alias' — safe because ORM property names are JS identifiers (no spaces). */
|
|
7
|
+
const FIELD_ALIAS_RE = /^(.+?)\s+as\s+(\w+)$/i;
|
|
6
8
|
/**
|
|
7
9
|
* SQL query builder with fluent interface.
|
|
8
10
|
*
|
|
@@ -67,6 +69,7 @@ export class QueryBuilder {
|
|
|
67
69
|
_joinedProps = new Map();
|
|
68
70
|
_cache;
|
|
69
71
|
_indexHint;
|
|
72
|
+
_collation;
|
|
70
73
|
_comments = [];
|
|
71
74
|
_hintComments = [];
|
|
72
75
|
flushMode;
|
|
@@ -103,8 +106,19 @@ export class QueryBuilder {
|
|
|
103
106
|
this.ensureNotFinalized();
|
|
104
107
|
this._fields = Utils.asArray(fields).flatMap(f => {
|
|
105
108
|
if (typeof f !== 'string') {
|
|
109
|
+
// Normalize sql.ref('prop') and sql.ref('prop').as('alias') to string form
|
|
110
|
+
if (isRaw(f) && f.sql === '??' && f.params.length === 1) {
|
|
111
|
+
return this.resolveNestedPath(String(f.params[0]));
|
|
112
|
+
}
|
|
113
|
+
if (isRaw(f) && f.sql === '?? as ??' && f.params.length === 2) {
|
|
114
|
+
return `${this.resolveNestedPath(String(f.params[0]))} as ${String(f.params[1])}`;
|
|
115
|
+
}
|
|
106
116
|
return f;
|
|
107
117
|
}
|
|
118
|
+
const asMatch = f.match(FIELD_ALIAS_RE);
|
|
119
|
+
if (asMatch) {
|
|
120
|
+
return `${this.resolveNestedPath(asMatch[1].trim())} as ${asMatch[2]}`;
|
|
121
|
+
}
|
|
108
122
|
return this.resolveNestedPath(f);
|
|
109
123
|
});
|
|
110
124
|
if (distinct) {
|
|
@@ -307,7 +321,7 @@ export class QueryBuilder {
|
|
|
307
321
|
for (const p of targetMeta.getPrimaryProps()) {
|
|
308
322
|
fields.push(...this.driver.mapPropToFieldNames(this, p, alias, targetMeta, schema));
|
|
309
323
|
}
|
|
310
|
-
if (explicitFields) {
|
|
324
|
+
if (explicitFields && explicitFields.length > 0) {
|
|
311
325
|
for (const field of explicitFields) {
|
|
312
326
|
const [a, f] = this.helper.splitField(field);
|
|
313
327
|
const p = targetMeta.properties[f];
|
|
@@ -321,7 +335,7 @@ export class QueryBuilder {
|
|
|
321
335
|
}
|
|
322
336
|
targetMeta.props
|
|
323
337
|
.filter(prop => {
|
|
324
|
-
if (!explicitFields) {
|
|
338
|
+
if (!explicitFields || explicitFields.length === 0) {
|
|
325
339
|
return this.platform.shouldHaveColumn(prop, populate);
|
|
326
340
|
}
|
|
327
341
|
return prop.primary && !explicitFields.includes(prop.name) && !explicitFields.includes(`${alias}.${prop.name}`);
|
|
@@ -457,7 +471,18 @@ export class QueryBuilder {
|
|
|
457
471
|
if (reset) {
|
|
458
472
|
this._orderBy = [];
|
|
459
473
|
}
|
|
460
|
-
|
|
474
|
+
const selectAliases = this.getSelectAliases();
|
|
475
|
+
Utils.asArray(orderBy).forEach(orig => {
|
|
476
|
+
// Shallow clone to avoid mutating the caller's object — safe because the clone
|
|
477
|
+
// is only used within this loop iteration and `orig` is not referenced afterward.
|
|
478
|
+
const o = { ...orig };
|
|
479
|
+
// Wrap known select aliases in raw() so they bypass property validation and alias prefixing
|
|
480
|
+
for (const key of Object.keys(o)) {
|
|
481
|
+
if (selectAliases.has(key)) {
|
|
482
|
+
o[raw('??', [key])] = o[key];
|
|
483
|
+
delete o[key];
|
|
484
|
+
}
|
|
485
|
+
}
|
|
461
486
|
this.helper.validateQueryOrder(o);
|
|
462
487
|
const processed = QueryHelper.processWhere({
|
|
463
488
|
where: o,
|
|
@@ -476,10 +501,27 @@ export class QueryBuilder {
|
|
|
476
501
|
});
|
|
477
502
|
return this;
|
|
478
503
|
}
|
|
504
|
+
/** Collect custom aliases from select fields (stored as 'resolved as alias' strings by select()). */
|
|
505
|
+
getSelectAliases() {
|
|
506
|
+
const aliases = new Set();
|
|
507
|
+
for (const field of this._fields ?? []) {
|
|
508
|
+
if (typeof field === 'string') {
|
|
509
|
+
const m = field.match(FIELD_ALIAS_RE);
|
|
510
|
+
if (m) {
|
|
511
|
+
aliases.add(m[2]);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
return aliases;
|
|
516
|
+
}
|
|
479
517
|
groupBy(fields) {
|
|
480
518
|
this.ensureNotFinalized();
|
|
481
519
|
this._groupBy = Utils.asArray(fields).flatMap(f => {
|
|
482
520
|
if (typeof f !== 'string') {
|
|
521
|
+
// Normalize sql.ref('prop') to string for proper formula resolution
|
|
522
|
+
if (isRaw(f) && f.sql === '??' && f.params.length === 1) {
|
|
523
|
+
return this.resolveNestedPath(String(f.params[0]));
|
|
524
|
+
}
|
|
483
525
|
return f;
|
|
484
526
|
}
|
|
485
527
|
return this.resolveNestedPath(f);
|
|
@@ -501,7 +543,7 @@ export class QueryBuilder {
|
|
|
501
543
|
if (typeof cond === 'string') {
|
|
502
544
|
cond = { [raw(`(${cond})`, params)]: [] };
|
|
503
545
|
}
|
|
504
|
-
const processed = CriteriaNodeFactory.createNode(this.metadata, this.mainAlias.entityName, cond, undefined, undefined, false).process(this);
|
|
546
|
+
const processed = CriteriaNodeFactory.createNode(this.metadata, this.mainAlias.entityName, cond, undefined, undefined, false).process(this, { type: 'having' });
|
|
505
547
|
if (!this._having || !operator) {
|
|
506
548
|
this._having = processed;
|
|
507
549
|
}
|
|
@@ -638,6 +680,14 @@ export class QueryBuilder {
|
|
|
638
680
|
this._indexHint = sql;
|
|
639
681
|
return this;
|
|
640
682
|
}
|
|
683
|
+
/**
|
|
684
|
+
* Adds COLLATE clause to ORDER BY expressions.
|
|
685
|
+
*/
|
|
686
|
+
collation(collation) {
|
|
687
|
+
this.ensureNotFinalized();
|
|
688
|
+
this._collation = collation;
|
|
689
|
+
return this;
|
|
690
|
+
}
|
|
641
691
|
/**
|
|
642
692
|
* Prepend comment to the sql query using the syntax `/* ... *‍/`. Some characters are forbidden such as `/*, *‍/` and `?`.
|
|
643
693
|
*/
|
|
@@ -682,7 +732,7 @@ export class QueryBuilder {
|
|
|
682
732
|
Utils.runIfNotEmpty(() => qb.groupBy(this.prepareFields(this._groupBy, 'groupBy', schema)), isNotEmptyObject(this._groupBy));
|
|
683
733
|
Utils.runIfNotEmpty(() => this.helper.appendQueryCondition(this.type, this._having, qb, undefined, 'having'), isNotEmptyObject(this._having));
|
|
684
734
|
Utils.runIfNotEmpty(() => {
|
|
685
|
-
const queryOrder = this.helper.getQueryOrder(this.type, this._orderBy, this._populateMap);
|
|
735
|
+
const queryOrder = this.helper.getQueryOrder(this.type, this._orderBy, this._populateMap, this._collation);
|
|
686
736
|
if (queryOrder.length > 0) {
|
|
687
737
|
const sql = Utils.unique(queryOrder).join(', ');
|
|
688
738
|
qb.orderBy(sql);
|
|
@@ -1015,7 +1065,7 @@ export class QueryBuilder {
|
|
|
1015
1065
|
// clone array/object properties
|
|
1016
1066
|
const properties = [
|
|
1017
1067
|
'flags', '_populate', '_populateWhere', '_populateFilter', '__populateWhere', '_populateMap', '_joins', '_joinedProps', '_cond', '_data', '_orderBy',
|
|
1018
|
-
'_schema', '_indexHint', '_cache', 'subQueries', 'lockMode', 'lockTables', '_groupBy', '_having', '_returning',
|
|
1068
|
+
'_schema', '_indexHint', '_collation', '_cache', 'subQueries', 'lockMode', 'lockTables', '_groupBy', '_having', '_returning',
|
|
1019
1069
|
'_comments', '_hintComments', 'aliasCounter',
|
|
1020
1070
|
];
|
|
1021
1071
|
for (const prop of Object.keys(this)) {
|
|
@@ -1167,14 +1217,23 @@ export class QueryBuilder {
|
|
|
1167
1217
|
}
|
|
1168
1218
|
prepareFields(fields, type = 'where', schema) {
|
|
1169
1219
|
const ret = [];
|
|
1170
|
-
const getFieldName = (name) => {
|
|
1171
|
-
|
|
1220
|
+
const getFieldName = (name, customAlias) => {
|
|
1221
|
+
const alias = customAlias ?? (type === 'groupBy' ? null : undefined);
|
|
1222
|
+
return this.helper.mapper(name, this.type, undefined, alias, schema);
|
|
1172
1223
|
};
|
|
1173
|
-
fields.forEach(
|
|
1174
|
-
if (typeof
|
|
1175
|
-
ret.push(
|
|
1224
|
+
fields.forEach(originalField => {
|
|
1225
|
+
if (typeof originalField !== 'string') {
|
|
1226
|
+
ret.push(originalField);
|
|
1176
1227
|
return;
|
|
1177
1228
|
}
|
|
1229
|
+
// Strip 'as alias' suffix if present — the alias is passed to mapper at the end
|
|
1230
|
+
let field = originalField;
|
|
1231
|
+
let customAlias;
|
|
1232
|
+
const asMatch = originalField.match(FIELD_ALIAS_RE);
|
|
1233
|
+
if (asMatch) {
|
|
1234
|
+
field = asMatch[1].trim();
|
|
1235
|
+
customAlias = asMatch[2];
|
|
1236
|
+
}
|
|
1178
1237
|
const join = Object.keys(this._joins).find(k => field === k.substring(0, k.indexOf('#')));
|
|
1179
1238
|
if (join && type === 'where') {
|
|
1180
1239
|
ret.push(...this.helper.mapJoinColumns(this.type, this._joins[join]));
|
|
@@ -1192,10 +1251,13 @@ export class QueryBuilder {
|
|
|
1192
1251
|
if (prop?.embedded || (prop?.kind === ReferenceKind.EMBEDDED && prop.object)) {
|
|
1193
1252
|
const name = prop.embeddedPath?.join('.') ?? prop.fieldNames[0];
|
|
1194
1253
|
const aliased = this._aliases[a] ? `${a}.${name}` : name;
|
|
1195
|
-
ret.push(getFieldName(aliased));
|
|
1254
|
+
ret.push(getFieldName(aliased, customAlias));
|
|
1196
1255
|
return;
|
|
1197
1256
|
}
|
|
1198
1257
|
if (prop?.kind === ReferenceKind.EMBEDDED) {
|
|
1258
|
+
if (customAlias) {
|
|
1259
|
+
throw new Error(`Cannot use 'as ${customAlias}' alias on embedded property '${field}' because it expands to multiple columns. Alias individual fields instead (e.g. '${field}.propertyName as ${customAlias}').`);
|
|
1260
|
+
}
|
|
1199
1261
|
const nest = (prop) => {
|
|
1200
1262
|
for (const childProp of Object.values(prop.embeddedProps)) {
|
|
1201
1263
|
if (childProp.fieldNames && (childProp.kind !== ReferenceKind.EMBEDDED || childProp.object) && childProp.persist !== false) {
|
|
@@ -1209,11 +1271,14 @@ export class QueryBuilder {
|
|
|
1209
1271
|
nest(prop);
|
|
1210
1272
|
return;
|
|
1211
1273
|
}
|
|
1212
|
-
if (prop && prop.fieldNames.length > 1) {
|
|
1274
|
+
if (prop && prop.fieldNames.length > 1 && !prop.fieldNames.includes(f)) {
|
|
1275
|
+
if (customAlias) {
|
|
1276
|
+
throw new Error(`Cannot use 'as ${customAlias}' alias on '${field}' because it expands to multiple columns (${prop.fieldNames.join(', ')}).`);
|
|
1277
|
+
}
|
|
1213
1278
|
ret.push(...prop.fieldNames.map(f => getFieldName(f)));
|
|
1214
1279
|
return;
|
|
1215
1280
|
}
|
|
1216
|
-
ret.push(getFieldName(field));
|
|
1281
|
+
ret.push(getFieldName(field, customAlias));
|
|
1217
1282
|
});
|
|
1218
1283
|
const requiresSQLConversion = this.mainAlias.meta.props.filter(p => p.hasConvertToJSValueSQL && p.persist !== false);
|
|
1219
1284
|
if (this.flags.has(QueryFlag.CONVERT_CUSTOM_TYPES) &&
|
|
@@ -1528,7 +1593,7 @@ export class QueryBuilder {
|
|
|
1528
1593
|
if (!this.flags.has(QueryFlag.DISABLE_PAGINATE) && this._groupBy.length === 0 && this.hasToManyJoins()) {
|
|
1529
1594
|
this.flags.add(QueryFlag.PAGINATE);
|
|
1530
1595
|
}
|
|
1531
|
-
if (meta && this.flags.has(QueryFlag.PAGINATE) && !this.flags.has(QueryFlag.DISABLE_PAGINATE) && (this._limit > 0 || this._offset > 0)) {
|
|
1596
|
+
if (meta && !meta.virtual && this.flags.has(QueryFlag.PAGINATE) && !this.flags.has(QueryFlag.DISABLE_PAGINATE) && (this._limit > 0 || this._offset > 0)) {
|
|
1532
1597
|
this.wrapPaginateSubQuery(meta);
|
|
1533
1598
|
}
|
|
1534
1599
|
if (meta && (this.flags.has(QueryFlag.UPDATE_SUB_QUERY) || this.flags.has(QueryFlag.DELETE_SUB_QUERY))) {
|
|
@@ -52,8 +52,8 @@ export declare class QueryBuilderHelper {
|
|
|
52
52
|
private getValueReplacement;
|
|
53
53
|
private getOperatorReplacement;
|
|
54
54
|
validateQueryOrder<T>(orderBy: QueryOrderMap<T>): void;
|
|
55
|
-
getQueryOrder(type: QueryType, orderBy: FlatQueryOrderMap | FlatQueryOrderMap[], populate: Dictionary<string
|
|
56
|
-
getQueryOrderFromObject(type: QueryType, orderBy: FlatQueryOrderMap, populate: Dictionary<string
|
|
55
|
+
getQueryOrder(type: QueryType, orderBy: FlatQueryOrderMap | FlatQueryOrderMap[], populate: Dictionary<string>, collation?: string): string[];
|
|
56
|
+
getQueryOrderFromObject(type: QueryType, orderBy: FlatQueryOrderMap, populate: Dictionary<string>, collation?: string): string[];
|
|
57
57
|
finalize(type: QueryType, qb: NativeQueryBuilder, meta?: EntityMetadata, data?: Dictionary, returning?: InternalField<any>[]): void;
|
|
58
58
|
splitField<T>(field: EntityKey<T>, greedyAlias?: boolean): [string, EntityKey<T>, string | undefined];
|
|
59
59
|
getLockSQL(qb: NativeQueryBuilder, lockMode: LockMode, lockTables?: string[], joinsMap?: Dictionary<JoinOptions>): void;
|
|
@@ -110,8 +110,8 @@ export class QueryBuilderHelper {
|
|
|
110
110
|
}
|
|
111
111
|
if (prop?.formula) {
|
|
112
112
|
const alias2 = this.platform.quoteIdentifier(a).toString();
|
|
113
|
-
const
|
|
114
|
-
const as =
|
|
113
|
+
const aliasName = alias === undefined ? prop.fieldNames[0] : alias;
|
|
114
|
+
const as = aliasName === null ? '' : ` as ${this.platform.quoteIdentifier(aliasName)}`;
|
|
115
115
|
const meta = this.aliasMap[a]?.meta ?? this.metadata.get(this.entityName);
|
|
116
116
|
const table = this.createFormulaTable(alias2, meta, schema);
|
|
117
117
|
const columns = meta.createColumnMappingObject(p => this.getTPTAliasForProperty(p.name, a), alias2);
|
|
@@ -605,20 +605,20 @@ export class QueryBuilderHelper {
|
|
|
605
605
|
].join('\n'));
|
|
606
606
|
}
|
|
607
607
|
}
|
|
608
|
-
getQueryOrder(type, orderBy, populate) {
|
|
608
|
+
getQueryOrder(type, orderBy, populate, collation) {
|
|
609
609
|
if (Array.isArray(orderBy)) {
|
|
610
|
-
return orderBy.flatMap(o => this.getQueryOrder(type, o, populate));
|
|
610
|
+
return orderBy.flatMap(o => this.getQueryOrder(type, o, populate, collation));
|
|
611
611
|
}
|
|
612
|
-
return this.getQueryOrderFromObject(type, orderBy, populate);
|
|
612
|
+
return this.getQueryOrderFromObject(type, orderBy, populate, collation);
|
|
613
613
|
}
|
|
614
|
-
getQueryOrderFromObject(type, orderBy, populate) {
|
|
614
|
+
getQueryOrderFromObject(type, orderBy, populate, collation) {
|
|
615
615
|
const ret = [];
|
|
616
616
|
for (const key of Utils.getObjectQueryKeys(orderBy)) {
|
|
617
617
|
const direction = orderBy[key];
|
|
618
618
|
const order = typeof direction === 'number' ? QueryOrderNumeric[direction] : direction;
|
|
619
619
|
if (Raw.isKnownFragmentSymbol(key)) {
|
|
620
620
|
const raw = Raw.getKnownFragment(key);
|
|
621
|
-
ret.push(...this.platform.getOrderByExpression(this.platform.formatQuery(raw.sql, raw.params), order));
|
|
621
|
+
ret.push(...this.platform.getOrderByExpression(this.platform.formatQuery(raw.sql, raw.params), order, collation));
|
|
622
622
|
continue;
|
|
623
623
|
}
|
|
624
624
|
for (const f of Utils.splitPrimaryKeys(key)) {
|
|
@@ -638,10 +638,10 @@ export class QueryBuilderHelper {
|
|
|
638
638
|
colPart = this.platform.formatQuery(colPart.sql, colPart.params);
|
|
639
639
|
}
|
|
640
640
|
if (Array.isArray(order)) {
|
|
641
|
-
order.forEach(part => ret.push(...this.getQueryOrderFromObject(type, part, populate)));
|
|
641
|
+
order.forEach(part => ret.push(...this.getQueryOrderFromObject(type, part, populate, collation)));
|
|
642
642
|
}
|
|
643
643
|
else {
|
|
644
|
-
ret.push(...this.platform.getOrderByExpression(colPart, order));
|
|
644
|
+
ret.push(...this.platform.getOrderByExpression(colPart, order, collation));
|
|
645
645
|
}
|
|
646
646
|
}
|
|
647
647
|
}
|
|
@@ -165,6 +165,7 @@ export class SchemaComparator {
|
|
|
165
165
|
if (this.diffComment(fromTable.comment, toTable.comment)) {
|
|
166
166
|
tableDifferences.changedComment = toTable.comment;
|
|
167
167
|
this.log(`table comment changed for ${tableDifferences.name}`, { fromTableComment: fromTable.comment, toTableComment: toTable.comment });
|
|
168
|
+
changes++;
|
|
168
169
|
}
|
|
169
170
|
const fromTableColumns = fromTable.getColumns();
|
|
170
171
|
const toTableColumns = toTable.getColumns();
|