0

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(..)?

3
  • Possible duplicate of How to properly pass mysql connection to routes with express.js Commented 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? Commented 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. Commented Mar 29, 2016 at 6:46

2 Answers 2

4

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/client object, etc.
Sign up to request clarification or add additional context in comments.

2 Comments

this method means that you don't have to pass your mysqlConnection into every module as in @Vadim's answer.
again you require in 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 ..
3

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

where you have 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.