@Entity()
export class Post {
@PrimaryGeneratedColumn()
id: number;
@Column()
title: string;
@Column()
description: string;
@Column()
viewCount: number;
}
As you can see all those entities have common columns: id, title, description. To reduce duplication and produce a better abstraction we can create a base class called Content for them:
@Entity()
export class Post extends Content {
@Column()
viewCount: number;
}
All columns (relations, embeds, etc.) from parent entities (parent can extend other entity as well) will be inherited and created in final entities.
This example will create 3 tables - photo, question and post.
Single Table Inheritance
TypeORM also supports single table inheritance. Single table inheritance is a pattern when you have multiple classes with their own properties, but in the database they are stored in the same table.
There is an amazing way to reduce duplication in your app (using composition over inheritance) by using embedded columns. Read more about embedded entities .