【发布时间】:2019-02-21 19:16:19
【问题描述】:
我构建了一个 AWS Lambda,它扫描和过滤我的 DynamoDB 并返回到我的 AWS API。因此,我有三个可能的搜索参数(项目名称、作者和类型),而我不知道查询中使用了哪些参数。起初,我实现了一个版本,其中所有搜索参数都是硬编码的。结果,如果没有定义所有搜索参数,我会得到错误。最后,我重新编写了代码以根据输入的搜索参数构建单独的扫描参数。
代码运行良好,但我认为对于这个问题有更好的实现,也许你可以给我一些改进建议。否则,这将帮助那些在可选搜索参数方面面临同样问题的人。
var AWS = require('aws-sdk');
var docClient = new AWS.DynamoDB.DocumentClient();
//This is the Lambda function
exports.handler = function(event, context, callback)
{
//In case we query without query attributes
if(!event.hasOwnProperty("queryStringParameters"))
{
console.log("NO queryStringParameters FOUND");
var emptyparams =
{
TableName: "blackboard-items",
};
docClient.scan(emptyparams, onScan);
return;
}
//we want to tailor this attributes for the params for docClient.scan(params, onScan);
var queryParam = event["queryStringParameters"];
var filterexpression = "";
var expressionAttributeNames = {}; //Instantiate
var expressionAttributeValues = {};
console.log("QUERY PARAMETERS: " + JSON.stringify(queryParam));
//Do we look for an author?
if(queryParam.hasOwnProperty("author"))
{
console.log("FOUND AUTHOR");
filterexpression += "contains(#author, :author)"; //Collect scan params
expressionAttributeNames['#author'] = 'author';
expressionAttributeValues[':author'] = event["queryStringParameters"]["author"];
}
//Do we look for an itemname?
if(queryParam.hasOwnProperty("itemname"))
{
console.log("FOUND ITEMNAME");
if(filterexpression !== "")
filterexpression += " AND contains(#itemname, :itemname)";
else
filterexpression += "contains(#itemname, :itemname)";
expressionAttributeNames['#itemname'] = 'itemname';
expressionAttributeValues[':itemname'] = queryParam["itemname"];
}
//Do we look for a type?
if(queryParam.hasOwnProperty("type"))
{
console.log("FOUND TYPE");
if(filterexpression !== "")
filterexpression += " AND #type = :type";
else
filterexpression += "#type = :type";
expressionAttributeNames['#type'] = 'type';
expressionAttributeValues[':type'] = event["queryStringParameters"]["type"];
}
//Build params based on the tailored parts
var params =
{
TableName: "blackboard-items",
FilterExpression: filterexpression,
ExpressionAttributeNames: expressionAttributeNames,
ExpressionAttributeValues: expressionAttributeValues,
};
//Use tailored params for scan()
docClient.scan(params, onScan);
var count = 0;
function onScan(err, data)
{
if (err)
{
console.error("Unable to scan the table. Error JSON:", JSON.stringify(err, null, 2));
}
else
{
console.log("Scan succeeded.");
data.Items.forEach(function(itemdata)
{
console.log("Item :", ++count,JSON.stringify(itemdata));
});
// continue scanning if we have more items
if (typeof data.LastEvaluatedKey != "undefined")
{
console.log("Scanning for more...");
params.ExclusiveStartKey = data.LastEvaluatedKey;
docClient.scan(params, onScan);
}
}
var response =
{
"isBase64Encoded": false,
"statusCode": "200",
"headers": { },
"body": JSON.stringify(data.Items)
};
callback(null, response);
}
};
注意: 数据库的主键是“itemname”,但我很快会重新设计数据库设计以获得排序键。
【问题讨论】:
-
您似乎没有任何实际问题。如果您只是想分享您的代码并在上面获取一些 cmets,那么 codereview.stackexchange.com 可能适合您。正如我所看到的,这篇文章在 stackoverflow 上是题外话。
标签: node.js amazon-web-services aws-lambda amazon-dynamodb dynamodb-queries