This repository was archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathParseDbResult.gs
127 lines (111 loc) · 2.79 KB
/
ParseDbResult.gs
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
var ParseDbResult_ = function(instance, query) {
// Params.
this.instance_ = instance;
this.query_ = query;
// Builder values.
this.limit_ = Number.MAX_VALUE;
this.skip_ = null;
this.order_ = null;
// Local results.
this.results_ = [];
// Total results fetched so far.
this.total_ = 0;
// Current offset.
this.offset_ = 0;
// Total number of results that match the query.
this.count_ = null;
// If the query has already been executed and can no longer be modified.
this.locked_ = false;
// If there are no more results to fetch.
this.done_ = false;
};
ParseDbResult_.PAGE_SIZE_ = 1000;
ParseDbResult_.prototype.hasNext = function() {
if (this.results_.length == 0 && !this.done_) {
this.fetch_();
}
return this.results_.length > 0;
};
ParseDbResult_.prototype.next = function() {
if (!this.hasNext()) {
throw 'No items left.';
}
return this.results_.shift();
};
ParseDbResult_.prototype.getSize = function() {
this.hasNext();
if (this.limit_ != Number.MAX_VALUE) {
return Math.min(this.count_, this.limit_);
} else if (this.skip_) {
return this.count_ - this.skip_;
} else {
return this.count_;
}
};
ParseDbResult_.prototype.limit = function(number) {
if (this.locked_) {
throw 'Cannot change limit.';
}
this.limit_ = number;
return this;
};
ParseDbResult_.prototype.startAt = function(number) {
if (this.locked_) {
throw 'Cannot change start.';
}
this.skip_ = number;
return this;
};
ParseDbResult_.prototype.sortBy = function(fieldPath, opt_direction,
opt_strategy) {
if (this.locked_) {
throw 'Cannot change sorting.';
}
if (opt_direction == ParseDbInstance_.prototype.DESCENDING) {
fieldPath = '-' + fieldPath;
}
this.order_ = fieldPath;
return this;
};
ParseDbResult_.prototype.paginate = function(pageNumber, pageSize) {
this.limit(pageSize);
this.startAt(pageSize * pageNumber);
return this;
};
/**
* Fetches the next page of results from the Parse API.
*/
ParseDbResult_.prototype.fetch_ = function() {
if (!this.locked_) {
this.locked_ = true;
this.offset_ = this.skip_ || 0;
}
var limit = Math.min(ParseDbResult_.PAGE_SIZE_, this.limit_ - this.total_);
if (limit == 0) {
this.done_ = true;
return;
}
var params = {
where: JSON.stringify(this.query_),
limit: limit,
skip: this.offset_,
count:1
};
if (this.order_) {
params.order = this.order_;
}
var response = this.instance_.call_({
params: params
});
if (response.results.length == 0) {
this.done_ = true;
} else {
this.total_ += response.results.length;
var items = response.results.map(function(item) {
return new ParseDbMap_(item);
});
this.results_ = this.results_.concat(items);
}
this.count_ = response.count;
this.offset_ += limit;
}