Himmators Himmators - 1 year ago 190
Node.js Question

Sequelize multiple connections?

When using sequlize to connect to a database I create a Sequlize object like this:

sequelize = new Sequelize(settings),

I need the sequlize-object in different modules in my app. Forexample in app.js to establish the connection and models.js, where I make my models

In order to avoid passing around parameters, I would like to create new sequlize-objects in the different models where I need them. Is this ok?

Answer Source

I don't think that'll work (well). Instead, what you could do is move the creation of the instance to a module, and use that module whenever you need to access it:

// database.js
var sequelize = require('sequelize');
var settings  = ...;

module.exports = new Sequelize(settings);

// app.js
var sequelize = require('./database');

// models.js
var sequelize = require('./database');

Because calls to require are cached, the same object will be shared amongst your files.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download