How to manage Microsoft Azure Table Storage with Node.js

[Infragistics] Mihail Mateev / Thursday, July 31, 2014

Node.js is one one of the the most popular growing platforms for development.  I started a series of posts dedicated to Node.js , starting with two articles about Node.js and Microsoft SQL Server ( you can see part 1 and part 2 ) .  This blog discusses how you can use Node.js and Microsoft Azure Table Storage . Open source solutions (OSS),  are very suitable for the implementation of platform independent and/or cloud applications. Node.js is widely used in the actual implementation of such cloud applications. I will try demonstrate in several blogs some details how to use Node.js with  Microsoft Azure Storage .

 

What is Azure Storage?

Microsoft Azure storage services allow us to store/retrieve the NON RELATIONAL data to/from Microsoft Cloud environment. (For relational data, SQL Azure services are used).

In Microsoft Azure Storage, the data can be stored in 4 different formats (v.i.z. Blobs, Tables and Queues, File Storage (in preview )). The retrieval/storage of the above data is done in RESTful way.

  • Blob storage stores file data. A blob can be any type of text or binary data, such as a document, media file, or application installer.
  • Table storage stores structured datasets. Table storage is a NoSQL key-attribute data store, which allows for rapid development and fast access to large quantities of data.
  • Queue storage provides reliable messaging for workflow processing and for communication between components of cloud services.
  • File storage offers shared storage for legacy applications using the standard SMB 2.1 protocol. Azure virtual machines and cloud services can share file data across application components via mounted shares, and on-premise applications can access file data in a share via the File service REST API.

 

 

This article is focused on how to handle Azure Table services with Node.js

 

Table Storage

The Azure Table storage service stores large amounts of structured data. The service is a NoSQL datastore which accepts authenticated calls from inside and outside the Azure cloud.

The Table service contains the following components:

 

 

Table Entities:

Table entities represent the units of data stored in a table and are similar to rows in a typical relational database table. Each entity defines a collection of properties. Each property is key/value pair defined by its name, value, and the value's data type. Entities must define the following three system properties as part of the property collection:

  • PartitionKey – The PartitionKey property stores string values that identify the partition that an entity belongs to. This means that entities with the same PartitionKey values belong in the same partition. Partitions, as discussed later, are integral to the scalability of the table.
  • RowKey – The RowKey property stores string values that uniquely identify entities within each partition.
  • Timestamp – The Timestamp property provides traceability for an entity. A timestamp is a DateTime value that tells you the last time the entity was modified. A timestamp is sometimes referred to as the entity's version. Modifications to timestamps are ignored because the table service maintains the value for this property during all inserts and update operations.

 

Consider PartitionKey, RowKey in your design. Think of PartitionKey and RowKey as being a primary index.

 

Table Partitions:

Azure Tables use keys that enable efficient querying, and you can employ one—the PartitionKey—for load balancing when the table service decides it’s time to spread your table over multiple servers. A table doesn’t have a specified schema. 

 

Partitions represent a collection of entities with the same PartitionKey values. Partitions are always served from one partition server and each partition server can serve one or more partitions.

 

Dealing with an Azure Table Storage

You can use different NodeJS packages to handle Azure Table Storage. In this post we will cover azure and azure-table-node Node packages.

 

  • Microsoft Azure SDK for Node.js

It is an official Microsoft Azure SDK for Node.js. This project provides a Node.js package that makes it easy to consume and manage Microsoft Azure Services.

The listed snippets below demonstrate how to manage Azure Table service using Microsoft Azure SDK for Node.js.

 

This is easy to do since the Azure SDK will look for credentials using environment variables first. The magical environment variable names are AZURE_STORAGE_ACCOUNT and AZURE_STORAGE_ACCESS_KEY.

 

  • Create a table service

Set credentials using environment variables

   1: var azure = require('azure');
   2:  
   3: //using enviroment variables for credentials
   4: var tableService = azure.createTableService(); // implicitly use env variables
   5:  
   6: tableService = azure.createTableService(
   7:   process.env.AZURE_STORAGE_ACCOUNT,
   8:   process.env.AZURE_STORAGE_ACCESS_KEY); // explicit
   9:  

 

