Updating to Node.js Azure-Storage 0.2.0 Package

This blog post is long overdue, but back in the summer 2014, a new Node package module (npm) was released for Azure-Storage.

Azure-Storage v0.2.0 broke some sh&t and is not backwards compatible with previous versions. With this version, the Edm type for each entity property must be declared. The Edm type is stored in the storage service and each table entity is now an object of the form:

EntityProperty: {
    _: <VALUE>,
    $: <EDM_TYPE: 'Edm.DateTime', 'Edm.String', etc>
}

What this means is that for each entity property we wish to persist, we need to declare its associated type.

Inserting

Before

var azure = require('azure-storage');
var tableService = azure.createTableService(),

var entity = {
    PartitionKey: <PARTITION_KEY>,
    RowKey: <ROW_KEY>,
    ...
}

tableService.insertEntity(<TABLE_NAME>, entity, function(error, result, response) {
    ...
});

After

We can use Entity Generator, a convenience function, to declare the type of each property.

var azure = require('azure-storage');
var tableService = azure.createTableService(),
var entGen = azure.TableUtilities.entityGenerator;

var entity = {  
    PartitionKey: entGen.String(<PARTITION_KEY>),
    RowKey: entGen.String(<ROW_KEY>),
    dateValue: entGen.DateTime(new Date(Date.UTC(2011, 10, 25))),
    boolValue: entGen.Boolean(false),
    ...
};

tableService.insertEntity(<TABLE_NAME>,entity, function (error, result, response) {
    ...
});

But that's a little crazy right? This is OK if we only wanted to save one or two different entities but what if you had different entities, are you really going to go through all of them and upgrade them to declare its type? Instead, string everything:

var azure = require('azure-storage');
var tableService = azure.createTableService(),

var rawEntity = {
    PartitionKey: <PARTITION_KEY>,
    RowKey: <ROW_KEY>,
    ...
}

var entity = {};
for(var prop in rawEntity) {
    entity[prop] = { '_': rawEntity[prop] };
}

tableService.insertEntity(<TABLE_NAME>, entity, function(error, result, response) {
    ...
});

Querying

This applies to both retrieveEntity and queryEntities methods.

Before

var azure = require('azure-storage');
var tableService = azure.createTableService();

var query = azure.TableQuery()
        .where('PartitionKey eq ?', <PARTITION_KEY>);

tableService.queryEntities(<TABLE_NAME>, query, null, function(error, result, response)    {
    // do something with result.entries
}); 

After

The service API returns:

  • metadata and
  • for each entity property: it's type ('$') and it's value ('_')

Let's parse this back to an object we can work with:

var azure = require('azure-storage');
var tableService = azure.createTableService();

var query = azure.TableQuery()
        .where('PartitionKey eq ?', <PARTITION_KEY>);

tableService.queryEntities(<TABLE_NAME>, query, null, function(error, result, response)    {
    var entityList = [];
    var azureEntities = result.entries;

    for (var i = 0; i < azureEntities.length; i++) {
        var entity = {};
        for (var key in azureEntities[i]) {
            if (key === '.metadata') {
                continue;
            }

            entity[key] = azureEntities[i][key]._;
        }
        entityList[entity.RowKey] = entity;
    }        

    // do something with entityList
}); 

Delete

No changes here.