🚨 Announcing Vendure v2 Beta

EntityHydrator

EntityHydrator

This is a helper class which is used to “hydrate” entity instances, which means to populate them with the specified relations. This is useful when writing plugin code which receives an entity and you need to ensure that one or more relations are present.

Example

const product = await this.productVariantService
  .getProductForVariant(ctx, variantId);

await this.entityHydrator
  .hydrate(ctx, product, { relations: ['facetValues.facet' ]});

In this above example, the product instance will now have the facetValues relation available, and those FacetValues will have their facet relations joined too.

This hydrate method will also automatically take care or translating any translatable entities (e.g. Product, Collection, Facet), and if the applyProductVariantPrices options is used (see HydrateOptions), any related ProductVariant will have the correct Channel-specific prices applied to them.

Custom field relations may also be hydrated:

Example

const customer = await this.customerService
  .findOne(ctx, id);

await this.entityHydrator
  .hydrate(ctx, customer, { relations: ['customFields.avatar' ]});

Signature

class EntityHydrator {
  constructor(connection: TransactionalConnection, productPriceApplicator: ProductPriceApplicator, translator: TranslatorService)
  async hydrate(ctx: RequestContext, target: Entity, options: HydrateOptions<Entity>) => Promise<Entity>;
}

Members

constructor

method
type:
(connection: TransactionalConnection, productPriceApplicator: ProductPriceApplicator, translator: TranslatorService) => EntityHydrator

hydrate

async method
v1.3.0
type:
(ctx: RequestContext, target: Entity, options: HydrateOptions<Entity>) => Promise<Entity>

Hydrates (joins) the specified relations to the target entity instance. This method mutates the target entity.

Example

await this.entityHydrator.hydrate(ctx, product, {
  relations: [
    'variants.stockMovements'
    'optionGroups.options',
    'featuredAsset',
  ],
  applyProductVariantPrices: true,
});