sequelize-require-models

0.1.0 • Public • Published

sequelize-require-models

Require all Sequelize models from a folder and associate them together.

Installation

npm install sequelize-require-models --save

Example

Given the following structure:

models/
    User.js
    Post.js
    index.js

Here's what the index.js file could contain:

const Sequelize = require('sequelize');
const requireModels = require('sequelize-require-models');
 
const database = new Sequelize('cms', 'root', 'password', {
    host: 'localhost',
    dialect: 'mysql'
});
const models = requireModels(database, __dirname);
 
module.exports = Object.assign({ database }, models);

Models can declare an "associate" function for associations. For example, here's what User.js may contain:

const Sequelize = require('sequelize');
 
function defineUser(database) {
    const User = database.define('user', {
        username: {
            type: Sequelize.STRING
        },
        password: {
            type: Sequelize.STRING
        }
    });
    User.associate = ({ Post }) => {
        User.hasMany(Post);
    };
    return User;
}
 
module.exports = defineUser;

Documentation

requireModels(database: Sequelize, folder: string)

  • database: an instance of Sequelize
  • folder: the folder to look into

Package Sidebar

Install

npm i sequelize-require-models

Weekly Downloads

1

Version

0.1.0

License

MIT

Unpacked Size

3.79 kB

Total Files

5

Last publish

Collaborators

  • gabinzhouzi