diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..b32ba9e
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,64 @@
+# Logs
+logs
+*.log
+npm-debug.log*
+yarn-debug.log*
+yarn-error.log*
+
+# Runtime data
+pids
+*.lock
+*.pid
+*.seed
+*.pid.lock
+package-lock.json
+
+# Directory for instrumented libs generated by jscoverage/JSCover
+lib-cov
+
+# Coverage directory used by tools like istanbul
+coverage
+
+# nyc test coverage
+.nyc_output
+
+# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
+.grunt
+
+# Bower dependency directory (https://bower.io/)
+bower_components
+
+# node-waf configuration
+.lock-wscript
+
+# Compiled binary addons (http://nodejs.org/api/addons.html)
+build/Release
+
+# Dependency directories
+node_modules/
+jspm_packages/
+
+# Typescript v1 declaration files
+typings/
+
+# Optional npm cache directory
+.npm
+
+# Optional eslint cache
+.eslintcache
+
+# Optional REPL history
+.node_repl_history
+
+# Output of 'npm pack'
+*.tgz
+
+# Yarn Integrity file
+.yarn-integrity
+
+# dotenv environment variables file
+.env
+
+# Sublime Text file
+*.sublime-*
+!*.sublime-project
diff --git a/README.html b/README.html
new file mode 100644
index 0000000..66d99b3
--- /dev/null
+++ b/README.html
@@ -0,0 +1,883 @@
+
sequelize-autoload
+An autoloader for Sequelize, inspired by PSR-0 and PSR-4.
+Installation
+npm install --save sequelize-autoload
+Usage
+const db = require('sequelize-autoload');
+db.load('/path/to/config');
+To generate sequelize models files:
+
+See sequelize-auto package.
+
+To make a config file:
+
+See Config File section.
+
+To get a Sequelize table instance:
+
+Notes:
+
+db.load()
reads config, but does not load table(s) immediately.
+- Tables are loaded when they are called.
+- Only uninitialized table(s) will be loaded, otherwise existing table instance(s) will be returned.
+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:
+{
+ "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. |
+
+
+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. |
+
+
+models.root |
+String |
+✔ |
+Path where generated scripts (by sequelize-auto ) located. |
+
+Notes:
+
+- If
models.root
is a relative path, it describes the path related to the config JSON file.
+
+License
+MIT
\ No newline at end of file
diff --git a/README.md b/README.md
index ff5b4e0..61f2023 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,72 @@
# sequelize-autoload
-An autoloader for SequelizeJs.
+An autoloader for [Sequelize][github-sequelize], inspired by [PSR-0][psr-0] and [PSR-4][psr-4].
+
+## Installation
+```bash
+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](#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`)](#usage) located. |
+
+**Notes:**
+1. If `models.root` is a relative path, it describes the path related to the config JSON file.
+
+## License
+MIT
+
+[psr-0]: https://www.php-fig.org/psr/psr-0/ "PSR 0: Autoloading Standard"
+[psr-4]: https://www.php-fig.org/psr/psr-4/ "PSR 4: Autoloader"
+[github-sequelize]: https://github.com/sequelize/sequelize "GitHub - sequelize"
+[npm-sequelize-auto]: https://www.npmjs.com/package/sequelize-auto "npm - sequelize-auto"
+[doc-sequelize-example-usage]: http://docs.sequelizejs.com/#example-usage "Example Usage - Sequelize"
+[doc-sequelize-options]: http://docs.sequelizejs.com/manual/installation/usage.html#options "Options - Sequelize"
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..d998cd0
--- /dev/null
+++ b/index.js
@@ -0,0 +1,68 @@
+const Sequelize = require('sequelize');
+const Pick = require('object.pick');
+const Path = require('path');
+
+let _config = {};
+let _config_path = '.';
+let _sequelize = {};
+let _models = new Proxy({}, {
+ get: (target, prop, receiver) => (prop in target ? true : autoload(prop)) && target[prop]
+});
+
+const read = function(path) {
+ if(Path.normalize(path) !== Path.resolve(path)) {
+ const callerFileName = (module.parent || {}).filename || __filename;
+ const callerPath = Path.dirname(callerFileName);
+
+ path = Path.resolve(callerPath, path);
+ }
+
+ return path;
+};
+
+const load = function(config = '') {
+ config = read(config);
+
+ _config = require(config);
+ _config_path = config;
+ _sequelize = new Sequelize(
+ _config.server.database,
+ _config.server.username,
+ _config.server.password,
+ Pick(_config.server, ['host', 'dialect', 'define'])
+ );
+ _models = new Proxy({}, {
+ get: (target, prop, receiver) => (prop in target ? true : autoload(prop)) && target[prop]
+ });
+
+ _prototype.sequelize = _sequelize;
+ _prototype.config = _config;
+ _prototype.models = _models;
+};
+
+const autoload = function(models = []) {
+ if(typeof models === 'string' && models !== '') {
+ if(models in _config.models.tables) {
+ let filename = _config.models.tables[models];
+ let path = Path.resolve(_config_path, _config.models.root, `${filename}.js`);
+
+ _models[models] = _sequelize.import(read(path));
+ }
+ } else if(typeof models === 'object' && Array.isArray(models) && models.length > 0) {
+ for(let model of models)
+ autoload(model);
+ } else {
+ return false;
+ }
+
+ return true;
+};
+
+let _prototype = {};
+_prototype.sequelize = _sequelize;
+_prototype.config = _config;
+_prototype.models = _models;
+_prototype.load = load;
+_prototype.Op = Sequelize.Op;
+
+module.exports = _prototype;
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..b54b52f
--- /dev/null
+++ b/package.json
@@ -0,0 +1,37 @@
+{
+ "name": "sequelize-autoload",
+ "version": "1.0.0",
+ "description": "Autoloader for SequelizeJS, inspired by PSR-0 and PSR-4.",
+ "main": "index.js",
+ "dependencies": {
+ "mysql2": "^1.5.2",
+ "object.pick": "^1.3.0",
+ "pg": "^7.4.1",
+ "pg-hstore": "^2.3.2",
+ "sequelize": "^4.34.0",
+ "sqlite3": "^3.1.13",
+ "tedious": "^2.3.1"
+ },
+ "devDependencies": {},
+ "scripts": {
+ "test": "echo \"Error: no test specified\" && exit 1"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git+https://github.com/boxsnake-nodejs/sequelize-autoload.git"
+ },
+ "keywords": [
+ "sequelize",
+ "database",
+ "autoload",
+ "autoloader",
+ "models",
+ "entities"
+ ],
+ "author": "Alex Fang