Importing sequelize
modules with ease.
npm install sequelize-import
Let we say we have following directory structure:
/path/to/models
├── Contacts
│ └── Contact.js
└── User.js
Model definitions inside *.js
files can be something like this:
module.exports = function (sequelize, DataTypes) {
return sequelize.define('User', {
email: DataTypes.STRING,
password: DataTypes.STRING
});
};
To include all defined models inside /path/to/models
directory and its subdirectories you can use following:
var models = require('sequelize-import')('/path/to/models', sequelize_connection, {
exclude: ['index.js']
});
Now you can access to Contact
and User
models in this way:
models.User
models.Contacts.Contact
Note: The model name is how it will be exposed, e.g: javascript var User = sequelize.define('User', {}, {});
will be exposed as javascript models.User
.
Note that sequelize-import
will recursively search for js
files inside specified directory.
With this you can separate models definitions into multiple files, and then load it into some central place, then define relations between models, etc.
List of files to exclude when generating models
{
exclude: ['index.js', 'something.js']
}
You can send me mail to pusic007@gmail.com if you have any questions.