From f9f5260a4e03f527db41ccc8c4639cf1a9bc94ec Mon Sep 17 00:00:00 2001 From: Eugene Nitsenko Date: Sun, 3 Mar 2024 15:02:52 +0100 Subject: [PATCH] feat(core): Fix for un-joined relations or joined with wrong alias --- .../list-query-builder/list-query-builder.ts | 24 ++++++------------- .../list-query-builder/parse-sort-params.ts | 5 ++-- 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/packages/core/src/service/helpers/list-query-builder/list-query-builder.ts b/packages/core/src/service/helpers/list-query-builder/list-query-builder.ts index 3ed18c0969..a1e9fafa65 100644 --- a/packages/core/src/service/helpers/list-query-builder/list-query-builder.ts +++ b/packages/core/src/service/helpers/list-query-builder/list-query-builder.ts @@ -297,6 +297,7 @@ export class ListQueryBuilder implements OnApplicationBootstrap { skip, where: extendedOptions.where || {}, relationLoadStrategy: extendedOptions?.relationLoadStrategy || 'query', + loadEagerRelations: true, }); // join the tables required by calculated columns @@ -307,22 +308,16 @@ export class ListQueryBuilder implements OnApplicationBootstrap { } const customFieldsForType = this.configService.customFields[entity.name as keyof CustomFields]; const sortParams = Object.assign({}, options.sort, extendedOptions.orderBy); - this.applyTranslationConditions(qb, entity, sortParams, extendedOptions.ctx, alias); + this.applyTranslationConditions(qb, entity, sortParams, extendedOptions.ctx); const sort = parseSortParams( - rawConnection, + qb.connection, entity, sortParams, customPropertyMap, - entityAlias, + qb.alias, customFieldsForType, ); - const filter = parseFilterParams( - rawConnection, - entity, - options.filter, - customPropertyMap, - entityAlias, - ); + const filter = parseFilterParams(qb.connection, entity, options.filter, customPropertyMap, qb.alias); if (filter.length) { const filterOperator = options.filterOperator ?? LogicalOperator.AND; @@ -537,16 +532,11 @@ export class ListQueryBuilder implements OnApplicationBootstrap { entity: Type, sortParams: NullOptionals> & FindOneOptions['order'], ctx?: RequestContext, - entityAlias?: string, ) { const languageCode = ctx?.languageCode || this.configService.defaultLanguageCode; - const { - columns, - translationColumns, - alias: defaultAlias, - } = getColumnMetadata(this.connection.rawConnection, entity); - const alias = entityAlias ?? defaultAlias; + const { translationColumns } = getColumnMetadata(qb.connection, entity); + const alias = qb.alias; const sortKeys = Object.keys(sortParams); let sortingOnTranslatableKey = false; diff --git a/packages/core/src/service/helpers/list-query-builder/parse-sort-params.ts b/packages/core/src/service/helpers/list-query-builder/parse-sort-params.ts index 608e48c3f1..eec3f522aa 100644 --- a/packages/core/src/service/helpers/list-query-builder/parse-sort-params.ts +++ b/packages/core/src/service/helpers/list-query-builder/parse-sort-params.ts @@ -1,6 +1,7 @@ import { Type } from '@vendure/common/lib/shared-types'; import { unique } from '@vendure/common/lib/unique'; -import { Connection, OrderByCondition } from 'typeorm'; +import { OrderByCondition } from 'typeorm'; +import { DataSource } from 'typeorm/data-source/DataSource'; import { ColumnMetadata } from 'typeorm/metadata/ColumnMetadata'; import { UserInputError } from '../../../common/error/errors'; @@ -22,7 +23,7 @@ import { getCalculatedColumns } from './get-calculated-columns'; * @param customFields */ export function parseSortParams( - connection: Connection, + connection: DataSource, entity: Type, sortParams?: NullOptionals> | null, customPropertyMap?: { [name: string]: string },