TypeORM defines entity relationships using the following decorators:
-
@OneToOne(): One-to-one relationship
- Use @JoinColumn() to specify the foreign key column
- Example: User and UserProfile
-
@OneToMany(): One-to-many relationship
- Defined on the "one" side, pointing to the "many" side
- Example: Author and Articles
-
@ManyToOne(): Many-to-one relationship
- Defined on the "many" side, pointing to the "one" side
- Example: Article and Author
-
@ManyToMany(): Many-to-many relationship
- Requires @JoinTable() to specify the junction table
- Example: Students and Courses
Cascade operations are configured through the cascade option, with possible values including "insert", "update", "remove", "soft-remove", "recover". For example: @OneToMany(() => Comment, comment => comment.post, { cascade: true })