diff options
Diffstat (limited to 'lib/db.js')
-rw-r--r-- | lib/db.js | 25 |
1 files changed, 14 insertions, 11 deletions
@@ -18,10 +18,7 @@ var db = { }; function getDBClient() { - if (config.debug) - return new pg.Client(config.postgresqlstring); - else - return new pg.Client(process.env.DATABASE_URL); + return new pg.Client(process.env.DATABASE_URL || config.postgresqlstring); } function readFromFile(callback) { @@ -49,12 +46,14 @@ function newToDB(id, owner, body, callback) { var client = getDBClient(); client.connect(function (err) { if (err) { + client.end(); callback(err, null); return console.error('could not connect to postgres', err); } var newnotequery = util.format(insertquery, id, owner, body); //console.log(newnotequery); client.query(newnotequery, function (err, result) { + client.end(); if (err) { callback(err, null); return console.error("new note to db failed: " + err); @@ -62,7 +61,6 @@ function newToDB(id, owner, body, callback) { if (config.debug) console.log("new note to db success"); callback(null, result); - client.end(); } }); }); @@ -72,23 +70,25 @@ function readFromDB(id, callback) { var client = getDBClient(); client.connect(function (err) { if (err) { + client.end(); callback(err, null); return console.error('could not connect to postgres', err); } var readquery = util.format(selectquery, id); //console.log(readquery); client.query(readquery, function (err, result) { + client.end(); if (err) { callback(err, null); return console.error("read from db failed: " + err); } else { //console.log(result.rows); if (result.rows.length <= 0) { - callback("not found note in db", null); + callback("not found note in db: " + id, null); } else { - console.log("read from db success"); + if(config.debug) + console.log("read from db success"); callback(null, result); - client.end(); } } }); @@ -99,12 +99,14 @@ function saveToDB(id, title, data, callback) { var client = getDBClient(); client.connect(function (err) { if (err) { + client.end(); callback(err, null); return console.error('could not connect to postgres', err); } var savequery = util.format(updatequery, title, data, id); //console.log(savequery); client.query(savequery, function (err, result) { + client.end(); if (err) { callback(err, null); return console.error("save to db failed: " + err); @@ -112,7 +114,6 @@ function saveToDB(id, title, data, callback) { if (config.debug) console.log("save to db success"); callback(null, result); - client.end(); } }); }); @@ -122,10 +123,12 @@ function countFromDB(callback) { var client = getDBClient(); client.connect(function (err) { if (err) { + client.end(); callback(err, null); return console.error('could not connect to postgres', err); } client.query(countquery, function (err, result) { + client.end(); if (err) { callback(err, null); return console.error("count from db failed: " + err); @@ -134,9 +137,9 @@ function countFromDB(callback) { if (result.rows.length <= 0) { callback("not found note in db", null); } else { - console.log("count from db success"); + if(config.debug) + console.log("count from db success"); callback(null, result); - client.end(); } } }); |