@mikro-orm/knex 6.0.0-dev.119 → 6.0.0-dev.120

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.
@@ -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>>;
@@ -209,7 +209,7 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
209
209
  });
210
210
  if ([core_1.ReferenceKind.MANY_TO_MANY, core_1.ReferenceKind.ONE_TO_MANY].includes(relation.kind)) {
211
211
  result[relation.name] ??= [];
212
- this.appendToCollection(meta2, result[relation.name], relationPojo);
212
+ result[relation.name].push(relationPojo);
213
213
  }
214
214
  else {
215
215
  result[relation.name] = relationPojo;
@@ -218,17 +218,6 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
218
218
  this.mapJoinedProps(relationPojo, meta2, populateChildren, qb, root, map, path);
219
219
  });
220
220
  }
221
- appendToCollection(meta, collection, relationPojo) {
222
- if (collection.length === 0) {
223
- return void collection.push(relationPojo);
224
- }
225
- const last = collection[collection.length - 1];
226
- const pk1 = core_1.Utils.getCompositeKeyHash(last, meta);
227
- const pk2 = core_1.Utils.getCompositeKeyHash(relationPojo, meta);
228
- if (pk1 !== pk2) {
229
- collection.push(relationPojo);
230
- }
231
- }
232
221
  async count(entityName, where, options = {}) {
233
222
  const meta = this.metadata.find(entityName);
234
223
  if (meta?.virtual) {
@@ -344,7 +333,7 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
344
333
  }
345
334
  if (meta && this.platform.usesReturningStatement()) {
346
335
  const returningProps = meta.props
347
- .filter(prop => prop.persist !== false && ((prop.primary && prop.autoincrement) || prop.defaultRaw))
336
+ .filter(prop => prop.persist !== false && ((prop.primary && prop.autoincrement) || prop.defaultRaw || prop.autoincrement))
348
337
  .filter(prop => !(prop.name in data[0]) || core_1.Utils.isRawSql(data[0][prop.name]));
349
338
  const returningFields = core_1.Utils.flatten(returningProps.map(prop => prop.fieldNames));
350
339
  /* istanbul ignore next */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/knex",
3
- "version": "6.0.0-dev.119",
3
+ "version": "6.0.0-dev.120",
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,9 +63,9 @@
63
63
  "sqlstring": "2.3.3"
64
64
  },
65
65
  "devDependencies": {
66
- "@mikro-orm/core": "^5.8.10"
66
+ "@mikro-orm/core": "^5.9.0"
67
67
  },
68
68
  "peerDependencies": {
69
- "@mikro-orm/core": "6.0.0-dev.119"
69
+ "@mikro-orm/core": "6.0.0-dev.120"
70
70
  }
71
71
  }
@@ -757,6 +757,12 @@ class QueryBuilder {
757
757
  }
