Skip to main content

Define a database entity

Your plugin can define new database entities to model the data it needs to store. For instance, a product review plugin would need a way to store reviews. This would be done by defining a new database entity.

This example shows how new TypeORM database entities can be defined by plugins.

Create the entity class

import { DeepPartial } from '@vendure/common/lib/shared-types';
import { VendureEntity, Product, EntityId, ID } from '@vendure/core';
import { Column, Entity, ManyToOne } from 'typeorm';

class ProductReview extends VendureEntity {
constructor(input?: DeepPartial<ProductReview>) {

@ManyToOne(type => Product)
product: Product;

productId: ID;

text: string;

rating: number;

Any custom entities must extend the VendureEntity class.

In this example, we are making use of the following TypeORM decorators:

  • @Entity() - defines the entity as a TypeORM entity. This is required for all entities. It tells TypeORM to create a new table in the database for this entity.
  • @Column() - defines a column in the database table. The data type of the column is inferred from the TypeScript type of the property, but can be overridden by passing an options object to the decorator. The @Column() also supports many other options for defining the column, such as nullable, default, unique, primary, enum etc.
  • @ManyToOne() - defines a many-to-one relationship between this entity and another entity. In this case, many ProductReview entities can be associated with a given Product. There are other types of relations that can be defined - see the TypeORM relations docs.

There is an additional Vendure-specific decorator:

  • @EntityId() marks a property as the ID of another entity. In this case, the productId property is the ID of the Product entity. The reason that we have a special decorator for this is that Vendure supports both numeric and string IDs, and the @EntityId() decorator will automatically set the database column to be the correct type. This productId is not necessary, but it is a useful convention to allow access to the ID of the associated entity without having to perform a database join.

Register the entity

The new entity is then passed to the entities array of the VendurePlugin metadata:

import { VendurePlugin } from '@vendure/core';
import { ProductReview } from './entities/product-review.entity';

entities: [ProductReview],
export class ReviewsPlugin {}

Once you have added a new entity to your plugin, and the plugin has been added to your VendureConfig plugins array, you must create a database migration to create the new table in the database.

Using the entity

The new entity can now be used in your plugin code. For example, you might want to create a new product review when a customer submits a review via the storefront:

import { Injectable } from '@nestjs/common';
import { RequestContext, Product, TransactionalConnection } from '@vendure/core';

import { ProductReview } from '../entities/product-review.entity';

export class ReviewService {
constructor(private connection: TransactionalConnection) {}

async createReview(ctx: RequestContext, productId: string, rating: number, text: string) {
const product = await this.connection.getEntityOrThrow(ctx, Product, productId);
const review = new ProductReview({
return this.connection.getRepository(ctx, ProductReview).save(review);

Available entity decorators

In addition to the decorators described above, there are many other decorators provided by TypeORM. Some commonly used ones are:

There is also another Vendure-specific decorator for representing monetary values specifically:


The full list of TypeORM decorators can be found in the TypeORM decorator reference

Corresponding GraphQL type

Once you have defined a new DB entity, it is likely that you want to expose it in your GraphQL API. Here's how to define a new type in your GraphQL API.