Skip to content

Commit 8091eb2

Browse files
committed
support mongoose 3.5
1 parent 1b404b5 commit 8091eb2

File tree

4 files changed

+24
-19
lines changed

4 files changed

+24
-19
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var bob = new User({ name : 'Bob' });
2424
var carol = new User({ name : 'Carol' });
2525

2626
// Set the parent relationships
27-
bob.parent = adam;
27+
bob.parent = adam;
2828
carol.parent = bob;
2929

3030
adam.save(function() {
@@ -36,20 +36,20 @@ adam.save(function() {
3636

3737
At this point in mongoDB you will have documents similar to
3838

39-
{
39+
{
4040
"_id" : ObjectId("50136e40c78c4b9403000001"),
4141
"name" : "Adam",
4242
"path" : "50136e40c78c4b9403000001"
4343
}
44-
{
44+
{
4545
"_id" : ObjectId("50136e40c78c4b9403000002"),
4646
"name" : "Bob",
4747
"parent" : ObjectId("50136e40c78c4b9403000001"),
4848
"path" : "50136e40c78c4b9403000001.50136e40c78c4b9403000002"
4949
}
50-
{
50+
{
5151
"_id" : ObjectId("50136e40c78c4b9403000003"),
52-
"name" : "Carol",
52+
"name" : "Carol",
5353
"parent" : ObjectId("50136e40c78c4b9403000002"),
5454
"path" : "50136e40c78c4b9403000001.50136e40c78c4b9403000002.50136e40c78c4b9403000003"
5555
}
@@ -78,16 +78,16 @@ adam.getChildren(true, function(err, users) {
7878
});
7979
```
8080

81-
### getAnsestors
81+
### getAncestors
8282

8383
Signature:
8484

85-
getAnsestors(cb);
85+
getAncestors(cb);
8686

8787
Based on the above hierarchy:
8888

8989
```javascript
90-
carol.getAnsestors(function(err, users) {
90+
carol.getAncestors(function(err, users) {
9191
// users is an array of adam and bob
9292
})
9393
```

lib/tree.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ function tree(schema, options) {
4545
var stream = cursor.stream();
4646
stream.on('data', function (doc) {
4747
var newPath = self.path+doc.path.substr(previousPath.length);
48-
self.collection.update({ _id : doc._id }, { $set : { path : newPath } });
48+
self.collection.update({ _id : doc._id }, { $set : { path : newPath } }, function(err) {
49+
if(err) return next(err);
50+
});
4951
});
5052
stream.on('close', function() {
5153
next();
@@ -83,7 +85,7 @@ function tree(schema, options) {
8385
return this.model(this.constructor.modelName).findOne({ _id : this.parent }, cb);
8486
});
8587

86-
schema.method('getAnsestors', function(cb) {
88+
var getAncestors = function(cb) {
8789
if(this.path) {
8890
var ids = this.path.split(".");
8991
ids.pop();
@@ -92,7 +94,10 @@ function tree(schema, options) {
9294
}
9395
var filter = { _id : { $in : ids } };
9496
return this.model(this.constructor.modelName).find(filter, cb);
95-
});
97+
};
98+
99+
schema.method('getAnsestors', getAncestors);
100+
schema.method('getAncestors', getAncestors);
96101

97102
schema.virtual('level').get(function() {
98103
return this.path ? this.path.split(".").length : 0;

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
"url": "git://github.com/briankircho/mongoose-tree.git"
99
},
1010
"main": "index.js",
11-
"version": "0.2.0",
11+
"version": "0.2.1",
1212
"engine": "node >= 0.4.0",
1313
"dependencies": {
14-
"mongoose": "~3.0.0"
14+
"mongoose": "~3.5.0"
1515
},
1616
"devDependencies": {
1717
"async": "~0.1.22",

test/tree.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe('tree tests', function() {
4343
users.forEach(function(user) {
4444
names[user.name] = user;
4545
});
46-
46+
4747
should.not.exist(names['Adam'].parent);
4848
names['Bob'].parent.toString().should.equal(names['Adam']._id.toString());
4949
names['Carol'].parent.toString().should.equal(names['Adam']._id.toString());
@@ -122,7 +122,7 @@ describe('tree tests', function() {
122122
users.forEach(function(user) {
123123
names[user.name] = user;
124124
});
125-
125+
126126
var carol = names['Carol'];
127127
var bob = names['Bob'];
128128

@@ -176,14 +176,14 @@ describe('tree tests', function() {
176176
});
177177
});
178178

179-
describe('get ansestors', function() {
179+
describe('get ancestors', function() {
180180
it('should return ancestors', function(done) {
181181
User.findOne({ 'name' : 'Dann' }, function(err, dann) {
182-
dann.getAnsestors(function(err, ansestors) {
182+
dann.getAncestors(function(err, ancestors) {
183183
should.not.exist(err);
184184

185-
ansestors.length.should.equal(2);
186-
_.pluck(ansestors, 'name').should.include('Carol').and.include('Adam');
185+
ancestors.length.should.equal(2);
186+
_.pluck(ancestors, 'name').should.include('Carol').and.include('Adam');
187187
done();
188188
});
189189
});

0 commit comments

Comments
 (0)