Skip to content

Commit 581a818

Browse files
committed
Add Query Range function to combine offset/limit functionality.
1 parent 82aba88 commit 581a818

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Query.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ var FirestoreQuery_ = function (from, callback) {
159159
this.offset = function (offset) {
160160
if (!isNumeric_(offset)) {
161161
throw new TypeError('Offset is not a valid number!')
162+
} else if (offset < 0) {
163+
throw new RangeError('Offset must be >= 0!')
162164
}
163165
query.offset = offset
164166
return this_
@@ -173,11 +175,38 @@ var FirestoreQuery_ = function (from, callback) {
173175
this.limit = function (limit) {
174176
if (!isNumeric_(limit)) {
175177
throw new TypeError('Limit is not a valid number!')
178+
} else if (limit < 0) {
179+
throw new RangeError('Limit must be >= 0!')
176180
}
177181
query.limit = limit
178182
return this_
179183
}
180184

185+
/**
186+
* Sets the range of Query results returned.
187+
*
188+
* @param {number} start Start result number (inclusive)
189+
* @param {number} end End result number (inclusive)
190+
* @returns {object} this query object for chaining
191+
*/
192+
this.range = function (start, end) {
193+
if (!isNumeric_(start)) {
194+
throw new TypeError('Range start is not a valid number!')
195+
} else if (!isNumeric_(end)) {
196+
throw new TypeError('Range end is not a valid number!')
197+
} else if (start < 0) {
198+
throw new RangeError('Range start must be >= 0!')
199+
} else if (end < 0) {
200+
throw new RangeError('Range end must be >= 0!')
201+
} else if (start >= end) {
202+
throw new RangeError('Range start must be less than range end!')
203+
}
204+
205+
query.offset = start
206+
query.limit = end - start
207+
return this_
208+
}
209+
181210
/**
182211
* Executes the query with the given callback method and the generated query.
183212
* Must be used at the end of any query for execution.

0 commit comments

Comments
 (0)