Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
95 views
in Technique[技术] by (71.8m points)

node.js - How to make multiple mysql requests, best way?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...