-
Notifications
You must be signed in to change notification settings - Fork 290
/
db.js
60 lines (54 loc) · 1.72 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
59
60
var mongoose = require('mongoose');
var statsd = require('./statsd');
var schema = mongoose.Schema({value: String});
var Values = mongoose.model('values', schema);
module.exports = {
connectDB : function() {
mongoose.connect(process.env.MONGODB_ADDON_URI, { useNewUrlParser: true });
},
updateGauge : function() {
Values.count(function(err, result) {
if(!err) {
statsd.gauge('values', result);
}
})
},
getVal : function(res) {
Values.find(function(err, result) {
if (err) {
console.log(err);
res.send('database error');
return
}
var values = {};
for (var i in result) {
var val = result[i];
values[val["_id"]] = val["value"]
}
var title = process.env.TITLE || 'NodeJS MongoDB demo'
res.render('index', {title, values: values});
});
},
sendVal : function(val, res) {
var request = new Values({value: val});
request.save((err, result) => {
if (err) {
console.log(err);
res.send(JSON.stringify({status: "error", value: "Error, db request failed"}));
return
}
this.updateGauge();
statsd.increment('creations');
res.status(201).send(JSON.stringify({status: "ok", value: result["value"], id: result["_id"]}));
});
},
delVal : function(id) {
Values.remove({_id: id}, (err) => {
if (err) {
console.log(err);
}
this.updateGauge();
statsd.increment('deletions');
});
}
};