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

mongoose-nested-set

v0.0.7

Published

A mongoose plugin implementing the nested set pattern (aka as trees or hierarchies) for mongoose models

Downloads

53

Readme

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

Options

The plugin supports the following options:

  • groupingKey: a key to denote that elements in the tree belong to a group. This is helpful to maintain multiple trees within the same collection. Eg. in the tests we have two trees, grouped by the attribute 'organization' on the model, in that case we've given the { groupingKey: 'organization' } as options.

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)

  • parent(callback)

  • ancestors(callback)

  • selfAndChildren(callback)

  • children(callback)

  • selfAndDescendants(callback)

  • descendants(callback)

  • level(callback)

  • selfAndSiblings(callback)

  • siblings(callback)

Related Links/Resources

  • Inspired by [Rails nested_set library] (https://github.com/skyeagle/nested_set)
  • [Mongoose Documentation] (http://mongoosejs.com/)
  • [Mongoose Plugins] (http://mongoosejs.com/docs/plugins.html)
  • [Tree used in test and examples] (https://github.com/groupdock/mongoose-nested-set/raw/master/docs/test_tree.png)
  • [Nested Set Model] (http://en.wikipedia.org/wiki/Nested_set_model)
  • [Storing Hierarchical Data in a Database Article] (http://www.sitepoint.com/hierarchical-data-database/)
  • [Trees in MongoDB] (http://www.mongodb.org/display/DOCS/Trees+in+MongoDB)

Development

To run the tests:

npm test

Changelog

Feb 14, 2015: Version 0.0.7

  • Converted methods to follow mongoose params model #11

Nov 23, 2014: Version 0.0.6

  • New Feature: added groupingKey option

Jun 30, 2013: Version 0.0.5

  • Added compound index on lft,rgt for selfAndDescendants and selfAndAncestors queries
  • A few small refactorings of tests

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/)