Package Exports
- sequelize-nested-set
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (sequelize-nested-set) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Sequelize Nested Set
Library to store and manage nested set trees using Sequelize. It supports multi-root trees.
Feel free to create an issue or PR if you have a bug or idea.
Installation
Warning! This library is on beta testing! Be careful with using it.
npm i sequelize-nested-set
Getting started
This library works as a wrapper for sequelize.define
, it has the same params: model name, attributes (aka table fields), options, but the first 2 params are sequelize connection and DataTypes object.
const ns = require('sequelize-nested-set');
module.exports = (sequelize, DataTypes) => {
const Tag = ns(sequelize, DataTypes, 'Tag', {
label: DataTypes.STRING,
}, {
tableName: 'tag',
timestamps: false,
hasManyRoots: true,
});
// add additional methods, associations to the model
return Tag;
};
Table structure
Yes, it requires a basic structure of table where you want to keep your tree.
Here are the required fields:
lft
- int, not null. You can use another column name usinglftColumnName
option.rgt
- int, not null. UsergtColumnName
option for custom column name.level
- int, not null. For its name you can also uselevelColumnName
option.
If you want to store a multi-root tree (several trees in other words), you need to set hasManyRoots
option to true
and have one more column:
rootId
- int, not null. Default field name isroot_id
, but you can also use your own withrootColumnName
option.
Nested Set Options
There are several options to customize your nested set:
hasManyRoots
(boolean, default:false
) - for cases when you want to store several trees in one table.lftColumnName
(string, default:lft
) - a column name for lft.rgtColumnName
(string, default:rgt
) - a column name for rgt.levelColumnName
(string, default:level
) - a column name for level.rootColumnName
(string, default:root_id
) - a column name for rootId. Value of this option makes sense only ifhasManyRoots
istrue
.