typeorm/docs/embedded-entities.md
Stian Jørgensrud e407d66d2c
docs: removed unused type variable from examples (#7928)
The documentation on [embedded entities](https://typeorm.io/#/embedded-entities) for some reason names a `type` identifier in the arrow syntax. Linters (and some TypeScript compilers) will show error such as: "'type' is declared but its value is never read." (ts) or "'type' is defined but never used." (eslint). Furthermore, `type` is a TypeScript keyword, so best to avoid it as variable name.

Apparently, it is not needed per the last comment in #2915 , so simply replacing it with `()`
2021-07-24 13:17:34 +05:00

4.2 KiB

Embedded Entities

There is an amazing way to reduce duplication in your app (using composition over inheritance) by using embedded columns. Embedded column is a column which accepts a class with its own columns and merges those columns into the current entity's database table. Example:

Let's say we have User, Employee and Student entities. All those entities have few things in common - first name and last name properties

import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";

@Entity()
export class User {
    
    @PrimaryGeneratedColumn()
    id: string;
    
    @Column()
    firstName: string;
    
    @Column()
    lastName: string;
    
    @Column()
    isActive: boolean;
    
}
import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";

@Entity()
export class Employee {
    
    @PrimaryGeneratedColumn()
    id: string;
    
    @Column()
    firstName: string;
    
    @Column()
    lastName: string;
    
    @Column()
    salary: string;
    
}
import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";

@Entity()
export class Student {
    
    @PrimaryGeneratedColumn()
    id: string;
    
    @Column()
    firstName: string;
    
    @Column()
    lastName: string;
    
    @Column()
    faculty: string;
    
}

What we can do is to reduce firstName and lastName duplication by creating a new class with those columns:

import {Column} from "typeorm";

export class Name {
    
    @Column()
    first: string;
    
    @Column()
    last: string;
    
}

Then you can "connect" those columns in your entities:

import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";
import {Name} from "./Name";

@Entity()
export class User {
    
    @PrimaryGeneratedColumn()
    id: string;
    
    @Column(() => Name)
    name: Name;
    
    @Column()
    isActive: boolean;
    
}
import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";
import {Name} from "./Name";

@Entity()
export class Employee {
    
    @PrimaryGeneratedColumn()
    id: string;
    
    @Column(() => Name)
    name: Name;
    
    @Column()
    salary: number;
    
}
import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";
import {Name} from "./Name";

@Entity()
export class Student {
    
    @PrimaryGeneratedColumn()
    id: string;
    
    @Column(() => Name)
    name: Name;
    
    @Column()
    faculty: string;
    
}

All columns defined in the Name entity will be merged into user, employee and student:

+-------------+--------------+----------------------------+
|                          user                           |
+-------------+--------------+----------------------------+
| id          | int(11)      | PRIMARY KEY AUTO_INCREMENT |
| nameFirst   | varchar(255) |                            |
| nameLast    | varchar(255) |                            |
| isActive    | boolean      |                            |
+-------------+--------------+----------------------------+

+-------------+--------------+----------------------------+
|                        employee                         |
+-------------+--------------+----------------------------+
| id          | int(11)      | PRIMARY KEY AUTO_INCREMENT |
| nameFirst   | varchar(255) |                            |
| nameLast    | varchar(255) |                            |
| salary      | int(11)      |                            |
+-------------+--------------+----------------------------+

+-------------+--------------+----------------------------+
|                         student                         |
+-------------+--------------+----------------------------+
| id          | int(11)      | PRIMARY KEY AUTO_INCREMENT |
| nameFirst   | varchar(255) |                            |
| nameLast    | varchar(255) |                            |
| faculty     | varchar(255) |                            |
+-------------+--------------+----------------------------+

This way code duplication in the entity classes is reduced. You can use as many columns (or relations) in embedded classes as you need. You even can have nested embedded columns inside embedded classes.