forked from fengmk2/nodeblog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
51 lines (46 loc) · 1.8 KB
/
db.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
var config = require('./config');
var mongo = require('mongoskin');
var dburl = '';
var uri = 'mongodb://';
if (config.db_options.user && config.db_options.password) {
dburl += config.db_options.user + ':' + config.db_options.password + '@';
}
dburl += config.db_options.host + ':' + config.db_options.port
+ '/' + config.db_options.database;
var db = module.exports = mongo.db(dburl);
db.ObjectID = db.db.bson_serializer.ObjectID;
db.bind('posts', {
list: function(query, options, callback) {
return this.findItems(query, options, function(err, posts) {
if(posts && posts.length > 0) {
var ids = [];
for(var i = 0, l = posts.length; i < l; i++) {
var post = posts[i];
var author_id = typeof post.author_id === 'string' ? db.ObjectID(post.author_id) : post.author_id;
ids.push(author_id);
}
db.users.findItems({_id: {$in: ids}}, function(err, users) {
if(users) {
var map = {};
for(var i = 0, l = users.length; i < l; i++) {
var user = users[i];
map[user._id] = user;
}
for(var i = 0, l = posts.length; i < l; i++) {
var post = posts[i];
post.author = map[post.author_id];
}
}
callback(err, posts);
});
} else {
callback(err, posts);
}
});
}
});
db.bind('users');
db.users.ensureIndex({uid: 1}, {unique: true}, function() {});
//tag用
db.posts.ensureIndex({tags: 1}, {}, function() {});
db.bind('comments');