Autoloader for SequelizeJS, inspired by PSR-0 and PSR-4.
npm install sequelize-autoloadbash
npm install --save sequelize-autoload
`
Usage
`javascript
const db = require('sequelize-autoload');
db.load('/path/to/config');
`
To generate sequelize models files:
> See [sequelize-auto][npm-sequelize-auto] package.
To make a config file:
> See Config File section.
To get a Sequelize table instance:
`javascript
db.models.model_name
`
Notes:
1. db.load() reads config, but does not load table(s) immediately.
2. Tables are loaded when they are called.
3. Only uninitialized table(s) will be loaded, otherwise existing table instance(s) will be returned.
4. db.load() can be called more than once, which will reload the config and clear all existing table instance(s).
Config File
Generally, the config file is a JSON separate from your main JS script. It contains database, tables and Sequelize-specific configurations. It looks like:
`json
{
"server": {
"dialect": "mysql",
"host": "localhost",
"database": "test",
"username": "username",
"password": "password",
"define": {}
},
"models": {
"root": "../models"
}
}
`
| Field Name | Type | Optional | Description |
|-------------------|:------:|:--------:|-----------------------------------------------------------------------|
| server.dialect | String | ❌ | Sequelize ORM dialect, see [here][doc-sequelize-example-usage]. |
| server.host | String | ❌ | Database host. |
| server.database | String | ❌ | Database name. |
| server.username | String | ❌ | Database connection username. |
| server.password | String | ❌ | Database connection password. |
| server.define | Object | ✔ | Sequelize global define, see [here][doc-sequelize-options]. |
| models.root | String | ✔ | Path where generated scripts (by sequelize-auto) located. |
Notes:
1. If models.root` is a relative path, it describes the path related to the config JSON file.