mirror of
https://github.com/typicode/json-server.git
synced 2025-07-28 04:32:24 +08:00
Merge pull request #276 from tony-kerz/tk/nested-sort
support sort by nested field (e.g. _sort=author.name)
This commit is contained in:
@ -141,7 +141,7 @@ module.exports = function (db, name) {
|
||||
_order = _order || 'ASC'
|
||||
|
||||
chain = chain.sortBy(function (element) {
|
||||
return element[_sort]
|
||||
return _.get(element, _sort)
|
||||
})
|
||||
|
||||
if (_order === 'DESC') {
|
||||
|
@ -47,6 +47,12 @@ describe('Server', function () {
|
||||
{ a: 1 }
|
||||
]
|
||||
|
||||
db.nested = [
|
||||
{resource: {name: 'dewey'}},
|
||||
{resource: {name: 'cheatem'}},
|
||||
{resource: {name: 'howe'}}
|
||||
]
|
||||
|
||||
server = jsonServer.create()
|
||||
router = jsonServer.router(db)
|
||||
server.use(jsonServer.defaults())
|
||||
@ -198,6 +204,14 @@ describe('Server', function () {
|
||||
.expect(db.posts.reverse())
|
||||
.expect(200, done)
|
||||
})
|
||||
|
||||
it('should sort on nested field', function (done) {
|
||||
request(server)
|
||||
.get('/nested?_sort=resource.name')
|
||||
.expect('Content-Type', /json/)
|
||||
.expect([db.nested[1], db.nested[0], db.nested[2]])
|
||||
.expect(200, done)
|
||||
})
|
||||
})
|
||||
|
||||
describe('GET /:resource?_start=&_end=', function () {
|
||||
|
Reference in New Issue
Block a user