Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(core): Upgrade EntityHydrator to add more performance #2742

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading