I defined mysql connection with all parameters necessary to app.js, how can make visible to other scripts in routes/ by default, without requiring or redefining mysql parameters, just using client.query(..)?
-
Possible duplicate of How to properly pass mysql connection to routes with express.jsdev– dev2016-03-28 23:21:35 +00:00Commented Mar 28, 2016 at 23:21
-
@EdJr whatever.. did you even looked at the date difference between the questions? 7-aug-2012 and 28-may-2013 ... you see the difference right?Gntem– Gntem2016-03-29 06:19:47 +00:00Commented Mar 29, 2016 at 6:19
-
There's a good reason I flagged this question. This sums up the reasoning nicely, and the fact that it has (useful) links to duplicates itself is another good illustration.dev– dev2016-03-29 06:46:20 +00:00Commented Mar 29, 2016 at 6:46
2 Answers
A pattern I use is to set up my db object in a module once and export it: (let's call it utils/mySQL.js)
//I haven't used real mysql in node so excuse the pseudo-syntax:
var db = require('mysql-driver-thingy');
db.connect('localhost', 'sqlport', options...);
db.otherSetupFunctions();
console.log("Finished db setup. You should only see this message once! Cool.");
module.exports = db;
And then I can require the db object everywhere I need it. Since requires are cached, this does't actually call the setup methods multiple times.
In app.js:
var db = require('./utils/mySQL.js');
...
In models/user.js:
var db = require('../utils/mySQL.js');
...
A final option, which isn't recommended, is to pollute the global namespace. This seems to be the answer you're really after:
//set up your db
...
// and now make it available everywhere:
global.client = db.client
You can now magically use the client object in all your modules, without even requiring it.
There are many reasons globals are bad, though:
- If your code and other code define globals, they could conflict and overwrite each other.
- It's hard to find where you defined the
db/clientobject, etc.
2 Comments
mysqlConnection into every module as in @Vadim's answer.user.js the mysql connection, while the other answer, passes it by requiring once and you can do something like module.exports.list=function(req,res,mysql) .. i think ..You can inject mysql connection into other scripts like this:
app.js
var mysqlConnection = new Conection(params);
require('controller/main.js)(mysqlConnection);
main.js
module.exports = function(mysqlConnection) {
// You can access your mysql connection here
};
UPDATE:
You can inject several variables same way. Also you still can export methods from module if you need this:
app.js
var mysqlConnection = new Conection(params);
var news = require('model/news.js)(app, mysqlConnection);
news.list(function(err, news) {
// Do something
});
news.js
module.exports = function(app, mysqlConnection) {
var methods = {};
// mysql connection and app available from here
methods.list = function(cb) {
mysqlConnection.list(function(err, data) {
cb(err, data);
});
};
return methods;
};
1 Comment
mysqlConnection using require, i pass the app variable, can i pass additional variables there? like require('..')(variable,variable) didn't find much documentation on this