├── .gitignore ├── LICENSE ├── README.md ├── images ├── icon-no.png └── icon-yes.png ├── index.js ├── package.json └── sequelize-autoload.sublime-project /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.lock 11 | *.pid 12 | *.seed 13 | *.pid.lock 14 | package-lock.json 15 | 16 | # Directory for instrumented libs generated by jscoverage/JSCover 17 | lib-cov 18 | 19 | # Coverage directory used by tools like istanbul 20 | coverage 21 | 22 | # nyc test coverage 23 | .nyc_output 24 | 25 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 26 | .grunt 27 | 28 | # Bower dependency directory (https://bower.io/) 29 | bower_components 30 | 31 | # node-waf configuration 32 | .lock-wscript 33 | 34 | # Compiled binary addons (http://nodejs.org/api/addons.html) 35 | build/Release 36 | 37 | # Dependency directories 38 | node_modules/ 39 | jspm_packages/ 40 | 41 | # Typescript v1 declaration files 42 | typings/ 43 | 44 | # Optional npm cache directory 45 | .npm 46 | 47 | # Optional eslint cache 48 | .eslintcache 49 | 50 | # Optional REPL history 51 | .node_repl_history 52 | 53 | # Output of 'npm pack' 54 | *.tgz 55 | 56 | # Yarn Integrity file 57 | .yarn-integrity 58 | 59 | # dotenv environment variables file 60 | .env 61 | 62 | # Sublime Text file 63 | *.sublime-* 64 | !*.sublime-project 65 | 66 | # Build markdown 67 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Boxsnake NodeJS Dev 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![][badge-version-npm]][npm-sequelize-autoload] [![][badge-version-node]][npm-sequelize-autoload] [![][badge-version-sequelize]][npm-sequelize] [![][badge-github-release]][github-sequelize-autoload-release] [![][badge-downloads-npm-month]][npm-sequelize-autoload] [![][badge-license]][npm-sequelize-autoload] [![][badge-commitizen-friendly]][github-commitizen] [![][badge-semantic-release]][github-semantic-release] [![][badge-gitter]][gitter] 2 | 3 | An autoloader for [Sequelize][github-sequelize], inspired by [PSR-0][psr-0] and [PSR-4][psr-4]. 4 | 5 | ## Installation 6 | ```bash 7 | npm install --save sequelize-autoload 8 | ``` 9 | 10 | ## Usage 11 | ```javascript 12 | const db = require('sequelize-autoload'); 13 | db.load('/path/to/config'); 14 | ``` 15 | 16 | To generate sequelize models files: 17 | > See [sequelize-auto][npm-sequelize-auto] package. 18 | 19 | To make a config file: 20 | > See [Config File](#config-file) section. 21 | 22 | To get a Sequelize table instance: 23 | ```javascript 24 | db.models.model_name 25 | ``` 26 | 27 | **Notes:** 28 | 29 | 1. `db.load()` reads config, but does not load table(s) immediately. 30 | 2. Tables are loaded when they are called. 31 | 3. **Only** uninitialized table(s) will be loaded, otherwise existing table instance(s) will be returned. 32 | 4. `db.load()` can be called more than once, which will reload the config and clear all existing table instance(s). 33 | 34 | ## Config File 35 | Generally, the config file is a JSON separate from your main JS script. It contains database, tables and Sequelize-specific configurations. It looks like: 36 | ```json 37 | { 38 | "server": { 39 | "dialect": "mysql", 40 | "host": "localhost", 41 | "database": "test", 42 | "username": "username", 43 | "password": "password", 44 | "define": {} 45 | }, 46 | "models": { 47 | "root": "../models" 48 | } 49 | } 50 | ``` 51 | 52 | | Field Name | Type | Optional | Description | 53 | |-------------------|:------:|:----------------:|-----------------------------------------------------------------------| 54 | | `server.dialect` | String | ![no][icon-no] | Sequelize ORM dialect, see [here][doc-sequelize-example-usage]. | 55 | | `server.host` | String | ![no][icon-no] | Database host. | 56 | | `server.database` | String | ![no][icon-no] | Database name. | 57 | | `server.username` | String | ![no][icon-no] | Database connection username. | 58 | | `server.password` | String | ![no][icon-no] | Database connection password. | 59 | | `server.define` | Object | ![yes][icon-yes] | Sequelize global define, see [here][doc-sequelize-options]. | 60 | | `models.root` | String | ![yes][icon-yes] | Path where [generated scripts](#usage) (by `sequelize-auto`) located. | 61 | 62 | **Notes:** 63 | 1. If `models.root` is a relative path, it describes the path related to the config JSON file. 64 | 65 | ## License 66 | MIT 67 | 68 | [badge-version-npm]: https://img.shields.io/npm/v/sequelize-autoload.svg "Sequelize Autoload" 69 | [badge-version-node]: https://img.shields.io/node/v/sequelize-autoload.svg "Sequelize Autoload" 70 | [badge-version-sequelize]: https://img.shields.io/badge/Sequelize-%3E%3D4.0.0-orange.svg "Sequelize" 71 | [badge-downloads-npm-month]: https://img.shields.io/npm/dm/sequelize-autoload.svg "Sequelize Autoload downloads" 72 | [badge-github-release]: https://img.shields.io/github/release/boxsnake-nodejs/sequelize-autoload.svg "Sequelize Autoload latest release" 73 | [badge-license]: https://img.shields.io/npm/l/sequelize-autoload.svg "License" 74 | [badge-commitizen-friendly]: https://img.shields.io/badge/commitizen-friendly-brightgreen.svg "Commitizen friendly" 75 | [badge-semantic-release]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg "semantic-release" 76 | [badge-gitter]: https://img.shields.io/gitter/room/nwjs/nw.js.svg?logo=gitter-white "gitter" 77 | 78 | [icon-yes]: https://raw.githubusercontent.com/boxsnake-nodejs/sequelize-autoload/master/images/icon-yes.png 79 | [icon-no]: https://raw.githubusercontent.com/boxsnake-nodejs/sequelize-autoload/master/images/icon-no.png 80 | 81 | [psr-0]: https://www.php-fig.org/psr/psr-0/ "PSR 0: Autoloading Standard" 82 | [psr-4]: https://www.php-fig.org/psr/psr-4/ "PSR 4: Autoloader" 83 | [github-sequelize]: https://github.com/sequelize/sequelize "Sequelize" 84 | [github-sequelize-autoload-release]: https://github.com/boxsnake-nodejs/sequelize-autoload/releases/latest "Sequelize-autoload Release" 85 | [github-commitizen]: http://commitizen.github.io/cz-cli/ "Commitizen friendly" 86 | [github-semantic-release]: https://github.com/semantic-release/semantic-release "Semantic Release" 87 | [github-semantic-release-node-version-requirement]: https://github.com/semantic-release/semantic-release/blob/caribou/docs/support/node-version.md#node-version-requirement "Semantic Release - Node version requirement" 88 | [npm-sequelize]: https://www.npmjs.com/package/sequelize "Sequelize" 89 | [npm-sequelize-auto]: https://www.npmjs.com/package/sequelize-auto "Sequelize-auto" 90 | [npm-sequelize-autoload]: https://www.npmjs.com/package/sequelize-autoload "Sequelize-autoload" 91 | [doc-sequelize-example-usage]: http://docs.sequelizejs.com/#example-usage "Sequelize - Example Usage" 92 | [doc-sequelize-options]: http://docs.sequelizejs.com/manual/installation/usage.html#options "Sequelize - Options" 93 | [gitter]: https://gitter.im/boxsnake/sequelize-autoload?utm_source=share-link&utm_medium=link&utm_campaign=share-link "Gitter - boxsnake/sequelize-autoload" 94 | -------------------------------------------------------------------------------- /images/icon-no.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xancy/sequelize-autoload/6e1c600847dd095b91ea7583342ad900efdb8c20/images/icon-no.png -------------------------------------------------------------------------------- /images/icon-yes.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xancy/sequelize-autoload/6e1c600847dd095b91ea7583342ad900efdb8c20/images/icon-yes.png -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const Sequelize = require('sequelize'); 2 | const Pick = require('object.pick'); 3 | const Path = require('path'); 4 | 5 | let _config = {}; 6 | let _config_path = '.'; 7 | let _sequelize = {}; 8 | let _models = new Proxy({}, { 9 | get: (target, prop, receiver) => (prop in target ? true : autoload(prop)) && target[prop] 10 | }); 11 | 12 | const read = function(path) { 13 | if(Path.normalize(path) !== Path.resolve(path)) { 14 | const callerFileName = (module.parent || {}).filename || __filename; 15 | const callerPath = Path.dirname(callerFileName); 16 | 17 | path = Path.resolve(callerPath, path); 18 | } 19 | 20 | return path; 21 | }; 22 | 23 | const load = function(config = '') { 24 | config = read(config); 25 | 26 | _config = require(config); 27 | _config_path = config; 28 | _sequelize = new Sequelize( 29 | _config.server.database, 30 | _config.server.username, 31 | _config.server.password, 32 | Pick(_config.server, ['host', 'dialect', 'define']) 33 | ); 34 | _models = new Proxy({}, { 35 | get: (target, prop, receiver) => (prop in target ? true : autoload(prop)) && target[prop] 36 | }); 37 | 38 | _prototype.sequelize = _sequelize; 39 | _prototype.config = _config; 40 | _prototype.models = _models; 41 | }; 42 | 43 | const autoload = function(models = []) { 44 | if(typeof models === 'string' && models !== '') { 45 | if(models in _config.models.tables) { 46 | let filename = _config.models.tables[models]; 47 | let path = Path.resolve(_config_path, _config.models.root, `${filename}.js`); 48 | 49 | _models[models] = _sequelize.import(read(path)); 50 | } 51 | } else if(typeof models === 'object' && Array.isArray(models) && models.length > 0) { 52 | for(let model of models) 53 | autoload(model); 54 | } else { 55 | return false; 56 | } 57 | 58 | return true; 59 | }; 60 | 61 | let _prototype = {}; 62 | _prototype.sequelize = _sequelize; 63 | _prototype.config = _config; 64 | _prototype.models = _models; 65 | _prototype.load = load; 66 | _prototype.Op = Sequelize.Op; 67 | 68 | module.exports = _prototype; 69 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sequelize-autoload", 3 | "version": "0.0.0-development", 4 | "description": "Autoloader for SequelizeJS, inspired by PSR-0 and PSR-4.", 5 | "main": "index.js", 6 | "dependencies": { 7 | "mysql2": "^1.5.2", 8 | "object.pick": "^1.3.0", 9 | "pg": "^7.4.1", 10 | "pg-hstore": "^2.3.2", 11 | "sequelize": "^4.34.0", 12 | "sqlite3": "^3.1.13", 13 | "tedious": "^2.3.1" 14 | }, 15 | "devDependencies": { 16 | "commitizen": "^2.9.6", 17 | "cz-conventional-changelog": "^2.1.0", 18 | "semantic-release": "^12.4.1" 19 | }, 20 | "scripts": { 21 | "commit": "./node_modules/.bin/git-cz", 22 | "semantic-release": "semantic-release" 23 | }, 24 | "repository": { 25 | "type": "git", 26 | "url": "https://github.com/boxsnake-nodejs/sequelize-autoload.git" 27 | }, 28 | "keywords": [ 29 | "sequelize", 30 | "database", 31 | "autoload", 32 | "autoloader", 33 | "models", 34 | "entities" 35 | ], 36 | "author": "Alex Fang ", 37 | "license": "MIT", 38 | "bugs": { 39 | "url": "https://github.com/boxsnake-nodejs/sequelize-autoload/issues" 40 | }, 41 | "homepage": "https://github.com/boxsnake-nodejs/sequelize-autoload#readme", 42 | "engines": { 43 | "node": ">=4.0.0" 44 | }, 45 | "config": { 46 | "commitizen": { 47 | "path": "./node_modules/cz-conventional-changelog" 48 | } 49 | }, 50 | "release": { 51 | "verifyConditions": [ 52 | "@semantic-release/npm", 53 | "@semantic-release/github" 54 | ] 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /sequelize-autoload.sublime-project: -------------------------------------------------------------------------------- 1 | { 2 | "folders": 3 | [ 4 | { 5 | "path": "." 6 | } 7 | ] 8 | } 9 | --------------------------------------------------------------------------------