-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
33 lines (27 loc) · 825 Bytes
/
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
'use strict';
var pg = require('pg');
var Q = require('q');
var jsonfile = require('jsonfile');
var path = require('path');
var client, deferred;
function getConnectionString () {
var json = jsonfile.readFileSync(path.resolve(__dirname, 'database.json'));
var conf = json.dev;
return 'postgres://'+ conf.user +':'+ conf.password +'@'+ conf.host +'/'+ conf.database;
}
module.exports = {
getClient: function() {
if ( ! client ) {
client = new pg.Client(getConnectionString());
deferred = Q.defer();
client.connect(function(err) {
if (err) {
deferred.reject(err);
} else {
deferred.resolve(client);
}
});
}
return deferred.promise;
},
};