Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Table Storage only supports the String as a Data Type?

I'm trying to insert data into an Azure table but everything is converted to strings.

E.g. I'm inserting numbers/booleans

var test={ PartitionKey : '4', RowKey : '2', foo: 4, bar: true };
tableService.insertEntity('mytable', test, ...);

but

tableService.queryEntity('mytable', '4', '2', ...);

returns

{ id: 'http://127.0.0.1:10002/devstoreaccount1/identid(PartitionKey=\'4\',RowKey=\'2\')',
  link: 'identid(PartitionKey=\'4\',RowKey=\'2\')',
  updated: '2012-12-12T10:26:44Z',
  etag: 'W/"datetime\'2012-12-12T10%3A26%3A44.547Z\'"',
  PartitionKey: '4',
  RowKey: '2',
  Timestamp: '2012-12-12T10:20:44.897Z',
  foo: '4',
  bar: 'true' }

How can I specify a data type?


OK, just saw in the SDK that you can specify the data type with

var test={ PartitionKey : '4', RowKey : '2', 
  foo: { '@': { type: 'Edm.Int32' }, '#': 4 } };

however are there any helper functions to add the type automatically?

like image 210
laktak Avatar asked Dec 12 '12 10:12

laktak


2 Answers

Since the SDK does not appear to contain anything useful I wrote these for now:

function azType(type, v) { return { "@": { type: type }, "#": v }; }
function azBool(v) { return azType("Edm.Boolean", v); }
function azBinary(v) { return azType("Edm.Binary", v); }
function azByte(v) { return azType("Edm.Byte", v); }
function azDateTime(v) { return azType("Edm.DateTime", v); }
function azDateTimeOffset(v) { return azType("Edm.DateTimeOffset", v); }
function azDecimal(v) { return azType("Edm.Decimal", v); }
function azDouble(v) { return azType("Edm.Double", v); }
function azGuid(v) { return azType("Edm.Guid", v); }
function azInt64(v) { return azType("Edm.Int64", v); }
function azInt32(v) { return azType("Edm.Int32", v); }
function azInt16(v) { return azType("Edm.Int16", v); }
function azSByte(v) { return azType("Edm.SByte", v); }
function azSingle(v) { return azType("Edm.Single", v); }
function azString(v) { return azType("Edm.String", v); }
function azTime(v) { return azType("Edm.Time", v); }

as in

var test={ PartitionKey : '4', RowKey : '2', foo: azInt32(4) };

I don't know why they changed it but starting with 0.6.10 the azType function needs to be replaced:

function azType(type, v) { return { "$": { type: type }, "_": v }; }
like image 53
laktak Avatar answered Sep 28 '22 12:09

laktak


According to here, only these 8 types are supported by the Table Service Data Model:

  • Edm.Binary
  • Edm.Boolean
  • Edm.DateTime
  • Edm.Double
  • Edm.Guid
  • Edm.Int32
  • Edm.Int64
  • Edm.String

Be careful with id property in your object:

{ PartitionKey : '4', RowKey : '2', id: azInt32(4) };

if you try to do that, error message is not so obvious:

Error inserting : { [Error: [object Object]] code: 'PropertiesNeedValue', message:{ _: 'The values are not specified for all properties in the entity...','$': { 'xml:lang': 'en-US' } } }

like image 28
eldevarth Avatar answered Sep 28 '22 12:09

eldevarth