1
- ## mongoose-tree
1
+ ## mongoose-path- tree
2
2
3
- Implements the materialized path strategy for storing a hierarchy of documents with mongoose
3
+ Implements the materialized path strategy with cascade child re-parenting on delete for storing a hierarchy of documents with mongoose
4
+ Version with all collected features and fixes from mongoose-tree, mongoose-tree-fix, mongoose-tree2, mongoose-reparenting-tree
4
5
5
6
# Usage
6
7
7
8
Install via NPM
8
9
9
- $ npm install mongoose-tree
10
+ $ npm install mongoose-path-tree
11
+
12
+ ## Options
13
+
14
+ ``` javascript
15
+ Model .plugin (tree, {
16
+ pathSeparator : ' #' // Default path separator
17
+ onDelete : ' REPARENT' // Can be set to 'DELETE' or 'REPARENT'. Default: 'REPARENT'
18
+ })
19
+ ```
10
20
11
21
Then you can use the plugin on your schemas
12
22
@@ -56,14 +66,6 @@ At this point in mongoDB you will have documents similar to
56
66
57
67
The path is used for recursive methods and is kept up to date by the plugin if the parent is changed
58
68
59
- ## Options
60
-
61
- ``` javascript
62
- Model .plugin (tree, {
63
- pathSeparator : ' #' // Default path separator
64
- })
65
- ```
66
-
67
69
# API
68
70
69
71
### getChildren
@@ -72,7 +74,7 @@ Signature:
72
74
73
75
getChildren([recursive], cb);
74
76
75
- if recursive is supplied and true subchildren are returned
77
+ if recursive is supplied and true, subchildren are returned
76
78
77
79
Based on the above hierarchy:
78
80
@@ -86,6 +88,79 @@ adam.getChildren(true, function(err, users) {
86
88
});
87
89
```
88
90
91
+ ### getChildrenTree
92
+
93
+ Signature:
94
+
95
+ getChildrenTree([args], cb);
96
+
97
+ return a recursive tree of subchildren.
98
+
99
+ args is an object you can defined with theses properties :
100
+
101
+ filters: mongoose query filter, optional, default null
102
+ example: filters: {owner:myId}
103
+
104
+ fields: mongoose fields, optional, default null (all columns)
105
+ example: columns: {"_id name owner"}
106
+
107
+ options: mongoose query option, optional, default null
108
+ example: options:{{sort:'-name'}}
109
+
110
+ minLevel: level at which will start the search, default 1
111
+ example: minLevel:2
112
+
113
+ recursive: boolean, default true
114
+ make the search recursive or only fetch childs for the specified level
115
+ example: recursive:false
116
+
117
+ allowEmptyChildren: boolean, default true
118
+ if true, every childs not having subchilds will have childs attribute (empty array)
119
+ if false, every childs not having subchilds will not have childs attribute
120
+
121
+ Example :
122
+
123
+ ```javascript
124
+ var args = {
125
+ filters: {owner:myId},
126
+ columns: {"_id name owner"},
127
+ minLevel:2,
128
+ recursive:true,
129
+ emptyChilds:false
130
+ }
131
+
132
+ getChildren(args,myCallback);
133
+ ```
134
+
135
+ Based on the above hierarchy:
136
+
137
+ ``` javascript
138
+ adam .getChildren ([function ](err , users ) {
139
+
140
+ /* if you dump users, you will have something like this :
141
+ {
142
+ "_id" : ObjectId("50136e40c78c4b9403000001"),
143
+ "name" : "Adam",
144
+ "path" : "50136e40c78c4b9403000001"
145
+ "childs" : [{
146
+ "_id" : ObjectId("50136e40c78c4b9403000002"),
147
+ "name" : "Bob",
148
+ "parent" : ObjectId("50136e40c78c4b9403000001"),
149
+ "path" : "50136e40c78c4b9403000001.50136e40c78c4b9403000002"
150
+ "childs" : [{
151
+ "_id" : ObjectId("50136e40c78c4b9403000003"),
152
+ "name" : "Carol",
153
+ "parent" : ObjectId("50136e40c78c4b9403000002"),
154
+ "path" : "50136e40c78c4b9403000001.50136e40c78c4b9403000002.50136e40c78c4b9403000003"
155
+ }]
156
+ }]
157
+ }
158
+ */
159
+
160
+ });
161
+
162
+ ```
163
+
89
164
### getAncestors
90
165
91
166
Signature:
0 commit comments