JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 144
  • Score
    100M100P100Q81639F
  • License MIT

Library to store and manage nested set trees using Sequelize

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

NPM Version Min Node Version Build Status Code Coverage Vulnerabilities License

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

Installation

npm install --save sequelize-nested-set

Please 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 using lftColumnName option.
  • rgt - int, not null. Use rgtColumnName option for custom column name.
  • level - int, not null. For its name you can also use levelColumnName 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 - column type must be the same with id column (default: int, but you can change it using rootColumnType option), not null. Default field name is root_id, but you can also use your own with rootColumnName 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 if hasManyRoots is true.
  • 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 if hasManyRoots is true.

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.