javascript - 如何通过多次调用 dynamodb scan 使用 Promise

标签 javascript promise amazon-dynamodb

我收到来自 dynamodb.scan() 的 promise 。当 promise 得到解决时,如果响应包含 LastEvaluatedKey,则必须再次调用 dynamodb.scan()。当响应不包含 LastEvaluatedKey 对象时,表的所有记录均已扫描且查询完成。我不知道提前需要调用 scan() 的次数。我不知道如何编写这个代码。这就是我现在拥有的:

function scan(options, startKey) {
  var parms = {
    TableName: options.source,
    ProjectionExpression: "book, bid, #kee, #unt, pid, #txt",
    FilterExpression: "contains(#txt, :v_qs)",
    ExpressionAttributeNames: {
      "#unt": "unit",
      "#txt": "text",
      "#kee": "key"
    },
    ExpressionAttributeValues: {
      ":v_qs": options.queryTransformed
    }
  };

  if (startKey) {
     parms.ExclusiveStartKey = startKey;
  }

  return dynamoDb.scan(parms).promise();
}

scan(options).then(function(response) {
  if (response.LastEvaluatedKey) {
    searchResults.push(response);
    return scan(options, response.LastEvaluatedkey);
  }
  else {
    return response
  }
}).then(function(response) {
  if (response.LastEvaluatedKey) {
    searchResults.push(response);
    return scan(options, response.LastEvaluatedKey);
  }
  else {
    return response;
  }
}).then(function(response) {
  //this is crazy - bailing even if another call is needed
  searchResults.push(response);
  return generateSearchResponse();
});

这显然不是正确的做法。此代码是 AWS Lambda 节点函数的一部分。在此先感谢您的帮助。

最佳答案

递归函数应该是你的 friend 。我自己无法测试,但类似的东西应该有效。如果您遇到任何错误,请告诉我

function scan(options, startKey) {
  var parms = {
    TableName: options.source,
    ProjectionExpression: "book, bid, #kee, #unt, pid, #txt",
    FilterExpression: "contains(#txt, :v_qs)",
    ExpressionAttributeNames: {
      "#unt": "unit",
      "#txt": "text",
      "#kee": "key"
    },
    ExpressionAttributeValues: {
      ":v_qs": options.queryTransformed
    }
  };

  if (startKey) {
     parms.ExclusiveStartKey = startKey;
  }

  return dynamoDb.scan(parms).promise();
}

function processResponse(options, response) {
    return (response && response.LastEvaluatedKey) ?
        scan(options, response.LastEvaluatedkey)
            .then(function(next_response) {
                return [response].concat(processResponse(next_response));
            })
        :
        Promise.resolve(response); // replace 'Promise' by whatever Promise library you are using (when, bluebird, ...)
}

scan(options)
    .then(processResponse.bind(null, options))
    .then(function(response_list) {
        // response_list is an array of response
        return generateSearchResponse();
    });

关于javascript - 如何通过多次调用 dynamodb scan 使用 Promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44646991/

相关文章:

javascript - 如何使用 ajax 将与每个按钮相关的数据发送到 php?

javascript - 类型错误 : Cannot read property 'then' of undefined

amazon-dynamodb - Dynamodb 流运动 - 在两者之间找到不完整的哈希范围

javascript - 淡出 Div 内容 - 空 Div 内容 - 淡入新内容

javascript - AngularJS : Factory $http assigning values returning null

javascript - 在完成之前的相同 Ajax 请求之前,不要发送 Ajax 请求

javascript - Promise.all 和 Promise.race 有效地做出所有 promise "handled"是记录在案的行为吗?

javascript - 异步API是否应该同步抛出?

elasticsearch - 结合使用AWS Appsync和DynamoDB,您是否应该通过将相关数据的 “redundant copies”存储在同一张表上(去规范化)来建立关系模型?

amazon-web-services - 适用于 DynamoDb bool 和列表数据类型的 AWS CloudFormation AttributeType