-
Notifications
You must be signed in to change notification settings - Fork 0
/
dao.js
270 lines (232 loc) · 7.25 KB
/
dao.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/**
* Created with JetBrains WebStorm.
* User: hadoop
* Date: 13-4-1
* Time: 下午5:37
* To change this template use File | Settings | File Templates.
*/
var mongoose = require('mongoose'),
schema = mongoose.Schema,
util = require('util'),
fs = require('fs'),
config = require('./config'),
Model
;
var Dao = function(){
var uri = config.getMongoUrl();
console.log('db uri: ' + uri);
mongoose.connect(uri);
Model = require('./model')(mongoose, schema);
function create(clazz, object, callback){
new clazz(object).save(function(err, doc){
if(err) throw err;
if(callback)
callback.call(this, doc);
console.log('doc saved. ');
});
}
function get(clazz, id, callback){
clazz.findOne({
_id: id
}, function(err, doc){
if(err) throw err;
if(callback)
callback.call(this, doc);
console.log('doc getted. ');
});
}
function remove(clazz, ids){
if(!util.isArray(ids)){
ids = [ids];
}
clazz.remove({
_id: {
$in: ids
}
}, function(err){
if(err) throw err;
console.log('doc removed. ');
});
}
function update(clazz, id, object){
clazz.update({
_id: id
}, {
$set: object
}, function(err){
if(err) throw err;
console.log('doc updated. ');
});
}
function getSubNode(docs, obj, cd){
docs.forEach(function(doc){
var node = {
id: doc._id,
text: doc.name,
leaf: true
};
if(!obj.children){
obj.children = [];
}
obj.children.push(node);
Model.Tag.find({
parentId: doc._id
}).sort('name').exec(function(err, _docs){
if(err) throw err;
if(_docs && _docs.length){
cd(doc.name, _docs.length);
node.leaf = false;
getSubNode(_docs, node, cd);
}
cd(obj.tagName, -1);
});
});
//console.log('obj: ' + JSON.stringify(obj));
}
function buildTree(userId, callback){
var map = {};
var tree = {};
function checkdone(key, value){
if(!map[key]){
map[key] = value;
}else{
map[key] += value;
}
var done = 0;
for(var k in map){
done += map[k];
}
if(done == 0){
//console.log('tree: ' + JSON.stringify(tree));
callback.call(this, JSON.stringify({
children: tree
}));
}
}
Model.Tag.findOne({
userId: userId
}).exec(function(err, doc){
if(err) throw err;
if(doc){
Model.Tag.find({
parentId: doc._id
}).exec(function(err, docs){
tree = {
id: doc._id,
text: doc.name,
expanded: true
}
checkdone(tree.tagName, docs.length);
getSubNode(docs, tree, checkdone);
});
}
});
}
// Cache = redis.createClient(config.redisCfg.port, config.redisCfg.hostname);
function buildTags(nodeId, expanded, callback){
Model.Tag.find({
parentId: nodeId
}).exec(function(err, docs){
var children = [];
if(docs && docs.length){
docs.forEach(function(d){
children.push({
id: d._id,
text: d.name,
expanded: expanded
});
});
}
callback.call(this, JSON.stringify({
children: children
}));
});
}
function createSharedTag(nodeId){
Model.Tag.findOne({
parentId: nodeId
}).exec(function(err, doc){
if(err) throw err;
if(doc){
console.log('find shared tag id: ' + doc._id);
//sharedTagId = doc._id;
//callback.call(this, doc._id);
}else{
create(Model.Tag, {
name: '/',
parentId: nodeId,
desc: 'public root tag'
}, function(doc2){
console.log('create shared tag id: ' + doc2._id);
//sharedTagId = doc2._id;
//callback.call(this, doc2._id);
});
}
});
}
createSharedTag('shared');
return {
countTags: function(filter, callback){
Model.Tag.count(filter, callback);
},
getTags: function(node, expanded, callback){
buildTags(node, expanded, callback);
},
createTag: function(object, callback){
create(Model.Tag, object, callback);
},
removeTag: function(ids){
remove(Model.Tag, ids);
},
updateTag: function(id, object){
update(Model.Tag, id, object);
},
createUser: function(object, callback){
object.createTime = new Date();
create(Model.User, object, callback);
},
passwd: function(username, password, callback){
Model.User.findOneAndUpdate({
username: username
},{
$set: {
password: password
}
}, callback);
},
getUserByUsername: function(username, callback){
Model.User.findOne({
username: username
}, callback);
},
getSnippets: function(start, limit, filter, callback){
Model.Snippet.find(filter).skip(start).limit(limit).select('title short createTime lang').sort('-createTime').exec(callback);
},
getSnippet: function(id, callback){
get(Model.Snippet, mongoose.Types.ObjectId(id), callback);
},
countSnippets: function(filter, callback){
Model.Snippet.count(filter, callback);
},
createSnippet: function(object, callback){
create(Model.Snippet, object, callback);
},
updateSnippet: function(id, object){
object.updateTime = new Date();
update(Model.Snippet, id, object);
},
removeSnippet: function(ids){
remove(Model.Snippet, ids);
},
moveSnippet: function(id, tagId, callback){
Model.Snippet.findOneAndUpdate({
_id: mongoose.Types.ObjectId(id)
},{
$set: {
tagId: tagId
}
}, callback);
}
};
}
module.exports = new Dao()