EntityOptions

EntityMetadataModifier

A function which allows TypeORM entity metadata to be manipulated prior to the DB schema being generated during bootstrap.

Certain DB schema modifications will result in auto-generated migrations which will lead to data loss. For instance, changing the data type of a column will drop the column & data and then re-create it. To avoid loss of important data, always check and modify your migration scripts as needed.

Example

import { Index } from 'typeorm';
import { EntityMetadataModifier, ProductVariant } from '@vendure/core';

// Adds a unique index to the ProductVariant.sku column
export const addSkuUniqueIndex: EntityMetadataModifier = metadata => {
  const instance = new ProductVariant();
  Index({ unique: true })(instance, 'sku');
};

Example

import { Column } from 'typeorm';
import { EntityMetadataModifier, ProductTranslation } from '@vendure/core';

// Use the "mediumtext" datatype for the Product's description rather than
// the default "text" type.
export const makeProductDescriptionMediumText: EntityMetadataModifier = metadata => {
    const descriptionColumnIndex = metadata.columns.findIndex(
        col => col.propertyName === 'description' && col.target === ProductTranslation,
    );
    if (-1 < descriptionColumnIndex) {
        // First we need to remove the existing column definition
        // from the metadata.
        metadata.columns.splice(descriptionColumnIndex, 1);
        // Then we add a new column definition with our custom
        // data type "mediumtext"
        // DANGER: this particular modification will generate a DB migration
        // which will result in data loss of existing descriptions. Make sure
        // to manually check & modify your migration scripts.
        const instance = new ProductTranslation();
        Column({ type: 'mediumtext' })(instance, 'description');
    }
};

Signature

type EntityMetadataModifier = (metadata: MetadataArgsStorage) => void | Promise<void>

EntityOptions

Package: @vendure/core File: vendure-config.ts
v1.3.0

Options relating to the internal handling of entities.

Signature

interface EntityOptions {
  entityIdStrategy?: EntityIdStrategy<any>;
  channelCacheTtl?: number;
  zoneCacheTtl?: number;
  metadataModifiers?: EntityMetadataModifier[];
}

Members

entityIdStrategy

property
v1.3.0
type:
EntityIdStrategy<any>
Defines the strategy used for both storing the primary keys of entities in the database, and the encoding & decoding of those ids when exposing entities via the API. The default uses a simple auto-increment integer strategy.

channelCacheTtl

property
v1.3.0
type:
number
default:
30000
Channels get cached in-memory as they are accessed very frequently. This setting determines how long the cache lives (in ms) until it is considered stale and refreshed. For multi-instance deployments (e.g. serverless, load-balanced), a smaller value here will prevent data inconsistencies between instances.

zoneCacheTtl

property
v1.3.0
type:
number
default:
30000
Zones get cached in-memory as they are accessed very frequently. This setting determines how long the cache lives (in ms) until it is considered stale and refreshed. For multi-instance deployments (e.g. serverless, load-balanced), a smaller value here will prevent data inconsistencies between instances.

metadataModifiers

property
v1.6.0
default:
[]
Allows the metadata of the built-in TypeORM entities to be manipulated. This allows you to do things like altering data types, adding indices etc. This is an advanced feature which should be used with some caution as it will result in DB schema changes. For examples see EntityMetadataModifier.