npm

Ready to take your JavaScript development to the next level? Meet npm Enterprise - the ultimate in enterprise JavaScript.Learn more »

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

install

npm i sequelize-require-models

Downloadsweekly downloads

2

version

0.1.0

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability