typeorm/docs/tables-and-table-inheritance.md
2016-04-26 16:45:27 +05:00

1.2 KiB

Tables and table inheritance

Tables

ORM creates tables for each class that you decorated with @Table decorator and which you loaded into your connection.

@Table("photos")
export class Photo {

}

Each table must have a primary column (using @PrimaryColumn decotator) [todo: really?] and can contain other columns.

Table inheritance

If multiple tables has same properties you may want to find them a common abstraction and create a base class for them. In this base class you'll have a common for all inherited classes columns. To ahieve this you must mark your table with @AbstractTable() decorator:

@AbstractTable()
export class BasePhoto {

    @PrimaryColumn("int", { generated: true })
    id: string;

    @Column()
    name: string;

    @Column()
    url: string;

}

@Table("public_photos")
export class Photo extends BasePhoto {

    @Column()
    authorName: string;

}

@Table("private_photos")
export class Photo extends BasePhoto {

    @Column()
    isPublished: boolean;

}

This will create you two tables public_photos and private_photos with 5 columns each. Right now only columns are inherited. Relations are ignored.