Eggjs -typeorm 之 TreeEntity 实战

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();
发布了406 篇原创文章 · 获赞 159 · 访问量 100万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 像素格子 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览