Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
MrSwitch committed Aug 15, 2019
2 parents d2f8299 + 37f0a52 commit bc8061a
Show file tree
Hide file tree
Showing 15 changed files with 562 additions and 120 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"prefer-named-capture-group": 0,
"prefer-object-spread": 0,
"prefer-promise-reject-errors": 2,
"require-atomic-updates": 0,
"require-await": 0,
"require-unicode-regexp": 0,
"sort-keys": 0,
Expand Down
25 changes: 25 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Node CI

on: [push]

jobs:
build:

name: Test on node ${{ matrix.node_version }} and ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
node_version: [8, 10, 12]
os: [ubuntu-latest, windows-latest, macOS-latest]

steps:
- uses: actions/checkout@master
- name: Use Node.js ${{ matrix.node_version }}
uses: actions/setup-node@v1
with:
version: ${{ matrix.node_version }}
- name: npm install, build, and test
run: |
npm install
npm run build --if-present
npm test
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# [0.32.0](https://github.com/5app/dare/compare/v0.31.4...v0.32.0) (2019-08-07)


### Features

* **Field Alias:** Enable aliasing field names ([#78](https://github.com/5app/dare/issues/78)) ([9d530d2](https://github.com/5app/dare/commit/9d530d2)), closes [#30](https://github.com/5app/dare/issues/30) [#30](https://github.com/5app/dare/issues/30) [#30](https://github.com/5app/dare/issues/30) [#30](https://github.com/5app/dare/issues/30) [#30](https://github.com/5app/dare/issues/30) [#30](https://github.com/5app/dare/issues/30) [#30](https://github.com/5app/dare/issues/30) [#30](https://github.com/5app/dare/issues/30) [#30](https://github.com/5app/dare/issues/30) [#30](https://github.com/5app/dare/issues/30) [#30](https://github.com/5app/dare/issues/30) [#30](https://github.com/5app/dare/issues/30)

## [0.31.4](https://github.com/5app/dare/compare/v0.31.3...v0.31.4) (2019-05-20)


Expand Down
116 changes: 81 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ npm i dare --save
Create an instance of Dare with some options

```javascript
const Dare = reqire('dare');
const Dare = require('dare');
const options = {
schema
Expand All @@ -79,10 +79,11 @@ The `options` themselves are a set of properties used to interpret and manipulat
The schema is used to define the structure of your SQL database. You can refer to it as `options.schema`. It's each property in the schema pertains to a database table. And defines the fields within the table.
e.g.
```javascript
{
users: {Field Definition's,...},
country: {Field Definition's,...}
users: {Field Definitions, ...},
country: {Field Definitions, ...}
}
```
Expand Down Expand Up @@ -118,7 +119,7 @@ Defining the `type` introduces additional features.

*currently this is limited to datetime*

E.g. in the example the type is defined as 'datetime', a conditional filter short hand for `created_time: 2017` would be expanded too `created_time BETWEEN '2017-01-01T00:00:00' AND '2017-12-31T23:59:59'
E.g. in the example the type is defined as 'datetime', a conditional filter short hand for `created_time: 2017` would be expanded too `created_time BETWEEN '2017-01-01T00:00:00' AND '2017-12-31T23:59:59`

```javascript
...
Expand All @@ -141,7 +142,7 @@ This will manipulate the request and response to create the property `avatar_url

```javascript
...
schema : {
schema: {
users: {
avatar_url(fields) {

Expand All @@ -153,6 +154,26 @@ This will manipulate the request and response to create the property `avatar_url
...
```
#### field alias
To alias a field, so that you can use a name different to the db column name, assign it a string name of the field in the current table. e.g. `emailAddress: 'email'`
```javascript
...
schema: {
users: {
emailAddress: 'email'
},
...
```
For example this will allow us to use the alias `emailAddress` in our api (see below), but the SQL generated will refer to it with it's true field name "`email`".
```javascript
dare.get('users', ['emailAddress'], {emailAddress: 'andrew@%'});
// SELECT email AS emailAddress FROM users WHERE email LIKE 'andrew@%'
```
Expand Down Expand Up @@ -413,18 +434,42 @@ Alternatively a options Object can be used instead.
e.g.
```javascript
dare.get({
table: 'users',
body: {name: 'Andrew', profession: 'Mad scientist'}
dare.post({
table: 'user',
body: {
name: 'Andrew',
profession: 'Mad scientist'
}
});

```
## dare.post(options Object) with multiple values
The body can be an Array of objects.
e.g.
```javascript
dare.post({
table: 'user',
body: [{
name: 'Andrew',
profession: 'Mad scientist'
}, {
name: 'Peppa'
}]
});
```
### Post options
This generates `INSERT INTO user (name, profession) VALUES ('Andrew', 'Mad Scientist'), ('Peppa', DEFAULT)`. Note where the key's differ between items in the Array the `DEFAULT` value is inserted instead.
### Post options (additional)
| Prop | Type | Description
|---------------|------------------|----------------
| duplicate_key | 'ignore' | Inserts SQL 'IGNORE' option
| duplicate_keys_update | Array(field1, field2, ...) | Appends ON DUPLICATE KEYS UPDATE field1=VALUES(field1)
| duplicate_keys | 'ignore' | Inserts SQL 'IGNORE' option
| duplicate_keys_update | Array(field1, field2, ...) | Appends `ON DUPLICATE KEYS UPDATE field1=VALUES(field1)`
Expand Down Expand Up @@ -545,30 +590,31 @@ E.g. here is an example using the before handlers to capture the original value
```javascript
...
patch: {
users(options) {

// Clone the request, and add fields to query
const opts = Object.assign(
{},
options,
{fields: ['id', 'name']}
);

return dare.get(opts).then(resp => {

const previous_name = resp.name;
const ref_id = resp.id;

// Set the after handler
this.after = () => {
dare.post('changelog', {
message: 'User updated',
type: 'users',
ref_id,
previous_name
})
};
});
async users(options) {

/**
* Check that the data to be modified
* By using the options to construct a SELECT request first
*/

// Clonse the options
const opts = {
...options,
fields: ['id', 'name']
};

// Execute a dare.get with the cloned options
const {id: ref_id, name: previous_name} = await dare.get(opts);

// Set the after handler
this.after = () => {
dare.post('changelog', {
message: 'User updated',
type: 'users',
ref_id,
previous_name
})
};
}
}
...
Expand Down
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "dare",
"version": "0.31.4",
"version": "0.32.0",
"description": "Database to REST, REST to Database",
"main": "src/index.js",
"engines": {
"node": ">=8.0.0"
},
"scripts": {
"test": "npm run lint && npm run spec && (nyc report --reporter=text-lcov | coveralls)",
"test": "npm run lint && npm run spec && ((nyc report --reporter=text-lcov | coveralls) || exit 0)",
"test:ci": "eslint ./ && nyc mocha test/specs/**/*.js --reporter mocha-circleci-reporter && (nyc report --reporter=text-lcov | coveralls)",
"spec": "nyc mocha test/specs/**/*.js",
"semantic-release": "semantic-release",
Expand Down Expand Up @@ -40,19 +40,19 @@
"src/"
],
"devDependencies": {
"@semantic-release/changelog": "^3.0.2",
"@semantic-release/git": "^7.0.8",
"@semantic-release/changelog": "^3.0.4",
"@semantic-release/git": "^7.0.14",
"chai": "^4.2.0",
"coveralls": "^3.0.3",
"eslint": "^5.16.0",
"eslint-config-5app": "^0.6.2",
"eslint-plugin-promise": "^4.1.1",
"coveralls": "^3.0.4",
"eslint": "^6.0.1",
"eslint-config-5app": "^0.6.5",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-security": "^1.4.0",
"mocha": "^6.1.4",
"mocha-circleci-reporter": "0.0.3",
"nyc": "^14.0.0",
"pre-commit": "^1.2.2",
"semantic-release": "^15.13.12"
"semantic-release": "^15.13.18"
},
"dependencies": {},
"release": {
Expand Down
7 changes: 7 additions & 0 deletions renovate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"rangeStrategy": "replace",
"automerge": true,
"extends": [
"config:base"
]
}
23 changes: 17 additions & 6 deletions src/format_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const orderbyReducer = require('./utils/orderby_reducer');
const checkKey = require('./utils/validate_field');
const checkTableAlias = require('./utils/validate_alias');
const formatDateTime = require('./utils/format_datetime');
const getFieldAttributes = require('./utils/field_attributes');


module.exports = function(options) {

Expand Down Expand Up @@ -112,8 +114,8 @@ async function format_specs(options) {
// Check this is a path
checkKey(key);

const type = table_schema[key] && table_schema[key].type;
filters.push(prepCondition(key, value, type, negate));
const key_definition = table_schema[key];
filters.push(prepCondition(key, value, key_definition, negate));

}

Expand Down Expand Up @@ -247,8 +249,8 @@ async function format_specs(options) {
// Check this is a path
checkKey(key);

const type = table_schema[key] && table_schema[key].type;
_join.push(prepCondition(key, value, type, negate));
const key_definition = table_schema[key];
_join.push(prepCondition(key, value, key_definition, negate));

}
options._join = _join;
Expand Down Expand Up @@ -376,7 +378,16 @@ function limit(opts, MAX_LIMIT) {

}

function prepCondition(field, value, type, negate) {
function prepCondition(field, value, key_definition, negate) {

const {type, alias} = getFieldAttributes(key_definition);

if (alias) {

// The key definition says the key is an alias
field = alias;

}

if (type === 'datetime') {

Expand Down Expand Up @@ -501,7 +512,7 @@ function prepCondition(field, value, type, negate) {
// Cond
sub_values.forEach(item => {

const [, cond, values] = prepCondition(null, item, type, negate);
const [, cond, values] = prepCondition(null, item, key_definition, negate);

// Add to condition
conds.push(cond);
Expand Down

0 comments on commit bc8061a

Please sign in to comment.