@mikro-orm/knex 6.0.0-dev.33 → 6.0.0-dev.35

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/knex",
3
- "version": "6.0.0-dev.33",
3
+ "version": "6.0.0-dev.35",
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.6.8"
67
67
  },
68
68
  "peerDependencies": {
69
- "@mikro-orm/core": "~6.0.0-dev.33"
69
+ "@mikro-orm/core": "~6.0.0-dev.35"
70
70
  }
71
71
  }
@@ -201,6 +201,7 @@ export declare class QueryBuilder<T extends object = AnyEntity> {
201
201
  private init;
202
202
  private getQueryBase;
203
203
  private finalize;
204
+ private processPopulateWhere;
204
205
  private hasToManyJoins;
205
206
  private wrapPaginateSubQuery;
206
207
  private wrapModifySubQuery;
@@ -781,6 +781,7 @@ class QueryBuilder {
781
781
  .filter(field => !this._fields.includes(field))
782
782
  .forEach(field => this.addSelect(field));
783
783
  }
784
+ this.processPopulateWhere();
784
785
  core_1.QueryHelper.processObjectParams(this._data);
785
786
  core_1.QueryHelper.processObjectParams(this._cond);
786
787
  core_1.QueryHelper.processObjectParams(this._having);
@@ -796,6 +797,40 @@ class QueryBuilder {
796
797
  }
797
798
  this.finalized = true;
798
799
  }
800
+ processPopulateWhere() {
801
+ if (this._populateWhere == null || this._populateWhere === core_1.PopulateHint.ALL) {
802
+ return;
803
+ }
804
+ const joins = Object.values(this._joins);
805
+ joins.forEach(join => {
806
+ join.cond_ = join.cond;
807
+ join.cond = {};
808
+ });
809
+ const replaceOnConditions = (cond) => {
810
+ Object.keys(cond).forEach(k => {
811
+ if (core_1.Utils.isOperator(k)) {
812
+ if (Array.isArray(cond[k])) {
813
+ return cond[k].forEach((c) => replaceOnConditions(c));
814
+ }
815
+ return replaceOnConditions(cond[k]);
816
+ }
817
+ const [a] = this.helper.splitField(k);
818
+ const join = joins.find(j => j.alias === a);
819
+ if (join) {
820
+ join.cond = { ...join.cond, [k]: cond[k] };
821
+ }
822
+ });
823
+ };
824
+ if (this._populateWhere === core_1.PopulateHint.INFER) {
825
+ replaceOnConditions(this._cond);
826
+ }
827
+ else if (typeof this._populateWhere === 'object') {
828
+ const cond = CriteriaNodeFactory_1.CriteriaNodeFactory
829
+ .createNode(this.metadata, this.mainAlias.entityName, this._populateWhere)
830
+ .process(this);
831
+ replaceOnConditions(cond);
832
+ }
833
+ }
799
834
  hasToManyJoins() {
800
835
  return Object.values(this._joins).some(join => {
801
836
  return [core_1.ReferenceKind.ONE_TO_MANY, core_1.ReferenceKind.MANY_TO_MANY].includes(join.prop.kind);
@@ -804,6 +839,8 @@ class QueryBuilder {
804
839
  wrapPaginateSubQuery(meta) {
805
840
  const pks = this.prepareFields(meta.primaryKeys, 'sub-query');
806
841
  const subQuery = this.clone().select(pks).groupBy(pks).limit(this._limit);
842
+ // revert the on conditions added via populateWhere, we want to apply those only once
843
+ Object.values(subQuery._joins).forEach(join => join.cond = join.cond_ ?? {});
807
844
  if (this._offset) {
808
845
  subQuery.offset(this._offset);
809
846
  }
@@ -828,14 +865,7 @@ class QueryBuilder {
828
865
  const subSubQuery = this.getKnex().select(pks).from(knexQuery);
829
866
  this._limit = undefined;
830
867
  this._offset = undefined;
831
- const cond = this._cond;
832
868
  this.select(this._fields).where({ [core_1.Utils.getPrimaryKeyHash(meta.primaryKeys)]: { $in: subSubQuery } });
833
- if (this._populateWhere === core_1.PopulateHint.INFER) {
834
- this.andWhere(cond);
835
- }
836
- else if (typeof this._populateWhere === 'object') {
837
- this.andWhere(this._populateWhere);
838
- }
839
869
  }
840
870
  wrapModifySubQuery(meta) {
841
871
  const subQuery = this.clone();
package/typings.d.ts CHANGED
@@ -25,6 +25,7 @@ export interface JoinOptions {
25
25
  path?: string;
26
26
  prop: EntityProperty;
27
27
  cond: Dictionary;
28
+ cond_?: Dictionary;
28
29
  }
29
30
  export interface Column {
30
31
  name: string;