From bbe414188fbc105e3119fc07a84eac7c705a76c6 Mon Sep 17 00:00:00 2001 From: Burcu Dogan Date: Mon, 4 Aug 2014 18:31:06 -0700 Subject: [PATCH] datastore: Add support for end cursors. --- lib/datastore/entity.js | 3 +++ lib/datastore/query.js | 7 +++++++ test/datastore.query.js | 10 +++++++--- test/testdata/proto_query.json | 2 +- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/datastore/entity.js b/lib/datastore/entity.js index 350df990a92..614c269fd4f 100644 --- a/lib/datastore/entity.js +++ b/lib/datastore/entity.js @@ -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; } diff --git a/lib/datastore/query.js b/lib/datastore/query.js index 01230d31484..393b4187936 100644 --- a/lib/datastore/query.js +++ b/lib/datastore/query.js @@ -27,6 +27,7 @@ function Query(namespace, kinds) { // pagination this.startVal = null; + this.endVal = null; this.limitVal = -1; this.offsetVal = -1; } @@ -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; diff --git a/test/datastore.query.js b/test/datastore.query.js index 33d370a59be..5cb11c19843 100644 --- a/test/datastore.query.js +++ b/test/datastore.query.js @@ -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(); }); @@ -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); diff --git a/test/testdata/proto_query.json b/test/testdata/proto_query.json index 7b6f5110a55..db69746ba1c 100644 --- a/test/testdata/proto_query.json +++ b/test/testdata/proto_query.json @@ -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}