@mikro-orm/knex 6.0.8-dev.15 → 6.0.8-dev.16

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, type Collection, type Configuration, type ConnectionType, type Constructor, type CountOptions, DatabaseDriver, type DeleteOptions, type Dictionary, type DriverMethodOptions, type EntityData, type EntityDictionary, type EntityField, EntityManagerType, type EntityMetadata, type EntityName, type EntityProperty, type FilterQuery, type FindOneOptions, type FindOptions, type IDatabaseDriver, type LockOptions, type LoggingOptions, type NativeInsertUpdateManyOptions, type NativeInsertUpdateOptions, type Options, type OrderDefinition, type PopulateOptions, type Primary, type QueryOrderMap, type QueryResult, type Transaction, type UpsertManyOptions, type UpsertOptions } from '@mikro-orm/core';
2
+ import { type AnyEntity, type Collection, type Configuration, type ConnectionType, type Constructor, type CountOptions, DatabaseDriver, type DeleteOptions, type Dictionary, type DriverMethodOptions, type EntityData, type EntityDictionary, type EntityField, EntityManagerType, type EntityMetadata, type EntityName, type EntityProperty, type FilterQuery, type FindOneOptions, type FindOptions, type IDatabaseDriver, type LockOptions, type LoggingOptions, type NativeInsertUpdateManyOptions, type NativeInsertUpdateOptions, type ObjectQuery, type Options, type OrderDefinition, type PopulateOptions, type Primary, type QueryOrderMap, type QueryResult, type Transaction, type UpsertManyOptions, type UpsertOptions } from '@mikro-orm/core';
3
3
  import type { AbstractSqlConnection } from './AbstractSqlConnection';
4
4
  import type { AbstractSqlPlatform } from './AbstractSqlPlatform';
5
5
  import { QueryBuilder, QueryType } from './query';
@@ -63,6 +63,7 @@ export declare abstract class AbstractSqlDriver<Connection extends AbstractSqlCo
63
63
  protected extractManyToMany<T>(entityName: string, data: EntityDictionary<T>): EntityData<T>;
64
64
  protected processManyToMany<T extends object>(meta: EntityMetadata<T> | undefined, pks: Primary<T>[], collections: EntityData<T>, clear: boolean, options?: DriverMethodOptions): Promise<void>;
65
65
  lockPessimistic<T extends object>(entity: T, options: LockOptions): Promise<void>;
66
+ protected buildPopulateWhere<T extends object>(meta: EntityMetadata<T>, joinedProps: PopulateOptions<T>[], options: Pick<FindOptions<any>, 'populateWhere'>): ObjectQuery<T>;
66
67
  protected buildOrderBy<T extends object>(qb: QueryBuilder<T>, meta: EntityMetadata<T>, populate: PopulateOptions<T>[], options: Pick<FindOptions<any>, 'strategy' | 'orderBy' | 'populateOrderBy'>): QueryOrderMap<T>[];
67
68
  protected buildPopulateOrderBy<T extends object>(qb: QueryBuilder<T>, meta: EntityMetadata<T>, populateOrderBy: QueryOrderMap<T>[], parentPath: string, explicit: boolean, parentAlias?: string): QueryOrderMap<T>[];
68
69
  protected buildJoinedPropsOrderBy<T extends object>(qb: QueryBuilder<T>, meta: EntityMetadata<T>, populate: PopulateOptions<T>[], options?: Pick<FindOptions<any>, 'strategy' | 'orderBy' | 'populateOrderBy'>, parentPath?: string): QueryOrderMap<T>[];
@@ -34,6 +34,7 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
34
34
  const qb = this.createQueryBuilder(entityName, options.ctx, options.connectionType, false, options.logging);
35
35
  const fields = this.buildFields(meta, populate, joinedProps, qb, qb.alias, options);
36
36
  const orderBy = this.buildOrderBy(qb, meta, populate, options);
37
+ const populateWhere = this.buildPopulateWhere(meta, joinedProps, options);
37
38
  core_1.Utils.asArray(options.flags).forEach(flag => qb.setFlag(flag));
38
39
  if (core_1.Utils.isPrimaryKey(where, meta.compositePK)) {
39
40
  where = { [core_1.Utils.getPrimaryKeyHash(meta.primaryKeys)]: where };
@@ -43,7 +44,7 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
43
44
  qb.__populateWhere = options._populateWhere;
44
45
  qb.select(fields)
45
46
  // only add populateWhere if we are populate-joining, as this will be used to add `on` conditions
46
- .populate(populate, joinedProps.length > 0 ? options.populateWhere : undefined)
47
+ .populate(populate, joinedProps.length > 0 ? populateWhere : undefined)
47
48
  .where(where)
48
49
  .groupBy(options.groupBy)
49
50
  .having(options.having)
@@ -981,6 +982,31 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
981
982
  qb.select((0, core_1.raw)('1')).where(cond).setLockMode(options.lockMode, options.lockTableAliases);
982
983
  await this.rethrow(qb.execute());
983
984
  }
985
+ buildPopulateWhere(meta, joinedProps, options) {
986
+ const where = {};
987
+ for (const hint of joinedProps) {
988
+ const [propName] = hint.field.split(':', 2);
989
+ const prop = meta.properties[propName];
990
+ if (!core_1.Utils.isEmpty(prop.where)) {
991
+ where[prop.name] = core_1.Utils.copy(prop.where);
992
+ }
993
+ if (hint.children) {
994
+ const inner = this.buildPopulateWhere(prop.targetMeta, hint.children, {});
995
+ if (!core_1.Utils.isEmpty(inner)) {
996
+ where[prop.name] ??= {};
997
+ Object.assign(where[prop.name], inner);
998
+ }
999
+ }
1000
+ }
1001
+ if (core_1.Utils.isEmpty(options.populateWhere)) {
1002
+ return where;
1003
+ }
1004
+ if (core_1.Utils.isEmpty(where)) {
1005
+ return options.populateWhere;
1006
+ }
1007
+ /* istanbul ignore next */
1008
+ return { $and: [options.populateWhere, where] };
1009
+ }
984
1010
  buildOrderBy(qb, meta, populate, options) {
985
1011
  const joinedProps = this.joinedProps(meta, populate, options);
986
1012
  // `options._populateWhere` is a copy of the value provided by user with a fallback to the global config option
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/knex",
3
- "version": "6.0.8-dev.15",
3
+ "version": "6.0.8-dev.16",
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": "^6.0.7"
67
67
  },
68
68
  "peerDependencies": {
69
- "@mikro-orm/core": "6.0.8-dev.15"
69
+ "@mikro-orm/core": "6.0.8-dev.16"
70
70
  }
71
71
  }
@@ -205,7 +205,11 @@ class QueryBuilderHelper {
205
205
  for (const key of Object.keys(join.cond)) {
206
206
  const hasPrefix = key.includes('.') || core_1.Utils.isOperator(key) || core_1.RawQueryFragment.isKnownFragment(key);
207
207
  const newKey = hasPrefix ? key : `${join.alias}.${key}`;
208
- conditions.push(this.processJoinClause(newKey, join.cond[key], join.alias, params));
208
+ const clause = this.processJoinClause(newKey, join.cond[key], join.alias, params);
209
+ /* istanbul ignore else */
210
+ if (clause !== '()') {
211
+ conditions.push(clause);
212
+ }
209
213
  }
210
214
  let sql = method + ' ';
211
215
  if (join.subquery) {