-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconnectDB.js
More file actions
47 lines (42 loc) · 1.26 KB
/
connectDB.js
File metadata and controls
47 lines (42 loc) · 1.26 KB
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
//load module node-postgres
var postgre = require('pg');
//expose the function pgRun in order to run queries against your postgres database
module.exports = {
pgRun: pgRun
};
function pgRun(runObject, callback) {
postgre.connect(getConnectionString(), function(err, client, done) {
client.query(runObject.query, function(err, res) {
if(err) {
if(runObject.insertUpdate) {
client.query('ROLLBACK', function(err) {
done(err);
});
}else {
done();
}
callback(err);
}else if(res) {
if(runObject.insertUpdate) {
client.query('COMMIT', done);
}else {
done();
}
callback(null, res);
}else {
//fallback / you can handle this as you wish.
callback({err:'error'})
}
});
});
}
function getConnectionString() {
return {
user: "YOUR_USER_ACCOUNT",
password: "YOUR_PASSWORD",
database: "YOUR_DATABASE",
port: 0, //YOUR_PORT
host: "xxx-xxx-xx-xxx-xx.xxxxxx-x.amazonaws.com",
ssl: true
};
}