使用azure-storage节点库获取属于分区键的最新记录的最佳方法是什么?

问题描述 投票:0回答:1

有人建议在Node中使用azure-storage库时获取属于分区键的最新记录的最佳解决方案?由于没有.orderBy()选项......什么是最好的方法?

在C#中,我可能会做类似的事情:

var latestRecord = results.OrderByDescending(r => r.Timestamp).FirstOrDefault();

将此节点库用于表存储时,等价物会是什么?

node.js azure azure-storage azure-table-storage azure-node-sdk
1个回答
0
投票

我们需要实现自定义比较功能来对结果进行排序。

tableService.queryEntities(tableName, query, null, function(error, result, response) {
  if (!error) {
      var latestRecord = result.entries.sort((a,b)=>{
          return new Date(b.Timestamp._) - new Date(a.Timestamp._);
      })[0])
 }});

结果用Edm类型装饰,所以我们需要b.Timestamp._

Timestamp: { '$': 'Edm.DateTime', _: 2018-10-26T07:32:58.490Z }

如果这种格式在某种程度上令人不愉快,我们可以从响应中获取没有元数据的实体。需要设置payloadFormat。

tableService.queryEntities(tableName, query, null, {payloadFormat:azure.TableUtilities.PayloadFormat.NO_METADATA},function(error, result, response) {
  if (!error) {
      var latestRecord = response.body.value.sort((a,b)=>{
          return new Date(b.Timestamp) - new Date(a.Timestamp);
      })[0]
 }});
© www.soinside.com 2019 - 2024. All rights reserved.