forked from soygul/koan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo.js
45 lines (38 loc) · 1.38 KB
/
mongo.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
'use strict';
/**
* MongoDB configuration using generators (with the help of co-mongo package).
* You can require this config file in your controllers and start using named collections directly.
* See /controllers directory for sample usage.
*/
var comongo = require('co-mongo'),
connect = comongo.connect,
config = require('./config');
// extending and exposing top co-mongo namespace like this is not optimal but it saves the user from one extra require();
module.exports = comongo;
/**
* Opens a new connection to the mongo database, closing the existing one if exists.
*/
comongo.connect = function *() {
if (comongo.db) {
yield comongo.db.close();
}
// export mongo db instance
var db = comongo.db = yield connect(config.mongo.url);
// export default collections
comongo.counters = yield db.collection('counters');
comongo.users = yield db.collection('users');
comongo.posts = yield db.collection('posts');
};
/**
* Retrieves the next sequence number for the given counter (indicated by @counterName).
* Useful for generating sequential integer IDs for certain collections (i.e. user collection).
*/
comongo.getNextSequence = function *(counterName) {
var results = yield comongo.counters.findAndModify(
{_id: counterName},
[],
{$inc: {seq: 1}},
{new: true}
);
return results[0].seq;
};