I have a file (db_slave.js) that looks like this:
var mysql = require('mysql')
var db = require('./db')
var conn = mysql.createConnection({
host: db.host,
user: db.user,
password: db.password
});
conn.connect(function (err) {
if (err) throw err;
console.log("Connected to db");
});
module.exports = conn;
so I want one of my files to have a permanent connection to the db because many other files will request this file to execute queries. my question is though, when other files call this file, will this file make new DB connections or only keep this one conn variable?
The other files would call this file like this:
var sql = require('./db_slave')
sql.query("select 2", function (err, result) {
if (err) console.log(err)
console.log(result)
});
so are there multiple instances of the DB_slave or multiple connections?
Is there a better way to do this?