Skip to content

Commit

Permalink
perf(core): Upgrade EntityHydrator performance to any hydrate call (#…
Browse files Browse the repository at this point in the history
…2742)

and even move with many relations
  • Loading branch information
monrostar authored Mar 18, 2024
1 parent 65888cb commit 77233cd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { ProductPriceApplicator } from '../product-price-applicator/product-pric
import { TranslatorService } from '../translator/translator.service';

import { HydrateOptions } from './entity-hydrator-types';
import { ListQueryBuilder } from '../list-query-builder/list-query-builder';
import { SelectQueryBuilder } from 'typeorm';

/**
* @description
Expand Down Expand Up @@ -78,6 +80,7 @@ export class EntityHydrator {
private connection: TransactionalConnection,
private productPriceApplicator: ProductPriceApplicator,
private translator: TranslatorService,
private listQueryBuilder: ListQueryBuilder,
) {}

/**
Expand Down Expand Up @@ -116,10 +119,17 @@ export class EntityHydrator {
}

if (missingRelations.length) {
const hydrated = await this.connection.getRepository(ctx, target.constructor).findOne({
const hydratedQb: SelectQueryBuilder<any> = this.connection
.getRepository(ctx, target.constructor)
.createQueryBuilder(target.constructor.name)
const processedRelations = this.listQueryBuilder
.joinTreeRelationsDynamically(hydratedQb, target.constructor, missingRelations)
hydratedQb.setFindOptions({
relationLoadStrategy: 'query',
where: { id: target.id },
relations: missingRelations,
relations: missingRelations.filter(relationPath => !processedRelations.has(relationPath)),
});
const hydrated = await hydratedQb.getOne();
const propertiesToAdd = unique(missingRelations.map(relation => relation.split('.')[0]));
for (const prop of propertiesToAdd) {
(target as any)[prop] = this.mergeDeep((target as any)[prop], (hydrated as any)[prop]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ export class ListQueryBuilder implements OnApplicationBootstrap {
* @template T extends VendureEntity The type of the entity for which relations are being joined. This type parameter
* should extend VendureEntity to ensure compatibility with Vendure's data access layer.
*/
private joinTreeRelationsDynamically<T extends VendureEntity>(
public joinTreeRelationsDynamically<T extends VendureEntity>(
qb: SelectQueryBuilder<T>,
entity: EntityTarget<T>,
requestedRelations: string[] = [],
Expand Down

0 comments on commit 77233cd

Please sign in to comment.