I'm trying to get posts by category like a CMS does.
For example query post by categorie A will include all posts attached to Categorie A and also post attached to child of Categorie A.
I really don't know how to build this query, so any help would be greatly appreciated :) .
Here is my entities:
@Tree("materialized-path")
export class Category {
@PrimaryGeneratedColumn()
id: number;
@Column()
title: string;
@ManyToMany((type) => Post, (post) => post.categories)
posts: Post[];
@Expose()
@TreeChildren()
children: Category[];
@Expose()
@TreeParent()
parent: Category;
}
export class Post{
@PrimaryGeneratedColumn()
id: number;
@Column()
title: string;
@ManyToMany((type) => Category, (category) => category.posts)
@JoinTable()
categories: Category[];
}
Followings SQL Query do the job(Example with category id 1)
SELECT * FROM post WHERE id IN (
SELECT postId FROM post_categories_category as postCat WHERE postCat.categoryId IN (
SELECT id FROM category WHERE category.mpath LIKE "1.%" OR category.mpath LIKE "%.1.%"
)
)
So the question is, how to convert this SQL query into a typeORM query ?