1

I'm trying to build out a basic database schema using Express and Sequelize. I define all of the models in separate files. I have a single file (models/index.js) where I create an instance of the Sequelize class, import the models, and establish the relationships among the models. I also have multiple controllers that each need to have access to the models exported from models/index.js.

Here's the file where the models are imported:

// models/index.js

var Sequelize = require('sequelize');
var sequelize = new Sequelize('myApp', 'username', 'password');

var User = sequelize.import('./users');
var Contact = sequelize.import('./contacts');
var Conversation = sequelize.import('./conversations');
var Medium = sequelize.import('./mediums');

User.hasMany(Contact);
Contact.belongsTo(User);

Contact.hasMany(Conversation);
Conversation.belongsTo(Contact);

Medium.hasMany(Conversation);
Conversation.belongsTo(Medium);

module.exports.Sequelize = Sequelize;
module.exports.sequelize = sequelize;

module.exports.User = User;
module.exports.Contact = Contact;
module.exports.Conversation = Conversation;
module.exports.Medium = Medium;

Here's one of the controllers that needs access to the models.

// controllers/users.js

var models = require('../models');

module.exports.addUser = function () {
};

module.exports.getUser = function () {
};

Here's another controller that needs access to the models.

// controllers/contacts.js

var models = require('../models');

module.exports.addContact = function () {
};

module.exports.getContact = function () {
};

module.exports.getAllContacts = function () {
};

My concern relates to the fact that both controllers require the models/index.js file. Each time the models/index.js file is required, a new instance of the Sequelize class is created, which establishes a new connection to the database. Does anybody have any suggestions to avoid multiple instances of the Sequelize class?

Thanks in advance!

Brandon
  • 891
  • 8
  • 11
  • You might want to have a look [here](http://stackoverflow.com/questions/13179109/singleton-pattern-in-nodejs-is-it-needed) – Amit Aug 09 '15 at 21:44
  • at the time of writing this comment sqeuelize.import is deprecated as indicated here: https://sequelize.org/docs/v6/moved/models-definition/ – epeleg Feb 21 '23 at 18:06

1 Answers1

2

Modules (files) are cached in node:

Modules are cached after the first time they are loaded. This means (among other things) that every call to require('foo') will get exactly the same object returned, if it would resolve to the same file.

Multiple calls to require('foo') may not cause the module code to be executed multiple times. This is an important feature. With it, "partially done" objects can be returned, thus allowing transitive dependencies to be loaded even when they would cause cycles.

If you want to have a module execute code multiple times, then export a function, and call that function.

https://nodejs.org/api/modules.html#modules_caching

This means, that the code on models/index.js will only be run once

Community
  • 1
  • 1
Jan Aagaard Meier
  • 28,078
  • 8
  • 95
  • 66