Skip to content
Snippets Groups Projects
Commit d1d53fb2 authored by Vytor Calixto's avatar Vytor Calixto :space_invader:
Browse files

Add multiquery execution

parent 0fb62b65
No related branches found
No related tags found
2 merge requests!116Release v1.0.0,!85Indicador infraestrutura
Pipeline #
...@@ -40,4 +40,10 @@ function execSqlQuery(sqlQuery, sqlQueryParams = []) { ...@@ -40,4 +40,10 @@ function execSqlQuery(sqlQuery, sqlQueryParams = []) {
}); });
} }
module.exports = execSqlQuery; function execMultiQuery(querySet = []) {
// Issue all queries concurrently to the database, for every query object in the iterable
// NOTE: Array.map() returns a copy of the original array with each object 'mapped'.
return querySet.map((qry) => { return execSqlQuery(qry.toString()); });
}
module.exports = {execSqlQuery, execMultiQuery};
const libs = `${process.cwd()}/libs`; const libs = `${process.cwd()}/libs`;
const log = require(`${libs}/log`)(module); const log = require(`${libs}/log`)(module);
const execQuery = require(`${libs}/db/query_exec`); const db = require(`${libs}/db/query_exec`);
// Middleware that executes a query defined by a squel object in req.sql // Middleware that executes a query defined by a squel object in req.sql
function query(req, res, next) { function query(req, res, next) {
let sql = req.sql.toParam(); let sql = req.sql.toParam();
log.info(`Executando query ${req.sql.toString()}`); log.info(`Executando query ${req.sql.toString()}`);
execQuery(sql.text, sql.values).then((result) => { execute(sql.text, sql.values, (err, result) => {
req.result = result; if(err) {
next(); log.error(err.stack);
next(new Error('Request could not be satisfied due to a database error.'));
} else {
req.result = result;
next();
}
});
}
function execute(text, values, cb) {
db.execSqlQuery(text, values).then((result) => {
cb(null, result);
}, (error) => { }, (error) => {
log.error(error.stack); log.error(error.stack);
next(new Error('Request could not be satisfied due to a database error.')); cb(new Error('Request could not be satisfied due to a database error.'));
}); });
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment