npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sequelize-nested-sets

v1.1.0

Published

The nested sets behavior for the Sequelize.

Downloads

13

Readme

Nested Sets Behavior for Sequelize

Behavior for storing and managing nested sets

Build Status Test Coverage npm

Installation

npm install --save sequelize-nested-sets

Configuring

The library works as a wrapper over the model before initialization.

Configure model:

// yourModel.js
const { Model } = require('sequelize');
const ns = require('sequelize-nested-sets');

// with class extending Model
module.exports = (sequelize, DataTypes) => {
  class Menu extends Model {
    // additional methods, associations
  }

  return ns(sequelize, 'menu', {
      name: DataTypes.STRING,
    }, {
      tableName: 'menu',
      treeAttribute: 'tree',
    },
    Menu
  );
};
// or without ES6 classes
module.exports = (sequelize, DataTypes) => {  
  const Menu = ns(sequelize, 'menu', {
      name: DataTypes.STRING,
    }, {
      tableName: 'menu',
      treeAttribute: 'tree',
    }
  );
  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

ns(sequelize, modelName, attributes, [options], [Model])

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.build({ name: 'Main Menu' });
await menu.makeRoot();

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.build({ name: 'Product Page' });
await productPage.prependTo(menu);

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.build({ name: 'FAQ Page' });
await faqPage.appendTo(menu);

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.build({ name: 'Home Page' });
await homePage.insertBefore(productPage);

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.build({ name: 'Contact Page' });
await contactPage.insertBefore(faqPage);

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.roots();

Getting the leaves nodes

To get all the leaves nodes

const leaves = await Menu.leaves();

To get all the leaves of a node

const menu = await Menu.findOne({ where: { name: 'Main Menu' } });
const leaves = await menu.leave();

Getting children of a node

model.children([depth = null], [options = {}]);

To get all the children of a node

const menu = await Menu.findOne({ where: { name: 'Main Menu' } });
const children = await menu.children();

To get the first level children of a node

const menu = await Menu.findOne({ where: { name: 'Main Menu' } });
const children = await menu.children(1);

To get the children by condition

const menu = await Menu.findOne({ where: { name: 'Main Menu' } });
const children = await menu.children(null, { where: { name: 'FAQ Page' } });

Getting parents of a node

model.parents([depth = null], [options = {}]);

To get all the parents of a node

const faqPage = await Menu.findOne({ where: { name: 'FAQ Page' } });
const parents = await faqPage.parents();

To get the first parent of a node

const faqPage = await Menu.findOne({ where: { name: 'FAQ Page' } });
const parents = await faqPage.parents(1);

To get the parent by condition

const faqPage = await Menu.findOne({ where: { name: 'FAQ Page' } });
const parents = await menu.parents(null, { where: { name: 'Main Menu' } });

All methods see in wiki