Hi. I am new to node.js I am trying to create a REST service (using Express) that access data from a SQL Server. I am using mssql module with Tedious drivers. I created this module:
/*
dbhelper.js
Helper para acceso a base de datos
Victor Espina S
NOI Quality Software, Chile
*/
function dbhelper(params) {
// Properties
this.dbcp = params.dbcp; // DB ConnectionsPool object (created in app.js)
this.config = require("../config");
// Methods
this.runQuery = runQuery;
this.runSP = runSP;
this.getRow = getRow;
this.getScalar = getScalar;
}
// runQuery
// Ejecuta una consulta y devuelve el resultado
//
function runQuery(query) {
var sql = require('mssql');
var dbcp = this.dbcp;
var runSelect = function(pool) {
console.timeEnd("connecting");
return new Promise(function(resolve, reject) {
console.time("request");
var request = new sql.Request(pool);
console.timeEnd("request");
console.time("querying");
request.query(query)
.then(resolve)
.catch(reject);
});
}
var returnResults = function(result) {
console.timeEnd("querying");
return new Promise(function(resolve, reject) {
console.time("closing");
dbcp.close();
console.timeEnd("closing");
resolve(result);
});
}
var resolver = function(resolve, reject) {
try {
console.time("connecting");
dbcp.connect()
.then(runSelect)
.then(returnResults)
.then(resolve)
.catch(reject);
} catch (err) {
reject(err);
}
}
return new Promise(resolver);
}
module.exports = dbhelper;
function dbTest(req, res, next) {
var dbhelper = require("../modules/dbhelper");
var db = new dbhelper({ dbcp: res.app.locals.dbcp });
db.runQuery("select clave,texto from registro order by clave")
.then(function(result) {
res.send(result);
});
}
Everything works great, but the response times are completely unaceptable: 10s for a 10 rows query!!! Check this image from the node noconsole after two consecutives requests to the server:
Any ideas about what may be going on ? I tried this same query from a client app and it takes less than 1s. I am running node.js under OSX and the SQL Server (2016) is running under a virtualized Windows Server 2016, running in the same osx machine.