@mikro-orm/knex 6.3.9-dev.2 → 6.3.9-dev.3

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.
@@ -44,7 +44,7 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
44
44
  qb.__populateWhere = options._populateWhere;
45
45
  qb.select(fields)
46
46
  // only add populateWhere if we are populate-joining, as this will be used to add `on` conditions
47
- .populate(populate, joinedProps.length > 0 ? populateWhere : undefined)
47
+ .populate(populate, joinedProps.length > 0 ? populateWhere : undefined, joinedProps.length > 0 ? options.populateFilter : undefined)
48
48
  .where(where)
49
49
  .groupBy(options.groupBy)
50
50
  .having(options.having)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/knex",
3
- "version": "6.3.9-dev.2",
3
+ "version": "6.3.9-dev.3",
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,7 +66,7 @@
66
66
  "@mikro-orm/core": "^6.3.8"
67
67
  },
68
68
  "peerDependencies": {
69
- "@mikro-orm/core": "6.3.9-dev.2",
69
+ "@mikro-orm/core": "6.3.9-dev.3",
70
70
  "better-sqlite3": "*",
71
71
  "libsql": "*",
72
72
  "mariadb": "*"
@@ -69,6 +69,8 @@ export declare class QueryBuilder<Entity extends object = AnyEntity, RootAlias e
69
69
  /** @internal */
70
70
  _populateWhere?: ObjectQuery<Entity> | PopulateHint | `${PopulateHint}`;
71
71
  /** @internal */
72
+ _populateFilter?: ObjectQuery<Entity> | PopulateHint | `${PopulateHint}`;
73
+ /** @internal */
72
74
  __populateWhere?: ObjectQuery<Entity> | PopulateHint | `${PopulateHint}`;
73
75
  /** @internal */
74
76
  _populateMap: Dictionary<string>;
@@ -161,7 +163,7 @@ export declare class QueryBuilder<Entity extends object = AnyEntity, RootAlias e
161
163
  /**
162
164
  * @internal
163
165
  */
164
- populate(populate: PopulateOptions<Entity>[], populateWhere?: ObjectQuery<Entity> | PopulateHint | `${PopulateHint}`): this;
166
+ populate(populate: PopulateOptions<Entity>[], populateWhere?: ObjectQuery<Entity> | PopulateHint | `${PopulateHint}`, populateFilter?: ObjectQuery<Entity> | PopulateHint | `${PopulateHint}`): this;
165
167
  limit(limit?: number, offset?: number): SelectQueryBuilder<Entity, RootAlias, Hint, Context>;
166
168
  offset(offset?: number): SelectQueryBuilder<Entity, RootAlias, Hint, Context>;
167
169
  withSchema(schema?: string): this;
@@ -281,6 +283,7 @@ export declare class QueryBuilder<Entity extends object = AnyEntity, RootAlias e
281
283
  private applyDiscriminatorCondition;
282
284
  private finalize;
283
285
  private processPopulateWhere;
286
+ private mergeOnConditions;
284
287
  private hasToManyJoins;
285
288
  protected wrapPaginateSubQuery(meta: EntityMetadata): void;
286
289
  private wrapModifySubQuery;
@@ -52,6 +52,8 @@ class QueryBuilder {
52
52
  /** @internal */
53
53
  _populateWhere;
54
54
  /** @internal */
55
+ _populateFilter;
56
+ /** @internal */
55
57
  __populateWhere;
56
58
  /** @internal */
57
59
  _populateMap = {};
@@ -393,10 +395,11 @@ class QueryBuilder {
393
395
  /**
394
396
  * @internal
395
397
  */
396
- populate(populate, populateWhere) {
398
+ populate(populate, populateWhere, populateFilter) {
397
399
  this.ensureNotFinalized();
398
400
  this._populate = populate;
399
401
  this._populateWhere = populateWhere;
402
+ this._populateFilter = populateFilter;
400
403
  return this;
401
404
  }
402
405
  limit(limit, offset = 0) {
@@ -782,7 +785,7 @@ class QueryBuilder {
782
785
  reset = reset || [];
783
786
  // clone array/object properties
784
787
  const properties = [
785
- 'flags', '_populate', '_populateWhere', '__populateWhere', '_populateMap', '_joins', '_joinedProps', '_cond', '_data', '_orderBy',
788
+ 'flags', '_populate', '_populateWhere', '_populateFilter', '__populateWhere', '_populateMap', '_joins', '_joinedProps', '_cond', '_data', '_orderBy',
786
789
  '_schema', '_indexHint', '_cache', 'subQueries', 'lockMode', 'lockTables', '_groupBy', '_having', '_returning',
787
790
  '_comments', '_hintComments', 'rawFragments', 'aliasCounter',
788
791
  ];
@@ -1141,7 +1144,8 @@ class QueryBuilder {
1141
1144
  }))
1142
1145
  .forEach(field => this._fields.push((0, core_1.raw)(field)));
1143
1146
  }
1144
- this.processPopulateWhere();
1147
+ this.processPopulateWhere(false);
1148
+ this.processPopulateWhere(true);
1145
1149
  core_1.QueryHelper.processObjectParams(this._data);
1146
1150
  core_1.QueryHelper.processObjectParams(this._cond);
1147
1151
  core_1.QueryHelper.processObjectParams(this._having);
@@ -1157,55 +1161,59 @@ class QueryBuilder {
1157
1161
  }
1158
1162
  this.finalized = true;
1159
1163
  }
1160
- processPopulateWhere() {
1161
- if (this._populateWhere == null || this._populateWhere === core_1.PopulateHint.ALL) {
1164
+ processPopulateWhere(filter) {
1165
+ const key = filter ? '_populateFilter' : '_populateWhere';
1166
+ if (this[key] == null || this[key] === core_1.PopulateHint.ALL) {
1162
1167
  return;
1163
1168
  }
1164
1169
  let joins = Object.values(this._joins);
1165
- joins.forEach(join => {
1166
- join.cond_ = join.cond;
1167
- join.cond = {};
1168
- });
1169
- const replaceOnConditions = (cond, op) => {
1170
- Object.keys(cond).forEach(k => {
1171
- if (core_1.Utils.isOperator(k)) {
1172
- if (Array.isArray(cond[k])) {
1173
- return cond[k].forEach((c) => replaceOnConditions(c, k));
1174
- }
1170
+ for (const join of joins) {
1171
+ join.cond_ ??= join.cond;
1172
+ // join.cond = {};
1173
+ join.cond = filter ? { ...join.cond } : {};
1174
+ }
1175
+ if (typeof this[key] === 'object') {
1176
+ const cond = CriteriaNodeFactory_1.CriteriaNodeFactory
1177
+ .createNode(this.metadata, this.mainAlias.entityName, this[key])
1178
+ .process(this, { matchPopulateJoins: true, ignoreBranching: true, preferNoBranch: true });
1179
+ // there might be new joins created by processing the `populateWhere` object
1180
+ joins = Object.values(this._joins);
1181
+ this.mergeOnConditions(joins, cond, filter);
1182
+ }
1183
+ }
1184
+ mergeOnConditions(joins, cond, filter, op) {
1185
+ for (const k of Object.keys(cond)) {
1186
+ if (core_1.Utils.isOperator(k)) {
1187
+ if (Array.isArray(cond[k])) {
1188
+ cond[k].forEach((c) => this.mergeOnConditions(joins, c, filter, k));
1189
+ }
1190
+ /* istanbul ignore next */
1191
+ this.mergeOnConditions(joins, cond[k], filter, k);
1192
+ }
1193
+ const [alias] = this.helper.splitField(k);
1194
+ const join = joins.find(j => j.alias === alias);
1195
+ if (join) {
1196
+ const parentJoin = joins.find(j => j.alias === join.ownerAlias);
1197
+ // https://stackoverflow.com/a/56815807/3665878
1198
+ if (parentJoin && !filter) {
1199
+ const nested = (parentJoin.nested ??= new Set());
1200
+ join.type = join.type === enums_1.JoinType.innerJoin || ([core_1.ReferenceKind.ONE_TO_MANY, core_1.ReferenceKind.MANY_TO_MANY].includes(parentJoin.prop.kind))
1201
+ ? enums_1.JoinType.nestedInnerJoin
1202
+ : enums_1.JoinType.nestedLeftJoin;
1203
+ nested.add(join);
1204
+ }
1205
+ if (join.cond[k]) {
1175
1206
  /* istanbul ignore next */
1176
- return replaceOnConditions(cond[k], k);
1207
+ join.cond = { [op ?? '$and']: [join.cond, { [k]: cond[k] }] };
1177
1208
  }
1178
- const [alias] = this.helper.splitField(k);
1179
- const join = joins.find(j => j.alias === alias);
1180
- if (join) {
1181
- const parentJoin = joins.find(j => j.alias === join.ownerAlias);
1182
- // https://stackoverflow.com/a/56815807/3665878
1183
- if (parentJoin) {
1184
- const nested = (parentJoin.nested ??= new Set());
1185
- join.type = join.type === enums_1.JoinType.innerJoin || [core_1.ReferenceKind.ONE_TO_MANY, core_1.ReferenceKind.MANY_TO_MANY].includes(parentJoin.prop.kind)
1186
- ? enums_1.JoinType.nestedInnerJoin
1187
- : enums_1.JoinType.nestedLeftJoin;
1188
- nested.add(join);
1189
- }
1190
- if (join.cond[k]) {
1191
- join.cond = { [op ?? '$and']: [join.cond, { [k]: cond[k] }] };
1192
- }
1193
- else if (op === '$or') {
1194
- join.cond.$or ??= [];
1195
- join.cond.$or.push({ [k]: cond[k] });
1196
- }
1197
- else {
1198
- join.cond = { ...join.cond, [k]: cond[k] };
1199
- }
1209
+ else if (op === '$or') {
1210
+ join.cond.$or ??= [];
1211
+ join.cond.$or.push({ [k]: cond[k] });
1200
1212
  }
1201
- });
1202
- };
1203
- if (typeof this._populateWhere === 'object') {
1204
- const cond = CriteriaNodeFactory_1.CriteriaNodeFactory
1205
- .createNode(this.metadata, this.mainAlias.entityName, this._populateWhere)
1206
- .process(this, { matchPopulateJoins: true, ignoreBranching: true, preferNoBranch: true });
1207
- joins = Object.values(this._joins); // there might be new joins created by processing the `populateWhere` object
1208
- replaceOnConditions(cond);
1213
+ else {
1214
+ join.cond = { ...join.cond, [k]: cond[k] };
1215
+ }
1216
+ }
1209
1217
  }
1210
1218
  }
1211
1219
  hasToManyJoins() {