Code Monkey home page Code Monkey logo

mongoose-tree's Introduction

mongoose-tree

Implements the materialized path strategy for storing a hierarchy of documents with mongoose

Usage

Install via NPM

$ npm install mongoose-tree

Then you can use the plugin on your schemas

var tree = require('mongoose-tree');

var UserSchema = new Schema({
  name : String
});
UserSchema.plugin(tree);
var User = mongoose.model('User', UserSchema);

var adam = new User({ name : 'Adam' });
var bob = new User({ name : 'Bob' });
var carol = new User({ name : 'Carol' });

// Set the parent relationships
bob.parent = adam;
carol.parent = bob;

adam.save(function() {
  bob.save(function() {
    carol.save();
  });
});

At this point in mongoDB you will have documents similar to

{
  "_id" : ObjectId("50136e40c78c4b9403000001"),
  "name" : "Adam",
  "path" : "50136e40c78c4b9403000001"
}
{
  "_id" : ObjectId("50136e40c78c4b9403000002"),
  "name" : "Bob",
  "parent" : ObjectId("50136e40c78c4b9403000001"),
  "path" : "50136e40c78c4b9403000001#50136e40c78c4b9403000002"
}
{
  "_id" : ObjectId("50136e40c78c4b9403000003"),
  "name" : "Carol",
  "parent" : ObjectId("50136e40c78c4b9403000002"),
  "path" : "50136e40c78c4b9403000001#50136e40c78c4b9403000002#50136e40c78c4b9403000003"
}

The path is used for recursive methods and is kept up to date by the plugin if the parent is changed

Options

Model.plugin(tree, {
  pathSeparator : '#' // Default path separator
})

API

getChildren

Signature:

getChildren([recursive], cb);

if recursive is supplied and true subchildren are returned

Based on the above hierarchy:

adam.getChildren(function(err, users) {
  // users is an array of with the bob document
});

adam.getChildren(true, function(err, users) {
  // users is an array with both bob and carol documents
});

getAncestors

Signature:

getAncestors(cb);

Based on the above hierarchy:

carol.getAncestors(function(err, users) {
  // users is an array of adam and bob
})

level

Equal to the level of the hierarchy

carol.level; // equals 3

Tests

To run the tests install mocha

npm install mocha -g

and then run

mocha

mongoose-tree's People

Contributors

briankircho avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mongoose-tree's Issues

sort error

when I use to sort, it display chaotic

Result set empty

I tried with the example provided in the readme file is giving the empty results.

Get Objects in Tree

Hi;

I want to get objects in a tree like this :

[ {name : 'adam',
childs: [
{
name:'bob',
childs:[
{
name : 'carol',
childs:[]
}
]
},
{
name:'bob2_anotherchild',
childs:[]
}
]
}]

How can do that ?

Failed to save children inside the parent node's save callback

Here is my code to create the tree:

`var Bob = new User({ name: 'Bob' });
var Carl = new User({ name: 'Carl' });
Carl.parent = Bob;

Bob.save(function(){
Carl.save(function(){
console.log(Carl);
});
});`

But the inner save of Carl like a blackhole, it never get returned and the console.log never got called.

I dig into the lib/tree.js and finally find that at line 45, "priviousePath" variable is undefined, I think the if condition to enter this block should also check the value of previousPath,
if(isParentChange && previousPath !== undefined ) { // When the parent is changed we must rewrite all children paths as well self.collection.find({ path : { '$regex' : '^' + previousPath + pathSeparator } }, function(err, cursor) { if(err) return next(err);

Spell

is getAnsestors spelling intentional or a mistake? (getAncestors)

Bug with bluebird and promisify [Fixed but don't know how to pull request]

I was getting an error: Unhandled rejection TypeError: expecting an array or an iterable object but got [object Null]

According to this, the method signature changed.

So I changed the tree.js file like so:
Line 80 was:

streamWorker(cursor.stream(), numWorkers, function streamOnData(doc, done) {

                            var newPath = self.path + doc.path.substr(previousPath.length);
                            self.collection.update({ _id: doc._id }, { $set: { path: newPath } }, done);
                        },
                        next);

I changed to:

 streamWorker(cursor.stream(), function streamOnData(doc, done) {

                            var newPath = self.path + doc.path.substr(previousPath.length);
                            self.collection.update({ _id: doc._id }, { $set: { path: newPath } }, done);
                        },
                            {concurrency: numWorkers},
                        next);

I'm sorry I'm pretty new and I don't know how to create a pull request with this.

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.