@mikro-orm/knex 5.8.11-dev.8 → 5.9.0
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 +0 -1
- package/AbstractSqlDriver.js +1 -12
- package/package.json +5 -5
- package/query/QueryBuilder.js +16 -9
- package/query/QueryBuilderHelper.js +8 -3
package/AbstractSqlDriver.d.ts
CHANGED
|
@@ -21,7 +21,6 @@ export declare abstract class AbstractSqlDriver<Connection extends AbstractSqlCo
|
|
|
21
21
|
protected wrapVirtualExpressionInSubquery<T extends object>(meta: EntityMetadata<T>, expression: string, where: FilterQuery<T>, options: FindOptions<T, any>, type: QueryType): Promise<T[] | number>;
|
|
22
22
|
mapResult<T extends object>(result: EntityData<T>, meta: EntityMetadata<T>, populate?: PopulateOptions<T>[], qb?: QueryBuilder<T>, map?: Dictionary): EntityData<T> | null;
|
|
23
23
|
private mapJoinedProps;
|
|
24
|
-
private appendToCollection;
|
|
25
24
|
count<T extends object>(entityName: string, where: any, options?: CountOptions<T>): Promise<number>;
|
|
26
25
|
nativeInsert<T extends object>(entityName: string, data: EntityDictionary<T>, options?: NativeInsertUpdateOptions<T>): Promise<QueryResult<T>>;
|
|
27
26
|
nativeInsertMany<T extends object>(entityName: string, data: EntityDictionary<T>[], options?: NativeInsertUpdateManyOptions<T>): Promise<QueryResult<T>>;
|
package/AbstractSqlDriver.js
CHANGED
|
@@ -186,7 +186,7 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
|
|
|
186
186
|
});
|
|
187
187
|
if ([core_1.ReferenceType.MANY_TO_MANY, core_1.ReferenceType.ONE_TO_MANY].includes(relation.reference)) {
|
|
188
188
|
result[relation.name] = result[relation.name] || [];
|
|
189
|
-
|
|
189
|
+
result[relation.name].push(relationPojo);
|
|
190
190
|
}
|
|
191
191
|
else {
|
|
192
192
|
result[relation.name] = relationPojo;
|
|
@@ -195,17 +195,6 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
|
|
|
195
195
|
this.mapJoinedProps(relationPojo, meta2, populateChildren, qb, root, map, path);
|
|
196
196
|
});
|
|
197
197
|
}
|
|
198
|
-
appendToCollection(meta, collection, relationPojo) {
|
|
199
|
-
if (collection.length === 0) {
|
|
200
|
-
return void collection.push(relationPojo);
|
|
201
|
-
}
|
|
202
|
-
const last = collection[collection.length - 1];
|
|
203
|
-
const pk1 = core_1.Utils.getCompositeKeyHash(last, meta);
|
|
204
|
-
const pk2 = core_1.Utils.getCompositeKeyHash(relationPojo, meta);
|
|
205
|
-
if (pk1 !== pk2) {
|
|
206
|
-
collection.push(relationPojo);
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
198
|
async count(entityName, where, options = {}) {
|
|
210
199
|
const meta = this.metadata.find(entityName);
|
|
211
200
|
if (meta?.virtual) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/knex",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.9.0",
|
|
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",
|
|
@@ -63,12 +63,12 @@
|
|
|
63
63
|
"sqlstring": "2.3.3"
|
|
64
64
|
},
|
|
65
65
|
"devDependencies": {
|
|
66
|
-
"@mikro-orm/core": "^5.
|
|
66
|
+
"@mikro-orm/core": "^5.9.0"
|
|
67
67
|
},
|
|
68
68
|
"peerDependencies": {
|
|
69
|
-
"@mikro-orm/core": "5.
|
|
70
|
-
"@mikro-orm/entity-generator": "5.
|
|
71
|
-
"@mikro-orm/migrations": "5.
|
|
69
|
+
"@mikro-orm/core": "^5.0.0",
|
|
70
|
+
"@mikro-orm/entity-generator": "^5.0.0",
|
|
71
|
+
"@mikro-orm/migrations": "^5.0.0",
|
|
72
72
|
"better-sqlite3": "*",
|
|
73
73
|
"mssql": "*",
|
|
74
74
|
"mysql": "*",
|
package/query/QueryBuilder.js
CHANGED
|
@@ -690,6 +690,12 @@ class QueryBuilder {
|
|
|
690
690
|
}
|
|
691
691
|
prepareFields(fields, type = 'where') {
|
|
692
692
|
const ret = [];
|
|
693
|
+
const getFieldName = (name) => {
|
|
694
|
+
if (type === 'groupBy') {
|
|
695
|
+
return this.helper.mapper(name, this.type, undefined, null);
|
|
696
|
+
}
|
|
697
|
+
return this.helper.mapper(name, this.type);
|
|
698
|
+
};
|
|
693
699
|
fields.forEach(field => {
|
|
694
700
|
if (!core_1.Utils.isString(field)) {
|
|
695
701
|
return ret.push(field);
|
|
@@ -706,19 +712,18 @@ class QueryBuilder {
|
|
|
706
712
|
}
|
|
707
713
|
if (prop?.embedded) {
|
|
708
714
|
const name = this._aliases[a] ? `${a}.${prop.fieldNames[0]}` : prop.fieldNames[0];
|
|
709
|
-
|
|
710
|
-
ret.push(fieldName);
|
|
715
|
+
ret.push(getFieldName(name));
|
|
711
716
|
return;
|
|
712
717
|
}
|
|
713
718
|
if (prop?.reference === core_1.ReferenceType.EMBEDDED) {
|
|
714
719
|
if (prop.object) {
|
|
715
|
-
ret.push(
|
|
720
|
+
ret.push(getFieldName(prop.fieldNames[0]));
|
|
716
721
|
}
|
|
717
722
|
else {
|
|
718
723
|
const nest = (prop) => {
|
|
719
724
|
for (const childProp of Object.values(prop.embeddedProps)) {
|
|
720
725
|
if (childProp.fieldNames) {
|
|
721
|
-
ret.push(
|
|
726
|
+
ret.push(getFieldName(childProp.fieldNames[0]));
|
|
722
727
|
}
|
|
723
728
|
else {
|
|
724
729
|
nest(childProp);
|
|
@@ -730,10 +735,10 @@ class QueryBuilder {
|
|
|
730
735
|
return;
|
|
731
736
|
}
|
|
732
737
|
if (prop && prop.fieldNames.length > 1) {
|
|
733
|
-
ret.push(...prop.fieldNames.map(f =>
|
|
738
|
+
ret.push(...prop.fieldNames.map(f => getFieldName(f)));
|
|
734
739
|
return;
|
|
735
740
|
}
|
|
736
|
-
ret.push(
|
|
741
|
+
ret.push(getFieldName(field));
|
|
737
742
|
});
|
|
738
743
|
const meta = this.mainAlias.metadata;
|
|
739
744
|
/* istanbul ignore next */
|
|
@@ -777,6 +782,8 @@ class QueryBuilder {
|
|
|
777
782
|
getQueryBase() {
|
|
778
783
|
const qb = this.getKnex();
|
|
779
784
|
const schema = this.getSchema(this.mainAlias);
|
|
785
|
+
// Joined tables doesn't need to belong to the same schema as the main table
|
|
786
|
+
const joinSchema = this._schema ?? this.em?.schema ?? schema;
|
|
780
787
|
if (schema) {
|
|
781
788
|
qb.withSchema(schema);
|
|
782
789
|
}
|
|
@@ -795,12 +802,12 @@ class QueryBuilder {
|
|
|
795
802
|
else if (this.flags.has(core_1.QueryFlag.DISTINCT)) {
|
|
796
803
|
qb.distinct();
|
|
797
804
|
}
|
|
798
|
-
this.helper.processJoins(qb, this._joins,
|
|
805
|
+
this.helper.processJoins(qb, this._joins, joinSchema);
|
|
799
806
|
break;
|
|
800
807
|
case enums_1.QueryType.COUNT: {
|
|
801
808
|
const m = this.flags.has(core_1.QueryFlag.DISTINCT) ? 'countDistinct' : 'count';
|
|
802
809
|
qb[m]({ count: this._fields.map(f => this.helper.mapper(f, this.type)) });
|
|
803
|
-
this.helper.processJoins(qb, this._joins,
|
|
810
|
+
this.helper.processJoins(qb, this._joins, joinSchema);
|
|
804
811
|
break;
|
|
805
812
|
}
|
|
806
813
|
case enums_1.QueryType.INSERT:
|
|
@@ -993,7 +1000,7 @@ class QueryBuilder {
|
|
|
993
1000
|
getSchema(alias) {
|
|
994
1001
|
const { metadata } = alias;
|
|
995
1002
|
const metaSchema = metadata?.schema && metadata.schema !== '*' ? metadata.schema : undefined;
|
|
996
|
-
return this._schema ?? metaSchema ?? this.em?.config.get('schema');
|
|
1003
|
+
return this._schema ?? metaSchema ?? this.em?.schema ?? this.em?.config.get('schema');
|
|
997
1004
|
}
|
|
998
1005
|
createAlias(entityName, aliasName, subQuery) {
|
|
999
1006
|
const metadata = this.metadata.find(entityName);
|
|
@@ -133,7 +133,7 @@ class QueryBuilderHelper {
|
|
|
133
133
|
return {
|
|
134
134
|
prop, type, cond, ownerAlias, alias,
|
|
135
135
|
table: this.getTableName(prop.type),
|
|
136
|
-
schema: this.driver.getSchemaName(prop.targetMeta),
|
|
136
|
+
schema: prop.targetMeta?.schema === '*' ? '*' : this.driver.getSchemaName(prop.targetMeta),
|
|
137
137
|
joinColumns: prop.referencedColumnNames,
|
|
138
138
|
primaryKeys: prop.fieldNames,
|
|
139
139
|
};
|
|
@@ -150,7 +150,7 @@ class QueryBuilderHelper {
|
|
|
150
150
|
primaryKeys: prop.referencedColumnNames,
|
|
151
151
|
cond: {},
|
|
152
152
|
table: pivotMeta.tableName,
|
|
153
|
-
schema: this.driver.getSchemaName(pivotMeta),
|
|
153
|
+
schema: prop.targetMeta?.schema === '*' ? '*' : this.driver.getSchemaName(pivotMeta),
|
|
154
154
|
path: path.endsWith('[pivot]') ? path : `${path}[pivot]`,
|
|
155
155
|
},
|
|
156
156
|
};
|
|
@@ -393,7 +393,11 @@ class QueryBuilderHelper {
|
|
|
393
393
|
processObjectSubCondition(cond, key, qb, method, m, type) {
|
|
394
394
|
// grouped condition for one field
|
|
395
395
|
let value = cond[key];
|
|
396
|
-
|
|
396
|
+
const size = core_1.Utils.getObjectKeysSize(value);
|
|
397
|
+
if (core_1.Utils.isPlainObject(value) && size === 0) {
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
if (size > 1) {
|
|
397
401
|
const subCondition = Object.entries(value).map(([subKey, subValue]) => ({ [key]: { [subKey]: subValue } }));
|
|
398
402
|
return subCondition.forEach(sub => this.appendQueryCondition(type, sub, qb, '$and', method));
|
|
399
403
|
}
|
|
@@ -402,6 +406,7 @@ class QueryBuilderHelper {
|
|
|
402
406
|
}
|
|
403
407
|
// operators
|
|
404
408
|
const op = Object.keys(core_1.QueryOperator).find(op => op in value);
|
|
409
|
+
/* istanbul ignore next */
|
|
405
410
|
if (!op) {
|
|
406
411
|
throw new Error(`Invalid query condition: ${(0, util_1.inspect)(cond)}`);
|
|
407
412
|
}
|