Skip to content

Commit

Permalink
https://github.com/GoogleCloudPlatform/nodejs-docs-samples/pull/16#di…
Browse files Browse the repository at this point in the history
…scussion_r42409314
  • Loading branch information
stephenplusplus committed Oct 19, 2015
1 parent 61fbb61 commit 12e40e3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
39 changes: 29 additions & 10 deletions datastore/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,33 +345,52 @@ Query.prototype.testLimit = function(callback) {

Query.prototype.testCursorPaging = function(callback) {
var pageSize = 1;
var pageCursor = '';

datastore.createQuery = this.datastore.createQuery;

// [START cursor_paging]
// By default, gcloud-node will paginate through all of the results that match
// a query, push them into an array, then return them to your callback after
// they have all been retrieved. You must execute `.autoPaginate(false)` on
// your query to disable this behavior.
var query = datastore.createQuery('Task')
// Pagination is enabled by default in gcloud-node. To switch it off:
.autoPaginate(false)
.limit(pageSize);
.limit(pageSize)
.start(pageCursor);

function onQueryResults(err, results, nextQuery) {
datastore.runQuery(query, function(err, results, nextQuery) {
if (err) {
// An error occurred while running the query.
return;
}

// If there are more results to retrieve, the cursor will have automatically
// been set on `nextQuery`.
var nextPageCursor;

if (nextQuery) {
datastore.runQuery(nextQuery, onQueryResults);
// If there are more results to retrieve, the start cursor is
// automatically set on `nextQuery`. To get this value directly, access
// the `startVal` property.
nextPageCursor = nextQuery.startVal;
} else {
// No more results exist.
}
}

datastore.runQuery(query, onQueryResults);
});
// [END cursor_paging]

delete datastore.createQuery;
this.datastore.runQuery(query, callback);
this.datastore.runQuery(query, function(err, results, nextQuery) {
if (err) {
callback(err);
return;
}

if (!nextQuery || !nextQuery.startVal) {
callback(new Error('A nextQuery with a startVal is not present.'));
} else {
callback();
}
});
};

Query.prototype.testEventualConsistentQuery = function(callback) {
Expand Down
6 changes: 1 addition & 5 deletions test/datastore/testQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,7 @@ describe('limit query', function() {

describe('cursor paging', function() {
it('allows manual pagination through results', function(done) {
query.testCursorPaging(function(err, results, nextQuery) {
assert.ifError(err);
assert.notStrictEqual(nextQuery, null);
done();
});
query.testCursorPaging(done);
});
});

Expand Down

0 comments on commit 12e40e3

Please sign in to comment.