@mikro-orm/knex 6.0.0-dev.69 → 6.0.0-dev.70

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.
@@ -1,5 +1,5 @@
1
1
  import type { Knex } from 'knex';
2
- import type { AnyEntity, ConnectionType, EntityData, EntityName, EntityRepository, GetRepository, QueryResult } from '@mikro-orm/core';
2
+ import type { AnyEntity, ConnectionType, EntityData, EntityName, EntityRepository, GetRepository, QueryResult, FilterQuery } from '@mikro-orm/core';
3
3
  import { EntityManager } from '@mikro-orm/core';
4
4
  import type { AbstractSqlDriver } from './AbstractSqlDriver';
5
5
  import { QueryBuilder } from './query';
@@ -22,4 +22,5 @@ export declare class SqlEntityManager<D extends AbstractSqlDriver = AbstractSqlD
22
22
  getKnex(type?: ConnectionType): Knex<any, any[]>;
23
23
  execute<T extends QueryResult | EntityData<AnyEntity> | EntityData<AnyEntity>[] = EntityData<AnyEntity>[]>(queryOrKnex: string | Knex.QueryBuilder | Knex.Raw, params?: any[], method?: 'all' | 'get' | 'run'): Promise<T>;
24
24
  getRepository<T extends object, U extends EntityRepository<T> = SqlEntityRepository<T>>(entityName: EntityName<T>): GetRepository<T, U>;
25
+ protected applyDiscriminatorCondition<Entity extends object>(entityName: string, where: FilterQuery<Entity>): FilterQuery<Entity>;
25
26
  }
@@ -32,5 +32,9 @@ class SqlEntityManager extends core_1.EntityManager {
32
32
  getRepository(entityName) {
33
33
  return super.getRepository(entityName);
34
34
  }
35
+ applyDiscriminatorCondition(entityName, where) {
36
+ // this is handled in QueryBuilder now for SQL drivers
37
+ return where;
38
+ }
35
39
  }
36
40
  exports.SqlEntityManager = SqlEntityManager;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/knex",
3
- "version": "6.0.0-dev.69",
3
+ "version": "6.0.0-dev.70",
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": "^5.7.2"
67
67
  },
68
68
  "peerDependencies": {
69
- "@mikro-orm/core": "~6.0.0-dev.69"
69
+ "@mikro-orm/core": "~6.0.0-dev.70"
70
70
  }
71
71
  }
@@ -206,6 +206,7 @@ export declare class QueryBuilder<T extends object = AnyEntity> {
206
206
  private prepareFields;
207
207
  private init;
208
208
  private getQueryBase;
209
+ private applyDiscriminatorCondition;
209
210
  private finalize;
210
211
  private processPopulateWhere;
211
212
  private hasToManyJoins;
@@ -792,6 +792,24 @@ class QueryBuilder {
792
792
  }
793
793
  return qb;
794
794
  }
795
+ applyDiscriminatorCondition() {
796
+ const meta = this.mainAlias.metadata;
797
+ if (!meta?.discriminatorValue) {
798
+ return;
799
+ }
800
+ const types = Object.values(meta.root.discriminatorMap).map(cls => this.metadata.find(cls));
801
+ const children = [];
802
+ const lookUpChildren = (ret, type) => {
803
+ const children = types.filter(meta2 => meta2.extends === type);
804
+ children.forEach(m => lookUpChildren(ret, m.className));
805
+ ret.push(...children.filter(c => c.discriminatorValue));
806
+ return children;
807
+ };
808
+ lookUpChildren(children, meta.className);
809
+ this.andWhere({
810
+ [meta.root.discriminatorColumn]: children.length > 0 ? { $in: [meta.discriminatorValue, ...children.map(c => c.discriminatorValue)] } : meta.discriminatorValue,
811
+ });
812
+ }
795
813
  finalize() {
796
814
  if (this.finalized) {
797
815
  return;
@@ -800,6 +818,7 @@ class QueryBuilder {
800
818
  this.select('*');
801
819
  }
802
820
  const meta = this.mainAlias.metadata;
821
+ this.applyDiscriminatorCondition();
803
822
  if (meta && this.flags.has(core_1.QueryFlag.AUTO_JOIN_ONE_TO_ONE_OWNER)) {
804
823
  const relationsToPopulate = this._populate.map(({ field }) => field);
805
824
  meta.relations
@@ -965,8 +984,8 @@ class QueryBuilder {
965
984
  const prop = this._cond[pivotMeta.name + '.' + owner.name] || this._orderBy[pivotMeta.name + '.' + owner.name] ? inverse : owner;
966
985
  const pivotAlias = this.getNextAlias(pivotMeta.name);
967
986
  this._joins[field] = this.helper.joinPivotTable(field, prop, this.mainAlias.aliasName, pivotAlias, 'leftJoin');
968
- core_1.Utils.renameKey(this._cond, `${field}.${owner.name}`, core_1.Utils.getPrimaryKeyHash(owner.fieldNames.map(fieldName => `${pivotAlias}.${fieldName}`)));
969
- core_1.Utils.renameKey(this._cond, `${field}.${inverse.name}`, core_1.Utils.getPrimaryKeyHash(inverse.fieldNames.map(fieldName => `${pivotAlias}.${fieldName}`)));
987
+ core_1.Utils.renameKey(this._cond, `${field}.${owner.name}`, core_1.Utils.getPrimaryKeyHash(owner.fieldNames.map(fieldName => `${pivotAlias}.${fieldName}`)), true);
988
+ core_1.Utils.renameKey(this._cond, `${field}.${inverse.name}`, core_1.Utils.getPrimaryKeyHash(inverse.fieldNames.map(fieldName => `${pivotAlias}.${fieldName}`)), true);
970
989
  this._populateMap[field] = this._joins[field].alias;
971
990
  }
972
991
  getSchema(alias) {
@@ -177,6 +177,11 @@ class QueryBuilderHelper {
177
177
  const right = `${join.alias}.${join.joinColumns[idx]}`;
178
178
  conditions.push(`${this.knex.ref(left)} = ${this.knex.ref(right)}`);
179
179
  });
180
+ if (join.prop.targetMeta.discriminatorValue && !join.path?.endsWith('[pivot]')) {
181
+ const typeProperty = join.prop.targetMeta.root.discriminatorColumn;
182
+ const alias = !join.prop.owner ? join.inverseAlias ?? join.alias : join.ownerAlias;
183
+ join.cond[`${alias}.${typeProperty}`] = join.prop.targetMeta.discriminatorValue;
184
+ }
180
185
  Object.keys(join.cond).forEach(key => {
181
186
  conditions.push(this.processJoinClause(key, join.cond[key], params));
182
187
  });