Search code examples
node.jsazureazure-storageazure-table-storage

Retrieving more than 1000 records from azure storage table - js


I have an issue of retrieving more than a thousand records. Somehow only the first 1000 are retrieved. From my research, I have figured out that I have to use getContinuation Token to get the subsequent records. Please advise how should I add it into my code.

tableService.queryEntities(table, tableQuery, null, function(error, results) {
    if (error) {
        alert('List table entities error, please open browser console to view detailed error');
        console.log(error);
    } else {
        //display records

});

Solution

  • To work with callback based on your code, use recursion.

    var entities = [];
    function queryEntitiesSegmented(table, tableQuery, continuationToken){
        tableService.queryEntities(table, tableQuery, continuationToken, (error, results)=> {
            if (error) {   console.log(error); } 
            else { 
                entities.push.apply(entities, results.entries);
                if(results.continuationToken){
                    queryEntitiesSegmented(table, tableQuery, results.continuationToken);
                }
                else{
                    entities.forEach(entity=>{
                       // display each one
                    })
                }
            }
        });
    }
    
    queryEntitiesSegmented(table, tableQuery, null);
    

    Another choice is async and await.

    const queryEntitiesSegmented = async(table, tableQuery, continuationToken)=>{
        return new Promise((resolve,reject)=>{
            tableService.queryEntities(table, tableQuery, continuationToken, (error, results)=> {
                if (error) {  reject(error); } 
                else { resolve(results);  }
            });
        });
    }
    
    async function queryAllEntities(){
        var tableQuery = new azure.TableQuery();
        var table = 'tableName';
        var continuationToken = null;
        var entities = [];
        do{
            var results =  await queryEntitiesSegmented(table, tableQuery, continuationToken);
            continuationToken = results.continuationToken;
            entities.push.apply(entities, results.entries);
        }
        while(continuationToken!=null);
        return entities;
    }
    
    queryAllEntities().then(entities=>{
        entities.forEach(entity=>{
        //display each one
        })
    }).catch(error=>{ console.log(error); })