fremail / sequelize-nested-set

Library to store and manage nested set trees using Sequelize
MIT License
40 stars 10 forks source link
multi-root-tree sequelize tree trees

Sequelize Nested Set

NPM Version Min Node Version Build Status Code Coverage Vulnerabilities License

Library to store and manage nested set trees using Sequelize version 4, 5 or 6! 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

npm install --save sequelize-nested-set

Please note, you have to install Sequelize yourself.

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:

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:

Some methods of this library require level or parentId field. If you don't have any of the fields, they will be generated on the fly which may result in high load and slow work. A simple way to optimize this issue is to add one of columns or both:

Nested Set Options

There are the options to customize your nested set. All these options are optional.

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.

Upgrading

See CHANGELOG.md for list of changes and UPGRADING.md for guide how to upgrade your code.