Created
July 31, 2019 13:00
-
-
Save bossley9/b0193fa2d963dc9bbd6f573ee68e25d8 to your computer and use it in GitHub Desktop.
efficient table querying in DynamoDB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment