Browse Source

perf(core): Upgrade EntityHydrator performance to any hydrate call (#2742)

and even move with many relations
Eugene Nitsenko 1 year ago
parent
commit
77233cdf86

+ 12 - 2
packages/core/src/service/helpers/entity-hydrator/entity-hydrator.service.ts

@@ -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
@@ -78,6 +80,7 @@ export class EntityHydrator {
         private connection: TransactionalConnection,
         private productPriceApplicator: ProductPriceApplicator,
         private translator: TranslatorService,
+        private listQueryBuilder: ListQueryBuilder,
     ) {}
 
     /**
@@ -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]);

+ 1 - 1
packages/core/src/service/helpers/list-query-builder/list-query-builder.ts

@@ -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[] = [],