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

前端 未结 11 2851
清酒与你
清酒与你 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:10

    This is a drop-in replacement to scan all records:

    const scanAll = async (params) => {
        let all = [];
        while (true) {
            let data = await new Promise((resolve, reject) => {
                db.scan(params, function (err, data) {
                    if (err)
                        reject(err);
                    else
                        resolve(data);
                });
            });
            all = all.concat(data.Items);
            if (data.LastEvaluatedKey)
                params.ExclusiveStartKey = data.LastEvaluatedKey;
            else
                break;
        }
        return all;
    };
    

    Usage:

    scanAll(query)
        .catch((err) => {
    
        })
        .then((records) => {
    
        });
    }
    

提交回复
热议问题