@mikro-orm/knex 6.1.10-dev.1 → 6.1.10-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/AbstractSqlDriver.js +32 -13
- package/package.json +2 -2
- package/query/QueryBuilder.js +5 -0
- package/query/QueryBuilderHelper.js +5 -3
package/AbstractSqlDriver.js
CHANGED
|
@@ -187,6 +187,10 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
|
|
|
187
187
|
path += '[pivot]';
|
|
188
188
|
}
|
|
189
189
|
const relationAlias = qb.getAliasForJoinPath(path, { matchPopulateJoins: true });
|
|
190
|
+
/* istanbul ignore next */
|
|
191
|
+
if (!relationAlias) {
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
190
194
|
// pivot ref joins via joined strategy need to be handled separately here, as they dont join the target entity
|
|
191
195
|
if (pivotRefJoin) {
|
|
192
196
|
let item;
|
|
@@ -212,10 +216,10 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
|
|
|
212
216
|
}));
|
|
213
217
|
if (!hasPK) {
|
|
214
218
|
if ([core_1.ReferenceKind.MANY_TO_MANY, core_1.ReferenceKind.ONE_TO_MANY].includes(prop.kind)) {
|
|
215
|
-
result[prop.name]
|
|
219
|
+
result[prop.name] = [];
|
|
216
220
|
}
|
|
217
221
|
if ([core_1.ReferenceKind.MANY_TO_ONE, core_1.ReferenceKind.ONE_TO_ONE].includes(prop.kind)) {
|
|
218
|
-
result[prop.name]
|
|
222
|
+
result[prop.name] = null;
|
|
219
223
|
}
|
|
220
224
|
return;
|
|
221
225
|
}
|
|
@@ -709,12 +713,12 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
|
|
|
709
713
|
const alias = qb.getNextAlias(prop.targetMeta.tableName);
|
|
710
714
|
qb.leftJoin(`${targetAlias}.${hint.field}`, alias);
|
|
711
715
|
// eslint-disable-next-line dot-notation
|
|
712
|
-
Object.values(qb['_joins'])
|
|
716
|
+
for (const join of Object.values(qb['_joins'])) {
|
|
713
717
|
const [propName] = hint.field.split(':', 2);
|
|
714
718
|
if (join.alias === alias && join.prop.name === propName) {
|
|
715
719
|
fields.push(...qb.helper.mapJoinColumns(qb.type, join));
|
|
716
720
|
}
|
|
717
|
-
}
|
|
721
|
+
}
|
|
718
722
|
});
|
|
719
723
|
}
|
|
720
724
|
qb.select(fields)
|
|
@@ -724,19 +728,30 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
|
|
|
724
728
|
if (owners.length === 1 && (options.offset != null || options.limit != null)) {
|
|
725
729
|
qb.limit(options.limit, options.offset);
|
|
726
730
|
}
|
|
731
|
+
// console.log('pivot qb', qb, qb._fields);
|
|
727
732
|
const res = owners.length ? await this.rethrow(qb.execute('all', { mergeResults: false, mapResults: false })) : [];
|
|
728
|
-
|
|
733
|
+
// console.log(res);
|
|
734
|
+
// const items = res.map((row: Dictionary) => super.mapResult(row, prop.targetMeta));
|
|
735
|
+
const tmp = {};
|
|
736
|
+
// const items = res.map((row: Dictionary) => this.mapResult(row, prop.targetMeta!, populate, qb, tmp));
|
|
737
|
+
// const items = res.map((row: Dictionary) => this.mapResult(row, pivotMeta, populate, qb, tmp));
|
|
738
|
+
const items = res.map((row) => {
|
|
739
|
+
const root = super.mapResult(row, prop.targetMeta);
|
|
740
|
+
this.mapJoinedProps(root, prop.targetMeta, populate, qb, root, tmp, pivotMeta.className + '.' + pivotProp1.name);
|
|
741
|
+
return root;
|
|
742
|
+
});
|
|
743
|
+
// console.log(prop.name, prop.targetMeta!.className, items);
|
|
729
744
|
qb.clearRawFragmentsCache();
|
|
730
745
|
const map = {};
|
|
731
746
|
const pkProps = ownerMeta.getPrimaryProps();
|
|
732
|
-
|
|
747
|
+
for (const owner of owners) {
|
|
733
748
|
const key = core_1.Utils.getPrimaryKeyHash(prop.joinColumns.map((_col, idx) => {
|
|
734
749
|
const pkProp = pkProps[idx];
|
|
735
750
|
return pkProp.customType ? pkProp.customType.convertToJSValue(owner[idx], this.platform) : owner[idx];
|
|
736
751
|
}));
|
|
737
|
-
|
|
738
|
-
}
|
|
739
|
-
|
|
752
|
+
map[key] = [];
|
|
753
|
+
}
|
|
754
|
+
for (const item of items) {
|
|
740
755
|
const key = core_1.Utils.getPrimaryKeyHash(prop.joinColumns.map((col, idx) => {
|
|
741
756
|
const pkProp = pkProps[idx];
|
|
742
757
|
return pkProp.customType ? pkProp.customType.convertToJSValue(item[`fk__${col}`], this.platform) : item[`fk__${col}`];
|
|
@@ -746,7 +761,7 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
|
|
|
746
761
|
prop.inverseJoinColumns.forEach((col, idx) => {
|
|
747
762
|
core_1.Utils.renameKey(item, `fk__${col}`, prop.targetMeta.primaryKeys[idx]);
|
|
748
763
|
});
|
|
749
|
-
}
|
|
764
|
+
}
|
|
750
765
|
return map;
|
|
751
766
|
}
|
|
752
767
|
getPivotOrderBy(prop, pivotProp, pivotAlias, orderBy) {
|
|
@@ -784,13 +799,14 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
|
|
|
784
799
|
*/
|
|
785
800
|
joinedProps(meta, populate, options) {
|
|
786
801
|
return populate.filter(hint => {
|
|
787
|
-
const [propName] = hint.field.split(':', 2);
|
|
802
|
+
const [propName, ref] = hint.field.split(':', 2);
|
|
788
803
|
const prop = meta.properties[propName] || {};
|
|
789
804
|
if (hint.filter && hint.strategy === core_1.LoadStrategy.JOINED) {
|
|
790
805
|
return true;
|
|
791
806
|
}
|
|
792
807
|
if ((options?.strategy || hint.strategy || prop.strategy || this.config.get('loadStrategy')) !== core_1.LoadStrategy.JOINED) {
|
|
793
|
-
|
|
808
|
+
// force joined strategy for explicit 1:1 owner populate hint as it would require a join anyway
|
|
809
|
+
return prop.kind === core_1.ReferenceKind.ONE_TO_ONE && !prop.owner;
|
|
794
810
|
}
|
|
795
811
|
return ![core_1.ReferenceKind.SCALAR, core_1.ReferenceKind.EMBEDDED].includes(prop.kind);
|
|
796
812
|
});
|
|
@@ -861,6 +877,7 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
|
|
|
861
877
|
const prop = meta.properties[propName];
|
|
862
878
|
// ignore ref joins of known FKs unless it's a filter hint
|
|
863
879
|
if (ref && !hint.filter && (prop.kind === core_1.ReferenceKind.MANY_TO_ONE || (prop.kind === core_1.ReferenceKind.ONE_TO_ONE && !prop.owner))) {
|
|
880
|
+
// // console.log('wat', hint);
|
|
864
881
|
return;
|
|
865
882
|
}
|
|
866
883
|
const meta2 = this.metadata.find(prop.type);
|
|
@@ -894,9 +911,11 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
|
|
|
894
911
|
fields.push(...this.getFieldsForJoinedLoad(qb, meta2, childExplicitFields.length === 0 ? undefined : childExplicitFields, childExclude, hint.children, options, tableAlias, path));
|
|
895
912
|
}
|
|
896
913
|
else if (hint.filter) {
|
|
897
|
-
fields.push(field);
|
|
914
|
+
// fields.push(field);
|
|
915
|
+
fields.push(...prop.referencedColumnNames.map(col => qb.helper.mapper(`${tableAlias}.${col}`, qb.type, undefined, `${tableAlias}__${col}`)));
|
|
898
916
|
}
|
|
899
917
|
});
|
|
918
|
+
// // console.log(fields, joinedProps);
|
|
900
919
|
return fields;
|
|
901
920
|
}
|
|
902
921
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/knex",
|
|
3
|
-
"version": "6.1.10-dev.
|
|
3
|
+
"version": "6.1.10-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
|
"main": "index.js",
|
|
6
6
|
"module": "index.mjs",
|
|
@@ -66,6 +66,6 @@
|
|
|
66
66
|
"@mikro-orm/core": "^6.1.9"
|
|
67
67
|
},
|
|
68
68
|
"peerDependencies": {
|
|
69
|
-
"@mikro-orm/core": "6.1.10-dev.
|
|
69
|
+
"@mikro-orm/core": "6.1.10-dev.2"
|
|
70
70
|
}
|
|
71
71
|
}
|
package/query/QueryBuilder.js
CHANGED
|
@@ -1072,6 +1072,7 @@ class QueryBuilder {
|
|
|
1072
1072
|
const alias = this.getNextAlias(prop.pivotEntity ?? prop.type);
|
|
1073
1073
|
const aliasedName = `${fromAlias}.${prop.name}#${alias}`;
|
|
1074
1074
|
this._joins[aliasedName] = this.helper.joinOneToReference(prop, this.mainAlias.aliasName, alias, enums_1.JoinType.leftJoin);
|
|
1075
|
+
this._joins[aliasedName].path = `${(Object.values(this._joins).find(j => j.alias === fromAlias)?.path ?? meta.className)}.${prop.name}`;
|
|
1075
1076
|
this._populateMap[aliasedName] = this._joins[aliasedName].alias;
|
|
1076
1077
|
}
|
|
1077
1078
|
});
|
|
@@ -1131,6 +1132,10 @@ class QueryBuilder {
|
|
|
1131
1132
|
if (join.cond[k]) {
|
|
1132
1133
|
join.cond = { [op ?? '$and']: [join.cond, { [k]: cond[k] }] };
|
|
1133
1134
|
}
|
|
1135
|
+
else if (op === '$or') {
|
|
1136
|
+
join.cond.$or ??= [];
|
|
1137
|
+
join.cond.$or.push({ [k]: cond[k] });
|
|
1138
|
+
}
|
|
1134
1139
|
else {
|
|
1135
1140
|
join.cond = { ...join.cond, [k]: cond[k] };
|
|
1136
1141
|
}
|
|
@@ -193,8 +193,10 @@ class QueryBuilderHelper {
|
|
|
193
193
|
conditions.push(`${left} = ${this.knex.ref(right)}`);
|
|
194
194
|
return;
|
|
195
195
|
}
|
|
196
|
-
const left =
|
|
197
|
-
|
|
196
|
+
const left = join.prop.object && join.prop.fieldNameRaw
|
|
197
|
+
? join.prop.fieldNameRaw.replaceAll(core_1.ALIAS_REPLACEMENT, join.ownerAlias)
|
|
198
|
+
: this.knex.ref(`${join.ownerAlias}.${primaryKey}`);
|
|
199
|
+
conditions.push(`${left} = ${this.knex.ref(right)}`);
|
|
198
200
|
});
|
|
199
201
|
}
|
|
200
202
|
if (join.prop.targetMeta?.discriminatorValue && !join.path?.endsWith('[pivot]')) {
|
|
@@ -295,7 +297,7 @@ class QueryBuilderHelper {
|
|
|
295
297
|
if (join.prop && [core_1.ReferenceKind.MANY_TO_ONE, core_1.ReferenceKind.ONE_TO_ONE].includes(join.prop.kind)) {
|
|
296
298
|
return join.prop.fieldNames.map((fieldName, idx) => {
|
|
297
299
|
const columns = join.prop.owner ? join.joinColumns : join.inverseJoinColumns;
|
|
298
|
-
return this.mapper(`${join.alias}.${columns[idx]}`, type, undefined,
|
|
300
|
+
return this.mapper(`${join.alias}.${columns[idx]}`, type, undefined, `${join.alias}__${columns[idx]}`);
|
|
299
301
|
});
|
|
300
302
|
}
|
|
301
303
|
return [
|