嵌入式实体
通过使用embedded columns,可以减少应用程序中的重复(使用组合而不是继承)。
嵌入列是一个列,它接受具有自己列的类,并将这些列合并到当前实体的数据库表中。
例如:
假设我们有UserEmployeeStudent实体。
这些属性都有少量的共同点,first namelast name属性。
1
import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";
2
3
@Entity()
4
export class User {
5
6
@PrimaryGeneratedColumn()
7
id: string;
8
9
@Column()
10
firstName: string;
11
12
@Column()
13
lastName: string;
14
15
@Column()
16
isActive: boolean;
17
18
}
Copied!
1
import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";
2
3
@Entity()
4
export class Employee {
5
6
@PrimaryGeneratedColumn()
7
id: string;
8
9
@Column()
10
firstName: string;
11
12
@Column()
13
lastName: string;
14
15
@Column()
16
salary: string;
17
18
}
Copied!
1
import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";
2
3
@Entity()
4
export class Student {
5
6
@PrimaryGeneratedColumn()
7
id: string;
8
9
@Column()
10
firstName: string;
11
12
@Column()
13
lastName: string;
14
15
@Column()
16
faculty: string;
17
18
}
Copied!
我们可以做的是通过创建一个包含firstNamelastName的新类:
1
import {Entity, Column} from "typeorm";
2
3
export class Name {
4
5
@Column()
6
first: string;
7
8
@Column()
9
last: string;
10
11
}
Copied!
然后"connect"实体中的这些列:
1
import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";
2
import {Name} from "./Name";
3
4
@Entity()
5
export class User {
6
7
@PrimaryGeneratedColumn()
8
id: string;
9
10
@Column(type => Name)
11
name: Name;
12
13
@Column()
14
isActive: boolean;
15
16
}
Copied!
1
import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";
2
import {Name} from "./Name";
3
4
@Entity()
5
export class Employee {
6
7
@PrimaryGeneratedColumn()
8
id: string;
9
10
@Column(type => Name)
11
name: Name;
12
13
@Column()
14
salary: number;
15
16
}
Copied!
1
import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";
2
import {Name} from "./Name";
3
4
@Entity()
5
export class Student {
6
7
@PrimaryGeneratedColumn()
8
id: string;
9
10
@Column(type => Name)
11
name: Name;
12
13
@Column()
14
faculty: string;
15
16
}
Copied!
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
+-------------+--------------+----------------------------+
11
| employee |
12
+-------------+--------------+----------------------------+
13
| id | int(11) | PRIMARY KEY AUTO_INCREMENT |
14
| nameFirst | varchar(255) | |
15
| nameLast | varchar(255) | |
16
| salary | int(11) | |
17
+-------------+--------------+----------------------------+
18
19
+-------------+--------------+----------------------------+
20
| student |
21
+-------------+--------------+----------------------------+
22
| id | int(11) | PRIMARY KEY AUTO_INCREMENT |
23
| nameFirst | varchar(255) | |
24
| nameLast | varchar(255) | |
25
| faculty | varchar(255) | |
26
+-------------+--------------+----------------------------+
Copied!
这种方式可以减少实体类中的代码重复。 你可以根据需要在嵌入式类中使用尽可能多的列(或关系)。 甚至可以在嵌入式类中嵌套嵌套列。
Last modified 1mo ago
Copy link