mirror of
https://github.com/typicode/json-server.git
synced 2025-07-30 13:42:11 +08:00
Add "not equal" operator
This commit is contained in:
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
* CLI option `-q/--quied`
|
* CLI option `-q/--quied`
|
||||||
* Nested route `POST /posts/1/comments`
|
* Nested route `POST /posts/1/comments`
|
||||||
|
* Not equal operator `GET /posts?id_ne=1`
|
||||||
|
|
||||||
## [0.8.2][2015-10-15]
|
## [0.8.2][2015-10-15]
|
||||||
|
|
||||||
|
10
README.md
10
README.md
@ -98,14 +98,20 @@ GET /posts?_sort=views&_order=DESC
|
|||||||
GET /posts/1/comments?_sort=votes&_order=ASC
|
GET /posts/1/comments?_sort=votes&_order=ASC
|
||||||
```
|
```
|
||||||
|
|
||||||
### Range
|
### Operators
|
||||||
|
|
||||||
Add `_gte` or `_lte`
|
Add `_gte` or `_lte` for getting a range
|
||||||
|
|
||||||
```
|
```
|
||||||
GET /posts?views_gte=10&views_lte=20
|
GET /posts?views_gte=10&views_lte=20
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Add `_ne` to exclude a value
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /posts?id_ne=1
|
||||||
|
```
|
||||||
|
|
||||||
### Full-text search
|
### Full-text search
|
||||||
|
|
||||||
Add `q`
|
Add `q`
|
||||||
|
@ -73,7 +73,8 @@ module.exports = function (db, name) {
|
|||||||
query === 'callback' ||
|
query === 'callback' ||
|
||||||
query === '_' ||
|
query === '_' ||
|
||||||
query.indexOf('_lte') !== -1 ||
|
query.indexOf('_lte') !== -1 ||
|
||||||
query.indexOf('_gte') !== -1
|
query.indexOf('_gte') !== -1 ||
|
||||||
|
query.indexOf('_ne') !== -1
|
||||||
) return
|
) return
|
||||||
}
|
}
|
||||||
delete req.query[query]
|
delete req.query[query]
|
||||||
@ -106,17 +107,23 @@ module.exports = function (db, name) {
|
|||||||
return arr
|
return arr
|
||||||
.map(utils.toNative)
|
.map(utils.toNative)
|
||||||
.map(function (value) {
|
.map(function (value) {
|
||||||
|
var isDifferent = key.indexOf('_ne') !== -1
|
||||||
var isRange = key.indexOf('_lte') !== -1 || key.indexOf('_gte') !== -1
|
var isRange = key.indexOf('_lte') !== -1 || key.indexOf('_gte') !== -1
|
||||||
|
var path = key.replace(/(_lte|_gte|_ne)$/, '')
|
||||||
|
var elementValue
|
||||||
|
|
||||||
if (isRange) {
|
if (isRange) {
|
||||||
var path = key.replace(/(_lte|_gte)$/, '')
|
|
||||||
var isLowerThan = key.indexOf('_gte') !== -1
|
var isLowerThan = key.indexOf('_gte') !== -1
|
||||||
var elementValue = _.get(element, path)
|
elementValue = _.get(element, path)
|
||||||
|
|
||||||
if (isLowerThan) {
|
if (isLowerThan) {
|
||||||
return value <= elementValue
|
return value <= elementValue
|
||||||
} else {
|
} else {
|
||||||
return value >= elementValue
|
return value >= elementValue
|
||||||
}
|
}
|
||||||
|
} else if (isDifferent) {
|
||||||
|
elementValue = _.get(element, path)
|
||||||
|
return value !== elementValue
|
||||||
} else {
|
} else {
|
||||||
return _.matchesProperty(key, value)(element)
|
return _.matchesProperty(key, value)(element)
|
||||||
}
|
}
|
||||||
|
@ -224,7 +224,7 @@ describe('Server', function () {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('GET /:resource?attr>=&attr<=', function () {
|
describe('GET /:resource?attr_gte=&attr_lte=', function () {
|
||||||
it('should respond with a limited array', function (done) {
|
it('should respond with a limited array', function (done) {
|
||||||
request(server)
|
request(server)
|
||||||
.get('/comments?id_gte=2&id_lte=3')
|
.get('/comments?id_gte=2&id_lte=3')
|
||||||
@ -234,6 +234,16 @@ describe('Server', function () {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('GET /:resource?attr_ne=', function () {
|
||||||
|
it('should respond with a limited array', function (done) {
|
||||||
|
request(server)
|
||||||
|
.get('/comments?id_ne=1')
|
||||||
|
.expect('Content-Type', /json/)
|
||||||
|
.expect(db.comments.slice(1))
|
||||||
|
.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)
|
||||||
|
Reference in New Issue
Block a user