Skip to content

Commit 9fc0ef5

Browse files
committed
Support OR in lists
1 parent f731f01 commit 9fc0ef5

File tree

5 files changed

+60
-27
lines changed

5 files changed

+60
-27
lines changed

CHANGELOG.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
1+
# Change Log
2+
3+
## [0.7.27][2015-09-02]
4+
5+
### Added
6+
7+
```bash
8+
# Support OR
9+
GET /posts?id=1&id2
10+
GET /posts?category=javascript&category=html
11+
```
12+
113
## [0.7.26][2015-09-01]
214

315
### Added
4-
- `GET /posts?embed=comments`
5-
- `GET /posts?expand=user`
16+
17+
```bash
18+
# Support embed and expand in lists
19+
GET /posts?embed=comments
20+
GET /posts?expand=user
21+
```

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,11 @@ To filter resources (use `.` to access deep properties)
7474

7575
```
7676
GET /posts?title=json-server&author=typicode
77+
GET /posts?id=1&id=2
7778
GET /comments?author.name=typicode
7879
```
7980

80-
To slice resources, add `_start` and `_end` or `_limit` (an `X-Total-Count` header is included in the response).
81+
To slice resources, add `_start` and `_end` or `_limit` (an `X-Total-Count` header is included in the response)
8182

8283
```
8384
GET /posts?_start=20&_end=30
@@ -92,33 +93,33 @@ GET /posts?_sort=views&_order=DESC
9293
GET /posts/1/comments?_sort=votes&_order=ASC
9394
```
9495

95-
To make a full-text search on resources, add `q`.
96+
To make a full-text search on resources, add `q`
9697

9798
```
9899
GET /posts?q=internet
99100
```
100101

101-
To embed resources, add `_embed`.
102+
To include children resources, add `_embed`
102103

103104
```
104105
GET /posts?_embed=comments
105106
GET /posts/1?_embed=comments
106107
```
107108

108-
To expand inner resources, add `_expand`.
109+
To include parent resource, add `_expand`.
109110

110111
```
111112
GET /comments?_expand=post
112113
GET /comments/1?_expand=post
113114
```
114115

115-
Returns database.
116+
Returns database
116117

117118
```
118119
GET /db
119120
```
120121

121-
Returns default index file or serves `./public` directory.
122+
Returns default index file or serves `./public` directory
122123

123124
```
124125
GET /

src/server/router/plural.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ module.exports = function (db, name) {
4141
// GET /name?_embed=&_expand=
4242
function list (req, res, next) {
4343

44-
// Filters list
45-
var filters = {}
46-
4744
// Resource chain
4845
var chain = db(name).chain()
4946

@@ -82,23 +79,24 @@ module.exports = function (db, name) {
8279

8380
}
8481

85-
// Add query parameters filters
86-
// Convert query parameters to their native counterparts
87-
for (var key in req.query) {
88-
// don't take into account JSONP query parameters
82+
Object.keys(req.query).forEach(function (key) {
83+
// Don't take into account JSONP query parameters
8984
// jQuery adds a '_' query parameter too
9085
if (key !== 'callback' && key !== '_') {
91-
filters[key] = utils.toNative(req.query[key])
92-
}
93-
}
94-
95-
// Filter
96-
if (!_(filters).isEmpty()) {
97-
for (var f in filters) {
98-
// This syntax allow for deep filtering using lodash (i.e. a.b.c[0])
99-
chain = chain.filter(f, filters[f])
86+
// Always use an array, in case req.query is an array
87+
var arr = [].concat(req.query[key])
88+
89+
chain = chain.filter(function (element) {
90+
return arr
91+
.map(utils.toNative)
92+
.map(function (value) {
93+
return _.matchesProperty(key, value)(element)
94+
}).reduce(function (a, b) {
95+
return a || b
96+
})
97+
})
10098
}
101-
}
99+
})
102100

103101
// Sort
104102
if (_sort) {

test/cli/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@ var request = require('supertest')
66
var rmrf = require('rimraf')
77
var pkg = require('../../package.json')
88

9-
request = request('http://localhost:3000')
9+
var PORT = 3100
10+
11+
request = request('http://localhost:' + PORT)
1012

1113
var tmpDir = path.join(__dirname, '../../tmp')
1214
var dbFile = path.join(tmpDir, 'db.json')
1315
var routesFile = path.join(tmpDir, 'routes.json')
1416

1517
function cli (args) {
1618
var bin = path.join(__dirname, '../..', pkg.bin)
17-
return cp.spawn('node', [bin].concat(args), {
19+
return cp.spawn('node', [bin, '-p', PORT].concat(args), {
1820
stdio: 'inherit',
1921
cwd: __dirname
2022
})

test/server/plural.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,29 @@ describe('Server', function () {
9595
.expect(200, done)
9696
})
9797

98+
it('should support multiple filters', function (done) {
99+
request(server)
100+
.get('/comments?id=1&id=2')
101+
.expect('Content-Type', /json/)
102+
.expect([db.comments[0], db.comments[1]])
103+
.expect(200, done)
104+
})
105+
98106
it('should support deep filter', function (done) {
99107
request(server)
100108
.get('/deep?a.b=1')
101109
.expect('Content-Type', /json/)
102110
.expect([db.deep[0]])
103111
.expect(200, done)
104112
})
113+
114+
it('should ignore JSONP query parameters callback and _ ', function (done) {
115+
request(server)
116+
.get('/comments?callback=1&_=1')
117+
.expect('Content-Type', /text/)
118+
.expect(new RegExp(db.comments[0].body)) // JSONP returns text
119+
.expect(200, done)
120+
})
105121
})
106122

107123
describe('GET /:resource?q=', function () {

0 commit comments

Comments
 (0)