sequelize-require-models
Require all Sequelize models from a folder and associate them together.
Last updated 2 years ago by zhouzi .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install sequelize-require-models 
SYNC missed versions from official npm registry.

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

Current Tags

  • 0.1.0                                ...           latest (2 years ago)

1 Versions

  • 0.1.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (0)
None
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |