From 71a9ea1f1a1bd272fb31b28cee0cd4383377f64e Mon Sep 17 00:00:00 2001 From: akiroz Date: Wed, 7 Sep 2016 17:40:29 +0800 Subject: [PATCH] Add method chaining API for query pagination --- lib/query.js | 15 +++++++++++++++ test/query.js | 20 ++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/lib/query.js b/lib/query.js index b00de9bb..2b819414 100644 --- a/lib/query.js +++ b/lib/query.js @@ -313,6 +313,21 @@ export default class Query { return this; } + limitResults(num) { + this.limit = num; + return this; + } + + skipResults(num) { + this.offset = num; + return this; + } + + retrievePage(num) { + this.page = num; + return this; + } + _orQuery(queries) { _.forEach(queries, (query) => { this._orPredicate.push(query.predicate); diff --git a/test/query.js b/test/query.js index 8b9a59a2..c2678bfb 100644 --- a/test/query.js +++ b/test/query.js @@ -26,6 +26,26 @@ describe('Query', function () { let Note = Record.extend('note'); let User = Record.extend('user'); + it('limit results', function () { + let q = new Query(Note); + q.limitResults(20); + expect(q.limit).to.be.equal(20); + }); + + it('skip results', function () { + let q = new Query(Note); + q.skipResults(42); + expect(q.offset).to.be.equal(42); + expect(q.limit).to.be.equal(50); + }); + + it('retrieve page', function () { + let q = new Query(Note); + q.retrievePage(3); + expect(q.page).to.be.equal(3); + expect(q.limit).to.be.equal(50); + }); + it('reject invalid recordType', function () { expect(function () { new Query({});