Nested Sets Behavior for Sequelize
Behavior for storing and managing nested sets
Installation
npm install --save sequelize-nested-sets
Configuring
The library works as a wrapper over the model before initialization.
Configure model:
// yourModel.jsconst Model = ;const ns = ; // with class extending Modelmodule { // additional methods, associations return ;};
// or without ES6 classesmodule { const Menu = ; return Menu;};
DB Table Structure:
Your table must have follow columns:
- lft (int unsigned NOT NULL)
- rgt (int unsigned NOT NULL)
- depth (int unsigned NOT NULL)
- tree (int unsigned NOT NULL) (if you use multiple tree)
You can specify custom column names in options
Nested Sets Options
To use multiple tree mode set treeAttribute.
Adds nested set behavior using the model wrapper
Param | Type | Default | Description |
---|---|---|---|
sequelize | sequelize |
Ready sequelize object with connection | |
modelName | string |
Model name | |
attributes | object |
Model attributes | |
[options] | object |
{} |
Sequelize and Nested sets options |
[options.treeAttribute] | string |
false |
Column name for tree id, specify for use multiple tree |
[options.leftAttribute] | string |
"lft" |
Column name for left attribute |
[options.rightAttribute] | string |
"rgt" |
Column name for right attribute |
[options.depthAttribute] | string |
"depth" |
Column name for depth attribute |
[Model] | Model |
|
Extended Model class |
Usage
Moving, Creating node
Making a root node
To make a root node
const menu = Menu;await menu;
The tree will look like this
— Main Menu
Prepending a node as the first child of another node
To prepend a node as the first child of another node
const productPage = Menu;await productPage;
The tree will look like this
— Main Menu
— — Product Page
Appending a node as the last child of another node
To prepend a node as the last child of another node
const faqPage= Menu;await faqPage;
The tree will look like this
— Main Menu
— — Product Page
— — FAQ Page
Inserting a node before another node
To insert a node before another node
const homePage = Menu;await homePage;
The tree will look like this
— Main Menu
— — Home Page
— — Product Page
— — FAQ Page
Inserting a node after another node
To insert a node after another node
const contactPage = Menu;await contactPage;
The tree will look like this
— Main Menu
— — Home Page
— — Product Page
— — FAQ Page
— — Contact Page
Getting nodes
Getting the root nodes
To get all the root nodes
const roots = await Menu;
Getting the leaves nodes
To get all the leaves nodes
const leaves = await Menu;
To get all the leaves of a node
const menu = await Menu;const leaves = await menu;
Getting children of a node
model;
To get all the children of a node
const menu = await Menu;const children = await menu;
To get the first level children of a node
const menu = await Menu;const children = await menu;
To get the children by condition
const menu = await Menu;const children = await menu;
Getting parents of a node
model;
To get all the parents of a node
const faqPage = await Menu;const parents = await faqPage;
To get the first parent of a node
const faqPage = await Menu;const parents = await faqPage;
To get the parent by condition
const faqPage = await Menu;const parents = await menu;