I'm using express with node-mysql to implement an MVC app. I want to ask the best way to get connection of mysql in each models file. My models file is in directory models. In app.js of express, I create a connection like :
var db = mysql.createConnection({
host: config.dbHost,
user: config.dbUser,
password: config.dbPassword,
database: dbName
});
I can use :
app.use(function(req,res,next){
req.db = db;
next();
});
then in modules,models get connection by call req.db.query().. but I think it's not good for performance. What should I do ?
Edit: if I have to access different database in each request, meaning create many connections. What 's the best way to do it ?