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 version 4 or 5. It supports multi-root trees.
Feel free to create an issue or PR if you have a bug or idea.
P.S. Don't forget to star this library. It stimulates me to support the library! ⭐️
P.P.S. If you feel like you could help me with the library (writing docs, adding new features, testing, fixing bugs or anything else) - you are welcome! If you need any info or have a question, just email me at fremail@yandex.com
Quick links
- Installation
- Getting started
- DB Table structure
- Nested Set Options
- Docs
- Docs: All functions
- Changelog
- License (MIT)
Installation
npm install --save sequelize-nested-setPlease note, it doesn't install Sequelize itself.
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;
};DB 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 usinglftColumnNameoption.rgt- int, not null. UsergtColumnNameoption for custom column name.level- int, not null. For its name you can also uselevelColumnNameoption.
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- column type must be the same withidcolumn (default: int, but you can change it usingrootColumnTypeoption), not null. Default field name isroot_id, but you can also use your own withrootColumnNameoption.
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 ifhasManyRootsistrue.rootColumnType(one of DataTypes.*, default:DataTypes.INTEGER) - a column type for rootId. It must be the same column type as the id column. Value of this option makes sense only ifhasManyRootsistrue.
API docs
I'm trying to keep all functions good-documented, but if you want more info about the functions with examples etc., please visit API docs in wiki.