-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgres.js
47 lines (38 loc) · 1.21 KB
/
postgres.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
/**
* Created with JetBrains WebStorm.
* User: vijay
* Date: 29/2/16
* Time: 11:22 AM
* To change this template use File | Settings | File Templates.
*/
var pg = require('pg');
var conString = 'postgres://postgres:[email protected]:5432/';
var _ = require('lodash');
var executeQueryWithParameters = function(dbName, query, params, callback){
getConnection(dbName, function(error, client, done){
if(error){
callback(error);
return;
}
console.log("Got connection for executing query");
client.query(query, params,function(err, result) {
done();
console.log("client.query result : " + err);
if(err) {
callback(err);
return console.error('error running query', err);
}
else{
var r = {};
r.data = result.rows;
callback(null,r);
}
});
});
};
exports.executeQueryWithParameters = executeQueryWithParameters;
var getConnection = function(dbName, callback){
console.log("Establishing connection with postgre");
pg.connect(conString + dbName, callback);
};
module.exports.getConnection = getConnection;