Hierarchical Data plugin for Bookshelf.js (WIP)
- http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
- https://github.com/cakephp/cakephp/blob/master/src/ORM/Behavior/TreeBehavior.php
- https://github.com/PhilWaldmann/openrecord/blob/master/lib/stores/sql/plugins/nested_set.js
Example
Example data from test-data.sql
:
mysql> source show-tree.sql;
+----+------------------------+
| id | name |
+----+------------------------+
| 1 | ELECTRONICS |
| 2 | TELEVISIONS |
| 3 | TUBE |
| 4 | LCD |
| 5 | PLASMA |
| 6 | PORTABLE ELECTRONICS |
| 7 | MP3 PLAYERS |
| 8 | FLASH |
| 9 | CD PLAYERS |
| 10 | 2 WAY RADIOS |
+----+------------------------+
Model definition
var Category = ORMModel;
You can configure the default configuration in the nestedSet
key:
fields: left: 'lft' right: 'rgt' parentId: 'parent_id'
Insert and delete node
// load the pluginORM; // wrap in transactionORM
findChildren
// get children of PORTABLE ELECTRONICSCategory
findPath
// get nodes leading to 3D TV (id 11)Category
setParent
// move TELEVISIONS (2) under PORTABLE ELECTRONICS (6);
Scope
// Configuring scope for a model instance with `section_id` = 2:{} scope: section_id: 2
Running tests
```mysql < test-data.sql ; npm test``
Todo
- transaction support
- findPath
- findChildren
- findTreeList
- formatTreeList
- removeFromTree
- setParent
- moveUp
- moveDown
- recover