From ffa799fa0fa762f74ac04b46b670ab981c715fbf Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Thu, 12 Jul 2012 12:44:18 +0200 Subject: o Proper error handling when the postgres db is down. o Doing a full COUNT() to find total number of rows. --- routes/index.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/routes/index.js b/routes/index.js index 782d361..7c9d170 100644 --- a/routes/index.js +++ b/routes/index.js @@ -44,7 +44,7 @@ function pager(req, count) { }; } -function connect(cb) { +function connect(res, cb) { pg.connect(process.env.DATABASE_URL, function(err, client) { if(err) { res.send('Unable to connect to PostgreSQL.', {'Content-Type': 'text/plain'}, 500); @@ -167,11 +167,11 @@ exports.index = method({ }); function getDepartments(req, res) { - connect(function(client) { - var sql = 'SELECT n_live_tup FROM pg_stat_user_tables WHERE relname=\'departments\''; + connect(res, function(client) { + var sql = 'SELECT COUNT(dept_no) AS count FROM departments'; var sql2 = 'SELECT dept_no, dept_name FROM departments OFFSET $1 LIMIT $2'; client.query(sql, after(res, function(rs) { - var p = pager(req, parseInt(rs.rows[0].n_live_tup)); + var p = pager(req, parseInt(rs.rows[0].count)); client.query(sql2, [ p.offset, p.limit ], after(res, function(rs2) { switch(req.accept.types.getBestMatch(['text/html', 'application/vnd.collection+json'])) { case 'text/html': @@ -238,7 +238,7 @@ exports.department = method({ }); function getEmployeesInDepartment(req, res) { - connect(function(client) { + connect(res, function(client) { var dept_no = req.params.dept_no; // TODO: Add dept_name to view // TODO: Add manager as a link @@ -283,7 +283,7 @@ exports.employeesInDepartment = method({ }); function getEmployees(req, res) { - connect(function(client) { + connect(res, function(client) { var emp_no = req.params.emp_no; var sql1, sql2; var sql1Params = []; @@ -351,7 +351,7 @@ exports.employees = method({ }); function getEmployee(req, res) { - connect(function(client) { + connect(res, function(client) { var emp_no = req.params.emp_no; var sql = 'SELECT * FROM employees WHERE employees.emp_no=$1'; client.query(sql, [ emp_no ], after(res, function(rs) { @@ -394,7 +394,7 @@ function getEmployee(req, res) { } function deleteEmployee(req, res) { - connect(function(client) { + connect(res, function(client) { var emp_no = parseInt(req.params.emp_no) || 0; if(emp_no < 10000 || emp_no > 999999) { return send_text(res, 404, 'Illegal emp_no.'); -- cgit v1.2.3