Skip to content

Instantly share code, notes, and snippets.

@bwinant
Created October 2, 2018 05:41
Show Gist options
  • Save bwinant/2124b8810645436a3a6a50dfb00e46f1 to your computer and use it in GitHub Desktop.
Save bwinant/2124b8810645436a3a6a50dfb00e46f1 to your computer and use it in GitHub Desktop.
Enhanced DynamoDB client that automatically handles LastEvaluatedKeys
'use strict';
const AWS = require("aws-sdk");
class EnhancedDynamoDBClient extends AWS.DynamoDB.DocumentClient {
constructor(options) {
super(options);
}
scanAll(params) {
const doScan = (items) => {
return this.scan(params).promise()
.then(result => {
items.push(...result.Items);
if (!result.LastEvaluatedKey) {
return items;
}
else {
console.log(result.LastEvaluatedKey)
params.ExclusiveStartKey = result.LastEvaluatedKey;
return doScan(items);
}
});
};
return doScan([]);
}
queryAll(params) {
const doQuery = (items) => {
return this.query(params).promise()
.then(result => {
items.push(...result.Items);
if (!result.LastEvaluatedKey) {
return items;
}
else {
params.ExclusiveStartKey = result.LastEvaluatedKey;
return doQuery(items);
}
});
};
return doQuery([]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment