Add range support

This commit is contained in:
Typicode
2015-09-09 08:34:24 +02:00
parent aa95754526
commit cdd6891d18
4 changed files with 65 additions and 10 deletions

View File

@ -1,5 +1,12 @@
# Change Log # Change Log
## [0.7.28][2015-09-09]
```bash
# Support range
GET /products?price_gte=50&price_lte=100
```
## [0.7.27][2015-09-02] ## [0.7.27][2015-09-02]
### Added ### Added

View File

@ -49,19 +49,18 @@ $ npm install -g json-server
Based on the previous `db.json` file, here are all the default routes. You can also add [other routes](#add-routes) using `--routes`. Based on the previous `db.json` file, here are all the default routes. You can also add [other routes](#add-routes) using `--routes`.
Plural resources ### Plural routes
``` ```
GET /posts GET /posts
GET /posts/1 GET /posts/1
GET /posts/1/comments
POST /posts POST /posts
PUT /posts/1 PUT /posts/1
PATCH /posts/1 PATCH /posts/1
DELETE /posts/1 DELETE /posts/1
``` ```
Singular resources ### Singular routes
``` ```
GET /profile GET /profile
@ -70,7 +69,9 @@ PUT /profile
PATCH /profile PATCH /profile
``` ```
To filter resources (use `.` to access deep properties) ### Filter
Use `.` to access deep properties
``` ```
GET /posts?title=json-server&author=typicode GET /posts?title=json-server&author=typicode
@ -78,7 +79,9 @@ GET /posts?id=1&id=2
GET /comments?author.name=typicode GET /comments?author.name=typicode
``` ```
To slice resources, add `_start` and `_end` or `_limit` (an `X-Total-Count` header is included in the response) ### Slice
Add `_start` and `_end` or `_limit` (an `X-Total-Count` header is included in the response)
``` ```
GET /posts?_start=20&_end=30 GET /posts?_start=20&_end=30
@ -86,19 +89,33 @@ GET /posts/1/comments?_start=20&_end=30
GET /posts/1/comments?_start=20&_limit=10 GET /posts/1/comments?_start=20&_limit=10
``` ```
To sort resources, add `_sort` and `_order` (ascending order by default). ### Sort
Add `_sort` and `_order` (ascending order by default)
``` ```
GET /posts?_sort=views&_order=DESC GET /posts?_sort=views&_order=DESC
GET /posts/1/comments?_sort=votes&_order=ASC GET /posts/1/comments?_sort=votes&_order=ASC
``` ```
To make a full-text search on resources, add `q` ### Range
Add `_gte` or `_lte`
```
GET /posts?views_gte=10&views_lte=20
```
### Full-text search
Add `q`
``` ```
GET /posts?q=internet GET /posts?q=internet
``` ```
### Relationships
To include children resources, add `_embed` To include children resources, add `_embed`
``` ```
@ -106,19 +123,27 @@ GET /posts?_embed=comments
GET /posts/1?_embed=comments GET /posts/1?_embed=comments
``` ```
To include parent resource, add `_expand`. To include parent resource, add `_expand`
``` ```
GET /comments?_expand=post GET /comments?_expand=post
GET /comments/1?_expand=post GET /comments/1?_expand=post
``` ```
Returns database To get nested resources (by default one level, [add routes](#add-routes) for more)
```
GET /posts/1/comments
```
### Database
``` ```
GET /db GET /db
``` ```
### Homepage
Returns default index file or serves `./public` directory Returns default index file or serves `./public` directory
``` ```

View File

@ -90,7 +90,20 @@ module.exports = function (db, name) {
return arr return arr
.map(utils.toNative) .map(utils.toNative)
.map(function (value) { .map(function (value) {
return _.matchesProperty(key, value)(element) var isRange = key.indexOf('_lte') !== -1 || key.indexOf('_gte') !== -1
if (isRange) {
var path = key.replace(/(_lte|_gte)$/, '')
var isLowerThan = key.indexOf('_gte') !== -1
var elementValue = _.get(element, path)
if (isLowerThan) {
return value <= elementValue
} else {
return value >= elementValue
}
} else {
return _.matchesProperty(key, value)(element)
}
}).reduce(function (a, b) { }).reduce(function (a, b) {
return a || b return a || b
}) })

View File

@ -216,6 +216,16 @@ describe('Server', function () {
}) })
}) })
describe('GET /:resource?attr>=&attr<=', function () {
it('should respond with a limited array', function (done) {
request(server)
.get('/comments?id_gte=2&id_lte=3')
.expect('Content-Type', /json/)
.expect(db.comments.slice(1, 3))
.expect(200, done)
})
})
describe('GET /:parent/:parentId/:resource', function () { describe('GET /:parent/:parentId/:resource', function () {
it('should respond with json and corresponding nested resources', function (done) { it('should respond with json and corresponding nested resources', function (done) {
request(server) request(server)