I am creating a util module that I'm using to communicate with a MS-SQL database. I want each public method to return a promise. I started with a private function that executes a DB query and returns a promise. The function establishes a connection to the DB then it executes the query. My current code just lets all errors fall through to a single catch, and I'm not sure if this is a best practice or not.
I was hoping someone could answer that for me and any other suggestions about code read ability or anything.
var config = require('./mainConfig')();
var sql = require('mssql');
var connection = new sql.Connection(config.db.sql);
var Promise = require("bluebird");
exports.getAll = function(table){
return executeQueryStatment("select * from " + table);
};
function executeQueryStatment(query, newconnection) {
var conn = newconnection || connection;
var request = new sql.Request(conn);
return new Promise(function(res, rej){
conn.connect().then(function(){
return request.query(query);
}).then(function (data) {
res(data);
conn.close();
})
.catch(function(err) {
// plan on handling all errors here
// is it better to handle each error individually upstream?
console.log(err);
conn.close();
})
});
};