Skip to content

Instantly share code, notes, and snippets.

@chadkouse
Created October 22, 2013 21:53
Show Gist options
  • Save chadkouse/7108865 to your computer and use it in GitHub Desktop.
Save chadkouse/7108865 to your computer and use it in GitHub Desktop.
node.js dynamodb scan
doScan: function (tableName, limit, startKey, callback) {
limit = typeof limit !== 'undefined' ? limit : null;
startKey = typeof startKey !== 'undefined' ? startKey : null;
var self = this;
var params = {TableName: tableName};
if (limit != null)
params.Limit = limit;
if (startKey != null)
params.ExclusiveStartKey = startKey;
var items = [];
var processNextBite = function (err, items, nextKey) {
if (nextKey && nextKey != null) {
params.ExclusiveStartKey = nextKey;
getNextBite(params, items, processNextBite);
} else {
callback(err, items);
}
};
var getNextBite = function (params, items, callback) {
var result = self.db.scan(params, function (err, data) {
var obj = null;
if (err != null) {
console.log(err); // TODO better error handling
obj = null;
callback(err, items, null);
return;
}
if (typeof data.Items == "undefined") {
}
if (data && data.Items && data.Items.length > 0)
items = items.concat(data.Items);
var lastStartKey = null;
if (data)
lastStartKey = data.LastEvaluatedKey;
callback(err, items, lastStartKey);
});
};
getNextBite(params, items, processNextBite);
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment