当前位置:网站首页>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();
边栏推荐
- 20201002 VS 2019 QT5.14 开发的程序打包
- Asynchronous data copy in CUDA
- Latex 编译报错 I found no \bibstyle & \bibdata & \citation command
- Summary of advertisement business bug replay
- 10 erreurs classiques de MySQL
- Redis——Cluster数据分布算法&哈希槽
- CUDA中的异步数据拷贝
- 计算属性普通函数写法 和 set get 写法
- JS modification element attribute flipping commonly used in selenium's Web Automation
- TensorRT的数据格式定义详解
猜你喜欢

Find the highest value of the current element Z-index of the page
![[literature reading and thought notes 13] unprocessing images for learned raw denoising](/img/a5/ed26a90b3edd75a37b2e5164f6b7d2.png)
[literature reading and thought notes 13] unprocessing images for learned raw denoising

Detailed definition of tensorrt data format

PgSQL学习笔记

QQ email cannot receive the email sent by Jenkins using email extension after construction (timestamp or auth...)

VSCODE 安装LATEX环境,参数配置,常见问题解决

Solution to the black screen of win computer screenshot

默认google浏览器打不开链接(点击超链接没有反应)

apt命令报证书错误 Certificate verification failed: The certificate is NOT trusted

unittest.TextTestRunner不生成txt测试报告
随机推荐
ZZQ的博客目录--更新于20210601
Sentinel 阿里开源流量防护组件
MySql索引
Sentinel rules persist to Nacos
web自动化切换窗口时报错“list“ object is not callable
FE - weex 开发 之 使用 weex-ui 组件与配置使用
Introduce two automatic code generators to help improve work efficiency
实现strStr() II
Golang -- map capacity expansion mechanism (including source code)
[daily question 1] write a function to judge whether a string is the string after the rotation of another string.
CUDA中的Warp matrix functions
华为MindSpore开源实习机试题
FE - Weex 使用简单封装数据加载插件为全局加载方法
CUDA中内置的Vector类型和变量
由于不正常断电导致的unexpected inconsistency;RUN fsck MANUALLY问题已解决
(第一百篇BLOG)写于博士二年级结束-20200818
Apt command reports certificate error certificate verification failed: the certificate is not trusted
20201025 Visual Studio2019 QT5.14 信号和槽功能的使用
Summary of WLAN related knowledge points
Data science [9]: SVD (2)