How to fetch/scan all items from `AWS dynamodb` using node.js

前端 未结 11 2829
清酒与你
清酒与你 2020-11-30 00:19

How to fetch/scan all items from AWS dynamodb using node.js. I am posting my code here.

var docClient = new aws.DynamoDB.DocumentCl         


        
11条回答
  •  爱一瞬间的悲伤
    2020-11-30 01:12

    A node express solution that returns the data in JSON format:

    let datapack=[];
    item = {
            TableName: ddbTable,
            FilterExpression: "aws = :e AND begins_with ( Id, :t )",
            ExpressionAttributeValues: {
                ":t"    :   "contact",
                ":e"    :   aws
            },
            ProjectionExpression: "Id,FirstName,LastName,cEmail",
        };
        docClient.scan(item, onScan);
        function onScan(err, data) {
            if (err) {
                console.error("Unable to scan the table. Error JSON:", JSON.stringify(err, null, 2));
            } else {        
                datapack = datapack.concat(data.Items);
                });
                if (typeof data.LastEvaluatedKey != "undefined") {
                    item.ExclusiveStartKey = data.LastEvaluatedKey;
                    docClient.scan(item, onScan);
                } else {
                    res.json(datapack);
                }
            }
        }
    

提交回复
热议问题