Set credentials explicitly using local variables

   1: var accessKey = '[accountKey]';
   2: var storageAccount = '[accountName]';
   3:  
   4: var tableService = azure.createTableService(
   5:   accessKey, storageAccount); // explicit

 

  • Insert an entity
   1: var tableService = azure.createTableService();
   2:  
   3: //insert an entity
   4:  var   task1 = {
   5:         PartitionKey : 'myPartitionKey',
   6:         RowKey: '1',
   7:         Description: 'Row description',
   8:         DueDate: new Date(2011, 12, 14, 12)
   9:  };
  10:     
  11: tableService.insertEntity('tasktable', task1, function(error){
  12:     if(!error){
  13:         // Entity inserted
  14:     }
  15: });

 

  • Query entities
   1: //query an entity
   2: var tableService = azure.createTableService();
   3: tableService.queryEntity('demotable', 'myPartitionKey', '1', function(error, serverEntity){
   4:     if(!error){
   5:         // Entity available in serverEntity variable
   6:     }
   7: });

 

 

  • azure-table-node:

It is a simplified Azure Table Storage client library for Node.js that supported:

  • creating, deleting and listing tables
  • creating, updating, querying and deleting entities
  • batch operation support
  • generating SAS (Shared Access Signature) and using it for authentication

 

Code samples below show how to use azure-table-node module to work with Azure Table Storage
 

  • Set Azure Storage credentials
   1: var azureTable = require('azure-table-node')
   2:  
   3: //set azure storage credentials
   4: azureTable.setDefaultClient({
   5:     accountUrl: 'http://[accountName].table.core.windows.net/',
   6:     accountName: '[accountName]',
   7:     accountKey: '[accountKey]'
   8: });

 

  • Create an Azure Table
   1: //create azure table
   2: app.get("/createTable", function (req, res) {
   3:  
   4:     var client = azureTable.getDefaultClient();
   5:     client.createTable('testtable', function (err, data) {
   6:     });
   7:  
   8:     client.insertEntity('testtable', {
   9:         PartitionKey: 'tests',
  10:         RowKey: '1',
  11:         value1: 'ABCDEFG'
  12:     }, function (err, data) {
  13:         res.write("Got error :-( " + err);
  14:     });
  15:  
  16:     res.end("Table created.");
  17: });

 

  • Display an Azure Table
   1: //display an azure table
   2: app.get("/displayTable", function (req, res) {
   3:  
   4:     var client = azureTable.getDefaultClient();
   5:  
   6:     client.queryEntities('testtable', {
   7:         query: azureTable.Query.create('PartitionKey', '==', 'tests') 
   8:  
   9:     }, function (err, data, continuation) {
  10:         if (err) {
  11:             res.writeHead(500, { 'Content-Type': 'text/plain' });
  12:             res.write("Got error :-( " + err);
  13:             res.end("");
  14:             return;
  15:         }
  16:  
  17:         var json = JSON.stringify(data);
  18:         res.writeHead(200, { 'Content-Type': 'text/plain' })
  19:  
  20:         res.end("Table displayed: " + json);
  21:        });
  22:  
  23: });

 

 

  • List  all Azure Tables
   1: //list all azure tables
   2: app.get("/listTables", function (req, res) {    
   3:  
   4:     var client = azureTable.getDefaultClient();
   5:  
   6:     client.listTables(function (err, data, continuation) {
   7:         if (err) {
   8:             res.writeHead(500, { 'Content-Type': 'text/plain' });
   9:             res.write("Got error :-( " + err);
  10:             res.end("");
  11:             return;
  12:         }
  13:  
  14:         res.writeHead(200, { 'Content-Type': 'text/plain' })
  15:  
  16:         for (var i = 0; i < data.length; i++) {
  17:             res.write("Table[" + i + "]: " + data[i] + " " );
  18:         }       
  19:  
  20:         res.end("Tables listed." + data);
  21:     });
  22:  
  23: });

 

 

  • Delete an Azure Table
   1: //delete azure table
   2: app.get("/deleteTable", function (req, res) {
   3:  
   4:     var client = azureTable.getDefaultClient();
   5:     client.deleteTable('testtable', function (err, data) {
   6:     });
   7:     res.end("Table testtable has been deleted.");
   8:  
   9: });

 

 

There’s so much more to learn about Azure Table services and Node.  The features and capabilities of table storage continue to grow. This post is just an intro how to start, covering the base cases and most popular Node.js modules for Azure Tables. It will be useful for both – JavaScript developers who don’t have experience with Microsoft Azure and Azure developers who have less experience with JavaScript and/or Node.js.

Developers on the Microsoft Azure Platform should become familiar with how Table Storage works and how it differs from the relational databases they are used to. Knowing how table storage works will help you determine if it is a good fit for your particular requirements.

 

You can download source code from Git repository .

  

If you want more information about how to use Microsoft Azure Storage  & Node.js feel free to contact me at mmateev@infragistics.com

You can learn more about Node.js , Microsoft Azure and related events like Azure Bootcamp Bulgaria if you follow us on Twitter @mihailmateev  and @Infragistics and stay in touch on Facebook, Google+, LinkedIn and Infragistics Friends User Group !