@mikro-orm/knex 5.8.11-dev.5 → 5.8.11-dev.7
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 +4 -4
- package/query/QueryBuilderHelper.js +32 -12
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/knex",
|
|
3
|
-
"version": "5.8.11-dev.
|
|
3
|
+
"version": "5.8.11-dev.7",
|
|
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
|
"main": "index.js",
|
|
6
6
|
"module": "index.mjs",
|
|
@@ -66,9 +66,9 @@
|
|
|
66
66
|
"@mikro-orm/core": "^5.8.10"
|
|
67
67
|
},
|
|
68
68
|
"peerDependencies": {
|
|
69
|
-
"@mikro-orm/core": "5.8.11-dev.
|
|
70
|
-
"@mikro-orm/entity-generator": "5.8.11-dev.
|
|
71
|
-
"@mikro-orm/migrations": "5.8.11-dev.
|
|
69
|
+
"@mikro-orm/core": "5.8.11-dev.7",
|
|
70
|
+
"@mikro-orm/entity-generator": "5.8.11-dev.7",
|
|
71
|
+
"@mikro-orm/migrations": "5.8.11-dev.7",
|
|
72
72
|
"better-sqlite3": "*",
|
|
73
73
|
"mssql": "*",
|
|
74
74
|
"mysql": "*",
|
|
@@ -29,7 +29,11 @@ class QueryBuilderHelper {
|
|
|
29
29
|
for (const p of fields) {
|
|
30
30
|
const [a, f] = this.splitField(p);
|
|
31
31
|
const prop = this.getProperty(f, a);
|
|
32
|
-
|
|
32
|
+
const fkIdx2 = prop?.fieldNames.findIndex(name => name === f) ?? -1;
|
|
33
|
+
if (fkIdx2 !== -1) {
|
|
34
|
+
parts.push(this.mapper(a !== this.alias ? `${a}.${prop.fieldNames[fkIdx2]}` : prop.fieldNames[fkIdx2], type, value, alias));
|
|
35
|
+
}
|
|
36
|
+
else if (prop) {
|
|
33
37
|
parts.push(...prop.fieldNames.map(f => this.mapper(a !== this.alias ? `${a}.${f}` : f, type, value, alias)));
|
|
34
38
|
}
|
|
35
39
|
else {
|
|
@@ -53,9 +57,11 @@ class QueryBuilderHelper {
|
|
|
53
57
|
const customExpression = QueryBuilderHelper.isCustomExpression(field, !!alias);
|
|
54
58
|
const [a, f] = this.splitField(field);
|
|
55
59
|
const prop = this.getProperty(f, a);
|
|
60
|
+
const fkIdx2 = prop?.fieldNames.findIndex(name => name === f) ?? -1;
|
|
61
|
+
const fkIdx = fkIdx2 === -1 ? 0 : fkIdx2;
|
|
56
62
|
// embeddable nested path instead of a regular property with table alias, reset alias
|
|
57
63
|
if (prop?.name === a && prop.embeddedProps[f]) {
|
|
58
|
-
return this.alias + '.' + prop.fieldNames[
|
|
64
|
+
return this.alias + '.' + prop.fieldNames[fkIdx];
|
|
59
65
|
}
|
|
60
66
|
const noPrefix = prop && prop.persist === false;
|
|
61
67
|
if (prop?.fieldNameRaw) {
|
|
@@ -68,16 +74,24 @@ class QueryBuilderHelper {
|
|
|
68
74
|
return this.knex.raw(`${prop.formula(alias2)}${as}`);
|
|
69
75
|
}
|
|
70
76
|
if (prop?.hasConvertToJSValueSQL) {
|
|
71
|
-
|
|
72
|
-
|
|
77
|
+
let valueSQL;
|
|
78
|
+
if (prop.fieldNames.length > 1 && fkIdx !== -1) {
|
|
79
|
+
const fk = prop.targetMeta.getPrimaryProps()[fkIdx];
|
|
80
|
+
const prefixed = this.prefix(field, isTableNameAliasRequired, true, fkIdx);
|
|
81
|
+
valueSQL = fk.customType.convertToJSValueSQL(prefixed, this.platform);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
const prefixed = this.prefix(field, isTableNameAliasRequired, true);
|
|
85
|
+
valueSQL = prop.customType.convertToJSValueSQL(prefixed, this.platform);
|
|
86
|
+
}
|
|
73
87
|
if (alias === null) {
|
|
74
88
|
return this.knex.raw(valueSQL);
|
|
75
89
|
}
|
|
76
|
-
return this.knex.raw(`${valueSQL} as ${this.platform.quoteIdentifier(alias ?? prop.fieldNames[
|
|
90
|
+
return this.knex.raw(`${valueSQL} as ${this.platform.quoteIdentifier(alias ?? prop.fieldNames[fkIdx])}`);
|
|
77
91
|
}
|
|
78
92
|
// do not wrap custom expressions
|
|
79
93
|
if (!customExpression) {
|
|
80
|
-
ret = this.prefix(field);
|
|
94
|
+
ret = this.prefix(field, false, false, fkIdx);
|
|
81
95
|
}
|
|
82
96
|
if (alias) {
|
|
83
97
|
ret += ' as ' + alias;
|
|
@@ -529,11 +543,11 @@ class QueryBuilderHelper {
|
|
|
529
543
|
const re = hasAlias ? /[ ?<>=()'"`:]|^\d/ : /[?<>=()'"`:]|^\d/;
|
|
530
544
|
return !!field.match(re);
|
|
531
545
|
}
|
|
532
|
-
prefix(field, always = false, quote = false) {
|
|
546
|
+
prefix(field, always = false, quote = false, idx) {
|
|
533
547
|
let ret;
|
|
534
548
|
if (!this.isPrefixed(field)) {
|
|
535
549
|
const alias = always ? (quote ? this.alias : this.platform.quoteIdentifier(this.alias)) + '.' : '';
|
|
536
|
-
const fieldName = this.fieldName(field, this.alias, always);
|
|
550
|
+
const fieldName = this.fieldName(field, this.alias, always, idx);
|
|
537
551
|
if (QueryBuilderHelper.isCustomExpression(fieldName)) {
|
|
538
552
|
return fieldName;
|
|
539
553
|
}
|
|
@@ -542,7 +556,7 @@ class QueryBuilderHelper {
|
|
|
542
556
|
else {
|
|
543
557
|
const [a, ...rest] = field.split('.');
|
|
544
558
|
const f = rest.join('.');
|
|
545
|
-
ret = a + '.' + this.fieldName(f, a);
|
|
559
|
+
ret = a + '.' + this.fieldName(f, a, always, idx);
|
|
546
560
|
}
|
|
547
561
|
if (quote) {
|
|
548
562
|
return this.platform.quoteIdentifier(ret);
|
|
@@ -568,7 +582,7 @@ class QueryBuilderHelper {
|
|
|
568
582
|
isPrefixed(field) {
|
|
569
583
|
return !!field.match(/[\w`"[\]]+\./);
|
|
570
584
|
}
|
|
571
|
-
fieldName(field, alias, always) {
|
|
585
|
+
fieldName(field, alias, always, idx = 0) {
|
|
572
586
|
const prop = this.getProperty(field, alias);
|
|
573
587
|
if (!prop) {
|
|
574
588
|
return field;
|
|
@@ -586,7 +600,7 @@ class QueryBuilderHelper {
|
|
|
586
600
|
return prop.fieldNameRaw;
|
|
587
601
|
}
|
|
588
602
|
/* istanbul ignore next */
|
|
589
|
-
return prop.fieldNames?.[
|
|
603
|
+
return prop.fieldNames?.[idx] ?? field;
|
|
590
604
|
}
|
|
591
605
|
getProperty(field, alias) {
|
|
592
606
|
const entityName = this.aliasMap[alias]?.entityName || this.entityName;
|
|
@@ -604,7 +618,13 @@ class QueryBuilderHelper {
|
|
|
604
618
|
return nest(prop);
|
|
605
619
|
}
|
|
606
620
|
}
|
|
607
|
-
|
|
621
|
+
if (meta) {
|
|
622
|
+
if (meta.properties[field]) {
|
|
623
|
+
return meta.properties[field];
|
|
624
|
+
}
|
|
625
|
+
return meta.relations.find(prop => prop.fieldNames?.some(name => field === name));
|
|
626
|
+
}
|
|
627
|
+
return undefined;
|
|
608
628
|
}
|
|
609
629
|
isTableNameAliasRequired(type) {
|
|
610
630
|
return [enums_1.QueryType.SELECT, enums_1.QueryType.COUNT].includes(type ?? enums_1.QueryType.SELECT);
|