简体   繁体   中英

Azure Table string entity

On inserting the entity:

{
  PartitionKey: { _: 'user@example.com', '$': 'Edm.String' },
  RowKey: { _: 'a31d564b-20bc-4721-8b76-57d124967987', '$': 'Edm.String' },
  filename: { _: 'file-foo', '$': 'Edm.String' }
 }

it gets retrieved as:

{
  PartitionKey: { '$': 'Edm.String', _: 'user@example.com' },
  RowKey: { '$': 'Edm.String', _: 'a31d564b-20bc-4721-8b76-57d124967987' },
  filename: { _: 'file-foo' },
  Timestamp: { '$': 'Edm.DateTime', _: 2016-07-25T07:31:11.117Z },
  '.metadata':
   { metadata: 'http://127.0.0.1:10002/devstoreaccount1/$metadata#UserJobs/@Element',
     etag: 'W/"datetime\'2016-07-25T07%3A31%3A11.117Z\'"' }
}

Notice that the retrieved entity's filename doesn't have property '$': 'Edm.String'

If the retrieveEntity() option autoResolveProperties is set, then filename gets the property '$': 'Edm.String'

However, it would feel safer to avoid the autoResolveProperties option since the documentation mentions The logic for returning entity types can get complicated .

The app doesn't need auto detection of properties. It only needs to retrieve the identical type which is explicitly inserted ( Edm.String ).

Is it possible to do this without setting autoResolveProperties .

As the description in the source code of Azure Storage SDK for node.js ,

The service only provides a type if JsonFullMetadata or JsonMinimalMetadata is used, and if the type is Int64, Guid, Binary, or DateTime.

So the string type should will not be provides by default. However, we can leverage the options.propertyResolver parameter to custom a function to retrieve the edm type.

Please try the following code snippet:

var propertyResolver = function (pk, rk, name, value) { 
    if (name.indexOf('BinaryField') !== -1) { 
    return 'Edm.Binary'; 
    } else if (name.indexOf('GuidField') !== -1) { 
    return 'Edm.Guid'; 
    } else if (name.indexOf('DateField') !== -1) { 
    return 'Edm.DateTime'; 
    } else if (name.indexOf('DoubleField') !== -1) { 
    return 'Edm.Double'; 
    } 
    return 'Edm.String'; 
};
var options = {};
options.propertyResolver = propertyResolver; 
tableSvc.retrieveEntity('table', 'user@example.com', 'a31d564b-20bc-4721-8b76-57d124967987',options, function(error, result, response){
  if(!error){
    // result contains the entity
    console.log(result)
  }
});

Please refer to https://blogs.msdn.microsoft.com/windowsazurestorage/2014/06/26/microsoft-azure-storage-client-module-for-node-js-v-0-2-0/ for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM