当前位置:网站首页>Eggjs -typeorm 之 TreeEntity 实战
Eggjs -typeorm 之 TreeEntity 实战
2022-07-02 06:21:00 【原小明】
Tree - Entitiy
教程地址
https://typeorm.io/#/tree-entities
案例
场景:分组设计、树设计、菜单设计
树结构 , 括号里是存储的 ID
- 华北(11)
- 河南(14)
- 郑州(17)
- 焦作(18)
- 河北(15)
- 太原(19)
- 华南(12)
- 西南(13)
- 四川(16)
UserGroup
@Entity()
@Tree("closure-table")
export class UserGroup {
@PrimaryGeneratedColumn()
id: number;
@Column({ default: 0 })
status: number;
@CreateDateColumn()
createDate: Date;
@UpdateDateColumn()
updateDate: Date;
@Column()
groupName: string;
@TreeChildren()
children: UserGroup[];
@TreeParent()
parent: UserGroup;
}
UserGroupRepository
@EntityRepository(UserGroup)
export default class UserGroupRepository extends TreeRepository<UserGroup> {
}
1.添加一级节点
代码
const userGroup = new UserGroup()
userGroup.groupName = '华北'
const saveResult = await userGroupRep.save(userGroup)
数据
{
"groupName": "华北",
"id": 11,
"status": 0,
"createDate": "2019-04-20T02:44:48.799Z",
"updateDate": "2019-04-20T02:44:48.799Z"
}
2.添加子节点
添加 四川
代码
const userGroupChild = new UserGroup()
userGroupChild.groupName = '四川'
const parent = await userGroupRep.findOne(13);
if(parent){
userGroupChild.parent = parent
}
await userGroupRep.save(userGroupChild)
数据格式与上面的一致
{
"code": 0,
"msg": "ok",
"data": [
{
"id": 11,
"status": 0,
"createDate": "2019-04-20T02:44:48.799Z",
"updateDate": "2019-04-20T02:44:48.799Z",
"groupName": "华北",
"children": [
{
"id": 14,
"status": 0,
"createDate": "2019-04-20T03:02:58.551Z",
"updateDate": "2019-04-20T03:02:58.551Z",
"groupName": "河南",
"children": [
{
"id": 17,
"status": 0,
"createDate": "2019-04-20T03:08:24.459Z",
"updateDate": "2019-04-20T03:08:24.459Z",
"groupName": "郑州",
"children": []
},
{
"id": 18,
"status": 0,
"createDate": "2019-04-20T03:08:39.077Z",
"updateDate": "2019-04-20T03:08:39.077Z",
"groupName": "焦作",
"children": []
}
]
},
{
"id": 15,
"status": 0,
"createDate": "2019-04-20T03:04:28.248Z",
"updateDate": "2019-04-20T03:04:28.248Z",
"groupName": "河北",
"children": []
}
]
},
{
"id": 12,
"status": 0,
"createDate": "2019-04-20T02:58:51.236Z",
"updateDate": "2019-04-20T02:58:51.236Z",
"groupName": "华南",
"children": []
},
{
"id": 13,
"status": 0,
"createDate": "2019-04-20T02:59:07.329Z",
"updateDate": "2019-04-20T02:59:07.329Z",
"groupName": "西南",
"children": [
{
"id": 16,
"status": 0,
"createDate": "2019-04-20T03:05:34.187Z",
"updateDate": "2019-04-20T03:05:34.187Z",
"groupName": "四川",
"children": []
}
]
}
]
}
3.所有数据嵌套显示
代码
await userGroupRep.findTrees()
数据
{
"code": 0,
"msg": "ok",
"data": [
{
"id": 11,
"status": 0,
"createDate": "2019-04-20T02:44:48.799Z",
"updateDate": "2019-04-20T02:44:48.799Z",
"groupName": "华北",
"children": [
{
"id": 14,
"status": 0,
"createDate": "2019-04-20T03:02:58.551Z",
"updateDate": "2019-04-20T03:02:58.551Z",
"groupName": "河南",
"children": [
{
"id": 17,
"status": 0,
"createDate": "2019-04-20T03:08:24.459Z",
"updateDate": "2019-04-20T03:08:24.459Z",
"groupName": "郑州",
"children": []
},
{
"id": 18,
"status": 0,
"createDate": "2019-04-20T03:08:39.077Z",
"updateDate": "2019-04-20T03:08:39.077Z",
"groupName": "焦作",
"children": []
}
]
},
{
"id": 15,
"status": 0,
"createDate": "2019-04-20T03:04:28.248Z",
"updateDate": "2019-04-20T03:04:28.248Z",
"groupName": "河北",
"children": []
}
]
},
{
"id": 12,
"status": 0,
"createDate": "2019-04-20T02:58:51.236Z",
"updateDate": "2019-04-20T02:58:51.236Z",
"groupName": "华南",
"children": []
},
{
"id": 13,
"status": 0,
"createDate": "2019-04-20T02:59:07.329Z",
"updateDate": "2019-04-20T02:59:07.329Z",
"groupName": "西南",
"children": [
{
"id": 16,
"status": 0,
"createDate": "2019-04-20T03:05:34.187Z",
"updateDate": "2019-04-20T03:05:34.187Z",
"groupName": "四川",
"children": []
}
]
}
]
}
4.只显示根级目录
代码
await userGroupRep.findRoots()
数据
{
"code": 0,
"msg": "ok",
"data": [
{
"id": 11,
"status": 0,
"createDate": "2019-04-20T02:44:48.799Z",
"updateDate": "2019-04-20T02:44:48.799Z",
"groupName": "华北"
},
{
"id": 12,
"status": 0,
"createDate": "2019-04-20T02:58:51.236Z",
"updateDate": "2019-04-20T02:58:51.236Z",
"groupName": "华南"
},
{
"id": 13,
"status": 0,
"createDate": "2019-04-20T02:59:07.329Z",
"updateDate": "2019-04-20T02:59:07.329Z",
"groupName": "西南"
}
]
}
5.指定 Parent 的所有子节点(无嵌套)
代码
const groups = await userGroupRep.findDescendants(parent);
数据
{
"code": 0,
"msg": "ok",
"data": [
{
"id": 11,
"status": 0,
"createDate": "2019-04-20T02:44:48.799Z",
"updateDate": "2019-04-20T02:44:48.799Z",
"groupName": "华北"
},
{
"id": 14,
"status": 0,
"createDate": "2019-04-20T03:02:58.551Z",
"updateDate": "2019-04-20T03:02:58.551Z",
"groupName": "河南"
},
{
"id": 15,
"status": 0,
"createDate": "2019-04-20T03:04:28.248Z",
"updateDate": "2019-04-20T03:04:28.248Z",
"groupName": "河北"
},
{
"id": 17,
"status": 0,
"createDate": "2019-04-20T03:08:24.459Z",
"updateDate": "2019-04-20T03:08:24.459Z",
"groupName": "郑州"
},
{
"id": 18,
"status": 0,
"createDate": "2019-04-20T03:08:39.077Z",
"updateDate": "2019-04-20T03:08:39.077Z",
"groupName": "焦作"
}
]
}
6.指定 Parent 的所有子节点(嵌套)
代码
await userGroupRep.findDescendantsTree(parent);
数据
{
"code": 0,
"msg": "ok",
"data": {
"id": 11,
"status": 0,
"createDate": "2019-04-20T02:44:48.799Z",
"updateDate": "2019-04-20T02:44:48.799Z",
"groupName": "华北",
"children": [
{
"id": 14,
"status": 0,
"createDate": "2019-04-20T03:02:58.551Z",
"updateDate": "2019-04-20T03:02:58.551Z",
"groupName": "河南",
"children": [
{
"id": 17,
"status": 0,
"createDate": "2019-04-20T03:08:24.459Z",
"updateDate": "2019-04-20T03:08:24.459Z",
"groupName": "郑州",
"children": []
},
{
"id": 18,
"status": 0,
"createDate": "2019-04-20T03:08:39.077Z",
"updateDate": "2019-04-20T03:08:39.077Z",
"groupName": "焦作",
"children": []
}
]
},
{
"id": 15,
"status": 0,
"createDate": "2019-04-20T03:04:28.248Z",
"updateDate": "2019-04-20T03:04:28.248Z",
"groupName": "河北",
"children": []
}
]
}
}
7.指定 Parent 的子节点数量
代码
const groups = await userGroupRep.countDescendants(parent);
数据
{
"code": 0,
"msg": "ok",
"data": 5
}
8.指定 Parent 的父节点相关操作
无嵌套数据:userGroupRep.findAncestors()
嵌套数据:userGroupRep.findAncestorsTree()
父节点数量:userGroupRep.countAncestors()
9.指定每个节点条件相关操作
const childrens = await repository
.createDescendantsQueryBuilder("category", "categoryClosure", parentCategory)
.andWhere("category.type = 'secondary'")
.getMany();
边栏推荐
- Redis——缓存击穿、穿透、雪崩
- The Chinese word segmentation task is realized by using traditional methods (n-gram, HMM, etc.), neural network methods (CNN, LSTM, etc.) and pre training methods (Bert, etc.)
- Redis - cluster data distribution algorithm & hash slot
- Sparse array (nonlinear structure)
- Redis——热点key问题
- Is there a really free applet?
- Find the highest value of the current element Z-index of the page
- LeetCode 39. Combined sum
- CUDA中内置的Vector类型和变量
- LeetCode 47. 全排列 II
猜你喜欢

