fremail / sequelize-nested-set

Library to store and manage nested set trees using Sequelize
MIT License
40 stars 10 forks source link

fetchTree just return 1 level child #13

Open mhf-ir opened 5 years ago

mhf-ir commented 5 years ago

I use fake data to generate about 10 root menu and about 100 menu.

I have simple test unit with fake data:

          const tree = await Menu.fetchTree(0, rootId, {
            raw: true,
          });

Sample output:

[ { id: 10,
          lft: 1,
          rgt: 20,
          level: 0,
          rootId: 10,
          title: 'Root Menu 9',
          description:
           'Hic in molestiae sed vel sit quia. Totam nulla adipisci porro. Ipsam quo eligendi aut voluptatum nihil. Nesciunt voluptates doloribus pariatur dolor repudiandae.',
          url: '/page/9',
          enable: 1,
          createdAt: 2019-10-05T09:49:57.000Z,
          updatedAt: 2019-10-05T09:49:58.000Z },
        { id: 16,
          lft: 2,
          rgt: 19,
          level: 1,
          rootId: 10,
          title: 'Child menu 4 : parent 10',
          description: 'et voluptas distinctio',
          url: '/child/page/4',
          enable: 1,
          createdAt: 2019-10-05T09:49:57.000Z,
          updatedAt: 2019-10-05T09:49:58.000Z },
        { id: 85,
          lft: 3,
          rgt: 8,
          level: 2,
          rootId: 10,
          title: 'Child menu 73 : parent 16',
          description: 'Voluptatem veniam enim culpa maiores velit ut.',
          url: '/child/page/73',
          enable: 1,
          createdAt: 2019-10-05T09:49:58.000Z,
          updatedAt: 2019-10-05T09:49:58.000Z },
        { id: 104,
          lft: 4,
          rgt: 7,
          level: 3,
          rootId: 10,
          title: 'Child menu 92 : parent 21',
          description: null,
          url: 'https://shaniya.net',
          enable: 1,
          createdAt: 2019-10-05T09:49:58.000Z,
          updatedAt: 2019-10-05T09:49:58.000Z },
        { id: 110,
          lft: 5,
          rgt: 6,
          level: 4,
          rootId: 10,
          title: 'Child menu 98 : parent 104',
          description: 'Aut itaque unde commodi nostrum quos temporibus rerum.',
          url: '/child/page/98',
          enable: 1,
          createdAt: 2019-10-05T09:49:58.000Z,
          updatedAt: 2019-10-05T09:49:58.000Z },
        { id: 23,
          lft: 9,
          rgt: 16,
          level: 2,
          rootId: 10,
          title: 'Child menu 11 : parent 16',
          description: null,
          url: '/child/page/11',
          enable: 1,
          createdAt: 2019-10-05T09:49:57.000Z,
          updatedAt: 2019-10-05T09:49:58.000Z },
        { id: 34,
          lft: 10,
          rgt: 13,
          level: 3,
          rootId: 10,
          title: 'Child menu 22 : parent 21',
          description: null,
          url: 'http://britney.net',
          enable: 1,
          createdAt: 2019-10-05T09:49:57.000Z,
          updatedAt: 2019-10-05T09:49:58.000Z },
        { id: 70,
          lft: 11,
          rgt: 12,
          level: 4,
          rootId: 10,
          title: 'Child menu 58 : parent 27',
          description: null,
          url: '/child/page/58',
          enable: 0,
          createdAt: 2019-10-05T09:49:58.000Z,
          updatedAt: 2019-10-05T09:49:58.000Z },
        { id: 27,
          lft: 14,
          rgt: 15,
          level: 3,
          rootId: 10,
          title: 'Child menu 15 : parent 23',
          description: 'velit',
          url: '/child/page/15',
          enable: 1,
          createdAt: 2019-10-05T09:49:57.000Z,
          updatedAt: 2019-10-05T09:49:58.000Z },
        { id: 21,
          lft: 17,
          rgt: 18,
          level: 2,
          rootId: 10,
          title: 'Child menu 9 : parent 16',
          description: null,
          url: 'https://hester.name',
          enable: 0,
          createdAt: 2019-10-05T09:49:57.000Z,
          updatedAt: 2019-10-05T09:49:58.000Z } ]

Desire output:

[ { id: 10,
          lft: 1,
          rgt: 20,
          level: 0,
          rootId: 10,
          title: 'Root Menu 9',
          description:
           'Hic in molestiae sed vel sit quia. Totam nulla adipisci porro. Ipsam quo eligendi aut voluptatum nihil. Nesciunt voluptates doloribus pariatur dolor repudiandae.',
          url: '/page/9',
          enable: 1,
          createdAt: 2019-10-05T09:49:57.000Z,
          updatedAt: 2019-10-05T09:49:58.000Z
         // here
        children: [
           // same as above
        ],
 } ]

My full table json dump: https://paste.ubuntu.com/p/67sy2dhJkr/

Where is children of children of children.

i'v try depth 10 or any number but array return me one step of child.

Use case VueJS Menu: https://jsfiddle.net/chrisvfritz/pnqzspoe

fremail commented 5 years ago

Hello @mhf-ir!

fetchTree function just fetches the all (or limited by depth param) nodes from database. It returns all levels as you can see in your posted example. But it doesn't iterate over the data to build the tree like you expected.

The behaviour of fetchTree function is very simple to keep it fast -- simple interface for fetching data from database. Just imagine a table with 10k rows. Each loop will slow down the code a lot.

Unfortunately, no one function in the library builds the tree. I'll appreciate for MR (a separate function to build the hierarchy tree from 1-level array).

mhf-ir commented 5 years ago

Tree algorithm in RDBM databases is limited use case not about 10K tree data. It's an antipattern for RDBMs databases. For many tree data correct you must use lazy load per node but during design many ting like Menu Category in typical use case this method will be helpful. Type orm implement it as official support problem of type orm is it's not support multi root and ordering nodes. https://github.com/typeorm/typeorm/blob/master/docs/tree-entities.md#working-with-tree-entities