Skip to content

Commit 726f375

Browse files
petitchevalrouxdigitalsadhu
authored andcommitted
feat(pagination): Implement paging
1 parent c3cb65d commit 726f375

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var update = require('./update')
1111
var del = require('./delete')
1212
var errors = require('./errors')
1313
var relationships = require('./relationships')
14+
var querystring = require('./querystring');
1415
var debug = require('debug')('loopback-component-jsonapi')
1516

1617
module.exports = function (app, options) {
@@ -38,4 +39,5 @@ module.exports = function (app, options) {
3839
update(app, options)
3940
errors(app, options)
4041
del(app, options)
42+
querystring(app, options)
4143
}

lib/querystring.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var utils = require('./utils');
2+
3+
module.exports = function (app, options) {
4+
var remotes = app.remotes();
5+
remotes.before('**', function (ctx, next) {
6+
if (utils.shouldNotApplyJsonApi(ctx, options)) {
7+
return next();
8+
}
9+
var query = ctx.req.query;
10+
ctx.args.filter = ctx.args.filter || {};
11+
if(typeof(query.page) === 'object') {
12+
[
13+
{'from':'offset','to':'skip'},
14+
{'from':'limit','to':'limit'}
15+
].forEach(function(p){
16+
if(typeof(query.page[p.from]) === 'string') {
17+
ctx.args.filter[p.to] = query.page[p.from];
18+
}
19+
});
20+
}
21+
return next();
22+
});
23+
};

test/find.test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,57 @@ describe('loopback json api component find methods', function () {
251251
})
252252
})
253253
})
254+
describe('Paging should filter', function () {
255+
beforeEach(function (done) {
256+
Post.create({
257+
title: 'deer can jump',
258+
content: 'deer can jump really high in their natural habitat'
259+
}, function () {
260+
Post.create({
261+
title: 'pigs dont fly',
262+
content: "contrary to the myth, pigs don't fly!"
263+
}, function () {
264+
Post.create({
265+
title: 'unicorns come from rainbows',
266+
content: 'at the end of a rainbow may be a pot of gold, but also a mythical unicorn'
267+
}, done)
268+
})
269+
})
270+
})
271+
272+
it('should filter only one', function (done) {
273+
request(app)
274+
.get('/posts?page[limit]=1')
275+
.expect(200)
276+
.end(function (err, res) {
277+
expect(err).to.equal(null)
278+
expect(res.body.data.length).to.equal(1)
279+
done()
280+
})
281+
})
282+
283+
it('should filter two', function (done) {
284+
request(app)
285+
.get('/posts?page[limit]=2')
286+
.expect(200)
287+
.end(function (err, res) {
288+
expect(err).to.equal(null)
289+
expect(res.body.data.length).to.equal(2)
290+
done()
291+
})
292+
})
293+
294+
it('should skip first', function (done) {
295+
request(app)
296+
.get('/posts?page[limit]=1&page[offset]=1')
297+
.expect(200)
298+
.end(function (err, res) {
299+
expect(res.body.data.length).to.equal(1)
300+
expect(res.body.data[0].id).to.equal('2')
301+
done()
302+
})
303+
})
304+
})
254305
})
255306

256307
describe('non standard primary key naming', function () {

0 commit comments

Comments
 (0)