🚨 Announcing Vendure v2 Beta

Defining a new database entity

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

// product-review.entity.ts
import { DeepPartial } from '@vendure/common/lib/shared-types';
import { VendureEntity } from '@vendure/core';
import { Column, Entity } from 'typeorm';

@Entity()
class ProductReview extends VendureEntity {
  constructor(input?: DeepPartial<ProductReview>) {
    super(input);
  }

  @Column()
  text: string;
  
  @Column()
  rating: number;
}

Note Any custom entities must extend the VendureEntity class.

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

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

@VendurePlugin({
  entities: [ProductReview],
})
export class ReviewsPlugin {}

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.