1.0.1 • Published 8 years ago

mongoose-nestedset-plugin v1.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
8 years ago

Mongoose netstedset behaviour plugin

Install

npm install mongoose-nestedset-plugin

Usage

var mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    nestedSetPlugin = require('mongoose-nestedset-plugin');

var ItemSchema = new Schema({
    title: String
});

ItemSchema.plugin(nestedSetPlugin);

var Item = mongoose.model('Item', ItemSchema);

The plugin adds the following fields to your node:

  • root - root Id to handle multiple-rooted trees
  • lft - left
  • rgt - right
  • level - nesting level

Methods

Add a new root

The method creates a new root node and fails if the ID has been already taken:

Item.addRoot(1, { title: "Root 1" }, function (error, createdRoot){
  // handle error or createdRoot node
});

Add a new child

The method add a new node after all the children:

Item.addChild( { _id: rootNodeId }, { title: "Child 1.1" }, function (error, createdChild){
  // handle error or createdChild
});

Add a child node after some other

The method add sibling node after selected one

Item.addAfter( { _id: siblingId }, { title: "Child 1.2" }, function (error, createdItem){
   // handle
});

Get child subtree

Item.getChildren( { _id: 123 }, function (error, children){
 // handle
});

Get immediate children only

Item.getDirectChildren( { _id: 123 }, function (error, children){
 // handle
});

Get tree

Item.getTree(function (error, treeNodes){
 // handle
});

Remove node

Item.removeNode( { _id: rootNodeId }, function (error, affected) {
 // handle
});