Skip to content

Commit 3a23d86

Browse files
committed
Add basic pagination
1 parent 706ab46 commit 3a23d86

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

src/server/router/plural.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ module.exports = function (db, name) {
4747
var q = req.query.q
4848
var _start = req.query._start
4949
var _end = req.query._end
50+
var _page = req.query._page
5051
var _sort = req.query._sort
5152
var _order = req.query._order
5253
var _limit = req.query._limit
@@ -146,17 +147,23 @@ module.exports = function (db, name) {
146147
}
147148

148149
// Slice result
149-
if (_end || _limit) {
150+
if (_end || _limit || _page) {
150151
res.setHeader('X-Total-Count', chain.size())
151152
res.setHeader('Access-Control-Expose-Headers', 'X-Total-Count')
152153
}
153154

154-
_start = parseInt(_start, 10) || 0
155-
156-
if (_end) {
155+
if (_page) {
156+
_page = parseInt(_page, 10)
157+
_page = _page >= 1 ? _page : 1
158+
_limit = parseInt(_limit, 10) || 10
159+
_start = (_page - 1) * _limit
160+
chain = chain.slice(_start, _start + _limit)
161+
} else if (_end) {
162+
_start = parseInt(_start, 10) || 0
157163
_end = parseInt(_end, 10)
158164
chain = chain.slice(_start, _end)
159165
} else if (_limit) {
166+
_start = parseInt(_start, 10) || 0
160167
_limit = parseInt(_limit, 10)
161168
chain = chain.slice(_start, _start + _limit)
162169
}

test/server/plural.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ describe('Server', function () {
5151
{resource: {name: 'howe'}}
5252
]
5353

54+
db.list = [
55+
{id: 1},
56+
{id: 2},
57+
{id: 3},
58+
{id: 4},
59+
{id: 5},
60+
{id: 6},
61+
{id: 7},
62+
{id: 8},
63+
{id: 9},
64+
{id: 10},
65+
{id: 11},
66+
{id: 12},
67+
{id: 13},
68+
{id: 14},
69+
{id: 15}
70+
]
71+
5472
server = jsonServer.create()
5573
router = jsonServer.router(db)
5674
server.use(jsonServer.defaults())
@@ -236,6 +254,30 @@ describe('Server', function () {
236254
})
237255
})
238256

257+
describe('GET /:resource?_page=', function () {
258+
it('should paginate', function (done) {
259+
request(server)
260+
.get('/list?_page=2')
261+
.expect('Content-Type', /json/)
262+
.expect('x-total-count', db.list.length.toString())
263+
.expect('Access-Control-Expose-Headers', 'X-Total-Count')
264+
.expect(db.list.slice(10, 20))
265+
.expect(200, done)
266+
})
267+
})
268+
269+
describe('GET /:resource?_page=&_limit=', function () {
270+
it('should paginate with a custom limit', function (done) {
271+
request(server)
272+
.get('/list?_page=2&_limit=1')
273+
.expect('Content-Type', /json/)
274+
.expect('x-total-count', db.list.length.toString())
275+
.expect('Access-Control-Expose-Headers', 'X-Total-Count')
276+
.expect(db.list.slice(1, 2))
277+
.expect(200, done)
278+
})
279+
})
280+
239281
describe('GET /:resource?attr_gte=&attr_lte=', function () {
240282
it('should respond with a limited array', function (done) {
241283
request(server)

0 commit comments

Comments
 (0)