const AWS = require('aws-sdk'); const docClient = new AWS.DynamoDB.DocumentClient(); /** * Given a parameters object, queries a table and returns the items in an array. * If no valid matching items are found, returns an empty array. * @param {object} params - DynamoDB query params * @param {boolean} recursiveEvaluate - boolean whether operation should continue * until all results are retrieved. Default value is true. */ exports.queryTable = async (params, recursiveEvaluate = true) => { let _data = [], _errors = []; let queryParams = params; const ERROR_THRESHOLD = 3; do { try { let query = await docClient.query(queryParams).promise(); queryParams.ExclusiveStartKey = null; // reset start key if (query.LastEvaluatedKey && recursiveEvaluate) { queryParams.ExclusiveStartKey = query.LastEvaluatedKey; } _data = [..._data, ...query.Items]; } catch(e) { _errors.push({ message: 'unable to query table', error: e }); } } while (queryParams.ExclusiveStartKey && _errors.length < ERROR_THRESHOLD); return { message: `Table ${params.TableName} queried.`, data: _data, errors: _errors, }; }