• 嵌入式实体

    嵌入式实体

    通过使用embedded columns,可以减少应用程序中的重复(使用组合而不是继承)。

    嵌入列是一个列,它接受具有自己列的类,并将这些列合并到当前实体的数据库表中。

    例如:

    假设我们有UserEmployeeStudent实体。

    这些属性都有少量的共同点,first namelast name属性。

    1. import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";
    2. @Entity()
    3. export class User {
    4. @PrimaryGeneratedColumn()
    5. id: string;
    6. @Column()
    7. firstName: string;
    8. @Column()
    9. lastName: string;
    10. @Column()
    11. isActive: boolean;
    12. }
    1. import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";
    2. @Entity()
    3. export class Employee {
    4. @PrimaryGeneratedColumn()
    5. id: string;
    6. @Column()
    7. firstName: string;
    8. @Column()
    9. lastName: string;
    10. @Column()
    11. salary: string;
    12. }
    1. import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";
    2. @Entity()
    3. export class Student {
    4. @PrimaryGeneratedColumn()
    5. id: string;
    6. @Column()
    7. firstName: string;
    8. @Column()
    9. lastName: string;
    10. @Column()
    11. faculty: string;
    12. }

    我们可以做的是通过创建一个包含firstNamelastName的新类:

    1. import {Entity, Column} from "typeorm";
    2. export class Name {
    3. @Column()
    4. first: string;
    5. @Column()
    6. last: string;
    7. }

    然后”connect”实体中的这些列:

    1. import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";
    2. import {Name} from "./Name";
    3. @Entity()
    4. export class User {
    5. @PrimaryGeneratedColumn()
    6. id: string;
    7. @Column(type => Name)
    8. name: Name;
    9. @Column()
    10. isActive: boolean;
    11. }
    1. import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";
    2. import {Name} from "./Name";
    3. @Entity()
    4. export class Employee {
    5. @PrimaryGeneratedColumn()
    6. id: string;
    7. @Column(type => Name)
    8. name: Name;
    9. @Column()
    10. salary: number;
    11. }
    1. import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";
    2. import {Name} from "./Name";
    3. @Entity()
    4. export class Student {
    5. @PrimaryGeneratedColumn()
    6. id: string;
    7. @Column(type => Name)
    8. name: Name;
    9. @Column()
    10. faculty: string;
    11. }

    Name实体中定义的所有列将合并为useremployeestudent

    1. +-------------+--------------+----------------------------+
    2. | user |
    3. +-------------+--------------+----------------------------+
    4. | id | int(11) | PRIMARY KEY AUTO_INCREMENT |
    5. | nameFirst | varchar(255) | |
    6. | nameLast | varchar(255) | |
    7. | isActive | boolean | |
    8. +-------------+--------------+----------------------------+
    9. +-------------+--------------+----------------------------+
    10. | employee |
    11. +-------------+--------------+----------------------------+
    12. | id | int(11) | PRIMARY KEY AUTO_INCREMENT |
    13. | nameFirst | varchar(255) | |
    14. | nameLast | varchar(255) | |
    15. | salary | int(11) | |
    16. +-------------+--------------+----------------------------+
    17. +-------------+--------------+----------------------------+
    18. | student |
    19. +-------------+--------------+----------------------------+
    20. | id | int(11) | PRIMARY KEY AUTO_INCREMENT |
    21. | nameFirst | varchar(255) | |
    22. | nameLast | varchar(255) | |
    23. | faculty | varchar(255) | |
    24. +-------------+--------------+----------------------------+

    这种方式可以减少实体类中的代码重复。你可以根据需要在嵌入式类中使用尽可能多的列(或关系)。甚至可以在嵌入式类中嵌套嵌套列。