Working With Azure Tables - Part Two

Introduction

Let us look at a basic sample which retrieves the Azure table data into the applications using Node JS.

In my previous article, you have seen the basic introduction to Azure Tables.

Start creating a node application by running npm init. Create the main file (JavaScript file) for writing the core functionality. In my case, index.js will be used.

 

The dependency files need to be imported. To access the Azure Table Storage data, Azure Storage functions and objects will be used. The npm package called azure-storage package is available for working with Azure Storage. This needs to be imported and installed using npm command.

 

The following code snippet shows the package.json file created for this project.

  1. {  
  2.   "name": "getazuretabledata",  
  3.   "version": "1.0.0",  
  4.   "description": "",  
  5.   "main": "index.js",  
  6.   "scripts": {  
  7.     "test": "echo \"Error: no test specified\" && exit 1"  
  8.   },  
  9.   "author": "nakkeeran",  
  10.   "license": "ISC",  
  11.   "dependencies": {  
  12.     "azure-storage": "^2.5.0"  
  13.   }  
  14. }  

From the Azure Table Storage, account name and access keys need to be noted and used in the node application for accessing the Azure Storage data. 

  • Navigate to https://portal.azure.com. Go to the Azure Storage account created.
  • Navigate to Access keys under Settings option. Copy the storage account name and any one of the keys available.
 
 
Main Index.js File 

Create a port and import necessary packages.

  1. var port = process.env.PORT || 8000;  
  2. var http = require('http');  
  3. var azure = require('azure-storage');  
Create the Server. In the Server function, the following steps are followed. 
  • Access the table service using azure.createTableService(accountName,accessKey) method.
  1. var port = process.env.PORT || 8000;  
  2. var http = require('http');  
  3. var azure = require('azure-storage');  
  • Table query is built. In the query, a necessary condition should be built using the filters available. In this sample, we are going to see how we are filtering the Azure Table data using partition keys.
  1. var accountName = "accountname copied from azure portal";  
  2. var accessKey = "access key copied from azure portal";  
  • Then, when the table service initialized, the necessary query is made using queryEntities method. The necessary parameters are - Azure table name, query, token, and the call back method.
  1. var server = http.createServer(function(request, response) {    
  2.     //response.end("Listening on port %s...", server.address().port);  
  3.     var tableService = azure.createTableService(accountName,accessKey); // explicit  
  4.     var TableQuery = azure.TableQuery;  
  5.     var TableUtilities = azure.TableUtilities;  
  6.     var queryCondition = TableQuery.stringFilter('PartitionKey', TableUtilities.QueryComparisons.EQUAL, "Grade 1");  
  7.     var query = new azure.TableQuery().where(queryCondition);  
  8.     tableService.queryEntities('azuretesttable', query, nullfunction(error, result, res) {  
  9.         if (!error) {  
  10.             response.end(JSON.stringify(result));  
  11.         }  
  12.         else{  
  13.             console.log(error);  
  14.         }  
  15.     });  
  16. });    

The following code snippet shows the entire main function logic.

  1. var port = process.env.PORT || 8000;  
  2. var http = require('http');  
  3. var azure = require('azure-storage');  
  4.   
  5. var accountName = "accountname copied from azure portal";  
  6. var accessKey = "access key copied from azure portal";  
  7.   
  8. var server = http.createServer(function(request, response) {    
  9.     
  10.     var tableService = azure.createTableService(accountName,accessKey);
  11.     var TableQuery = azure.TableQuery;  
  12.     var TableUtilities = azure.TableUtilities;  
  13.     var queryCondition = TableQuery.stringFilter('PartitionKey', TableUtilities.QueryComparisons.EQUAL, "Grade 1");  
  14.     var query = new azure.TableQuery().where(queryCondition);  
  15.     tableService.queryEntities('azuretesttable', query, nullfunction(error, result, res) {  
  16.         if (!error) {  
  17.             response.end(JSON.stringify(result));  
  18.         }  
  19.         else{  
  20.             console.log(error);  
  21.         }  
  22.     });  
  23. });    
  24.     
  25. server.listen(port);   
The following snapshot shows the results retrieved from the Azure Table.
 

Summary

In this article, you have seen the basic example of retrieving the Azure Storage Table data into Node.js application.