@mikro-orm/knex 6.4.8-dev.5 → 6.4.8-dev.7

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.
@@ -65,6 +65,9 @@ class AbstractSqlDriver extends core_1.DatabaseDriver {
65
65
  if (options.lockMode) {
66
66
  qb.setLockMode(options.lockMode, options.lockTableAliases);
67
67
  }
68
+ if (options.em) {
69
+ await qb.applyJoinedFilters(options.em, options.filters);
70
+ }
68
71
  const result = await this.rethrow(qb.execute('all'));
69
72
  if (isCursorPagination && !first && !!last) {
70
73
  result.reverse();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/knex",
3
- "version": "6.4.8-dev.5",
3
+ "version": "6.4.8-dev.7",
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.4.7"
67
67
  },
68
68
  "peerDependencies": {
69
- "@mikro-orm/core": "6.4.8-dev.5",
69
+ "@mikro-orm/core": "6.4.8-dev.7",
70
70
  "better-sqlite3": "*",
71
71
  "libsql": "*",
72
72
  "mariadb": "*"
@@ -203,16 +203,18 @@ class ObjectCriteriaNode extends CriteriaNode_1.CriteriaNode {
203
203
  const operator = core_1.Utils.isPlainObject(this.payload) && Object.keys(this.payload).every(k => core_1.Utils.isOperator(k, false));
204
204
  const field = `${alias}.${this.prop.name}`;
205
205
  const method = qb.hasFlag(core_1.QueryFlag.INFER_POPULATE) ? 'joinAndSelect' : 'join';
206
+ const path = this.getPath();
206
207
  if (this.prop.kind === core_1.ReferenceKind.MANY_TO_MANY && (scalar || operator)) {
207
- qb.join(field, nestedAlias, undefined, enums_1.JoinType.pivotJoin, this.getPath());
208
+ qb.join(field, nestedAlias, undefined, enums_1.JoinType.pivotJoin, path);
208
209
  }
209
210
  else {
210
211
  const prev = qb._fields?.slice();
211
- qb[method](field, nestedAlias, undefined, enums_1.JoinType.leftJoin, this.getPath());
212
+ qb[method](field, nestedAlias, undefined, enums_1.JoinType.leftJoin, path);
212
213
  if (!qb.hasFlag(core_1.QueryFlag.INFER_POPULATE)) {
213
214
  qb._fields = prev;
214
215
  }
215
216
  }
217
+ qb.scheduleFilterCheck(path);
216
218
  return nestedAlias;
217
219
  }
218
220
  isPrefixed(field) {
@@ -1,6 +1,6 @@
1
1
  import { inspect } from 'node:util';
2
2
  import type { Knex } from 'knex';
3
- import { type AnyEntity, type ConnectionType, type Dictionary, type EntityData, type EntityKey, type EntityMetadata, type EntityName, type EntityProperty, type ExpandProperty, type FlushMode, type GroupOperator, type Loaded, LockMode, type LoggingOptions, type MetadataStorage, type ObjectQuery, PopulateHint, type PopulateOptions, type QBFilterQuery, type QBQueryOrderMap, QueryFlag, type QueryOrderMap, type QueryResult, RawQueryFragment, type RequiredEntityData } from '@mikro-orm/core';
3
+ import { type AnyEntity, type ConnectionType, type Dictionary, type EntityData, type EntityKey, type EntityManager, type EntityMetadata, type EntityName, type EntityProperty, type ExpandProperty, type FlushMode, type GroupOperator, type Loaded, LockMode, type LoggingOptions, type MetadataStorage, type ObjectQuery, PopulateHint, type PopulateOptions, type QBFilterQuery, type QBQueryOrderMap, QueryFlag, type QueryOrderMap, type QueryResult, RawQueryFragment, type RequiredEntityData } from '@mikro-orm/core';
4
4
  import { JoinType, QueryType } from './enums';
5
5
  import type { AbstractSqlDriver } from '../AbstractSqlDriver';
6
6
  import { type Alias, QueryBuilderHelper } from './QueryBuilderHelper';
@@ -148,6 +148,15 @@ export declare class QueryBuilder<Entity extends object = AnyEntity, RootAlias e
148
148
  * Apply filters to the QB where condition.
149
149
  */
150
150
  applyFilters(filterOptions?: Dictionary<boolean | Dictionary> | string[] | boolean): Promise<void>;
151
+ private readonly autoJoinedPaths;
152
+ /**
153
+ * @internal
154
+ */
155
+ scheduleFilterCheck(path: string): void;
156
+ /**
157
+ * @internal
158
+ */
159
+ applyJoinedFilters(em: EntityManager, filterOptions?: Dictionary<boolean | Dictionary> | string[] | boolean): Promise<void>;
151
160
  withSubQuery(subQuery: Knex.QueryBuilder, alias: string): this;
152
161
  where(cond: QBFilterQuery<Entity>, operator?: keyof typeof GroupOperator): this;
153
162
  where(cond: string, params?: any[], operator?: keyof typeof GroupOperator): this;
@@ -267,6 +267,34 @@ class QueryBuilder {
267
267
  const cond = await this.em.applyFilters(this.mainAlias.entityName, {}, filterOptions, 'read');
268
268
  this.andWhere(cond);
269
269
  }
270
+ autoJoinedPaths = [];
271
+ /**
272
+ * @internal
273
+ */
274
+ scheduleFilterCheck(path) {
275
+ this.autoJoinedPaths.push(path);
276
+ }
277
+ /**
278
+ * @internal
279
+ */
280
+ async applyJoinedFilters(em, filterOptions = {}) {
281
+ for (const path of this.autoJoinedPaths) {
282
+ const join = this.getJoinForPath(path);
283
+ if (join.type === enums_1.JoinType.pivotJoin) {
284
+ continue;
285
+ }
286
+ const cond = await em.applyFilters(join.prop.type, join.cond, filterOptions, 'read');
287
+ if (core_1.Utils.hasObjectKeys(cond)) {
288
+ if (core_1.Utils.hasObjectKeys(join.cond)) {
289
+ /* istanbul ignore next */
290
+ join.cond = { $and: [join.cond, cond] };
291
+ }
292
+ else {
293
+ join.cond = { ...cond };
294
+ }
295
+ }
296
+ }
297
+ }
270
298
  withSubQuery(subQuery, alias) {
271
299
  this.ensureNotFinalized();
272
300
  this.subQueries[alias] = subQuery.toString();
@@ -1197,7 +1225,7 @@ class QueryBuilder {
1197
1225
  let joins = Object.values(this._joins);
1198
1226
  for (const join of joins) {
1199
1227
  join.cond_ ??= join.cond;
1200
- join.cond = filter ? { ...join.cond } : {};
1228
+ join.cond = { ...join.cond };
1201
1229
  }
1202
1230
  if (typeof this[key] === 'object') {
1203
1231
  const cond = CriteriaNodeFactory_1.CriteriaNodeFactory
package/typings.d.ts CHANGED
@@ -163,6 +163,7 @@ export interface IQueryBuilder<T> {
163
163
  setFlag(flag: QueryFlag): this;
164
164
  unsetFlag(flag: QueryFlag): this;
165
165
  hasFlag(flag: QueryFlag): boolean;
166
+ scheduleFilterCheck(path: string): void;
166
167
  }
167
168
  export interface ICriteriaNodeProcessOptions {
168
169
  alias?: string;