Is there a way to implement 3 way many-to-many relationship in typeOrm
an example of a 3 way many-to-many relationship would be like
i found a solution for that; it is actually written in typorm's documentation https://github.com/typeorm/typeorm/blob/master/docs/many-to-many-relations.md#many-to-many-relations-with-custom-properties
many-to-many relations with custom properties In case you need to have additional properties to your many-to-many relationship you have to create a new entity yourself. For example if you would like entities Post and Category to have a many-to-many relationship with additional order column, you need to create entity PostToCategory with two ManyToOne relations pointing in both directions and custom columns in it:
import { Entity, Column, ManyToOne, PrimaryGeneratedColumn } from "typeorm";
import { Post } from "./post";
import { Category } from "./category";
@Entity()
export class PostToCategory {
@PrimaryGeneratedColumn()
public postToCategoryId!: number;
@Column()
public postId!: number;
@Column()
public categoryId!: number;
@Column()
public order!: number;
@ManyToOne(type => Post, post => post.postToCategories)
public post!: Post;
@ManyToOne(type => Category, category => category.postToCategories)
public category!: Category;
}
Additionally you will have to add a relationship like the following to Post and Category:
// category.ts
...
@OneToMany(type => PostToCategory, postToCategory => postToCategory.category)
public postToCategories!: PostToCategory[];
// post.ts
...
@OneToMany(type => PostToCategory, postToCategory => postToCategory.post)
public postToCategories!: PostToCategory[];