From a727e1ccd3fc8b14498ed0f813d6b473286ad40d Mon Sep 17 00:00:00 2001 From: Seiji Akiyama Date: Fri, 15 Apr 2016 15:17:53 -0300 Subject: [PATCH] Adds limit = 0 as a valid parameter for queries (#1493) * Remove results if limit = 0; * Adds tests for limit=0 and count=1. * Improves readability. --- spec/RestQuery.spec.js | 29 +++++++++++++++++++++++++++++ src/RestQuery.js | 4 ++++ src/Routers/ClassesRouter.js | 2 +- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/spec/RestQuery.spec.js b/spec/RestQuery.spec.js index 4655bca7..5635590e 100644 --- a/spec/RestQuery.spec.js +++ b/spec/RestQuery.spec.js @@ -191,4 +191,33 @@ describe('rest query', () => { }); }); + it('query with limit = 0', (done) => { + rest.create(config, nobody, 'TestObject', {foo: 'baz'} + ).then(() => { + return rest.create(config, nobody, + 'TestObject', {foo: 'qux'}); + }).then(() => { + return rest.find(config, nobody, + 'TestObject', {}, {limit: 0}); + }).then((response) => { + expect(response.results.length).toEqual(0); + done(); + }); + }); + + it('query with limit = 0 and count = 1', (done) => { + rest.create(config, nobody, 'TestObject', {foo: 'baz'} + ).then(() => { + return rest.create(config, nobody, + 'TestObject', {foo: 'qux'}); + }).then(() => { + return rest.find(config, nobody, + 'TestObject', {}, {limit: 0, count: 1}); + }).then((response) => { + expect(response.results.length).toEqual(0); + expect(response.count).toEqual(2); + done(); + }); + }); + }); diff --git a/src/RestQuery.js b/src/RestQuery.js index 34201a06..7f707fc4 100644 --- a/src/RestQuery.js +++ b/src/RestQuery.js @@ -325,6 +325,10 @@ RestQuery.prototype.replaceDontSelect = function() { // Returns a promise for whether it was successful. // Populates this.response with an object that only has 'results'. RestQuery.prototype.runFind = function() { + if (this.findOptions.limit === 0) { + this.response = {results: []}; + return Promise.resolve(); + } return this.config.database.find( this.className, this.restWhere, this.findOptions).then((results) => { if (this.className === '_User') { diff --git a/src/Routers/ClassesRouter.js b/src/Routers/ClassesRouter.js index fd90217b..9803858c 100644 --- a/src/Routers/ClassesRouter.js +++ b/src/Routers/ClassesRouter.js @@ -23,7 +23,7 @@ export class ClassesRouter extends PromiseRouter { if (body.skip) { options.skip = Number(body.skip); } - if (body.limit) { + if (body.limit || body.limit === 0) { options.limit = Number(body.limit); } else { options.limit = Number(100);