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

always return endcursor - fixes #288 #290

Merged
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
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) {});

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

* });
*/
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 = '';

This comment was marked as spam.

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);

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

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) {

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

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