当前位置:网站首页>Eggjs -typeorm treeenity practice
Eggjs -typeorm treeenity practice
2022-07-02 06:39:00 【Xiao Ming Yuan】
Tree - Entitiy
Tutorial address
https://typeorm.io/#/tree-entities
Case study
scene : Grouping design 、 Tree design 、 Menu design
Tree structure , In parentheses is stored ID
- The north China (11)
- Henan (14)
- zhengzhou (17)
- Jiaozuo (18)
- hebei (15)
- taiyuan (19)
- south China (12)
- southwest (13)
- sichuan (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. Add a level one node
Code
const userGroup = new UserGroup()
userGroup.groupName = ' The north China '
const saveResult = await userGroupRep.save(userGroup)
data
{
"groupName": " The north China ",
"id": 11,
"status": 0,
"createDate": "2019-04-20T02:44:48.799Z",
"updateDate": "2019-04-20T02:44:48.799Z"
}
2. Add child nodes
add to sichuan
Code
const userGroupChild = new UserGroup()
userGroupChild.groupName = ' sichuan '
const parent = await userGroupRep.findOne(13);
if(parent){
userGroupChild.parent = parent
}
await userGroupRep.save(userGroupChild)
The data format is consistent with the above
{
"code": 0,
"msg": "ok",
"data": [
{
"id": 11,
"status": 0,
"createDate": "2019-04-20T02:44:48.799Z",
"updateDate": "2019-04-20T02:44:48.799Z",
"groupName": " The north China ",
"children": [
{
"id": 14,
"status": 0,
"createDate": "2019-04-20T03:02:58.551Z",
"updateDate": "2019-04-20T03:02:58.551Z",
"groupName": " Henan ",
"children": [
{
"id": 17,
"status": 0,
"createDate": "2019-04-20T03:08:24.459Z",
"updateDate": "2019-04-20T03:08:24.459Z",
"groupName": " zhengzhou ",
"children": []
},
{
"id": 18,
"status": 0,
"createDate": "2019-04-20T03:08:39.077Z",
"updateDate": "2019-04-20T03:08:39.077Z",
"groupName": " Jiaozuo ",
"children": []
}
]
},
{
"id": 15,
"status": 0,
"createDate": "2019-04-20T03:04:28.248Z",
"updateDate": "2019-04-20T03:04:28.248Z",
"groupName": " hebei ",
"children": []
}
]
},
{
"id": 12,
"status": 0,
"createDate": "2019-04-20T02:58:51.236Z",
"updateDate": "2019-04-20T02:58:51.236Z",
"groupName": " south China ",
"children": []
},
{
"id": 13,
"status": 0,
"createDate": "2019-04-20T02:59:07.329Z",
"updateDate": "2019-04-20T02:59:07.329Z",
"groupName": " southwest ",
"children": [
{
"id": 16,
"status": 0,
"createDate": "2019-04-20T03:05:34.187Z",
"updateDate": "2019-04-20T03:05:34.187Z",
"groupName": " sichuan ",
"children": []
}
]
}
]
}
3. All data is displayed nested
Code
await userGroupRep.findTrees()
data
{
"code": 0,
"msg": "ok",
"data": [
{
"id": 11,
"status": 0,
"createDate": "2019-04-20T02:44:48.799Z",
"updateDate": "2019-04-20T02:44:48.799Z",
"groupName": " The north China ",
"children": [
{
"id": 14,
"status": 0,
"createDate": "2019-04-20T03:02:58.551Z",
"updateDate": "2019-04-20T03:02:58.551Z",
"groupName": " Henan ",
"children": [
{
"id": 17,
"status": 0,
"createDate": "2019-04-20T03:08:24.459Z",
"updateDate": "2019-04-20T03:08:24.459Z",
"groupName": " zhengzhou ",
"children": []
},
{
"id": 18,
"status": 0,
"createDate": "2019-04-20T03:08:39.077Z",
"updateDate": "2019-04-20T03:08:39.077Z",
"groupName": " Jiaozuo ",
"children": []
}
]
},
{
"id": 15,
"status": 0,
"createDate": "2019-04-20T03:04:28.248Z",
"updateDate": "2019-04-20T03:04:28.248Z",
"groupName": " hebei ",
"children": []
}
]
},
{
"id": 12,
"status": 0,
"createDate": "2019-04-20T02:58:51.236Z",
"updateDate": "2019-04-20T02:58:51.236Z",
"groupName": " south China ",
"children": []
},
{
"id": 13,
"status": 0,
"createDate": "2019-04-20T02:59:07.329Z",
"updateDate": "2019-04-20T02:59:07.329Z",
"groupName": " southwest ",
"children": [
{
"id": 16,
"status": 0,
"createDate": "2019-04-20T03:05:34.187Z",
"updateDate": "2019-04-20T03:05:34.187Z",
"groupName": " sichuan ",
"children": []
}
]
}
]
}
4. Show only the root directory
Code
await userGroupRep.findRoots()
data
{
"code": 0,
"msg": "ok",
"data": [
{
"id": 11,
"status": 0,
"createDate": "2019-04-20T02:44:48.799Z",
"updateDate": "2019-04-20T02:44:48.799Z",
"groupName": " The north China "
},
{
"id": 12,
"status": 0,
"createDate": "2019-04-20T02:58:51.236Z",
"updateDate": "2019-04-20T02:58:51.236Z",
"groupName": " south China "
},
{
"id": 13,
"status": 0,
"createDate": "2019-04-20T02:59:07.329Z",
"updateDate": "2019-04-20T02:59:07.329Z",
"groupName": " southwest "
}
]
}
5. Appoint Parent All child nodes of ( No nesting )
Code
const groups = await userGroupRep.findDescendants(parent);
data
{
"code": 0,
"msg": "ok",
"data": [
{
"id": 11,
"status": 0,
"createDate": "2019-04-20T02:44:48.799Z",
"updateDate": "2019-04-20T02:44:48.799Z",
"groupName": " The north China "
},
{
"id": 14,
"status": 0,
"createDate": "2019-04-20T03:02:58.551Z",
"updateDate": "2019-04-20T03:02:58.551Z",
"groupName": " Henan "
},
{
"id": 15,
"status": 0,
"createDate": "2019-04-20T03:04:28.248Z",
"updateDate": "2019-04-20T03:04:28.248Z",
"groupName": " hebei "
},
{
"id": 17,
"status": 0,
"createDate": "2019-04-20T03:08:24.459Z",
"updateDate": "2019-04-20T03:08:24.459Z",
"groupName": " zhengzhou "
},
{
"id": 18,
"status": 0,
"createDate": "2019-04-20T03:08:39.077Z",
"updateDate": "2019-04-20T03:08:39.077Z",
"groupName": " Jiaozuo "
}
]
}
6. Appoint Parent All child nodes of ( nesting )
Code
await userGroupRep.findDescendantsTree(parent);
data
{
"code": 0,
"msg": "ok",
"data": {
"id": 11,
"status": 0,
"createDate": "2019-04-20T02:44:48.799Z",
"updateDate": "2019-04-20T02:44:48.799Z",
"groupName": " The north China ",
"children": [
{
"id": 14,
"status": 0,
"createDate": "2019-04-20T03:02:58.551Z",
"updateDate": "2019-04-20T03:02:58.551Z",
"groupName": " Henan ",
"children": [
{
"id": 17,
"status": 0,
"createDate": "2019-04-20T03:08:24.459Z",
"updateDate": "2019-04-20T03:08:24.459Z",
"groupName": " zhengzhou ",
"children": []
},
{
"id": 18,
"status": 0,
"createDate": "2019-04-20T03:08:39.077Z",
"updateDate": "2019-04-20T03:08:39.077Z",
"groupName": " Jiaozuo ",
"children": []
}
]
},
{
"id": 15,
"status": 0,
"createDate": "2019-04-20T03:04:28.248Z",
"updateDate": "2019-04-20T03:04:28.248Z",
"groupName": " hebei ",
"children": []
}
]
}
}
7. Appoint Parent Number of child nodes of
Code
const groups = await userGroupRep.countDescendants(parent);
data
{
"code": 0,
"msg": "ok",
"data": 5
}
8. Appoint Parent Operations related to the parent node of
No nested data :userGroupRep.findAncestors()
Nested data :userGroupRep.findAncestorsTree()
Number of parent nodes :userGroupRep.countAncestors()
9. Specify the operation related to each node condition
const childrens = await repository
.createDescendantsQueryBuilder("category", "categoryClosure", parentCategory)
.andWhere("category.type = 'secondary'")
.getMany();
边栏推荐
- Warp shuffle in CUDA
- 一口气说出 6 种实现延时消息的方案
- 2020-9-23 use of QT timer qtimer class.
- 20210306 reprint how to make TextEdit have background pictures
- Sentinel规则持久化到Nacos
- Pytest (1) case collection rules
- Functions of tensorrt
- Self cultivation of programmers - Reflection on job hunting
- selenium备忘录:selenium\webdriver\remote\remote_connection.py:374: ResourceWarning: unclosed<xxxx>解决办法
- Win10网络图标消失,网络图标变成灰色,打开网络设置闪退等问题解决
猜你喜欢

Latex参考文献引用失败 报错 LaTeX Warning: Citation “*****” on page y undefined on input line *

Sparse array (nonlinear structure)

Redis——缓存击穿、穿透、雪崩

Pytest (2) mark function

Introduce two automatic code generators to help improve work efficiency

Summary of advertisement business bug replay

Sentinel 阿里开源流量防护组件

AWD学习

Blog directory of zzq -- updated on 20210601

Distributed transactions: the final consistency scheme of reliable messages
随机推荐
20210306 reprint how to make TextEdit have background pictures
Codeforces Round #797 (Div. 3) A—E
CUDA中的函数执行空间说明符
NodeJs - Express 中间件修改 Header: TypeError [ERR_INVALID_CHAR]: Invalid character in header content
Summary of WLAN related knowledge points
Data science [9]: SVD (2)
CUDA用户对象
There is no way to drag the win10 desktop icon (you can select it, open it, delete it, create it, etc., but you can't drag it)
底层机制Mvcc
Redis - grande question clé
Redis——大Key問題
Sentinel Alibaba open source traffic protection component
Solution to the black screen of win computer screenshot
【文献阅读与想法笔记13】 Unprocessing Images for Learned Raw Denoising
Data science [viii]: SVD (I)
Win10:添加或者删除开机启动项,在开机启动项中添加在用户自定义的启动文件
提高用户体验 防御性编程
CUDA中的异步数据拷贝
(the 100th blog) written at the end of the second year of doctor's degree -20200818
No process runs when querying GPU, but the video memory is occupied