758
758
  prepareFields(fields, type = 'where') {
759
759
  const ret = [];
760
+ const getFieldName = (name) => {
761
+ if (type === 'groupBy') {
762
+ return this.helper.mapper(name, this.type, undefined, null);
763
+ }
764
+ return this.helper.mapper(name, this.type);
765
+ };
760
766
  fields.forEach(field => {
761
767
  const rawField = core_1.RawQueryFragment.getKnownFragment(field);
762
768
  if (rawField) {
@@ -782,19 +788,18 @@ class QueryBuilder {
782
788
  if (prop?.embedded) {
783
789
  const name = prop.embeddedPath?.join('.') ?? prop.fieldNames[0];
784
790
  const aliased = this._aliases[a] ? `${a}.${name}` : name;
785
- const fieldName = this.helper.mapper(aliased, this.type);
786
- ret.push(fieldName);
791
+ ret.push(getFieldName(aliased));
787
792
  return;
788
793
  }
789
794
  if (prop?.kind === core_1.ReferenceKind.EMBEDDED) {
790
795
  if (prop.object) {
791
- ret.push(this.helper.mapper(prop.fieldNames[0], this.type));
796
+ ret.push(getFieldName(prop.fieldNames[0]));
792
797
  }
793
798
  else {
794
799
  const nest = (prop) => {
795
800
  for (const childProp of Object.values(prop.embeddedProps)) {
796
801
  if (childProp.fieldNames && (childProp.kind !== core_1.ReferenceKind.EMBEDDED || childProp.object) && childProp.persist !== false) {
797
- ret.push(this.helper.mapper(childProp.fieldNames[0], this.type));
802
+ ret.push(getFieldName(childProp.fieldNames[0]));
798
803
  }
799
804
  else {
800
805
  nest(childProp);
@@ -806,10 +811,10 @@ class QueryBuilder {
806
811
  return;
807
812
  }
808
813
  if (prop && prop.fieldNames.length > 1) {
809
- ret.push(...prop.fieldNames.map(f => this.helper.mapper(f, this.type)));
814
+ ret.push(...prop.fieldNames.map(f => getFieldName(f)));
810
815
  return;
811
816
  }
812
- ret.push(this.helper.mapper(field, this.type));
817
+ ret.push(getFieldName(field));
813
818
  });
814
819
  const meta = this.mainAlias.metadata;
815
820
  /* istanbul ignore next */
@@ -853,6 +858,8 @@ class QueryBuilder {
853
858
  getQueryBase() {
854
859
  const qb = this.getKnex();
855
860
  const schema = this.getSchema(this.mainAlias);
861
+ // Joined tables doesn't need to belong to the same schema as the main table
862
+ const joinSchema = this._schema ?? this.em?.schema ?? schema;
856
863
  if (schema) {
857
864
  qb.withSchema(schema);
858
865
  }
@@ -871,12 +878,12 @@ class QueryBuilder {
871
878
  else if (this.flags.has(core_1.QueryFlag.DISTINCT)) {
872
879
  qb.distinct();
873
880
  }
874
- this.helper.processJoins(qb, this._joins, schema);
881
+ this.helper.processJoins(qb, this._joins, joinSchema);
875
882
  break;
876
883
  case enums_1.QueryType.COUNT: {
877
884
  const m = this.flags.has(core_1.QueryFlag.DISTINCT) ? 'countDistinct' : 'count';
878
885
  qb[m]({ count: this._fields.map(f => this.helper.mapper(f, this.type)) });
879
- this.helper.processJoins(qb, this._joins, schema);
886
+ this.helper.processJoins(qb, this._joins, joinSchema);
880
887
  break;
881
888
  }
882
889
  case enums_1.QueryType.INSERT:
@@ -1080,7 +1087,7 @@ class QueryBuilder {
1080
1087
  getSchema(alias) {
1081
1088
  const { metadata } = alias;
1082
1089
  const metaSchema = metadata?.schema && metadata.schema !== '*' ? metadata.schema : undefined;
1083
- return this._schema ?? metaSchema ?? this.em?.config.get('schema');
1090
+ return this._schema ?? metaSchema ?? this.em?.schema ?? this.em?.config.get('schema');
1084
1091
  }
1085
1092
  createAlias(entityName, aliasName, subQuery) {
1086
1093
  const metadata = this.metadata.find(entityName);
@@ -144,7 +144,7 @@ class QueryBuilderHelper {
144
144
  return {
145
145
  prop, type, cond, ownerAlias, alias,
146
146
  table: this.getTableName(prop.type),
147
- schema: this.driver.getSchemaName(prop.targetMeta, { schema }),
147
+ schema: prop.targetMeta?.schema === '*' ? '*' : this.driver.getSchemaName(prop.targetMeta, { schema }),
148
148
  joinColumns: prop.referencedColumnNames,
149
149
  primaryKeys: prop.fieldNames,
150
150
  };
@@ -161,7 +161,7 @@ class QueryBuilderHelper {
161
161
  primaryKeys: prop.referencedColumnNames,
162
162
  cond: {},
163
163
  table: pivotMeta.tableName,
164
- schema: this.driver.getSchemaName(pivotMeta, { schema }),
164
+ schema: prop.targetMeta?.schema === '*' ? '*' : this.driver.getSchemaName(pivotMeta, { schema }),
165
165
  path: path.endsWith('[pivot]') ? path : `${path}[pivot]`,
166
166
  },
167
167
  };
@@ -403,7 +403,11 @@ class QueryBuilderHelper {
403
403
  processObjectSubCondition(cond, key, qb, method, m, type) {
404
404
  // grouped condition for one field
405
405
  let value = cond[key];
406
- if (core_1.Utils.getObjectKeysSize(value) > 1) {
406
+ const size = core_1.Utils.getObjectKeysSize(value);
407
+ if (core_1.Utils.isPlainObject(value) && size === 0) {
408
+ return;
409
+ }
410
+ if (size > 1) {
407
411
  const subCondition = Object.entries(value).map(([subKey, subValue]) => ({ [key]: { [subKey]: subValue } }));
408
412
  return subCondition.forEach(sub => this.appendQueryCondition(type, sub, qb, '$and', method));
409
413
  }
@@ -412,6 +416,7 @@ class QueryBuilderHelper {
412
416
  }
413
417
  // operators
414
418
  const op = Object.keys(core_1.QueryOperator).find(op => op in value);
419
+ /* istanbul ignore next */
415
420
  if (!op) {
416
421
  throw new Error(`Invalid query condition: ${(0, util_1.inspect)(cond)}`);
417
422
  }