-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
major changes: - get function modified to accept params and then options - count function modified to accept params and then options and returns an array - insert, update, and delete functions changed to ins, upd, and del to avoid clashing with knex methods - updated documentation - experimental unit test mode note: - this may be a breaking change as method signatures are changed - will create a unified eslint rule later
- Loading branch information
1 parent
a3f496e
commit 0853384
Showing
6 changed files
with
320 additions
and
91 deletions.
There are no files selected for viewing
This file contains 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,30 +1,45 @@ | ||
module.exports = { | ||
extends: 'eslint:recommended', | ||
parserOptions: { | ||
ecmaVersion: 8, | ||
sourceType: "module" | ||
}, | ||
rules: { | ||
'global-require': 2, | ||
extends: 'eslint:recommended', | ||
parserOptions: { | ||
ecmaVersion: 8, | ||
sourceType: 'module' | ||
}, | ||
rules: { | ||
'global-require': 2, | ||
'default-case': 2, | ||
eqeqeq: [2, 'smart'], | ||
'no-eq-null': 2, | ||
strict: [2, 'global'], | ||
'callback-return': 2, | ||
'no-process-env': 2, | ||
'no-process-exit': 2, | ||
'no-var': 2 | ||
}, | ||
globals: { | ||
require: false, | ||
console: false, | ||
module: false, | ||
'__dirname': false, | ||
Promise: false | ||
}, | ||
env: { | ||
node: true, | ||
'no-var': 2, | ||
indent: ['error', 'tab', { "SwitchCase": 1 }], | ||
'prefer-const': 2, | ||
'no-unused-expressions': 1, | ||
semi: 2, | ||
'func-names': 1, | ||
'prefer-destructuring': 2, | ||
'object-shorthand': 2, | ||
quotes: ['error', 'single'], | ||
'no-buffer-constructor': 2, | ||
'linebreak-style': 2, | ||
'max-len': [ | ||
1, | ||
{ | ||
code: 100, | ||
} | ||
] | ||
}, | ||
globals: { | ||
require: false, | ||
console: false, | ||
module: false, | ||
__dirname: false | ||
}, | ||
env: { | ||
node: true, | ||
mocha: true, | ||
es6: true | ||
} | ||
} | ||
} | ||
}; |
This file contains 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
This file contains 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,16 +1,150 @@ | ||
# AMK-SQL | ||
|
||
AMK-SQL is a plugin for express to simplify the usage of MySQL database | ||
AMK-SQL is a plugin for [express](https://expressjs.com/) using [knex](https://knexjs.org/) to simplify the usage of SQL databases | ||
|
||
## Usage | ||
## Setup | ||
Set the following environment variables | ||
- MYSQL_USERNAME | ||
- MYSQL_PASSWORD | ||
- MYSQL_HOST | ||
- MYSQL_DATABASE | ||
- MYSQL_POOL_MIN (connection pool, default = 2) | ||
- MYSQL_POOL_MAX (connection pool, default = 10) | ||
- AMK_SQL_USERNAME | ||
- AMK_SQL_PASSWORD | ||
- AMK_SQL_HOST | ||
- AMK_SQL_DATABASE | ||
- AMK_SQL_CLIENT | ||
- AMK_SQL_POOL_MIN (connection pool, default = 2) | ||
- AMK_SQL_POOL_MAX (connection pool, default = 10) | ||
|
||
you can achieve this by choosing one of the options below: | ||
1. use [dotenv](https://github.com/motdotla/dotenv) to set the variables | ||
2. issue the command ``` export AMK_SQL_USERNAME=username ``` | ||
3. put it on the ``` .bashrc ``` or ```.bash_profile``` file | ||
|
||
## Usage | ||
|
||
After setting up environment variables, inherit from this `amk-sql`. refer to code snipet below: | ||
|
||
**users.js** | ||
``` | ||
const SQL = require('amk-sql'); | ||
class Users extends SQL { | ||
constructor() { | ||
super('users', 'u') | ||
} | ||
} | ||
module.exports = Users; | ||
``` | ||
|
||
using **users.js** | ||
``` | ||
Users = require('./users'); | ||
const users = new Users(); | ||
// this will give you a list of 20 users that are active | ||
const rs = await users.get({active: 1}, { limit=20, orderBy='email'}) | ||
``` | ||
|
||
For more advance usage, please refer to [this](https://github.com/amkjs/amk-sql/wiki/Sample-Usage) | ||
|
||
## API | ||
|
||
#### ```find(param1, param2, param3)``` | ||
--- | ||
Simple query with filters | ||
|
||
##### Arguments | ||
* **param1** **_(string or object)_** - can be an object to filter | ||
* **param2** **_(string)_** - can be ('param', 'value') | ||
* **param3** **_(string)_** - can be ('param', 'in', 'value') | ||
|
||
##### Returns | ||
* **_(Array|Promise)_** - result set in an array or query object | ||
|
||
#### ```get(params, { limit, offset, groupBy, orderBy})``` | ||
--- | ||
Querying dataset with pagination | ||
|
||
##### Arguments | ||
* **param** **_(string)_** - query parameter | ||
* **limit** **_(number)_** - limit | ||
* **offset** **_(number)_** - offset | ||
* **groupBy** **_(string)_** - group by statement | ||
* **orderBy** **_(string|Object)_** - order by or order by and direction | ||
|
||
##### Returns | ||
* **_(Array|Promise)_** - result set in an array or query object | ||
|
||
#### ```count(params, { limit, offset, groupBy, orderBy})``` | ||
--- | ||
Count the number of entry in a table | ||
|
||
##### Arguments | ||
* **param** **_(string)_** - query parameter | ||
* **limit** **_(number)_** - limit | ||
* **offset** **_(number)_** - offset | ||
* **groupBy** **_(string)_** - group by statement | ||
* **orderBy** **_(string|Object)_** - order by or order by and direction | ||
|
||
##### Returns | ||
* **_(Array|Promise)_** - count in an array or query object. suggest to use destructuring like `let [count] = model.count()` to get the value | ||
|
||
#### ```ins(params, returning)``` | ||
--- | ||
function to insert a single row of data | ||
|
||
##### Arguments | ||
* **params** **_(object)_** - the row you are going to insert | ||
* **returning** **_(Array|Promise)_** - the row value you want returned i.e. primary keys or the query object | ||
|
||
##### Returns | ||
* **_(Array)_** - returns the number of rows inserted or the return value specified on the arguments | ||
|
||
#### ```upd(updateValue, params, returning)``` | ||
--- | ||
function to update rows of data | ||
|
||
##### Arguments | ||
* **updateValue** **_(object)_** - value you want to change | ||
* **params** **_(object)_** - the criteria of which row to update | ||
* **returning** **_(string)_** - the row value you want returned i.e. primary keys | ||
|
||
##### Returns | ||
* **_(Array|Promise)_** - returns the number of rows inserted or the return value specified on the arguments or the query object | ||
|
||
#### ```del(params)``` | ||
--- | ||
function to delete rows of data | ||
|
||
##### Arguments | ||
* **params** **_(object)_** - the criteria of which row to delete | ||
|
||
##### Returns | ||
* **_(Array|Promise)_** - returns the number of rows deleted or the query object | ||
|
||
#### ```getDB()``` | ||
--- | ||
returns the knex object with table name | ||
|
||
##### Returns | ||
* **_(Promise)_** - similar to `knex(TABLE_NAME)` that can be chained | ||
|
||
#### ```getJoinDB()``` | ||
--- | ||
returns the knex object table name with alias to make it easier to join | ||
|
||
##### Returns | ||
* **_(Promise)_** - similar to `knex(TABLE_NAME).as(alias)` that can be chained | ||
|
||
#### ```getConn()``` | ||
--- | ||
returns the knex object | ||
##### Returns | ||
* **_(Promise)_** - similar to `knex()` | ||
|
||
## Testing | ||
Work in progress | ||
|
||
## Feedback | ||
|
||
you can do this by issuing the command ``` export MYSQL_USERNAME=username ``` or putting it on the ``` .bashrc ``` or ```.bash_profile``` file | ||
All bugs, feature requests, pull requests, feedback, etc., are welcome. [Create an issue](https://github.com/amkjs/amk-sql/issues). | ||
|
||
After setting up environment variables, inherit from this class. | ||
## License | ||
[MIT](https://github.com/amkjs/amk-sql/blob/master/LICENSE) |
This file contains 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
Oops, something went wrong.