json-server 0.10.0 → 0.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/README.md +8 -2
- package/lib/server/router/plural.js +4 -8
- package/package.json +1 -1
- package/test/server/plural.js +24 -0
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -154,8 +154,14 @@ _10 items are returned by default_
|
|
|
154
154
|
Add `_sort` and `_order` (ascending order by default)
|
|
155
155
|
|
|
156
156
|
```
|
|
157
|
-
GET /posts?_sort=views&_order=
|
|
158
|
-
GET /posts/1/comments?_sort=votes&_order=
|
|
157
|
+
GET /posts?_sort=views&_order=asc
|
|
158
|
+
GET /posts/1/comments?_sort=votes&_order=asc
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
For multiple fields, use the following format:
|
|
162
|
+
|
|
163
|
+
```
|
|
164
|
+
GET /posts?_sort=user,views&_order=desc,asc
|
|
159
165
|
```
|
|
160
166
|
|
|
161
167
|
### Slice
|
|
@@ -134,15 +134,11 @@ module.exports = function (db, name) {
|
|
|
134
134
|
|
|
135
135
|
// Sort
|
|
136
136
|
if (_sort) {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
return _.get(element, _sort);
|
|
137
|
+
var _sortSet = _sort.split(',');
|
|
138
|
+
var _orderSet = (_order || '').split(',').map(function (s) {
|
|
139
|
+
return s.toLowerCase();
|
|
141
140
|
});
|
|
142
|
-
|
|
143
|
-
if (_order === 'DESC') {
|
|
144
|
-
chain = chain.reverse();
|
|
145
|
-
}
|
|
141
|
+
chain = chain.orderBy(_sortSet, _orderSet);
|
|
146
142
|
}
|
|
147
143
|
|
|
148
144
|
// Slice result
|
package/package.json
CHANGED
package/test/server/plural.js
CHANGED
|
@@ -42,6 +42,18 @@ describe('Server', () => {
|
|
|
42
42
|
{ id: 5, body: 'quux', published: false, postId: 2, userId: 1 }
|
|
43
43
|
]
|
|
44
44
|
|
|
45
|
+
db.buyers = [
|
|
46
|
+
{ id: 1, name: 'Aileen', country: 'Colombia', total: 100 },
|
|
47
|
+
{ id: 2, name: 'Barney', country: 'Colombia', total: 200 },
|
|
48
|
+
{ id: 3, name: 'Carley', country: 'Colombia', total: 300 },
|
|
49
|
+
{ id: 4, name: 'Daniel', country: 'Belize', total: 30 },
|
|
50
|
+
{ id: 5, name: 'Ellen', country: 'Belize', total: 20 },
|
|
51
|
+
{ id: 6, name: 'Frank', country: 'Belize', total: 10 },
|
|
52
|
+
{ id: 7, name: 'Grace', country: 'Argentina', total: 1 },
|
|
53
|
+
{ id: 8, name: 'Henry', country: 'Argentina', total: 2 },
|
|
54
|
+
{ id: 9, name: 'Isabelle', country: 'Argentina', total: 3 }
|
|
55
|
+
]
|
|
56
|
+
|
|
45
57
|
db.refs = [
|
|
46
58
|
{ id: 'abcd-1234', url: 'http://example.com', postId: 1, userId: 1 }
|
|
47
59
|
]
|
|
@@ -269,6 +281,18 @@ describe('Server', () => {
|
|
|
269
281
|
.expect([ db.nested[1], db.nested[0], db.nested[2] ])
|
|
270
282
|
.expect(200)
|
|
271
283
|
))
|
|
284
|
+
|
|
285
|
+
it('should sort on multiple fields', () => (
|
|
286
|
+
request(server)
|
|
287
|
+
.get('/buyers?_sort=country,total&_order=asc,desc')
|
|
288
|
+
.expect('Content-Type', /json/)
|
|
289
|
+
.expect([
|
|
290
|
+
db.buyers[8], db.buyers[7], db.buyers[6],
|
|
291
|
+
db.buyers[3], db.buyers[4], db.buyers[5],
|
|
292
|
+
db.buyers[2], db.buyers[1], db.buyers[0]
|
|
293
|
+
])
|
|
294
|
+
.expect(200)
|
|
295
|
+
))
|
|
272
296
|
})
|
|
273
297
|
|
|
274
298
|
describe('GET /:resource?_start=&_end=', () => {
|