TypeOrm
  • Getting Started
  • About
  • Connection
    • Working with Connection
    • Using ormconfig.json
    • Connection Options
    • Multiple connections
    • Connection APIs
  • Entity
    • What is Entity?
    • Embedded Entities
    • Entity Inheritance
    • Tree Entities
    • View Entities
    • Separating Entity Definition
  • Relations
    • What are Relations?
    • One-to-One
    • Many-to-one and One-to-Many
    • Many-to-Many
    • Eager and Lazy Relations
    • Relations FAQ
  • Entity Manager and Repository
    • What is EntityManager
    • Working with Repository
    • Find Options
    • Custom Repositories
    • Entity Manager API
    • Repository API
  • Query Builder
    • Select using Query Builder
    • Insert using Query Builder
    • Update using Query Builder
    • Delete using Query Builder
    • Working with Relations
    • Caching Results
  • Advanced Topics
    • Using CLI
    • Logging
    • Listeners and Subscribers
    • Indices
    • Transactions
    • Migrations
  • Guides
    • Active Record vs Data Mapper
    • Working with MongoDB
    • Using Validation
    • Example with Express
    • Usage with JavaScript
    • Migration from Sequelize
  • Help
    • FAQ
    • Supported Platforms
    • Decorators reference
    • Roadmap
    • Changelog
Powered by GitBook
On this page
  • Concrete Table Inheritance
  • Single Table Inheritance
  • Using embeddeds

Was this helpful?

  1. Entity

Entity Inheritance

Concrete Table Inheritance

You can reduce duplication in your code by using entity inheritance patterns. The simplest and the most effective is concrete table inheritance.

For example, you have Photo, Question, Post entities:

@Entity()
export class Photo {

    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    title: string;

    @Column()
    description: string;

    @Column()
    size: string;

}
@Entity()
export class Question {

    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    title: string;

    @Column()
    description: string;

    @Column()
    answersCount: number;

}
@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:

export abstract class Content {

    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    title: string;

    @Column()
    description: string;

}
@Entity()
export class Photo extends Content {

    @Column()
    size: string;

}
@Entity()
export class Question extends Content {

    @Column()
    answersCount: number;

}
@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.

@Entity()
@TableInheritance({ column: { type: "varchar", name: "type" } })
export class Content {

    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    title: string;

    @Column()
    description: string;

}
@ChildEntity()
export class Photo extends Content {

    @Column()
    size: string;

}
@ChildEntity()
export class Question extends Content {

    @Column()
    answersCount: number;

}
@ChildEntity()
export class Post extends Content {

    @Column()
    viewCount: number;

}

This will create a single table called content and all instances of photos, questions and posts will be saved into this table.

Using embeddeds

PreviousEmbedded EntitiesNextTree Entities

Last updated 4 years ago

Was this helpful?

There is an amazing way to reduce duplication in your app (using composition over inheritance) by using embedded columns. Read more about embedded entities .

here