Ian - 11.12.2020
TypeORM
Decorators
TOC
• Entity

• Column

• Relation

• Subscriber

• Others
Entity
Entity
1.1. Entity
• name:
• engine: 

• database: 

• schema:
• synchronize: 

• orderBy:
Entity
1.1. Entity
• mysql schema database


• table < schema < database
(oracle db)

• schema
Entity
1.2. SQL view ?
•
• , view 

• 1 view 

• 

•
Entity
1.3. ViewEntity
• @ViewColumn


• name: 

• database: 

• schema: 

• expression: sql 

queryBuilder
Entity
1.3. ViewEntity -
• ,
ViewEntity view
Entity
1.4. entity column
• id, title, description DRY ?

• 3
Entity
1.4. entity column - Concrete table inheritance
•
Entity
1.4. entity column - Single table inheritance
• 

• Content 

• @TableInheritance, @ChildEntity
Entity
1.4. entity column - Embedded entities
• , 

• nameFirst, nameLast
Entity
1.4. entity column -
• Concrete table inheritance: 

• Single table inheritance: @TableInheritance, @ChildEntity .
@TableInheritance 

• Embedded entities: 

• id, createdAt, updatedAt
Concrete table inheritance
Column
Column
2.1. Column
• type: 

• length: 

• onUpdate: cascading ON UPDATE 

• nullable: false NOT NULL 

• default: 

• unique: false constraint 

• enum: enum 

• enumName: enum 

• transformer: json boolean integer
Type
Enum
Transformer
Column
2.2. ID, Date - ID
• @PrimaryColumn: pk 

• @PrimaryGeneratedColumn:
AUTO_INCREMENT id uuid


• @Generated: pk uuid
PrimaryGeneratedColumn
Generated
Column
2.2. ID, Date - Date
• DATETIME 

• @CreateDateColumn: 

• @UpdateDateColumn: 

• @DeleteDateColumn: soft delete
CreateDateColumn
UpdateDateColumn
DeleteDateColumn
Column
2.3. Soft delete ?
• , deletedAt 

• UPDATE 

• 

• JOIN
•
Relation
Relation
3.1. 1:1/1:N/M:N
• 1:1 

• ,


• M:N 

• ,
Relation
3.1. 1:1/1:N/M:N - 1:1 - Entity
• @OneToOne uni-directional, bi-
directional 

• @JoinColumn 

•
Relation
3.1. 1:1/1:N/M:N - 1:1 - SQL desc
• DESC profile; DESC user;
Relation
3.1. 1:1/1:N/M:N - 1:1 - Relation query
•
• 2
User Profile
Relation
3.1. 1:1/1:N/M:N - 1:N - Entity
• User Photo 1:N (User has many Photos, Photo belongs to User)

• @JoinColumn 

• @OneToMany @ManyToOne 

• @ManyToOne @OneToMany
Relation
3.1. 1:1/1:N/M:N - 1:N - SQL desc
• DESC photo; DESC user;
Relation
3.1. 1:1/1:N/M:N - 1:N - Relation query
•
• 2 

• photos
Relation
3.1. 1:1/1:N/M:N - M:N - Entity
• junction @JoinTable


• @ManyToMany uni-directional,
bi-directional
Relation
3.1. 1:1/1:N/M:N - M:N - SQL desc
• DESC category; DESC question; DESC question_categories_category;
Relation
3.1. 1:1/1:N/M:N - M:N - Relation query
•
• 2 

• categories.questions
Relation
3.2. Self join ?
• 1 - 

• : < < 

• : < < 

• : / / < / < / 

• 4
Relation
3.2. 1:1/M:N - Adjacency list
• @ManyToOne, @OneToMany 

• JOIN (4 )
Relation
3.2. 1:1/M:N - Nested set/Materialized path/Closure table
• @Tree, @TreeChildren, @TreeParent


• Nested set: ↓ / ↑

• Materialized path (aka Path enumeration): 



• Closure table: ↑ / ↑
nested-set | 

materialized-path | 

closure-table
Relation
3.2. 1:1/M:N -
• @Tree* .
adjacency list
@Tree*
Relation
3.3. Join* - JoinColumn
• 

• . categoryId 

• @ManyToOne , @OneToOne
Relation
3.3. Join* - JoinTable
• N:M junction 

• junction source
Relation
3.4. RelationId
• relation id 

• entity
Subscriber
Subscriber
4.1. Subscriber
• DB CRUD 

• @AfterLoad - @AfterInsert/@BeforeInsert - @AfterUpdate/@BeforeUpdate -
@AfterRemove/@BeforeRemove 

• ORM connector logging SQL 

•
Subscriber
4.1. Subscriber
•
Others
Others
5.1. Index ?
• .. 😭
Others
5.1. Index ?
• 

• 1 

• / , > 

• 

• WHERE 

• 

• JOIN 

• /
Others
5.2. Index
• 

•
Others
5.3. Unique
• 

• Index
Others
5.4. Check
• /
Others
5.5. Transaction ?
• , 

• DB 

• 

• 

• 

• 

• transaction locking 

• 3
Others
5.6. Transaction - Transaction method
• 1 

• READ UNCOMMITTED

• READ COMMITTED

• REPEATABLE READ

• SERIALIZABLE
Others
5.6. Transaction - Decorators
• @Transaction, @TransactionManager, @TransactionRepository 

• 2
Others
5.6. Transaction - Query runner
• 

• startTransaction: 

• commitTransaction: 

• rollbackTransaction:
EOF,
Ian - 10.12.2020

Typeorm decorators