npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sequelize-auto-import

v1.1.1

Published

Import sequelize models automagically

Downloads

92

Readme

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:

  • sequelizeInstace A sequelize instance generated by your app
  • pathToModels The path where your models are located, if no specified default to current directory
  • options An options object, see bellow for all the available options

Options

  • recursive: boolean Whether to search inside all subdirectories or only one level, defaults to true

  • associate: boolean When true the associate method in models will be call it when found, defaults to true

  • tableNameFormat: string | function This option specifies how the tableName for the model will be generated, when a string is passed one of the established formats will be used:

    • snakeCase A model like CustomModel.js will have custom_model as tableName

    if a function is passed you can generate the tableName as you want, for example for a model like CustomModel.js and with a function like this the tableName will be custommodel:

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)

  • exclude: Array A list of files to ignore when importing

  • schemas: Array A list of schema names to append in all models inside the schema folder (Multi-Tenancy Support)

Collaborators

License

See license