如何在typeorm中加入树结构中的每个子实体
How to join every child entity in a tree structure in typeorm
我有一个类别的树结构。这些类别与其他表有关系。
import {Entity, Column, PrimaryGeneratedColumn, ManyToOne, OneToMany} from "typeorm";
@Entity()
export class Category {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@Column()
description: string;
@ManyToOne(type => Category, category => category.children)
parent: Category;
@OneToMany(type => Category, category => category.parent)
children: Category[];
@OneToMany(type => Site, site => site.category)
sites: Site[];
}
我获取树的代码:
console.log(await getConnection().getTreeRepository(Category).findTrees());
结果:
[
Category {
pk: 8,
id: '1C606380-020A-4CED-8EBA-AA1C9C16FCF2',
title: 'Category EXPLICIT',
createdBy: 'SAFETYMANUAL',
createdAt: 2021-07-27T19:57:03.213Z,
hidden: false,
orderValue: 0,
deletionDate: null,
childCategories: [ [Category] ]
}
]
我想用关系加载整棵树,在本例中是用站点数组。将 eager 设置为 true 不会改变任何内容。我只取回没有站点数组的树。有没有可能加入树结构?
我实施了一个启用树加载关系的拉取请求:
const treeCategoriesWithRelations = await repository.findTrees({ relations: ["sites"] });
// automatically joins the sites relation
我有一个类别的树结构。这些类别与其他表有关系。
import {Entity, Column, PrimaryGeneratedColumn, ManyToOne, OneToMany} from "typeorm";
@Entity()
export class Category {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@Column()
description: string;
@ManyToOne(type => Category, category => category.children)
parent: Category;
@OneToMany(type => Category, category => category.parent)
children: Category[];
@OneToMany(type => Site, site => site.category)
sites: Site[];
}
我获取树的代码:
console.log(await getConnection().getTreeRepository(Category).findTrees());
结果:
[
Category {
pk: 8,
id: '1C606380-020A-4CED-8EBA-AA1C9C16FCF2',
title: 'Category EXPLICIT',
createdBy: 'SAFETYMANUAL',
createdAt: 2021-07-27T19:57:03.213Z,
hidden: false,
orderValue: 0,
deletionDate: null,
childCategories: [ [Category] ]
}
]
我想用关系加载整棵树,在本例中是用站点数组。将 eager 设置为 true 不会改变任何内容。我只取回没有站点数组的树。有没有可能加入树结构?
我实施了一个启用树加载关系的拉取请求:
const treeCategoriesWithRelations = await repository.findTrees({ relations: ["sites"] });
// automatically joins the sites relation