当前位置:网站首页>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();
边栏推荐
- Selenium+msedgedriver+edge browser installation driver pit
- Find the highest value of the current element Z-index of the page
- Learn about various joins in SQL and their differences
- Sentinel 阿里开源流量防护组件
- 广告业务Bug复盘总结
- 20210306转载如何使TextEdit有背景图片
- 2020-9-23 use of QT timer qtimer class.
- apt命令报证书错误 Certificate verification failed: The certificate is NOT trusted
- Latex 报错 LaTeX Error: The font size command \normalsize is not defined问题解决
- CUDA用户对象
猜你喜欢
Win10:添加或者删除开机启动项,在开机启动项中添加在用户自定义的启动文件
apt命令报证书错误 Certificate verification failed: The certificate is NOT trusted
【张三学C语言之】—深入理解数据存储
Uploading attachments using Win32 in Web Automation
Apt command reports certificate error certificate verification failed: the certificate is not trusted
Pytest (2) mark function
Sparse array (nonlinear structure)
Log (common log framework)
Summary of advertisement business bug replay
ctf三计
随机推荐
自学table au
ZZQ的博客目录--更新于20210601
unittest.TextTestRunner不生成txt测试报告
最新CUDA环境配置(Win10 + CUDA 11.6 + VS2019)
Sentinel Alibaba open source traffic protection component
CUDA中的线程层次
Sentinel rules persist to Nacos
Android - Kotlin 下使用 Room 遇到 There are multiple good constructors and Room will ... 问题
FE - Eggjs 结合 Typeorm 出现连接不了数据库
pytest(2) mark功能
默认google浏览器打不开链接(点击超链接没有反应)
Hydration failed because the initial UI does not match what was rendered on the server. One of the reasons for the problem
ShardingSphere-JDBC篇
分布式事务 :可靠消息最终一致性方案
Automation - when Jenkins pipline executes the nodejs command, it prompts node: command not found
日志 - 7 - 记录一次丢失文件(A4纸)的重大失误
代码技巧——Controller参数注解@RequestParam
利用NVIDIA GPU将Minecraft场景渲染成真实场景
Data science [9]: SVD (2)
Golang -- map capacity expansion mechanism (including source code)