Skip to content

Commit 14c3d30

Browse files
committed
Merge branch 'pr/4' into develop
* pr/4: fix to make it work with normal ObjectId, introduced custom idType option, fix for pathSeparator indentation fix adding shortid to support deeper trees Conflicts: package.json
2 parents 7c98aee + 34d5a7d commit 14c3d30

File tree

3 files changed

+75
-22
lines changed

3 files changed

+75
-22
lines changed

lib/tree.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ function tree(schema, options) {
1818

1919
var pathSeparator = options && options.pathSeparator || '#'
2020
, onDelete = options && options.onDelete || 'DELETE' //'REPARENT'
21-
, numWorkers = options && options.numWorkers || 5;
21+
, numWorkers = options && options.numWorkers || 5
22+
, idType = options && options.idType || Schema.ObjectId
23+
, pathSeparatorRegex = '[' + pathSeparator + ']';
2224

2325
/**
2426
* Add parent and path properties
@@ -28,7 +30,7 @@ function tree(schema, options) {
2830
*/
2931
schema.add({
3032
parent: {
31-
type: Schema.ObjectId,
33+
type: idType,
3234
set: function (val) {
3335
return (val instanceof Object && val._id) ? val._id : val;
3436
},
@@ -68,7 +70,7 @@ function tree(schema, options) {
6870

6971
if (isParentChange) {
7072
// When the parent is changed we must rewrite all children paths as well
71-
self.collection.find({ path: { '$regex': '^' + previousPath + pathSeparator } }, function (err, cursor) {
73+
self.collection.find({ path: { '$regex': '^' + previousPath + pathSeparatorRegex } }, function (err, cursor) {
7274

7375
if (err) {
7476
return next(err);
@@ -104,7 +106,7 @@ function tree(schema, options) {
104106
return next();
105107

106108
if (onDelete == 'DELETE') {
107-
this.collection.remove({ path: { '$regex': '^' + this.path + pathSeparator } }, next);
109+
this.collection.remove({ path: { '$regex': '^' + this.path + pathSeparatorRegex } }, next);
108110
}
109111
else {
110112
var self = this,
@@ -128,7 +130,7 @@ function tree(schema, options) {
128130
return next(err);
129131
}
130132

131-
self.collection.find({ path: { $regex: previousParent + pathSeparator} }, function (err, cursor) {
133+
self.collection.find({ path: { $regex: previousParent + pathSeparatorRegex} }, function (err, cursor) {
132134

133135
var subStream = cursor.stream();
134136
streamWorker(subStream, numWorkers, function subStreamOnData(doc, done) {
@@ -198,9 +200,9 @@ function tree(schema, options) {
198200

199201
if (recursive) {
200202
if(filters['$query']){
201-
filters['$query']['path'] = {$regex: '^' + this.path + pathSeparator};
203+
filters['$query']['path'] = {$regex: '^' + this.path + pathSeparatorRegex};
202204
} else {
203-
filters['path'] = {$regex: '^' + this.path + pathSeparator};
205+
filters['path'] = {$regex: '^' + this.path + pathSeparatorRegex};
204206
}
205207
} else {
206208
if(filters['$query']){
@@ -317,7 +319,7 @@ function tree(schema, options) {
317319
// filters: Add recursive path filter or not
318320
if (recursive) {
319321
if (root) {
320-
filters.path = { $regex: '^' + root.path + pathSeparator };
322+
filters.path = { $regex: '^' + root.path + pathSeparatorRegex };
321323
}
322324

323325
if (filters.parent === null) {

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"url": "git://github.com/swayf/mongoose-path-tree.git"
1717
},
1818
"main": "index.js",
19-
"version": "1.3.1",
19+
"version": "1.3.2",
2020
"engine": "node >= 0.4.0",
2121
"dependencies": {
2222
"mongoose": "3.x.x",
@@ -26,13 +26,14 @@
2626
"async": "0.x.x",
2727
"should": "1.x.x",
2828
"lodash": "2.x.x",
29-
"mocha": "1.x.x"
29+
"mocha": "1.x.x",
30+
"shortid": "2.0.0"
3031
},
3132
"directories": {
3233
"test": "test"
3334
},
3435
"scripts": {
35-
"test": "./node_modules/.bin/mocha"
36+
"test": "MONGOOSE_TREE_SHORTID=1 ./node_modules/.bin/mocha\nMONGOOSE_TREE_SHORTID=0 ./node_modules/.bin/mocha"
3637
},
3738
"keywords": [
3839
"mongoose",

test/tree.js

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var Tree = require('../lib/tree');
33
var Async = require('async');
44
var should = require('should');
55
var _ = require('lodash');
6-
6+
var shortId = require('shortid');
77

88
var Schema = Mongoose.Schema;
99

@@ -12,11 +12,29 @@ Mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/mongoose-
1212

1313
describe('tree tests', function () {
1414

15-
// Schema for tests
16-
var UserSchema = new Schema({
15+
var userSchema = {
1716
name: String
18-
});
19-
UserSchema.plugin(Tree, {pathSeparator: '.'});
17+
};
18+
19+
var pluginOptions = {
20+
pathSeparator: '.'
21+
};
22+
23+
if (process.env.MONGOOSE_TREE_SHORTID === '1') {
24+
userSchema._id = {
25+
type: String,
26+
unique: true,
27+
'default': function(){
28+
return shortId.generate();
29+
}
30+
};
31+
32+
pluginOptions.idType = String
33+
}
34+
35+
// Schema for tests
36+
var UserSchema = new Schema(userSchema);
37+
UserSchema.plugin(Tree, pluginOptions);
2038
var User = Mongoose.model('User', UserSchema);
2139

2240
// Set up the fixture
@@ -77,6 +95,7 @@ describe('tree tests', function () {
7795
User.findOne({ name: 'Emily' }, function (err, emily) {
7896

7997
emily.remove(function (err) {
98+
8099
should.not.exist(err);
81100

82101
User.find(function (err, users) {
@@ -190,6 +209,7 @@ describe('tree tests', function () {
190209
should.not.exist(err);
191210

192211
adam.getChildren(function (err, users) {
212+
193213
should.not.exist(err);
194214

195215
users.length.should.equal(2);
@@ -276,8 +296,8 @@ describe('tree tests', function () {
276296
User.findOne({ 'name': 'Dann' }, function (err, dann) {
277297

278298
dann.getAncestors(function (err, ancestors) {
279-
should.not.exist(err);
280299

300+
should.not.exist(err);
281301
ancestors.length.should.equal(2);
282302
_.pluck(ancestors, 'name').should.include('Carol').and.include('Adam');
283303
done();
@@ -329,8 +349,28 @@ describe('tree tests', function () {
329349

330350
should.not.exist(err);
331351
childrenTree.length.should.equal(2);
332-
childrenTree[0].children[1].children[0].children[0].name.should.equal('Emily');
333-
childrenTree[0].children[1].children[0].children.length.should.equal(1);
352+
353+
var adamTree = _.find(childrenTree, function(x){ return x.name == 'Adam'});
354+
var edenTree = _.find(childrenTree, function(x){ return x.name == 'Eden'});
355+
356+
var bobTree = _.find(adamTree.children, function(x){ return x.name == 'Bob'});
357+
358+
var carolTree = _.find(adamTree.children, function(x){ return x.name == 'Carol'});
359+
var danTree = _.find(carolTree.children, function(x){ return x.name == 'Dann'});
360+
var emilyTree = _.find(danTree.children, function(x){ return x.name == 'Emily'});
361+
362+
363+
adamTree.children.length.should.equal(2);
364+
edenTree.children.length.should.equal(0);
365+
366+
bobTree.children.length.should.equal(0);
367+
368+
carolTree.children.length.should.equal(1);
369+
370+
danTree.children.length.should.equal(1);
371+
danTree.children[0].name.should.equal('Emily');
372+
373+
emilyTree.children.length.should.equal(0);
334374
done();
335375
});
336376
});
@@ -342,9 +382,19 @@ describe('tree tests', function () {
342382
adam.getChildrenTree(function (err, childrenTree) {
343383

344384
should.not.exist(err);
345-
childrenTree.length.should.equal(2);
346-
childrenTree[1].children[0].children[0].name.should.equal('Emily');
347-
childrenTree[1].children[0].children.length.should.equal(1);
385+
386+
var bobTree = _.find(childrenTree, function(x){ return x.name == 'Bob'});
387+
388+
var carolTree = _.find(childrenTree, function(x){ return x.name == 'Carol'});
389+
var danTree = _.find(carolTree.children, function(x){ return x.name == 'Dann'});
390+
var emilyTree = _.find(danTree.children, function(x){ return x.name == 'Emily'});
391+
392+
bobTree.children.length.should.equal(0);
393+
carolTree.children.length.should.equal(1);
394+
danTree.children.length.should.equal(1);
395+
danTree.children[0].name.should.equal('Emily');
396+
emilyTree.children.length.should.equal(0);
397+
348398
done();
349399
});
350400
});

0 commit comments

Comments
 (0)