Created
October 2, 2018 05:41
-
-
Save bwinant/2124b8810645436a3a6a50dfb00e46f1 to your computer and use it in GitHub Desktop.
Enhanced DynamoDB client that automatically handles LastEvaluatedKeys
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
'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