1
1
var Schema = require ( 'mongoose' ) . Schema ;
2
2
var streamWorker = require ( 'stream-worker' ) ;
3
- var shortId = require ( 'shortid' ) ;
4
3
5
4
module . exports = exports = tree ;
6
5
@@ -19,7 +18,9 @@ function tree(schema, options) {
19
18
20
19
var pathSeparator = options && options . pathSeparator || '#'
21
20
, 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 + ']' ;
23
24
24
25
/**
25
26
* Add parent and path properties
@@ -29,11 +30,11 @@ function tree(schema, options) {
29
30
*/
30
31
schema . add ( {
31
32
parent : {
32
- type : String ,
33
+ type : idType ,
33
34
set : function ( val ) {
34
- return ( val instanceof Object && val . _id ) ? val . _id . toString ( ) : val ;
35
+ return ( val instanceof Object && val . _id ) ? val . _id : val ;
35
36
} ,
36
- unique : true
37
+ index : true
37
38
} ,
38
39
path : {
39
40
type : String ,
@@ -69,7 +70,7 @@ function tree(schema, options) {
69
70
70
71
if ( isParentChange ) {
71
72
// 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 ) {
73
74
74
75
if ( err ) {
75
76
return next ( err ) ;
@@ -105,7 +106,7 @@ function tree(schema, options) {
105
106
return next ( ) ;
106
107
107
108
if ( onDelete == 'DELETE' ) {
108
- this . collection . remove ( { path : { '$regex' : '^' + this . path + pathSeparator } } , next ) ;
109
+ this . collection . remove ( { path : { '$regex' : '^' + this . path + pathSeparatorRegex } } , next ) ;
109
110
}
110
111
else {
111
112
var self = this ,
@@ -129,7 +130,7 @@ function tree(schema, options) {
129
130
return next ( err ) ;
130
131
}
131
132
132
- self . collection . find ( { path : { $regex : previousParent + pathSeparator } } , function ( err , cursor ) {
133
+ self . collection . find ( { path : { $regex : previousParent + pathSeparatorRegex } } , function ( err , cursor ) {
133
134
134
135
var subStream = cursor . stream ( ) ;
135
136
streamWorker ( subStream , numWorkers , function subStreamOnData ( doc , done ) {
@@ -199,9 +200,9 @@ function tree(schema, options) {
199
200
200
201
if ( recursive ) {
201
202
if ( filters [ '$query' ] ) {
202
- filters [ '$query' ] [ 'path' ] = { $regex : '^' + this . path + pathSeparator } ;
203
+ filters [ '$query' ] [ 'path' ] = { $regex : '^' + this . path + pathSeparatorRegex } ;
203
204
} else {
204
- filters [ 'path' ] = { $regex : '^' + this . path + pathSeparator } ;
205
+ filters [ 'path' ] = { $regex : '^' + this . path + pathSeparatorRegex } ;
205
206
}
206
207
} else {
207
208
if ( filters [ '$query' ] ) {
@@ -318,7 +319,7 @@ function tree(schema, options) {
318
319
// filters: Add recursive path filter or not
319
320
if ( recursive ) {
320
321
if ( root ) {
321
- filters . path = { $regex : '^' + root . path + pathSeparator } ;
322
+ filters . path = { $regex : '^' + root . path + pathSeparatorRegex } ;
322
323
}
323
324
324
325
if ( filters . parent === null ) {
0 commit comments