Skip to content

Commit

Permalink
Merge pull request #94 from rakyll/endcursor
Browse files Browse the repository at this point in the history
datastore: Add support for end cursors
  • Loading branch information
Burcu Dogan committed Aug 5, 2014
2 parents c1e37e1 + bbe4141 commit 1a07340
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
3 changes: 3 additions & 0 deletions lib/datastore/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ var queryToQueryProto = function(q) {
if (q.startVal) {
query.startCursor = q.startVal;
}
if (q.endVal) {
query.endCursor = q.endVal;
}
if (q.offsetVal > 0) {
query.offset = q.offsetVal;
}
Expand Down
7 changes: 7 additions & 0 deletions lib/datastore/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function Query(namespace, kinds) {

// pagination
this.startVal = null;
this.endVal = null;
this.limitVal = -1;
this.offsetVal = -1;
}
Expand Down Expand Up @@ -79,6 +80,12 @@ Query.prototype.start = function(start) {
return q;
}

Query.prototype.end = function(end) {
var q = util.extend(this, new Query());
q.endVal = end;
return q;
};

Query.prototype.limit = function(n) {
var q = util.extend(this, new Query());
q.limitVal = n;
Expand Down
10 changes: 7 additions & 3 deletions test/datastore.query.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,12 @@ describe('Query', function() {
done();
});

it('should allow page start tokens', function(done) {
var q = ds.createQuery(['kind1']).start('abc123');
it('should allow page start and end tokens', function(done) {
var q = ds.createQuery(['kind1'])
.start('abc123')
.end('def987');
assert.strictEqual(q.startVal, 'abc123');
assert.strictEqual(q.endVal, 'def987');
done();
});

Expand All @@ -119,7 +122,8 @@ describe('Query', function() {
.filter('name =', 'Burcu')
.order('-count')
.groupBy(['count'])
.start('cursor')
.start('start-cursor')
.end('end-cursor')
.offset(5)
.limit(10);
assert.deepEqual(entity.queryToQueryProto(q), queryProto);
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/proto_query.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"projection":[{"property":{"name":"name"}},{"property":{"name":"count"}}],"kinds":[{"name":"Kind"}],"filter":{"compositeFilter":{"filters":[{"propertyFilter":{"property":{"name":"count"},"operator":"GREATER_THAN_OR_EQUAL","value":{"integerValue":5}}},{"propertyFilter":{"property":{"name":"name"},"operator":"EQUAL","value":{"stringValue":"Burcu"}}}],"operator":"AND"}},"order":[{"property":{"name":"count"},"direction":"DESCENDING"}],"groupBy":[{"name":"count"}],"startCursor":"cursor","offset":5,"limit":10}
{"projection":[{"property":{"name":"name"}},{"property":{"name":"count"}}],"kinds":[{"name":"Kind"}],"filter":{"compositeFilter":{"filters":[{"propertyFilter":{"property":{"name":"count"},"operator":"GREATER_THAN_OR_EQUAL","value":{"integerValue":5}}},{"propertyFilter":{"property":{"name":"name"},"operator":"EQUAL","value":{"stringValue":"Burcu"}}}],"operator":"AND"}},"order":[{"property":{"name":"count"},"direction":"DESCENDING"}],"groupBy":[{"name":"count"}],"startCursor":"start-cursor","endCursor": "end-cursor","offset":5,"limit":10}

0 comments on commit 1a07340

Please sign in to comment.