Skip to content

Commit

Permalink
Merge pull request #290 from stephenplusplus/spp--datastore-return-en…
Browse files Browse the repository at this point in the history
…dcursor

always return endcursor - fixes #288
  • Loading branch information
silvolu committed Nov 7, 2014
2 parents 247cbbf + b494f59 commit 7c851ba
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 28 deletions.
21 changes: 10 additions & 11 deletions lib/datastore/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,15 +308,15 @@ DatastoreRequest.prototype.delete = function(keys, callback) {
* //-
*
* // Retrieve 5 companies.
* transaction.runQuery(queryObject, function(err, entities, nextQuery) {
* // `nextQuery` is not null if there are more results.
* if (nextQuery) {
* transaction.runQuery(nextQuery, function(err, entities, nextQuery) {});
* }
* transaction.runQuery(queryObject, function(err, entities, endCursor) {
* // Use `endCursor` as the starting cursor for your next query.
* var nextQuery = queryObject.start(endCursor);
* transaction.runQuery(nextQuery, function(err, entities, endCursor) {});
* });
*/
DatastoreRequest.prototype.runQuery = function(q, callback) {
callback = callback || util.noop;

var req = {
read_options: {},
query: entity.queryToQueryProto(q)
Expand All @@ -333,14 +333,13 @@ DatastoreRequest.prototype.runQuery = function(q, callback) {
callback(err);
return;
}
var nextQuery = null;

var cursor = '';
if (resp.batch.end_cursor) {
var cursor = resp.batch.end_cursor.toBase64();
if (cursor !== q.startVal) {
nextQuery = q.start(cursor).offset(0);
}
cursor = resp.batch.end_cursor.toBase64();
}
callback(null, entity.formatArray(resp.batch.entity_result), nextQuery);

callback(null, entity.formatArray(resp.batch.entity_result), cursor);
});
};

Expand Down
22 changes: 10 additions & 12 deletions regression/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,19 +231,17 @@ describe('datastore', function() {
it('should limit queries', function(done) {
var q = ds.createQuery('Character').hasAncestor(ancestor)
.limit(5);
ds.runQuery(q, function(err, firstEntities, secondQuery) {
ds.runQuery(q, function(err, firstEntities, firstEndCursor) {
assert.ifError(err);
assert.equal(firstEntities.length, 5);
assert(secondQuery);
ds.runQuery(secondQuery, function(err, secondEntities, thirdQuery) {
var secondQ = q.start(firstEndCursor);
ds.runQuery(secondQ, function(err, secondEntities, secondEndCursor) {
assert.ifError(err);
assert.equal(secondEntities.length, 3);
// TODO(silvano): it currently requires an additional request that
// brings an empty page and a null query
//assert.equal(thirdQuery, null)
ds.runQuery(thirdQuery, function(err, thirdEntities, fourthQuery) {
var thirdQ = q.start(secondEndCursor);
ds.runQuery(thirdQ, function(err, thirdEntities) {
assert.ifError(err);
assert.equal(fourthQuery, null);
assert.equal(thirdEntities.length, 0);
done();
});
});
Expand Down Expand Up @@ -324,11 +322,12 @@ describe('datastore', function() {
.offset(2)
.limit(3)
.order('appearances');
ds.runQuery(q, function(err, entities, secondQuery) {
ds.runQuery(q, function(err, entities, endCursor) {
assert.ifError(err);
assert.equal(entities.length, 3);
assert.equal(entities[0].data.name, 'Robb');
assert.equal(entities[2].data.name, 'Catelyn');
var secondQuery = q.start(endCursor).offset(0);
ds.runQuery(secondQuery, function(err, secondEntities) {
assert.equal(secondEntities.length, 3);
assert.equal(secondEntities[0].data.name, 'Sansa');
Expand All @@ -343,13 +342,12 @@ describe('datastore', function() {
.offset(2)
.limit(2)
.order('appearances');
ds.runQuery(q, function(err, entities, nextQuery) {
ds.runQuery(q, function(err, entities, endCursor) {
assert.ifError(err);
var startCursor = nextQuery.startVal;
var cursorQuery =
ds.createQuery('Character').hasAncestor(ancestor)
.order('appearances')
.start(startCursor);
.start(endCursor);
ds.runQuery(cursorQuery, function(err, secondEntities) {
assert.ifError(err);
assert.equal(secondEntities.length, 4);
Expand Down
24 changes: 19 additions & 5 deletions test/datastore/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,15 +329,29 @@ describe('Request', function() {
});
});

it('should return a new query if results remain', function() {
it('should return an empty string if no end cursor exists', function(done) {
request.makeReq_ = function(method, req, callback) {
assert.equal(method, 'runQuery');
callback(null, mockResponse.withResultsAndEndCursor);
callback(null, mockResponse.withResults);
};

request.runQuery(query, function(err, entities, endCursor) {
assert.ifError(err);
assert.strictEqual(endCursor, '');
done();
});
});

it('should return the end cursor from the last query', function(done) {
var response = mockResponse.withResultsAndEndCursor;

request.makeReq_ = function(method, req, callback) {
callback(null, response);
};

request.runQuery(query, function(err, entities, nextQuery) {
request.runQuery(query, function(err, entities, endCursor) {
assert.ifError(err);
assert.equal(nextQuery.constructor.name, 'Query');
assert.equal(endCursor, response.batch.end_cursor.toBase64());
done();
});
});
});
Expand Down

0 comments on commit 7c851ba

Please sign in to comment.