Skip to content

Commit 34d5a7d

Browse files
committed
fix to make it work with normal ObjectId, introduced custom idType option, fix for pathSeparator
1 parent d51c0b4 commit 34d5a7d

File tree

3 files changed

+42
-25
lines changed

3 files changed

+42
-25
lines changed

lib/tree.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
var Schema = require('mongoose').Schema;
22
var streamWorker = require('stream-worker');
3-
var shortId = require('shortid');
43

54
module.exports = exports = tree;
65

@@ -19,7 +18,9 @@ function tree(schema, options) {
1918

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

2425
/**
2526
* Add parent and path properties
@@ -29,11 +30,11 @@ function tree(schema, options) {
2930
*/
3031
schema.add({
3132
parent: {
32-
type: String,
33+
type: idType,
3334
set: function (val) {
34-
return (val instanceof Object && val._id) ? val._id.toString() : val;
35+
return (val instanceof Object && val._id) ? val._id : val;
3536
},
36-
unique: true
37+
index: true
3738
},
3839
path: {
3940
type: String,
@@ -69,7 +70,7 @@ function tree(schema, options) {
6970

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

7475
if (err) {
7576
return next(err);
@@ -105,7 +106,7 @@ function tree(schema, options) {
105106
return next();
106107

107108
if (onDelete == 'DELETE') {
108-
this.collection.remove({ path: { '$regex': '^' + this.path + pathSeparator } }, next);
109+
this.collection.remove({ path: { '$regex': '^' + this.path + pathSeparatorRegex } }, next);
109110
}
110111
else {
111112
var self = this,
@@ -129,7 +130,7 @@ function tree(schema, options) {
129130
return next(err);
130131
}
131132

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

134135
var subStream = cursor.stream();
135136
streamWorker(subStream, numWorkers, function subStreamOnData(doc, done) {
@@ -199,9 +200,9 @@ function tree(schema, options) {
199200

200201
if (recursive) {
201202
if(filters['$query']){
202-
filters['$query']['path'] = {$regex: '^' + this.path + pathSeparator};
203+
filters['$query']['path'] = {$regex: '^' + this.path + pathSeparatorRegex};
203204
} else {
204-
filters['path'] = {$regex: '^' + this.path + pathSeparator};
205+
filters['path'] = {$regex: '^' + this.path + pathSeparatorRegex};
205206
}
206207
} else {
207208
if(filters['$query']){
@@ -318,7 +319,7 @@ function tree(schema, options) {
318319
// filters: Add recursive path filter or not
319320
if (recursive) {
320321
if (root) {
321-
filters.path = { $regex: '^' + root.path + pathSeparator };
322+
filters.path = { $regex: '^' + root.path + pathSeparatorRegex };
322323
}
323324

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

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,24 @@
1616
"url": "git://github.com/swayf/mongoose-path-tree.git"
1717
},
1818
"main": "index.js",
19-
"version": "1.2.1",
19+
"version": "1.3.2",
2020
"engine": "node >= 0.4.0",
2121
"dependencies": {
2222
"mongoose": "3.x.x",
23-
"stream-worker": "0.x.x",
24-
"shortid": "2.0.0"
23+
"stream-worker": "0.x.x"
2524
},
2625
"devDependencies": {
2726
"async": "0.x.x",
2827
"should": "1.x.x",
2928
"lodash": "2.x.x",
30-
"mocha": "1.x.x"
29+
"mocha": "1.x.x",
30+
"shortid": "2.0.0"
3131
},
3232
"directories": {
3333
"test": "test"
3434
},
3535
"scripts": {
36-
"test": "./node_modules/.bin/mocha"
36+
"test": "MONGOOSE_TREE_SHORTID=1 ./node_modules/.bin/mocha\nMONGOOSE_TREE_SHORTID=0 ./node_modules/.bin/mocha"
3737
},
3838
"keywords": [
3939
"mongoose",

test/tree.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +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({
17-
_id: {
15+
var userSchema = {
16+
name: String
17+
};
18+
19+
var pluginOptions = {
20+
pathSeparator: '.'
21+
};
22+
23+
if (process.env.MONGOOSE_TREE_SHORTID === '1') {
24+
userSchema._id = {
1825
type: String,
1926
unique: true,
20-
'default': function(){ return shortId.generate(); }
21-
},
22-
name: String
23-
});
24-
UserSchema.plugin(Tree, {pathSeparator: '.'});
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);
2538
var User = Mongoose.model('User', UserSchema);
2639

2740
// Set up the fixture
@@ -82,6 +95,7 @@ describe('tree tests', function () {
8295
User.findOne({ name: 'Emily' }, function (err, emily) {
8396

8497
emily.remove(function (err) {
98+
8599
should.not.exist(err);
86100

87101
User.find(function (err, users) {
@@ -195,6 +209,7 @@ describe('tree tests', function () {
195209
should.not.exist(err);
196210

197211
adam.getChildren(function (err, users) {
212+
198213
should.not.exist(err);
199214

200215
users.length.should.equal(2);
@@ -281,8 +296,8 @@ describe('tree tests', function () {
281296
User.findOne({ 'name': 'Dann' }, function (err, dann) {
282297

283298
dann.getAncestors(function (err, ancestors) {
284-
should.not.exist(err);
285299

300+
should.not.exist(err);
286301
ancestors.length.should.equal(2);
287302
_.pluck(ancestors, 'name').should.include('Carol').and.include('Adam');
288303
done();
@@ -378,6 +393,7 @@ describe('tree tests', function () {
378393
carolTree.children.length.should.equal(1);
379394
danTree.children.length.should.equal(1);
380395
danTree.children[0].name.should.equal('Emily');
396+
emilyTree.children.length.should.equal(0);
381397

382398
done();
383399
});

0 commit comments

Comments
 (0)