I am relatively new to the Node JS and I would like to query different databases. I wish I could group the query result into one array. I don't know the best way. i have a json example where i get database data from. Databases can be 2 or > 2. I thought if it made sense to use a loop.
The query is the same for each database, the databases have the same tables
SELECT * FROM my_db.USER (this is an example)
const express = require('express')
const mysql = require('mysql2')
const app = express()
const database = [
{
"host": "127.0.0.1",
"user": "root",
"password": "administrator",
"database": "demo_db"
},
{
"host": "127.0.0.1",
"user": "root",
"password": "administrator",
"database": "demo_db"
},
{
"host": "127.0.0.1",
"user": "root",
"password": "administrator",
"database": "demo_db"
}
];
const connections = database.map(c => mysql.createConnection(c))
app.get('/', function (req, res) {
const sql = 'SELECT * FROM users'
Promise.all(connections.map(conn => conn.execute(sql, (err, ris, fileds) => {
console.log(ris); //this send the result of query
}))).then(results => {
res.send(results);
})
app.listen(5000, () => {
console.log('Server started')
})
How can I adapt this code to multiple database connection? Thanks in advance