Skip to content
Browse files

support mongoose 3.5

  • Loading branch information...
1 parent 1b404b5 commit 8091eb2bce056a3bec4fb2f78df59f5ac64b554d @briankircho committed Jan 17, 2013
Showing with 24 additions and 19 deletions.
  1. +8 −8 README.md
  2. +8 −3 lib/tree.js
  3. +2 −2 package.json
  4. +6 −6 test/tree.js
View
16 README.md
@@ -24,7 +24,7 @@ var bob = new User({ name : 'Bob' });
var carol = new User({ name : 'Carol' });
// Set the parent relationships
-bob.parent = adam;
+bob.parent = adam;
carol.parent = bob;
adam.save(function() {
@@ -36,20 +36,20 @@ adam.save(function() {
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",
+ "name" : "Carol",
"parent" : ObjectId("50136e40c78c4b9403000002"),
"path" : "50136e40c78c4b9403000001.50136e40c78c4b9403000002.50136e40c78c4b9403000003"
}
@@ -78,16 +78,16 @@ adam.getChildren(true, function(err, users) {
});
```
-### getAnsestors
+### getAncestors
Signature:
- getAnsestors(cb);
+ getAncestors(cb);
Based on the above hierarchy:
```javascript
-carol.getAnsestors(function(err, users) {
+carol.getAncestors(function(err, users) {
// users is an array of adam and bob
})
```
View
11 lib/tree.js
@@ -45,7 +45,9 @@ function tree(schema, options) {
var stream = cursor.stream();
stream.on('data', function (doc) {
var newPath = self.path+doc.path.substr(previousPath.length);
- self.collection.update({ _id : doc._id }, { $set : { path : newPath } });
+ self.collection.update({ _id : doc._id }, { $set : { path : newPath } }, function(err) {
+ if(err) return next(err);
+ });
});
stream.on('close', function() {
next();
@@ -83,7 +85,7 @@ function tree(schema, options) {
return this.model(this.constructor.modelName).findOne({ _id : this.parent }, cb);
});
- schema.method('getAnsestors', function(cb) {
+ var getAncestors = function(cb) {
if(this.path) {
var ids = this.path.split(".");
ids.pop();
@@ -92,7 +94,10 @@ function tree(schema, options) {
}
var filter = { _id : { $in : ids } };
return this.model(this.constructor.modelName).find(filter, cb);
- });
+ };
+
+ schema.method('getAnsestors', getAncestors);
+ schema.method('getAncestors', getAncestors);
schema.virtual('level').get(function() {
return this.path ? this.path.split(".").length : 0;
View
4 package.json
@@ -8,10 +8,10 @@
"url": "git://github.com/briankircho/mongoose-tree.git"
},
"main": "index.js",
- "version": "0.2.0",
+ "version": "0.2.1",
"engine": "node >= 0.4.0",
"dependencies": {
- "mongoose": "~3.0.0"
+ "mongoose": "~3.5.0"
},
"devDependencies": {
"async": "~0.1.22",
View
12 test/tree.js
@@ -43,7 +43,7 @@ describe('tree tests', function() {
users.forEach(function(user) {
names[user.name] = user;
});
-
+
should.not.exist(names['Adam'].parent);
names['Bob'].parent.toString().should.equal(names['Adam']._id.toString());
names['Carol'].parent.toString().should.equal(names['Adam']._id.toString());
@@ -122,7 +122,7 @@ describe('tree tests', function() {
users.forEach(function(user) {
names[user.name] = user;
});
-
+
var carol = names['Carol'];
var bob = names['Bob'];
@@ -176,14 +176,14 @@ describe('tree tests', function() {
});
});
- describe('get ansestors', function() {
+ describe('get ancestors', function() {
it('should return ancestors', function(done) {
User.findOne({ 'name' : 'Dann' }, function(err, dann) {
- dann.getAnsestors(function(err, ansestors) {
+ dann.getAncestors(function(err, ancestors) {
should.not.exist(err);
- ansestors.length.should.equal(2);
- _.pluck(ansestors, 'name').should.include('Carol').and.include('Adam');
+ ancestors.length.should.equal(2);
+ _.pluck(ancestors, 'name').should.include('Carol').and.include('Adam');
done();
});
});

0 comments on commit 8091eb2

Please sign in to comment.
Something went wrong with that request. Please try again.