Skip to content

Commit

Permalink
abe-sql test and doc
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaslabbe committed Oct 3, 2016
1 parent 7b2ec6a commit b392e68
Show file tree
Hide file tree
Showing 6 changed files with 330 additions and 136 deletions.
89 changes: 3 additions & 86 deletions docs/abe-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,53 +183,6 @@ Notes: You can use variables on from like this
{{abe type='data' key='articles' desc='articles' source="select * from /{{some_json_key}}/{{some_other}}" display="title" editable='true'"}}
```

###WHERE

```html
{{abe type='data' key='articles' desc='articles' source="select * from articles where title='my title'" display="title" editable='true'"}}
```

(*Only take article if __title__ is "my title"*)

__OPERATOR__

- =
- !=
- LIKE

###AND

```html
{{abe type='data' key='articles' desc='articles' source="select * from articles where template="article" AND title='my title'" display="title" editable='true'"}}
```

(*Only take article the type of the template is __article__ and the __title__ is "my title"*)

###OR

```
Not working
```

###LIMIT

```html
{{abe type='data' key='articles' desc='articles' source="select * from articles where template="article" AND title='my title' LIMIT 2" display="title" editable='true'"}}
```

(*Will not add more than __two__ article because of the limit*)

ORDER BY date
- DESC
- ASC
- RANDOM

```html
{{abe type='data' key='articles' desc='articles' source="select * from articles where template="article" AND title='my title' order by date DESC LIMIT 2" display="title" editable='true'"}}
```

(*Order by date only*)

##Use it

```html
Expand All @@ -238,45 +191,6 @@ ORDER BY date
{{/each}}
```

> complex json
If you use a complex json data source for example

```json
{
"articles": [
{"title":"my title number 1"},
{"title":"my title number 1"}
]
}
```

You request may look like this

```html
{{abe type='data' key='articles' desc='articles' source="select * from articles where \`articles.title\`='my title number 1'" display="articles.title" editable='true'"}}
```

notice the use of sql syntax ```\` ``` on ```\`articles.title\` ``` to select title from article object


##Dynamic variable select

Select where can have dynamic variable too

we want to use colors value inside sql where select
First we declare an array of colors (*inline / file / webservice*)

```html
{{abe type='data' key='colors' desc='Pick a color' source='["red", "green", "yellow"]'}}
```
then we select all article with title = colors using ```{{}}``` around variable

```html
{{abe type='data' key='articles' desc='articles' source="select * from articles where title='{{colors}}'" editable='true'"}}
```
(*if the user choose "red" then save the request will look like* __```select * from articles where title='red'```__)

##Other parameter

> editable="true"
Expand All @@ -298,3 +212,6 @@ will add default content values
> prefill-quantity="10"
will as many content as the value


[More documentation about abe sql](abe-sql.md)
250 changes: 250 additions & 0 deletions docs/abe-sql.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
# Abe sql request

# RULES

## Quotes/Double quotes

```
select some_variable from / where `title`='mon title' => NOT OK
select some_variable from / where `title`=`mon title` => OK
```

## Meta

The variable abe_meta. When you select a document even if you don't require meta they'll be added to the result

Request:
```
select some_variable from /
```

Result:
```json
{
"some_variable": "some_value",
"abe_meta": {
"template": "template_name",
"link": "/web/url/of/document.html",
"status": "publish",
"date": "2016-07-11T16:40:41.974Z"
}
}
```

## Dynamic variables

You can use document variable to select document (use `{some_variable_name}`)

Request:
```
select some_json_variable from / where abe_meta.link != `{{abe_meta.link}}`
```

*this request will select all document except the same as the current one

Explaination:
select the variable `some_json_variable` from `root` directory, where the child json variable from `abe_meta` named `link` is not the same as (where the magic append) `current doc abe_meta.link` (ex: /path/to/the/document)

Variable can be use on the value or the key inside request

```
select some_json_variable from / where {{abe_meta.link}} != `abe_meta.link`
```
* In this case the result is the same

# OPERATORS

## =

Request:
```
select some_variable from / where `title` = `a title`
// ex with variables
select some_variable from / where `title` = `{{title}}`
select some_variable from / where `{{title}}` = `title`
```

## !=

Request:
```
select some_variable from / where `title` != `a title`
// ex with variables
select some_variable from / where `title` != `{{title}}`
select some_variable from / where `{{title}}` != `title`
```

## >

Request:
```
select some_variable from / where `some_variable_counter` > 1
// ex with variables
select some_variable from / where `some_variable_counter` > `{{some_variable_counter}}`
select some_variable from / where `{{some_variable_counter}}` > 1
```
* work only on numbers

## >=

Request:
```
select some_variable from / where `some_variable_counter` >= 1
// ex with variables
select some_variable from / where `some_variable_counter` >= `{{some_variable_counter}}`
select some_variable from / where `{{some_variable_counter}}` >= 1
```
* work only on numbers

# <

Request:
```
select some_variable from / where `some_variable_counter` < 1
// ex with variables
select some_variable from / where `some_variable_counter` < `{{some_variable_counter}}`
select some_variable from / where `{{some_variable_counter}}` < 1
```
* work only on numbers

## <=

Request:
```
select some_variable from / where `some_variable_counter` >= 1
// ex with variables
select some_variable from / where `some_variable_counter` >= `{{some_variable_counter}}`
select some_variable from / where `{{some_variable_counter}}` >= 1
```
* work only on numbers

## LIKE

Request:
```
select some_variable from / where `some_variable_string` LIKE `part_of_a_string`
// ex with variables
select some_variable from / where `some_variable_string` LIKE `{{some_variable_string}}`
select some_variable from / where `{{some_variable_string}}` LIKE `part_of_a_string`
```

## NOT LIKE

Request:
```
select some_variable from / where `some_variable_string` NOT LIKE `part_of_a_string`
// ex with variables
select some_variable from / where `some_variable_string` NOT LIKE `{{some_variable_string}}`
select some_variable from / where `{{some_variable_string}}` NOT LIKE `part_of_a_string`
```

## AND

Request:
```
select some_variable from / where `title` = `a title` AND `abe_meta.template` = `template_name`
// ex with variables
select some_variable from / where `title` = `{{title}}` AND `abe_meta.template` = `{{abe_meta.template}}`
select some_variable from / where `{{title}}` = `a title` AND `{{abe_meta.template}}` == `template_name`
```

## OR

Request:
```
select some_variable from / where `title` = `a title` OR `abe_meta.template` = `template_name`
// ex with variables
select some_variable from / where `title` = `{{title}}` OR `abe_meta.template` = `{{abe_meta.template}}`
select some_variable from / where `{{title}}` = `a title` OR `{{abe_meta.template}}` == `template_name`
```

## IN

Request:
```
select some_variable from / where `abe_meta.template` IN (`template_name_1`, `template_name_2`, `template_name_3`)
// ex with variables
select some_variable from / where `title` IN `{{variable_array}}`
select some_variable from / where `{{abe_meta.template}}` IN `a title`
```

variable_array can be something like this

Use case :

If the json look like this

```json
{
"variable_array": [
"abe_meta": {
"template": "homepage",
"link": "/homepage-1.json",
"status": "publish",
"date": "2016-07-11T16:40:41.974Z"
},
"abe_meta": {
"template": "article",
"link": "/article-1.json",
"status": "publish",
"date": "2016-07-11T16:40:41.974Z"
}
]
}
```

the result of the request look like that

```
select some_variable from / where `title` IN `{{variable_array}}`
=
select some_variable from / where `title` IN (`homepage`, `article`)
```

## NOT IN

Request:
```
select some_variable from / where `abe_meta.template` NOT IN (`template_name_1`, `template_name_2`, `template_name_3`)
// ex with variables
select some_variable from / where `title` NOT IN `{{variable_array}}`
select some_variable from / where `{{abe_meta.template}}` NOT IN `a title`
```

Same as `IN`

```
select some_variable from / where `title` NOT IN `{{variable_array}}`
=
select some_variable from / where `title` NOT IN (`homepage`, `article`)
```

## ORDER BY

### DESC

```
select some_variable from / ORDER BY date DESC
```

### ASC

```
select some_variable from / ORDER BY date ASC
```

### RANDOM

```
select some_variable from / ORDER BY random
```

## LIMIT

```
select some_variable from / LIMIT 10
```
Loading

0 comments on commit b392e68

Please sign in to comment.