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 CHANGED
@@ -1,5 +1,9 @@
1
1
  # Change Log
2
2
 
3
+ ## [0.10.1][2017-05-16] `GET /posts?_sort=user,views&_order=desc,asc`
4
+
5
+ * Multiple fields sorting
6
+
3
7
  ## [0.10.0][2017-04-26]
4
8
 
5
9
  * __Drop Node `v0.12` support__
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=DESC
158
- GET /posts/1/comments?_sort=votes&_order=ASC
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
- _order = _order || 'ASC';
138
-
139
- chain = chain.sortBy(function (element) {
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-server",
3
- "version": "0.10.0",
3
+ "version": "0.10.1",
4
4
  "description": "Serves JSON files through REST routes.",
5
5
  "main": "./lib/server/index.js",
6
6
  "bin": "./bin/index.js",
@@ -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=', () => {