From 0797b42a0680f0b85d349d7c1a69ebac0c17ff1c Mon Sep 17 00:00:00 2001 From: Seiji Akiyama Date: Fri, 8 Apr 2016 10:51:14 -0300 Subject: [PATCH 1/3] Remove results if limit = 0; --- src/RestQuery.js | 38 +++++++++++++++++++++--------------- src/Routers/ClassesRouter.js | 2 +- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/RestQuery.js b/src/RestQuery.js index 34201a06bd..8695700326 100644 --- a/src/RestQuery.js +++ b/src/RestQuery.js @@ -325,23 +325,26 @@ 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() { - return this.config.database.find( - this.className, this.restWhere, this.findOptions).then((results) => { - if (this.className === '_User') { - for (var result of results) { - delete result.password; + if (this.findOptions.limit !== 0) { + return this.config.database.find( + this.className, this.restWhere, this.findOptions).then((results) => { + if (this.className === '_User') { + for (var result of results) { + delete result.password; + } } } - this.config.filesController.expandFilesInObject(this.config, results); + this.config.filesController.expandFilesInObject(this.config, results); - if (this.keys) { - var keySet = this.keys; - results = results.map((object) => { - var newObject = {}; - for (var key in object) { - if (keySet.has(key)) { - newObject[key] = object[key]; + if (this.keys) { + var keySet = this.keys; + results = results.map((object) => { + var newObject = {}; + for (var key in object) { + if (keySet.has(key)) { + newObject[key] = object[key]; + } } } return newObject; @@ -352,9 +355,12 @@ RestQuery.prototype.runFind = function() { for (var r of results) { r.className = this.redirectClassName; } - } - this.response = {results: results}; - }); + this.response = {results: results}; + }); + } else { + this.response = {results: []}; + return Promise.resolve(); + } }; // Returns a promise for whether it was successful. diff --git a/src/Routers/ClassesRouter.js b/src/Routers/ClassesRouter.js index fd90217bcf..9803858c7c 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); From b7a6be302f8006c8d546dbf39598007a0b3d3ea3 Mon Sep 17 00:00:00 2001 From: Seiji Akiyama Date: Thu, 14 Apr 2016 16:00:27 -0300 Subject: [PATCH 2/3] Adds tests for limit=0 and count=1. --- spec/RestQuery.spec.js | 29 +++++++++++++++++++++++++++++ src/RestQuery.js | 3 +-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/spec/RestQuery.spec.js b/spec/RestQuery.spec.js index 4655bca705..5635590e68 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 8695700326..cccd17d11a 100644 --- a/src/RestQuery.js +++ b/src/RestQuery.js @@ -333,7 +333,6 @@ RestQuery.prototype.runFind = function() { delete result.password; } } - } this.config.filesController.expandFilesInObject(this.config, results); @@ -346,7 +345,6 @@ RestQuery.prototype.runFind = function() { newObject[key] = object[key]; } } - } return newObject; }); } @@ -355,6 +353,7 @@ RestQuery.prototype.runFind = function() { for (var r of results) { r.className = this.redirectClassName; } + } this.response = {results: results}; }); } else { From 559bce1bf2a98fc038edb6524ed4895fd4838b75 Mon Sep 17 00:00:00 2001 From: Seiji Akiyama Date: Fri, 15 Apr 2016 14:58:26 -0300 Subject: [PATCH 3/3] Improves readability. --- src/RestQuery.js | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/src/RestQuery.js b/src/RestQuery.js index cccd17d11a..7f707fc47d 100644 --- a/src/RestQuery.js +++ b/src/RestQuery.js @@ -325,26 +325,29 @@ 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) { - return this.config.database.find( - this.className, this.restWhere, this.findOptions).then((results) => { - if (this.className === '_User') { - for (var result of results) { - delete result.password; - } + 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') { + for (var result of results) { + delete result.password; } + } - this.config.filesController.expandFilesInObject(this.config, results); + this.config.filesController.expandFilesInObject(this.config, results); - if (this.keys) { - var keySet = this.keys; - results = results.map((object) => { - var newObject = {}; - for (var key in object) { - if (keySet.has(key)) { - newObject[key] = object[key]; - } + if (this.keys) { + var keySet = this.keys; + results = results.map((object) => { + var newObject = {}; + for (var key in object) { + if (keySet.has(key)) { + newObject[key] = object[key]; } + } return newObject; }); } @@ -354,12 +357,8 @@ RestQuery.prototype.runFind = function() { r.className = this.redirectClassName; } } - this.response = {results: results}; - }); - } else { - this.response = {results: []}; - return Promise.resolve(); - } + this.response = {results: results}; + }); }; // Returns a promise for whether it was successful.