-
Notifications
You must be signed in to change notification settings - Fork 0
JCN-68-database-dispatcher #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
b9abc72
Initial commit
738afab
develop files
8ceaaf3
Unit tests
b500d40
Development and fixes
7c5afc2
Updated README
a044d5d
Unnecessary instance methods converted to static
bc7f36f
Fixed test for static methods
74565ab
Updated docs
38f2fb4
Fixed readme
14e5983
Fixed readme
c22f517
DBDrivers packages are now optional
4422287
Unit tests updated
b3a1e88
Removed dbdrivers dependencies
c50f678
Updated readme
7182d81
Fixed cwd modules require
ec83f75
Updated tests
86195cb
Updated changelog
6def940
Updated docs
3a1c736
travis badges implemetation
9c75728
travis badges implemetation
a4464fc
Fixed config file path
6b956ff
Updated docs
e6616d7
Optimized tests
1d474ec
Config docs
677899a
Fixed docs
728e619
Fixed docs examples
bcd0330
Fixed docs
8d74072
Required/optional indications for config file keys
17944bd
Minor code fixes and simplifications
40ba835
Updated tests
b17dcaf
Improved tests
fbcb339
Removed unused sinon package
8f13b9c
Config file datatype checkouts
9484dde
Invalid config error
7fd27b0
Updated tests
d86b827
Updated docs
81c9970
Updated docs
17f13ab
Updated tests
c543201
Invalid db type config error
300f039
config validator and set config methods
c5bd6e2
Updated docs
2ba071d
DB type not found in config new error code
5a8e6dd
Converted to static and optimized config methods
2138148
Updated tests
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| 'use strict'; | ||
|
|
||
| module.exports = { | ||
| extends: 'airbnb-base', | ||
|
|
||
| env: { | ||
| node: true, | ||
| es6: true, | ||
| mocha: true | ||
| }, | ||
|
|
||
| globals: { | ||
| __rootpath: true, | ||
| coreRequire: true, | ||
| mainRequire: true, | ||
| JANIS_CORE: true, | ||
| JANIS_ENV: true, | ||
| JANIS_ENV_ALIAS: true | ||
| }, | ||
|
|
||
| parserOptions: { | ||
| sourceType: 'script' | ||
| }, | ||
|
|
||
| rules: { | ||
| 'operator-linebreak': 0, | ||
| 'no-continue': 0, | ||
| 'no-plusplus': 0, | ||
| 'prefer-spread': 0, | ||
| 'prefer-rest-params': 0, | ||
| 'class-methods-use-this': 0, | ||
| 'consistent-return': 0, | ||
| 'prefer-template': 0, | ||
| 'import/no-unresolved': 0, | ||
| 'import/no-extraneous-dependencies': ['error', { devDependencies: ['**/tests/**/*.js'] }], | ||
|
|
||
| 'no-bitwise': 0, | ||
|
|
||
| curly: ['error', 'multi-or-nest'], | ||
|
|
||
| 'no-underscore-dangle': ['warn', { | ||
| allowAfterThis: true, | ||
| allowAfterSuper: true, | ||
| allow: ['_call', '__rootpath', '_where'] | ||
| }], | ||
|
|
||
| 'no-tabs': 0, | ||
|
|
||
| 'no-new': 0, | ||
|
|
||
| 'func-names': 0, | ||
|
|
||
| 'space-before-function-paren': ['error', 'never'], | ||
|
|
||
| 'arrow-parens': ['error', 'as-needed'], | ||
| 'arrow-body-style': 0, | ||
|
|
||
| indent: ['error', 'tab', { | ||
| SwitchCase: 1 | ||
| }], | ||
|
|
||
| 'comma-dangle': ['error', 'never'], | ||
|
|
||
| 'padded-blocks': 0, | ||
|
|
||
| 'max-len': ['error', { | ||
| code: 150, | ||
| tabWidth: 1, | ||
| comments: 200 | ||
| }], | ||
|
|
||
| 'spaced-comment': ['error', 'always', { | ||
| exceptions: ['*'] | ||
| }], | ||
|
|
||
| 'newline-per-chained-call': ['error', { | ||
| ignoreChainWithDepth: 2 | ||
| }], | ||
|
|
||
| 'no-param-reassign': 0, | ||
|
|
||
| 'no-prototype-builtins': 0, | ||
|
|
||
| 'keyword-spacing': ['error', { | ||
| overrides: { | ||
| if: { | ||
| after: false | ||
| }, | ||
| for: { | ||
| after: false | ||
| }, | ||
| while: { | ||
| after: false | ||
| }, | ||
| switch: { | ||
| after: false | ||
| }, | ||
| catch: { | ||
| after: false | ||
| } | ||
| } | ||
| }], | ||
|
|
||
| 'no-restricted-syntax': ['error', 'ForInStatement', 'LabeledStatement', 'WithStatement'], | ||
| 'function-paren-newline': 0, | ||
| 'no-await-in-loop': 0, | ||
|
|
||
| 'object-curly-newline': ['error', { | ||
| ObjectExpression: { minProperties: 5, multiline: true, consistent: true }, | ||
| ObjectPattern: { minProperties: 5, multiline: true, consistent: true } | ||
| }], | ||
| 'nonblock-statement-body-position': ['error', 'below', { overrides: { else: 'any' } }] | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| "exclude": [ | ||
| "tests/", | ||
| "mocks/", | ||
| ".eslintrc.js", | ||
| ".travis.yml" | ||
| ], | ||
| "extension": [ | ||
| ".js" | ||
| ], | ||
| "cache": true, | ||
| "all": true, | ||
| "default-excludes": true, | ||
| "check-coverage": true, | ||
| "statements": 95, | ||
| "branches": 95, | ||
| "functions": 95, | ||
| "lines": 95 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| language: node_js | ||
| node_js: | ||
| - "lts/*" | ||
| cache: npm | ||
| script: | ||
| - | | ||
| # Run test script | ||
| npm run test-ci | ||
| after_script: | ||
| - | | ||
| # Upload coverage to coveralls | ||
| if [[ -d .nyc_output ]]; then | ||
| npm install --save-dev coveralls@2 | ||
| nyc report --reporter=text-lcov | coveralls | ||
| fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # Changelog | ||
|
|
||
| All notable changes to this project will be documented in this file. | ||
|
|
||
| The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) | ||
| and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). | ||
|
|
||
| ## [Unreleased] | ||
|
|
||
| ### Added | ||
| - `Database dispatcher package` | ||
| - `Unit tests` | ||
| - `Docs` | ||
| - `badges in readme` | ||
| - `Invalid config error` | ||
| - `Invalid db type in config error` | ||
| - `Config validation methods` | ||
|
|
||
| ### Fixes | ||
| - `Fixed error while loading database drivers dependencies` | ||
| - `Fixed config file path` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,120 @@ | ||
| # database-dispatcher | ||
| # database-dispatcher | ||
|
|
||
| [](https://travis-ci.org/janis-commerce/database-dispatcher) | ||
| [](https://coveralls.io/github/janis-commerce/database-dispatcher?branch=JCN-68-database-dispatcher) | ||
|
|
||
| **DatabaseDispatcher** is a package that returns the necessary DB driver from a received model. Access to the databases configuration then returns the driver instance with the connection. It caches the connections and configs. | ||
|
|
||
| ## Installation | ||
|
|
||
| ```sh | ||
| npm install @janiscommerce/database-dispatcher | ||
|
|
||
| # Then install all the drivers that you need. | ||
| npm install --save @janiscommerce/mysql | ||
| npm install --save @janiscommerce/mongodb | ||
| ``` | ||
|
|
||
| ## Configs | ||
|
|
||
| This package requires a config file: `/path/to/root/config/database.json` | ||
|
|
||
| ### Structure | ||
|
|
||
| - Root key: the model `databaseKey` | ||
| Includes the driver type and connections settings. | ||
|
|
||
| ```js | ||
| { | ||
| "databaseKey": { | ||
| "type": "mysql" | ||
| // ... | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Keys | ||
|
|
||
| - `type [String]` (required): Database driver type, example `"mysql"`. | ||
| - `host [String]` (required): Database connection host. | ||
| - `port [Number]` (required): Database connection port. | ||
| - `user [String]` (optional): Database login user name. | ||
| - `password [String]` (optional): Database login password. | ||
| - `database [String]` (required): Database name for connection, example `"myDB"`. | ||
| - `connectionLimit [Number]` (optional): Connection limit. | ||
| - `prefix [String]` (optional): Prefix for values. | ||
|
|
||
| ### Example | ||
|
|
||
| ```json | ||
| { | ||
| "core": { | ||
| "type": "mysql", | ||
| "host": "localhost", | ||
| "user": "root", | ||
| "database": "myDB", | ||
| "port": 3306 | ||
| }, | ||
| "services": { | ||
| "type": "mongodb", | ||
| "host": "mongodb://localhost", | ||
| "user": "sarasa", | ||
| "password": "foobar", | ||
| "database": "myDB", | ||
| "port": 27017 | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## API | ||
|
|
||
| - `config` | ||
| Returns the loaded database config json. | ||
| - `getDatabase("databaseKey")` | ||
| Receives the database key `[String]` then returns the database driver instance with the connection from the config json. | ||
| If the `databaseKey` not exists on the config json will throw a `DatabaseDispatcherError`. | ||
| The default value of `databaseKey` parameter is `"_default"`. | ||
| - `clearCaches()` | ||
| Clear all caches, including configs and DB connections. | ||
|
|
||
| ## Errors | ||
|
|
||
| The errors are informed with a `DatabaseDispatcherError`. | ||
| This object has a code that can be useful for a correct error handling. | ||
| The codes are the following: | ||
|
|
||
| | Code | Description | | ||
| |------|------------------------------------| | ||
| | 1 | Config not found | | ||
| | 2 | Invalid databaseKey | | ||
| | 3 | Database driver not installed | | ||
| | 4 | Invald config | | ||
| | 5 | Invalid db type in config | | ||
| | 6 | DB type setting not found in config| | ||
|
|
||
| ## Usage | ||
|
|
||
| ```js | ||
| const DatabaseDispatcher = require('@janiscommerce/database-dispatcher'); | ||
|
|
||
| const DBConfig = DatabaseDispatcher.config; | ||
|
|
||
| /* | ||
| core: { | ||
| type: 'mysql', | ||
| host: 'foo', | ||
| ... | ||
| }, | ||
| services: { | ||
| type: 'mongodb', | ||
| host: 'foo', | ||
| ... | ||
| } | ||
| */ | ||
|
|
||
| const myDBConnection = DatabaseDispatcher.getDatabase('core'); // A new DBDriver instance is returned. | ||
|
|
||
| let fields = myDBConnection.get(model, { item: 'sarasa' }); // should connect to db driver and return the items... | ||
|
|
||
| DatabaseDispatcher.clearCaches(); // cached connections and configs cleared. | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| 'use strict'; | ||
|
|
||
| class DatabaseDispatcherError extends Error { | ||
|
|
||
| static get codes() { | ||
|
|
||
| return { | ||
| CONFIG_NOT_FOUND: 1, | ||
| INVALID_DB_KEY: 2, | ||
| DB_DRIVER_NOT_INSTALLED: 3, | ||
| INVALID_CONFIG: 4, | ||
| INVALID_DB_TYPE_CONFIG: 5, | ||
| CONFIG_DB_TYPE_NOT_FOUND: 6 | ||
| }; | ||
| } | ||
|
|
||
| constructor(err, code) { | ||
| super(err); | ||
| this.message = err.message || err; | ||
| this.code = code; | ||
| this.name = 'DatabaseDispatcherError'; | ||
| } | ||
| } | ||
|
|
||
| module.exports = DatabaseDispatcherError; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.