-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdb.js
73 lines (66 loc) · 2.07 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
61
62
63
64
65
66
67
68
69
70
71
72
73
var pg = require('pg');
var statsd = require('./statsd');
var pool;
var createTableText = `
CREATE TABLE IF NOT EXISTS values (
id SERIAL PRIMARY KEY,
data JSONB
);
`;
module.exports = {
connectDB : function() {
pool = new pg.Pool({
connectionString: process.env.POSTGRESQL_ADDON_DIRECT_URI || process.env.POSTGRESQL_ADDON_URI,
})
pool.query(createTableText, [], (err, res) => {
if (err) {
throw err
}
})
},
updateGauge : function() {
pool.query('SELECT COUNT(1) FROM values;', [], (err, res) => {
if (err) {
throw err
}
statsd.gauge('values', res.rows[0]);
})
},
getVal : function(result) {
pool.query('SELECT * FROM values;', [], (err, res) => {
if (err) {
console.log(err);
res.send('database error');
return
}
var values = {};
for (var i in res.rows) {
var val = res.rows[i];
values[val["id"]] = val["data"]["value"]
}
result.render('index', {title: 'NodeJS PostgreSQL demo', values: values});
});
},
sendVal : function(val, res) {
pool.query('INSERT INTO values(data) VALUES($1) RETURNING *', [{'value': val}], (err, queryData) => {
if (err) {
console.log(err);
res.send(JSON.stringify({status: "error", value: "Error, db request failed"}));
return
}
this.updateGauge();
statsd.increment('creations');
result = queryData.rows[0];
res.status(201).send(JSON.stringify({status: "ok", value: result["data"].value, id: result["id"]}));
});
},
delVal : function(id) {
pool.query('DELETE FROM values where id = $1', [id], (err, result) => {
if (err) {
console.log(err);
}
this.updateGauge();
statsd.increment('deletions');
});
}
};