Get a Quote Right Now

Edit Template

Single Table Inheritance

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.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *