GithubHelp home page GithubHelp logo

nvdnkpr / mongoose-nested-set Goto Github PK

View Code? Open in Web Editor NEW

This project forked from groupdock/mongoose-nested-set

0.0 2.0 0.0 131 KB

A mongoose plugin implementing the nested set pattern for mongoose models

License: MIT License

mongoose-nested-set's Introduction

mongoose-nested-set

A mongoose plugin implementing the nested set pattern for mongoose models

Usage

var mongoose = require('mongoose'),
    NestedSetPlugin = require('mongoose-nested-set'),
    Schema = mongoose.Schema;
    
mongoose.connect('mongodb://localhost/nested_set_test');

var UserSchema = new Schema({
  username: {type: String}
});

// Include plugin
UserSchema.plugin(NestedSetPlugin);

var User = mongoose.model('User', UserSchema);

Attributes

The plugin adds the following attributes to the model:

  • lft: holds the left value of the node in the tree

  • rgt: holds the right value of the node in the tree

  • parentId: holds the _id of the parent node

Examples

Examples are based on the following tree:

The Office

User.findOne({username: 'michael'}, function(err, michael) {
  User.rebuildTree(michael, 1, function() {
    // at this point, the tree is built and every node has a lft and rgt value.
    michael.descendants(function(err, data) {
      // data contains a list of michael descendants
      console.log(data);
    });
    console.log('Is Michael a leaf node?', michael.isLeaf());
    console.log('Is Michael a child node?', michael.isChild());
  });
});

For more examples, see our [test suite] (https://github.com/groupdock/mongoose-nested-set/blob/master/tests/nested_set_test.js).

API

Static methods

  • Model.rebuildTree(rootNode, leftValueOfRootNode, callback)

Instance methods that return values:

The following methods return a boolean:

  • isLeaf()

  • isChild()

  • isDescendantOf(otherNode)

  • isAncestorOf(otherNode)

Instance methods that use a callback function:

The following methods must be used with a callback. The callback method will be called with two arguments: the first argument is the error object (if there was no error, it will be null) and the second argument is the data returned.

  • selfAndAncestors(callback)

  • ancestors(callback)

  • selfAndChildren(callback)

  • children(callback)

  • selfAndDescendants(callback)

  • descendants(callback)

  • level(callback)

  • selfAndSiblings(callback)

  • siblings(callback)

Related Links/Resources

Development

To run the tests:

npm test

Changelog

Oct 26, 2012: Version 0.0.4

  • Bug fixes

Oct 25, 2012: Version 0.0.3

  • Bug fixes

Oct 25, 2012: Version 0.0.2

  • Better tests
  • Code cleanup
  • Updated package.json to latest dependencies
  • Added mongodb indexes on lft, rgt, and parentId

Authors

Sponsor

[Intellum] (http://www.intellum.com/)

mongoose-nested-set's People

Contributors

luccastera avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.