I need to make multiple SQL requests, but only if there needed. I'm not sure if this is the best way of doing it, I was wondering if there is a easier or more efficient way?
var async = require('async');
var mysql = require('mysql');
var connection = mysql.createConnection({ host: 'x.x.x.x', user: 'user', password: 'password', database: 'site' });
connection.connect();
var q1 = true
var q2 = true
var q3 = true
// might be false
var qs = {}
if (q1) {
qs['q1'] = function (parallelCb) {
connection.query(`SELECT * FROM account;`, function (error, results, fields) {
parallelCb(null, { err: error, res: results, fields: fields })
})
}
};
if (q2) {
qs['q2'] = function (parallelCb) {
connection.query(`SELECT * FROM otc;`, function (error, results, fields) {
parallelCb(null, { err: error, res: results, fields: fields })
})
}
};
if (q3) {
qs['q3'] = function (parallelCb) {
connection.query(`SELECT * FROM session;`, function (error, results, fields) {
parallelCb(null, { err: error, res: results, fields: fields })
})
}
};
async.parallel(qs, function (err, results) {
if (q1) { console.log(results.q1.res) };
if (q2) { console.log(results.q2.res) };
if (q3) { console.log(results.q3.res) };
// do more stuff with it
});
Thank you!
question from:
https://stackoverflow.com/questions/65911105/how-to-make-multiple-mysql-requests-best-way 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…