The goal is simple. I have a table created by my self-hosted Strapi CMS, that I connect to with my NestJS backend. With this approach I want to keep working in the same Database, without making additional API calls to Strapi. The structure of the tables of interest looks like this:
@Entity({ name: 'posts' })
export class PostEntity {
@PrimaryGeneratedColumn()
id!: number;
???
category!: CategoryEntity;
}
@Entity({ name: 'categories' })
export class CategoryEntity {
@PrimaryGeneratedColumn()
id!: number;
???
posts!: PostEntity[];
}
@Entity('posts_category_links')
export class PostCategoryLinkEntity {
@PrimaryGeneratedColumn()
id!: number;
@Column({ name: 'post_order' })
postOrder!: number
???
categoryId!: number;
???
postId!: number;
}
The third table holds the IDs of the two Entities.
Conditions:
- DB synchronize is set to false, Strapi might break if tables are modifified outside of it
Goals:
-
Following code should return all Posts with the CategoryEntity in the category key:
this.postsRepository.find({relations: [‘category’]})