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

Gntem
  • 6,949
  • 2
  • 35
  • 48
  • Possible duplicate of [How to properly pass mysql connection to routes with express.js](http://stackoverflow.com/questions/16800418/how-to-properly-pass-mysql-connection-to-routes-with-express-js) – dev Mar 28 '16 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 Mar 29 '16 at 06:19
  • There's a good reason I flagged this question. [This](https://meta.stackoverflow.com/a/315475/3089595) sums up the reasoning nicely, and the fact that it has (useful) links to duplicates itself is another good illustration. – dev Mar 29 '16 at 06:46

2 Answers2

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.
rdrey
  • 9,379
  • 4
  • 40
  • 52
  • this method means that you don't have to pass your `mysqlConnection` into every module as in @Vadim's answer. – rdrey Aug 07 '12 at 12:05
  • 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 .. – Gntem Aug 07 '12 at 12:10
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;
};
Vadim Baryshev
  • 25,689
  • 4
  • 56
  • 48
  • 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 – Gntem Aug 07 '12 at 11:41