Skip to content

Commit a727e1c

Browse files
seijiakiyamadrew-gross
authored andcommitted
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.
1 parent f076078 commit a727e1c

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

spec/RestQuery.spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,33 @@ describe('rest query', () => {
191191
});
192192
});
193193

194+
it('query with limit = 0', (done) => {
195+
rest.create(config, nobody, 'TestObject', {foo: 'baz'}
196+
).then(() => {
197+
return rest.create(config, nobody,
198+
'TestObject', {foo: 'qux'});
199+
}).then(() => {
200+
return rest.find(config, nobody,
201+
'TestObject', {}, {limit: 0});
202+
}).then((response) => {
203+
expect(response.results.length).toEqual(0);
204+
done();
205+
});
206+
});
207+
208+
it('query with limit = 0 and count = 1', (done) => {
209+
rest.create(config, nobody, 'TestObject', {foo: 'baz'}
210+
).then(() => {
211+
return rest.create(config, nobody,
212+
'TestObject', {foo: 'qux'});
213+
}).then(() => {
214+
return rest.find(config, nobody,
215+
'TestObject', {}, {limit: 0, count: 1});
216+
}).then((response) => {
217+
expect(response.results.length).toEqual(0);
218+
expect(response.count).toEqual(2);
219+
done();
220+
});
221+
});
222+
194223
});

src/RestQuery.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,10 @@ RestQuery.prototype.replaceDontSelect = function() {
325325
// Returns a promise for whether it was successful.
326326
// Populates this.response with an object that only has 'results'.
327327
RestQuery.prototype.runFind = function() {
328+
if (this.findOptions.limit === 0) {
329+
this.response = {results: []};
330+
return Promise.resolve();
331+
}
328332
return this.config.database.find(
329333
this.className, this.restWhere, this.findOptions).then((results) => {
330334
if (this.className === '_User') {

src/Routers/ClassesRouter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class ClassesRouter extends PromiseRouter {
2323
if (body.skip) {
2424
options.skip = Number(body.skip);
2525
}
26-
if (body.limit) {
26+
if (body.limit || body.limit === 0) {
2727
options.limit = Number(body.limit);
2828
} else {
2929
options.limit = Number(100);

0 commit comments

Comments
 (0)