-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (59 loc) · 2.28 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var Parse = require('parse-shim');
var PAGE_SIZE = 1000;
var MAX_QUERIES = 10;
//
// Run the specified parse query multiple times, modifying skip/limit each
// time to download all available objects. Return a promise with the stitched
// results from all individual queries. This works around the limit of 1000
// results per parse query.
//
// For safety, unless superStitch is specified, this limits the total number
// of queries to 10, yielding an effective max of 10,000 results.
//
// If the 'superStitch' option is specified, results are ordered and additionally paged
// by the 'createdAt' date field after exhausting the 10k skip. This effectively
// allows a query to yield unlimited results.
//
// @param query {Parse.Query} The query to stitch.
// @param options {Object} StitchQuery options below, all others passed to query as query opts.
// - superStitch {Boolean} If you want more than 10k rows you can superStitch.
//
// NOTE: superStitch is not suitable for use with a sort (as it applies its own sort on createdAt)
// NOTE: This will modify the specified query, do not reuse the query after calling this function.
//
function StitchQuery(query, options) {
options = options || {};
query.limit(PAGE_SIZE);
if (options.superStitch) {
query.ascending('objectId');
}
var stitchedPromise = new Parse.Promise();
var stitchedResults = [];
function getNextPage(curPage, lastObjectId) {
query.skip(curPage * PAGE_SIZE);
if (lastObjectId) {
query.greaterThan('objectId', lastObjectId);
}
var pagePromise = query.find(options);
pagePromise.done(function(pageResults) {
pageResults.forEach(function(result) { stitchedResults.push(result); });
var maxBatchSize = pageResults.length === PAGE_SIZE;
var lastResult = stitchedResults[stitchedResults.length - 1];
if (maxBatchSize) {
if (curPage + 1 < MAX_QUERIES) {
getNextPage(curPage + 1, lastObjectId);
} else if (options.superStitch) {
getNextPage(0, lastResult.id);
} else {
stitchedPromise.resolve(stitchedResults);
}
} else {
stitchedPromise.resolve(stitchedResults);
}
});
pagePromise.fail(stitchedPromise.reject);
};
getNextPage(0);
return stitchedPromise;
};
module.exports = StitchQuery;