Skip to content
Browse files

Fix #6

  • Loading branch information...
1 parent 8091eb2 commit 10acb972789de0b9f4a59784b1d05511b917c9f6 @briankircho committed Jun 22, 2013
Showing with 21 additions and 11 deletions.
  1. +10 −2 README.md
  2. +8 −6 lib/tree.js
  3. +1 −1 package.json
  4. +2 −2 test/tree.js
View
12 README.md
@@ -45,17 +45,25 @@ At this point in mongoDB you will have documents similar to
"_id" : ObjectId("50136e40c78c4b9403000002"),
"name" : "Bob",
"parent" : ObjectId("50136e40c78c4b9403000001"),
- "path" : "50136e40c78c4b9403000001.50136e40c78c4b9403000002"
+ "path" : "50136e40c78c4b9403000001#50136e40c78c4b9403000002"
}
{
"_id" : ObjectId("50136e40c78c4b9403000003"),
"name" : "Carol",
"parent" : ObjectId("50136e40c78c4b9403000002"),
- "path" : "50136e40c78c4b9403000001.50136e40c78c4b9403000002.50136e40c78c4b9403000003"
+ "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
+
+```javascript
+Model.plugin(tree, {
+ pathSeparator : '#' // Default path separator
+})
+```
+
# API
### getChildren
View
14 lib/tree.js
@@ -4,6 +4,8 @@ var Schema = require('mongoose').Schema;
module.exports = exports = tree;
function tree(schema, options) {
+ var pathSeparator = options && options.pathSeparator || '#';
+
schema.add({
parent : {
type : Schema.ObjectId,
@@ -35,11 +37,11 @@ function tree(schema, options) {
if(err) return next(err);
var previousPath = self.path;
- self.path = doc.path + '.' + self._id.toString();
+ self.path = doc.path + pathSeparator + self._id.toString();
if(isParentChange) {
// When the parent is changed we must rewrite all children paths as well
- self.collection.find({ path : { '$regex' : '^' + previousPath + '.' } }, function(err, cursor) {
+ self.collection.find({ path : { '$regex' : '^' + previousPath + pathSeparator } }, function(err, cursor) {
if(err) return next(err);
var stream = cursor.stream();
@@ -69,15 +71,15 @@ function tree(schema, options) {
if(!this.path) {
return next();
}
- this.collection.remove({ path : { '$regex' : '^' + this.path + '.' } }, next);
+ this.collection.remove({ path : { '$regex' : '^' + this.path + pathSeparator } }, next);
});
schema.method('getChildren', function(recursive, cb) {
if(typeof(recursive) === "function") {
cb = recursive;
recursive = false;
}
- var filter = recursive ? { path : { $regex : '^' + this.path + '.' } } : { parent : this._id };
+ var filter = recursive ? { path : { $regex : '^' + this.path + pathSeparator } } : { parent : this._id };
return this.model(this.constructor.modelName).find(filter, cb);
});
@@ -87,7 +89,7 @@ function tree(schema, options) {
var getAncestors = function(cb) {
if(this.path) {
- var ids = this.path.split(".");
+ var ids = this.path.split(pathSeparator);
ids.pop();
} else {
var ids = [];
@@ -100,6 +102,6 @@ function tree(schema, options) {
schema.method('getAncestors', getAncestors);
schema.virtual('level').get(function() {
- return this.path ? this.path.split(".").length : 0;
+ return this.path ? this.path.split(pathSeparator).length : 0;
});
}
View
2 package.json
@@ -8,7 +8,7 @@
"url": "git://github.com/briankircho/mongoose-tree.git"
},
"main": "index.js",
- "version": "0.2.1",
+ "version": "0.2.2",
"engine": "node >= 0.4.0",
"dependencies": {
"mongoose": "~3.5.0"
View
4 test/tree.js
@@ -50,7 +50,7 @@ describe('tree tests', function() {
names['Dann'].parent.toString().should.equal(names['Carol']._id.toString());
names['Emily'].parent.toString().should.equal(names['Dann']._id.toString());
- var expectedPath = [names['Adam']._id, names['Carol']._id, names['Dann']._id].join('.');
+ var expectedPath = [names['Adam']._id, names['Carol']._id, names['Dann']._id].join('#');
names['Dann'].path.should.equal(expectedPath);
done();
@@ -106,7 +106,7 @@ describe('tree tests', function() {
return;
}
should.exist(ids[user.parent]);
- user.path.should.equal(ids[user.parent].path+"."+user._id);
+ user.path.should.equal(ids[user.parent].path+"#"+user._id);
});
done();

0 comments on commit 10acb97

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