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
.
我正在尝试使用Express和Sequelize构建一个基本的数据库模式。我在单独的文件中定义所有模型。我有一个文件(models / index.js),我在其中创建Sequelize类的实例,导入模型,并建立模型之间的关系。我还有多个控制器,每个控制器都需要访问从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. I'm under the impression that multiple connections to the database are not desirable. Does anybody have any suggestions to avoid multiple instances of the Sequelize class? Is it even a big problem to have multiple instances?
我关心的是两个控制器都需要models / index.js文件。每次需要models / index.js文件时,都会创建一个Sequelize类的新实例,该实例将建立与数据库的新连接。我的印象是不希望与数据库建立多个连接。有没有人建议避免Sequelize类的多个实例?拥有多个实例甚至是一个大问题?
Thanks in advance!
提前致谢!
1 个解决方案
#1
3
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.
模块在第一次加载后进行缓存。这意味着(除其他外)每次调用require('foo')将获得完全相同的返回对象,如果它将解析为同一个文件。
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.
多次调用require('foo')可能不会导致模块代码多次执行。这是一个重要特征。有了它,就可以返回“部分完成”的对象,从而允许加载传递依赖,即使它们会导致循环。
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
https://nodejs.org/api/modules.html#modules_caching
This means, that the code on models/index.js
will only be run once
这意味着,models / index.js上的代码只能运行一次
#1
3
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.
模块在第一次加载后进行缓存。这意味着(除其他外)每次调用require('foo')将获得完全相同的返回对象,如果它将解析为同一个文件。
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.
多次调用require('foo')可能不会导致模块代码多次执行。这是一个重要特征。有了它,就可以返回“部分完成”的对象,从而允许加载传递依赖,即使它们会导致循环。
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
https://nodejs.org/api/modules.html#modules_caching
This means, that the code on models/index.js
will only be run once
这意味着,models / index.js上的代码只能运行一次