Skip to content

Commit

Permalink
feat(query-builder): allow autocomplete on qb.orderBy()
Browse files Browse the repository at this point in the history
Related: #2747
  • Loading branch information
B4nan committed Feb 11, 2022
1 parent 7ce12d6 commit fdf03c3
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/entity/EntityLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,8 @@ export class EntityLoader {
const filtered = Utils.unique(children);
const fields = this.buildFields(options.fields, prop);
const innerOrderBy = Utils.asArray(options.orderBy)
.filter(orderBy => Utils.isObject(orderBy[prop.name]))
.map(orderBy => orderBy[prop.name]);
.filter(orderBy => Utils.isObject(orderBy[prop.name as string]))
.map(orderBy => orderBy[prop.name as string]);
const { refresh, filters, ignoreLazyScalarProperties, populateWhere } = options;

await this.populate<T>(prop.type, filtered, populate.children, {
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/enums.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ExpandProperty } from './typings';
import type { Dictionary, ExcludeFunctions, ExpandProperty } from './typings';
import type { Transaction } from './connections';

export const enum FlushMode {
Expand Down Expand Up @@ -74,9 +74,11 @@ export type QueryOrderKeysFlat = QueryOrder | QueryOrderNumeric | keyof typeof Q
export type QueryOrderKeys<T> = QueryOrderKeysFlat | QueryOrderMap<T>;

export type QueryOrderMap<T> = {
[K in keyof T]?: QueryOrderKeys<ExpandProperty<T[K]>>;
[K in keyof T as ExcludeFunctions<T, K>]?: QueryOrderKeys<ExpandProperty<T[K]>>;
};

export type QBQueryOrderMap<T> = QueryOrderMap<T> | Dictionary;

export interface FlatQueryOrderMap {
[x: string]: QueryOrderKeysFlat;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/knex/src/query/QueryBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Knex } from 'knex';
import type {
AnyEntity, Dictionary, EntityData, EntityMetadata, EntityProperty, FlatQueryOrderMap, RequiredEntityData,
GroupOperator, MetadataStorage, PopulateOptions, QBFilterQuery, QueryOrderMap, QueryResult, FlushMode, FilterQuery,
GroupOperator, MetadataStorage, PopulateOptions, QBFilterQuery, QueryOrderMap, QueryResult, FlushMode, FilterQuery, QBQueryOrderMap,
} from '@mikro-orm/core';
import { LoadStrategy, LockMode, QueryFlag, QueryHelper, ReferenceType, Utils, ValidationError } from '@mikro-orm/core';
import { QueryType } from './enums';
Expand Down Expand Up @@ -226,7 +226,7 @@ export class QueryBuilder<T extends AnyEntity<T> = AnyEntity> {
return this.where(cond as string, params, '$or');
}

orderBy(orderBy: QueryOrderMap<unknown> | QueryOrderMap<unknown>[]): this {
orderBy(orderBy: QBQueryOrderMap<T> | QBQueryOrderMap<T>[]): this {
this._orderBy = [];
Utils.asArray(orderBy).forEach(o => {
const processed = QueryHelper.processWhere(o as Dictionary, this.entityName, this.metadata, this.platform, false)!;
Expand Down
2 changes: 1 addition & 1 deletion packages/mongodb/src/MongoConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export class MongoConnection extends Connection {
let query = `db.getCollection('${collection}').find(${this.logObject(where)}, ${this.logObject(options)})`;
orderBy = Utils.asArray(orderBy);

if (orderBy.length > 0) {
if (Array.isArray(orderBy) && orderBy.length > 0) {
const orderByTuples: [string, number][] = [];
orderBy.forEach(o => {
Object.keys(o).forEach(k => {
Expand Down

0 comments on commit fdf03c3

Please sign in to comment.