Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

datastore: Add support for end cursors #94

Merged
merged 1 commit into from
Aug 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}