{"id":16531296,"url":"https://github.com/bjrmatos/sequelize-auto-import","last_synced_at":"2025-03-21T09:32:02.180Z","repository":{"id":72388939,"uuid":"59373488","full_name":"bjrmatos/sequelize-auto-import","owner":"bjrmatos","description":"Import sequelize models automagically :sparkles:","archived":false,"fork":false,"pushed_at":"2020-07-09T09:47:48.000Z","size":36,"stargazers_count":16,"open_issues_count":8,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-17T23:49:55.033Z","etag":null,"topics":["models","sequelize","sequelizejs"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bjrmatos.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-05-21T17:41:25.000Z","updated_at":"2025-01-25T05:03:22.000Z","dependencies_parsed_at":"2023-04-11T16:46:44.052Z","dependency_job_id":null,"html_url":"https://github.com/bjrmatos/sequelize-auto-import","commit_stats":{"total_commits":21,"total_committers":3,"mean_commits":7.0,"dds":"0.23809523809523814","last_synced_commit":"93d53be4a5202084cd28c423bc31bdd007d87ec1"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bjrmatos%2Fsequelize-auto-import","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bjrmatos%2Fsequelize-auto-import/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bjrmatos%2Fsequelize-auto-import/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bjrmatos%2Fsequelize-auto-import/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bjrmatos","download_url":"https://codeload.github.com/bjrmatos/sequelize-auto-import/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244772348,"owners_count":20507970,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["models","sequelize","sequelizejs"],"created_at":"2024-10-11T18:08:25.473Z","updated_at":"2025-03-21T09:32:02.174Z","avatar_url":"https://github.com/bjrmatos.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"sequelize-auto-import\n=====================\n\n[![NPM Version](http://img.shields.io/npm/v/sequelize-auto-import.svg?style=flat-square)](https://npmjs.com/package/sequelize-auto-import)[![License](http://img.shields.io/npm/l/sequelize-auto-import.svg?style=flat-square)](http://opensource.org/licenses/MIT)[![Build Status](https://travis-ci.org/bjrmatos/sequelize-auto-import.png?branch=master)](https://travis-ci.org/bjrmatos/sequelize-auto-import)\n\n\u003e **Import sequelize models automagically**\n\nThis module let you import sequelize models defined in a directory and help you define each model with metadata generated based on directory structure.\n\nInstallation\n------------\n\n```bash\nnpm install sequelize-auto-import\n```\n\nUsage\n-----\n\nBased on a directory like this:\n\n```\n/path/to/models\n├── Item.js\n├── Service.js\n├── accounts\n│   └── Person.js\n├── base\n│   ├── Animal.js\n│   └── Contact.js\n└── index.js\n```\n\nModel definitions inside `*.js` files can be something like this:\n\n```js\nmodule.exports = function(sequelize, DataTypes, meta) {\n  // using the meta object to help us define the model\n  return sequelize.define(meta.modelName, {\n    id: {\n      type: DataTypes.INTEGER(11),\n      allowNull: false,\n      primaryKey: true\n    },\n    name: {\n      type: DataTypes.STRING(250),\n      allowNull: true\n    }\n  }, {\n    timestamps: false,\n    freezeTableName: true,\n    tableName: meta.tableName,\n    schema: meta.schema\n  });\n};\n```\n\nAs 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.\n\nThe `meta` object will have the following properties based on the location of the model inside the directory:\n\n```js\n{\n  schema: string?, // value to use in the `schema` option of `sequelize.define`\n  schemaName: string, // the schema name of the model for your convenience\n  modelName: string, // value to use as the model name of `sequelize.define`\n  tableName: string, // value to use as the `tableName` option of `sequelize.define`\n  completeTableName: string, // the result of `schemaName` + `separator` + `tableName` for your convenience\n  separator: string // the separator that we use for your convenience\n}\n```\n\nFor example for the `Item.js` model the values of `meta` will be:\n\n```js\n{\n  schema: undefined,\n  schemaName: '',\n  modelName: 'Item',\n  tableName: 'item',\n  completeTableName: 'item',\n  separator: '.'\n}\n```\n\nand for the `accounts/Person.js` model:\n\n```js\n{\n  schema: 'accounts',\n  schemaName: 'accounts',\n  modelName: 'accounts.Person',\n  tableName: 'person',\n  completeTableName: 'accounts.person',\n  separator: '.'\n}\n```\n\nyou can customize how the `tableName` is generated, see [options.](https://github.com/bjrmatos/sequelize-auto-import/#options)\n\nWith the models defined we can import all the models inside `/path/to/models` directory and its subdirectories using the following in `index.js`:\n\n```js\nvar Sequelize = require('sequelize');\n\nvar sequelize = new Sequelize('test', 'test', 'test', {\n  dialect: 'mysql',\n  host: 'localhost',\n  port: 3306\n});\n\n// you can pass options, see bellow for details\nvar models = require('sequelize-auto-import')(sequelize, '/path/to/models');\n\n// export all the models for your convenience\nmodule.exports = models;\n```\n\nNow you can access the models in this way:\n\n```js\nmodels.Item\nmodels.Service\nmodels.accounts.Person\nmodels.base.Animal\nmodels.base.Contact\n```\n\nNote that `sequelize-auto-import` will recursively search for `js` files inside the specified directory.\n\nOptionally 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.\n\nMulti-Tenancy Support (Shared Database and Separate Schemas)\n---\n\nAlso you can create a **schema** folder to create multiples schemas using the same models. For example:\n\n```js\n/path/to/models\n├── user.js\n├── schema\n│   ├── product.js\n│   └── contact.js\n└── index.js\n```\n\nAnd with the following configuration you can indicate the schemas:\n\n```js\nvar models = require('sequelize-auto-import')(sequelize, '/path/to/models', {\n   schemas: ['company1', 'company2', 'company3']\n});\n\n```\n\nAnd access the models in this way:\n\n```js\nmodels.user\nmodels.company1.product\nmodels.company1.contact\nmodels.company2.product\nmodels.company2.contact\nmodels.company3.product\nmodels.company3.contact\n```\n\nAPI\n---\n\nThere is only one function exported with the following parameters:\n\n-\t`sequelizeInstace` A sequelize instance generated by your app\n-\t`pathToModels` The path where your models are located, if no specified default to current directory\n-\t`options` An options object, see bellow for all the available options\n\n### Options\n\n-\t`recursive`: boolean Whether to search inside all subdirectories or only one level, defaults to `true`\n-\t`associate`: boolean When `true` the `associate` method in models will be call it when found, defaults to `true`\n-\t`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:\n\n\t-\t`snakeCase` A model like `CustomModel.js` will have `custom_model` as `tableName`\n\n\tif 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`:\n\n```js\ntableNameFormat: function(modelName) {\n  // modelName === 'CustomModel'\n  return modelName.toLowerCase();\n}\n```\n\n(for now we only support `snakeCase` as one of the available formats, open a PR if you want other formats)\n\n-\t`exclude`: Array A list of files to ignore when importing\n\n- `schemas`: Array A list of schema names to append in all models inside the **schema** folder (Multi-Tenancy Support)\n\n\nCollaborators\n-------\n* [Juan David Nicholls Cardona](https://github.com/jdnichollsc)\n\nLicense\n-------\n\nSee [license](https://github.com/bjrmatos/electron-html-to/blob/master/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbjrmatos%2Fsequelize-auto-import","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbjrmatos%2Fsequelize-auto-import","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbjrmatos%2Fsequelize-auto-import/lists"}