Handling Continuation Tokens with Node.js on Windows Azure Table Storage

If you’re querying Windows Azure Table Storage, and you could have more than 1,000 results, or your entities span more than one PartitionKey – you may have your results split. In the C# SDK, there is a handy ‘AsTableServiceQuery‘ extension method, that handles this automatically for you. In the node SDK (as far as I know) this doesn’t exist. Instead, you are passed a continuation token object which you can use to retrieve the remaining results. It’s slightly awkard to use, so here is an example of some code which will call you back when all results have been retrieved:

var tableService = require("azure").createTableService();;

function queryWithContinuation(query, cb) {
    tableService.queryEntities(query, function(error, entities, continuationToken){
        if (continuationToken.nextPartitionKey) { 
            nextPage(entities, continuationToken, cb);
        } else {
            cb(entities);                    
        }
    });
}

// used to recursively retrieve the results
function nextPage(entities, continuationToken, cb){
    continuationToken.getNextPage(function(error, results, newContinuationToken){
        entities = entities.concat(results);
        if (newContinuationToken.nextPartitionKey){
            nextPage(entities, newContinuationToken, cb);
        } else {
            cb(entities);
        }
    });
}

// example usage
var query = azure.TableQuery.select().from('really-big-table');
queryWithContinuation(query, function(results){
    console.log(results);
});

This code has no error handling, but it shows how the continuation token is used.

Advertisements