@mikro-orm/knex 6.0.0-dev.188 → 6.0.0-dev.189

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.
@@ -34,6 +34,7 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
34
34
  const fields = this.buildFields(meta, populate, joinedProps, qb, qb.alias, options.fields);
35
35
  const joinedPropsOrderBy = this.buildJoinedPropsOrderBy(entityName, qb, meta, joinedProps);
36
36
  const orderBy = [...core_1.Utils.asArray(options.orderBy), ...joinedPropsOrderBy];
37
+ core_1.Utils.asArray(options.flags).forEach(flag => qb.setFlag(flag));
37
38
  if (core_1.Utils.isPrimaryKey(where, meta.compositePK)) {
38
39
  where = { [core_1.Utils.getPrimaryKeyHash(meta.primaryKeys)]: where };
39
40
  }
@@ -61,7 +62,6 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
61
62
  if (options.lockMode) {
62
63
  qb.setLockMode(options.lockMode, options.lockTableAliases);
63
64
  }
64
- core_1.Utils.asArray(options.flags).forEach(flag => qb.setFlag(flag));
65
65
  const result = await this.rethrow(qb.execute('all'));
66
66
  if (isCursorPagination && !first && !!last) {
67
67
  result.reverse();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/knex",
3
- "version": "6.0.0-dev.188",
3
+ "version": "6.0.0-dev.189",
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.9.2"
67
67
  },
68
68
  "peerDependencies": {
69
- "@mikro-orm/core": "6.0.0-dev.188"
69
+ "@mikro-orm/core": "6.0.0-dev.189"
70
70
  }
71
71
  }
@@ -155,13 +155,16 @@ class ObjectCriteriaNode extends CriteriaNode_1.CriteriaNode {
155
155
  const scalar = core_1.Utils.isPrimaryKey(this.payload) || this.payload instanceof RegExp || this.payload instanceof Date || customExpression;
156
156
  const operator = core_1.Utils.isPlainObject(this.payload) && Object.keys(this.payload).every(k => core_1.Utils.isOperator(k, false));
157
157
  const field = `${alias}.${this.prop.name}`;
158
+ const method = qb.hasFlag(core_1.QueryFlag.INFER_POPULATE) ? 'joinAndSelect' : 'join';
158
159
  if (this.prop.kind === core_1.ReferenceKind.MANY_TO_MANY && (scalar || operator)) {
159
160
  qb.join(field, nestedAlias, undefined, enums_1.JoinType.pivotJoin, this.getPath());
160
161
  }
161
162
  else {
162
163
  const prev = qb._fields?.slice();
163
- qb.join(field, nestedAlias, undefined, enums_1.JoinType.leftJoin, this.getPath());
164
- qb._fields = prev;
164
+ qb[method](field, nestedAlias, undefined, enums_1.JoinType.leftJoin, this.getPath());
165
+ if (!qb.hasFlag(core_1.QueryFlag.INFER_POPULATE)) {
166
+ qb._fields = prev;
167
+ }
165
168
  }
166
169
  return nestedAlias;
167
170
  }
@@ -132,6 +132,7 @@ export declare class QueryBuilder<T extends object = AnyEntity> {
132
132
  setFlushMode(flushMode?: FlushMode): this;
133
133
  setFlag(flag: QueryFlag): this;
134
134
  unsetFlag(flag: QueryFlag): this;
135
+ hasFlag(flag: QueryFlag): boolean;
135
136
  cache(config?: boolean | number | [string, number]): this;
136
137
  /**
137
138
  * Adds index hint to the FROM clause.
@@ -186,7 +186,6 @@ class QueryBuilder {
186
186
  if (subquery) {
187
187
  this._joins[`${fromAlias}.${prop.name}#${alias}`].subquery = subquery;
188
188
  }
189
- this.addSelect(this.getFieldsForJoinedLoad(prop, alias, fields));
190
189
  const populate = this._joinedProps.get(fromAlias);
191
190
  const item = { field: prop.name, strategy: core_1.LoadStrategy.JOINED, children: [] };
192
191
  if (populate) {
@@ -196,6 +195,7 @@ class QueryBuilder {
196
195
  this._populate.push(item);
197
196
  }
198
197
  this._joinedProps.set(alias, item);
198
+ this.addSelect(this.getFieldsForJoinedLoad(prop, alias, fields));
199
199
  return this;
200
200
  }
201
201
  leftJoinAndSelect(field, alias, cond = {}, fields, schema) {
@@ -212,8 +212,22 @@ class QueryBuilder {
212
212
  }
213
213
  getFieldsForJoinedLoad(prop, alias, explicitFields) {
214
214
  const fields = [];
215
+ const populate = [];
216
+ const joinKey = Object.keys(this._joins).find(join => join.endsWith(`#${alias}`));
217
+ if (joinKey) {
218
+ const path = this._joins[joinKey].path.split('.').slice(1);
219
+ let children = this._populate;
220
+ for (let i = 0; i < path.length; i++) {
221
+ const child = children.filter(hint => hint.field === path[i]);
222
+ if (child.length === 0) {
223
+ break;
224
+ }
225
+ children = child.flatMap(c => c.children);
226
+ }
227
+ populate.push(...children);
228
+ }
215
229
  prop.targetMeta.props
216
- .filter(prop => explicitFields ? explicitFields.includes(prop.name) || prop.primary : this.platform.shouldHaveColumn(prop, this._populate))
230
+ .filter(prop => explicitFields ? explicitFields.includes(prop.name) || prop.primary : this.platform.shouldHaveColumn(prop, populate))
217
231
  .forEach(prop => fields.push(...this.driver.mapPropToFieldNames(this, prop, alias)));
218
232
  return fields;
219
233
  }
@@ -337,7 +351,7 @@ class QueryBuilder {
337
351
  return this;
338
352
  }
339
353
  returning(fields) {
340
- this._returning = fields != null ? core_1.Utils.asArray(fields) : fields;
354
+ this._returning = core_1.Utils.asArray(fields);
341
355
  return this;
342
356
  }
343
357
  /**
@@ -391,6 +405,9 @@ class QueryBuilder {
391
405
  this.flags.delete(flag);
392
406
  return this;
393
407
  }
408
+ hasFlag(flag) {
409
+ return this.flags.has(flag);
410
+ }
394
411
  cache(config = true) {
395
412
  this.ensureNotFinalized();
396
413
  this._cache = config;
package/typings.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { Knex } from 'knex';
2
- import type { CheckCallback, Dictionary, EntityProperty, GroupOperator, RawQueryFragment, QBFilterQuery, QueryOrderMap, Type } from '@mikro-orm/core';
2
+ import type { CheckCallback, Dictionary, EntityProperty, GroupOperator, RawQueryFragment, QBFilterQuery, QueryOrderMap, Type, QueryFlag } from '@mikro-orm/core';
3
3
  import type { JoinType, QueryType } from './query/enums';
4
4
  import type { DatabaseSchema, DatabaseTable } from './schema';
5
5
  export interface Table {
@@ -144,6 +144,9 @@ export interface IQueryBuilder<T> {
144
144
  getAliasForJoinPath(path: string): string | undefined;
145
145
  getNextAlias(entityName?: string): string;
146
146
  clone(reset?: boolean): IQueryBuilder<T>;
147
+ setFlag(flag: QueryFlag): this;
148
+ unsetFlag(flag: QueryFlag): this;
149
+ hasFlag(flag: QueryFlag): boolean;
147
150
  }
148
151
  export interface ICriteriaNode<T extends object> {
149
152
  readonly entityName: string;