发电机。我只想列出项目

DynamoDB. I just want to list items

我目前正在努力完成非常有帮助的 Serverless Stack Tutorial。在本教程中,创建了一个 AWS Lambda API 到 DynamoDB,其中的端点称为 "create"、"get"、"list" 等等。

尝试在我自己的项目中实现 "get" 功能,效果很好。 (这个函数使用了dynamoDbLib.call("get", params);操作(我不明白为什么不应该是Documentation) I am using the template of this chapter and the lib-files of this chapter中的getItem。 但是尝试使用 batchgetBatchGetItem 会导致以下错误消息:dynamoDb[action] is not a function.

只是为了表明我的意图:我只想列出我的 table 的所有项目(并且可能稍后指定主键值的范围)。我试过改编tutorial的"list"函数,但是"query"动作好像不是suitable,因为"KeyConditionExpression": "string",只允许主键与相等运算符 (=) 进行比较。 (尽管可以使用 a between b and c 查询排序键)。

你对我应该如何进行有什么建议吗? BatchGetItem 运算符并不理想,而且我必须将项目指定为数组而不是数字范围!

最后,这是代码的最后状态:

    import * as dynamoDbLib from "./libs/dynamodb-lib";
import { success, failure } from "./libs/response-lib";

export async function main(event, context, callback) {
    var params = {
        "RequestItems" : {
            "my-table-name" : {
                "ConsistentRead":false,
                "Keys":[{
                    "id":{
                        "N":"1",
                        "N":"2"
                    }
                }]

        }
        }


    };

    try {
        const result = await dynamoDbLib.call("BatchGet", params);
        if (result.Item) {
            // Return the retrieved item
            callback(null, success(result.Item));
        } else {
            callback(null, failure({ status: false, error: "Item not found." }));
        }
    } catch (e) {
        console.log(e);
        callback(null, failure({ status: false }));
    }
}

感谢您的支持和反馈,但不要对我太苛刻,因为我认为自己是菜鸟!

祝你有愉快的一天!

首先要注意的是DocumentClient不同于AWS.DynamoDB

文档说:

The document client simplifies working with items in Amazon DynamoDB by abstracting away the notion of attribute values.

dynamoDb[action] is not a function - 表示它没有该功能(由于区分大小写,请尝试添加 batchGet)。

我查看了文档,发现文档 documentclient 支持此功能:

  • batchGet(参数,回调)
  • batchWrite(参数,回调)
  • createSet(列表,选项)
  • 删除(参数,回调)
  • 获取(参数,回调)
  • put(参数,回调)
  • 查询(参数,回调)
  • 扫描(参数,回调)
  • 更新(参数,回调)

来自 : https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html

为了获取项目列表并仅获取所需的属性,您可以使用 scan

const params = {
  ProjectionExpression: 'id, name', // fields you need
  TableName: 'table_name'
};

try {
  const result = await dynamoDbLib.call("scan", params);
  // Return the list of items in response body
  callback(null, success(result.Items));
} catch (e) {
  callback(null, failure({ status: false }));
}

希望这对您有所帮助。如果您还有其他问题,请随时提出。