@mikro-orm/knex 5.7.15-dev.45 → 5.7.15-dev.46

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": "5.7.15-dev.45",
3
+ "version": "5.7.15-dev.46",
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,9 +66,9 @@
66
66
  "@mikro-orm/core": "^5.7.14"
67
67
  },
68
68
  "peerDependencies": {
69
- "@mikro-orm/core": "~5.7.15-dev.45",
70
- "@mikro-orm/entity-generator": "~5.7.15-dev.45",
71
- "@mikro-orm/migrations": "~5.7.15-dev.45",
69
+ "@mikro-orm/core": "~5.7.15-dev.46",
70
+ "@mikro-orm/entity-generator": "~5.7.15-dev.46",
71
+ "@mikro-orm/migrations": "~5.7.15-dev.46",
72
72
  "better-sqlite3": "*",
73
73
  "mssql": "*",
74
74
  "mysql": "*",
@@ -214,6 +214,7 @@ export declare class QueryBuilder<T extends object = AnyEntity> {
214
214
  private init;
215
215
  private getQueryBase;
216
216
  private finalize;
217
+ private processPopulateWhere;
217
218
  private hasToManyJoins;
218
219
  private wrapPaginateSubQuery;
219
220
  private wrapModifySubQuery;
@@ -850,6 +850,7 @@ class QueryBuilder {
850
850
  .filter(field => !this._fields.includes(field))
851
851
  .forEach(field => this.addSelect(field));
852
852
  }
853
+ this.processPopulateWhere();
853
854
  core_1.QueryHelper.processObjectParams(this._data);
854
855
  core_1.QueryHelper.processObjectParams(this._cond);
855
856
  core_1.QueryHelper.processObjectParams(this._having);
@@ -865,6 +866,40 @@ class QueryBuilder {
865
866
  }
866
867
  this.finalized = true;
867
868
  }
869
+ processPopulateWhere() {
870
+ if (this._populateWhere == null || this._populateWhere === core_1.PopulateHint.ALL) {
871
+ return;
872
+ }
873
+ const joins = Object.values(this._joins);
874
+ joins.forEach(join => {
875
+ join.cond_ = join.cond;
876
+ join.cond = {};
877
+ });
878
+ const replaceOnConditions = (cond) => {
879
+ Object.keys(cond).forEach(k => {
880
+ if (core_1.Utils.isOperator(k)) {
881
+ if (Array.isArray(cond[k])) {
882
+ return cond[k].forEach((c) => replaceOnConditions(c));
883
+ }
884
+ return replaceOnConditions(cond[k]);
885
+ }
886
+ const [a] = this.helper.splitField(k);
887
+ const join = joins.find(j => j.alias === a);
888
+ if (join) {
889
+ join.cond = { ...join.cond, [k]: cond[k] };
890
+ }
891
+ });
892
+ };
893
+ if (this._populateWhere === core_1.PopulateHint.INFER) {
894
+ replaceOnConditions(this._cond);
895
+ }
896
+ else if (typeof this._populateWhere === 'object') {
897
+ const cond = CriteriaNodeFactory_1.CriteriaNodeFactory
898
+ .createNode(this.metadata, this.mainAlias.entityName, this._populateWhere)
899
+ .process(this);
900
+ replaceOnConditions(cond);
901
+ }
902
+ }
868
903
  hasToManyJoins() {
869
904
  return Object.values(this._joins).some(join => {
870
905
  return [core_1.ReferenceType.ONE_TO_MANY, core_1.ReferenceType.MANY_TO_MANY].includes(join.prop.reference);
@@ -873,6 +908,8 @@ class QueryBuilder {
873
908
  wrapPaginateSubQuery(meta) {
874
909
  const pks = this.prepareFields(meta.primaryKeys, 'sub-query');
875
910
  const subQuery = this.clone().select(pks).groupBy(pks).limit(this._limit);
911
+ // revert the on conditions added via populateWhere, we want to apply those only once
912
+ Object.values(subQuery._joins).forEach(join => join.cond = join.cond_ ?? {});
876
913
  if (this._offset) {
877
914
  subQuery.offset(this._offset);
878
915
  }
@@ -915,14 +952,7 @@ class QueryBuilder {
915
952
  subSubQuery.__raw = true; // tag it as there is now way to check via `instanceof`
916
953
  this._limit = undefined;
917
954
  this._offset = undefined;
918
- const cond = this._cond;
919
955
  this.select(this._fields).where({ [core_1.Utils.getPrimaryKeyHash(meta.primaryKeys)]: { $in: subSubQuery } });
920
- if (this._populateWhere === core_1.PopulateHint.INFER) {
921
- this.andWhere(cond);
922
- }
923
- else if (typeof this._populateWhere === 'object') {
924
- this.andWhere(this._populateWhere);
925
- }
926
956
  }
927
957
  wrapModifySubQuery(meta) {
928
958
  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;