栈(线性结构)

自学table au

The difference between session and cookies

递归(迷宫问题、8皇后问题)

Contest3145 - the 37th game of 2021 freshman individual training match_ H: Eat fish

IPv6 experiment and summary

Contest3147 - game 38 of 2021 Freshmen's personal training match_ E: Listen to songs and know music

Find the highest value of the current element Z-index of the page

Learn about various joins in SQL and their differences

Redis - hot key issues
随机推荐
【张三学C语言之】—深入理解数据存储
栈(线性结构)
数据科学【九】:SVD(二)
队列(线性结构)
In depth understanding of JUC concurrency (I) what is JUC
深入学习JVM底层(四):类文件结构
实现strStr() II
I/o multiplexing & event driven yyds dry inventory
注解和反射详解以及运用
Top 10 classic MySQL errors
LeetCode 27. 移除元素
Invalid operation: Load into table ‘sources_orderdata‘ failed. Check ‘stl_load_errors‘ system table
CUDA中内置的Vector类型和变量
Bgp Routing preference Rules and notice Principles
LeetCode 40. 组合总和 II
亚马逊aws数据湖工作之坑1
深入学习JVM底层(五):类加载机制
Amazon AWS data Lake Work Pit 1
LeetCode 40. Combined sum II
Name six schemes to realize delayed messages at one go