Home

Awesome

sequelize-auto-import

NPM VersionLicenseBuild Status

Import sequelize models automagically

This module let you import sequelize models defined in a directory and help you define each model with metadata generated based on directory structure.

Installation

npm install sequelize-auto-import

Usage

Based on a directory like this:

/path/to/models
├── Item.js
├── Service.js
├── accounts
│   └── Person.js
├── base
│   ├── Animal.js
│   └── Contact.js
└── index.js

Model definitions inside *.js files can be something like this:

module.exports = function(sequelize, DataTypes, meta) {
  // using the meta object to help us define the model
  return sequelize.define(meta.modelName, {
    id: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      primaryKey: true
    },
    name: {
      type: DataTypes.STRING(250),
      allowNull: true
    }
  }, {
    timestamps: false,
    freezeTableName: true,
    tableName: meta.tableName,
    schema: meta.schema
  });
};

As you can see we are using the meta object generated by sequelize-auto-import to help us define the model, of course you can choose to not use these values and put what you want.

The meta object will have the following properties based on the location of the model inside the directory:

{
  schema: string?, // value to use in the `schema` option of `sequelize.define`
  schemaName: string, // the schema name of the model for your convenience
  modelName: string, // value to use as the model name of `sequelize.define`
  tableName: string, // value to use as the `tableName` option of `sequelize.define`
  completeTableName: string, // the result of `schemaName` + `separator` + `tableName` for your convenience
  separator: string // the separator that we use for your convenience
}

For example for the Item.js model the values of meta will be:

{
  schema: undefined,
  schemaName: '',
  modelName: 'Item',
  tableName: 'item',
  completeTableName: 'item',
  separator: '.'
}

and for the accounts/Person.js model:

{
  schema: 'accounts',
  schemaName: 'accounts',
  modelName: 'accounts.Person',
  tableName: 'person',
  completeTableName: 'accounts.person',
  separator: '.'
}

you can customize how the tableName is generated, see options.

With the models defined we can import all the models inside /path/to/models directory and its subdirectories using the following in index.js:

var Sequelize = require('sequelize');

var sequelize = new Sequelize('test', 'test', 'test', {
  dialect: 'mysql',
  host: 'localhost',
  port: 3306
});

// you can pass options, see bellow for details
var models = require('sequelize-auto-import')(sequelize, '/path/to/models');

// export all the models for your convenience
module.exports = models;

Now you can access the models in this way:

models.Item
models.Service
models.accounts.Person
models.base.Animal
models.base.Contact

Note that sequelize-auto-import will recursively search for js files inside the specified directory.

Optionally if your models have a associate method sequelize-auto-import will call it passing all the loaded models as a parameter, in that method you can define relations between your models if you want.

Multi-Tenancy Support (Shared Database and Separate Schemas)

Also you can create a schema folder to create multiples schemas using the same models. For example:

/path/to/models
├── user.js
├── schema
│   ├── product.js
│   └── contact.js
└── index.js

And with the following configuration you can indicate the schemas:

var models = require('sequelize-auto-import')(sequelize, '/path/to/models', {
   schemas: ['company1', 'company2', 'company3']
});

And access the models in this way:

models.user
models.company1.product
models.company1.contact
models.company2.product
models.company2.contact
models.company3.product
models.company3.contact

API

There is only one function exported with the following parameters:

Options

tableNameFormat: function(modelName) {
  // modelName === 'CustomModel'
  return modelName.toLowerCase();
}

(for now we only support snakeCase as one of the available formats, open a PR if you want other formats)

Collaborators

License

See license