Skip to content

Commit

Permalink
getting ready for v1.0.0 (#5)
Browse files Browse the repository at this point in the history
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
heinrich10 authored Aug 22, 2018
1 parent a3f496e commit 0853384
Show file tree
Hide file tree
Showing 6 changed files with 320 additions and 91 deletions.
55 changes: 35 additions & 20 deletions .eslintrc.js
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
}
}
}
};
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 amkjs
Copyright (c) 2016-2018 Heinrich Chan <guiltyrage10@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
154 changes: 144 additions & 10 deletions README.md
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)
53 changes: 39 additions & 14 deletions lib/knex.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,47 @@

const knex = require('knex');

let conn = {
host: process.env.MYSQL_HOST || 'localhost',
user: process.env.MYSQL_USERNAME || root,
password: process.env.MYSQL_PASSWORD || '',
database: process.env.MYSQL_DATABASE || 'default'
};
const client = process.env.AMK_SQL_CLIENT;

let conn;
let testMode = false;

switch(client) {
case 'sqlite3':
conn = {
filename: process.env.AMK_SQL_FILENAME
};
break;
case 'pg':
case 'mysql':
conn = {
host: process.env.AMK_SQL_HOST || 'localhost',
user: process.env.AMK_SQL_USERNAME || root,
password: process.env.AMK_SQL_PASSWORD || '',
database: process.env.AMK_SQL_DATABASE || 'default'
};
break;
case 'test':
testMode = true;
break;
default:
throw new Error(client, 'not supported');
}


function connect() {
return knex({
client: 'mysql',
connection: conn,
pool: {
min: +process.env.MYSQL_POOL_MIN || 2,
max: +process.env.MYSQL_POOL_MAX || 10
}
});
if (testMode) {
return knex({ client: 'mysql' });
} else {
return knex({
client,
connection: conn,
pool: {
min: +process.env.AMK_SQL_POOL_MIN || 2,
max: +process.env.AMK_SQL_POOL_MAX || 10
}
});
}
}

module.exports = connect;
Loading

0 comments on commit 0853384

Please sign in to comment.