Schaemelhout
Schaemelhout

Reputation: 705

Returning raw entities from Azure Table Storage in Node.js

My JavaScript objects/entities look like this:

{
  id: 1,
  name: 'foo',
  timestamp: new Date()
}

The entities I need to pass to the Azure TableService are going to look something like this:

{
  id: { '_': 1, '$': 'Edm.Int32' },
  name: { '_': 'foo', '$': 'Edm.String' },
  timestamp:{ '_': new Date(), '$': 'Edm.DateTime' },
}

This is easily done using the entityGenerator and this is also the format in which the entities are returned from the TableService.

Is it possible though to return the raw values from the TableService when fetching data from a table? I don't really need all of these OData types and metadata on my JavaScript objects.

I might need to use something like a PropertyResolver but the documentation is quite confusing.

Upvotes: 3

Views: 2026

Answers (2)

Wojciech
Wojciech

Reputation: 51

How about trying with options parameter?

tableService.retrieveEntity(tableName, partitionKey, rowKey, {payloadFormat:"application/json;odata=nometadata"}, function(error, result, response) {});

It can be used also with queries:

tableService.queryEntities(tableName, query, null,{payloadFormat:"application/json;odata=nometadata"}, function(error,result, response) {});

And to get cleaner json you might take response.body instead result.

Example:

tableService.retrieveEntity(tableName, partitionKey, rowKey, {payloadFormat:"application/json;odata=nometadata"}, function(error, result, response) {
        if (!error)
        {
            var myEntity = response.body;
        }
});

Upvotes: 5

Aaron Chen
Aaron Chen

Reputation: 9950

You can use entityResolver to achieve this.

Example:

var entityResolver = function(entity) {
    var resolvedEntity = {};

    for(key in entity) {
        resolvedEntity[key] = entity[key]._;
    }
    return resolvedEntity;
}

var options = {};
options.entityResolver = entityResolver;

tableSvc.retrieveEntity('mytable', 'hometasks', '1', options, function(error, result, response) {

    if(!error) {
        console.log('result: ', result);
    }
});

Upvotes: 3

Related Questions