mirror of
https://github.com/typicode/json-server.git
synced 2025-07-30 13:42:11 +08:00
Add range support
This commit is contained in:
@ -1,5 +1,12 @@
|
||||
# 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]
|
||||
|
||||
### Added
|
||||
|
43
README.md
43
README.md
@ -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`.
|
||||
|
||||
Plural resources
|
||||
### Plural routes
|
||||
|
||||
```
|
||||
GET /posts
|
||||
GET /posts/1
|
||||
GET /posts/1/comments
|
||||
POST /posts
|
||||
PUT /posts/1
|
||||
PATCH /posts/1
|
||||
DELETE /posts/1
|
||||
```
|
||||
|
||||
Singular resources
|
||||
### Singular routes
|
||||
|
||||
```
|
||||
GET /profile
|
||||
@ -70,7 +69,9 @@ PUT /profile
|
||||
PATCH /profile
|
||||
```
|
||||
|
||||
To filter resources (use `.` to access deep properties)
|
||||
### Filter
|
||||
|
||||
Use `.` to access deep properties
|
||||
|
||||
```
|
||||
GET /posts?title=json-server&author=typicode
|
||||
@ -78,7 +79,9 @@ GET /posts?id=1&id=2
|
||||
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
|
||||
@ -86,19 +89,33 @@ GET /posts/1/comments?_start=20&_end=30
|
||||
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/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
|
||||
```
|
||||
|
||||
### Relationships
|
||||
|
||||
To include children resources, add `_embed`
|
||||
|
||||
```
|
||||
@ -106,19 +123,27 @@ GET /posts?_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/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
|
||||
```
|
||||
|
||||
### Homepage
|
||||
|
||||
Returns default index file or serves `./public` directory
|
||||
|
||||
```
|
||||
|
@ -90,7 +90,20 @@ module.exports = function (db, name) {
|
||||
return arr
|
||||
.map(utils.toNative)
|
||||
.map(function (value) {
|
||||
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) {
|
||||
return a || b
|
||||
})
|
||||
|
@ -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 () {
|
||||
it('should respond with json and corresponding nested resources', function (done) {
|
||||
request(server)
|
||||
|
Reference in New Issue
Block a user