-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
58 lines (50 loc) · 1.2 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
52
53
54
55
56
57
58
var mongo = require('mongodb'),
ObjectID = require('mongodb').ObjectID;
var mongoUri = (process.env.OPENSHIFT_MONGODB_DB_HOST ?
'mongodb://admin:E3W9bKIybng5@'
+ process.env.OPENSHIFT_MONGODB_DB_HOST
+ ':' + process.env.OPENSHIFT_MONGODB_DB_PORT
+ '/apidocs'
: 'mongodb://localhost/test');
var mongoDB;
mongo.Db.connect(mongoUri, function (err, db) {
if(err)
console.log(err);
mongoDB = db;
});
function get(col,params,callback) {
var collection = mongoDB.collection(col);
collection.find(params).toArray(callback);
}
function put(col,items,callback) {
var api = mongoDB.collection(col);
api.insert(items,{w:1},
function(err,res) {
if(err)
console.log(err);
callback(err,res);
});
}
function update(col,items,params,callback) {
var api = mongoDB.collection(col);
api.update(items,params, {upsert : true},
function(err,res) {
if(err)
console.log(err);
callback(err,res);
});
}
function del(col,items,callback) {
var api = mongoDB.collection(col);
api.insert(items,{w:1},
function(err,res) {
if(err)
console.log(err);
callback(err,res);
});
}
exports.get = get
exports.put = put
exports.update = update
exports.del = del
exports.ObjectID = ObjectID