Question:
So I need a recursive function in node.js
for replacing this function call:
docClient.scan(params, callback)
More info see http://docs.aws.amazon.com/amazondynamodb/latest/gettingstartedguide/GettingStarted.NodeJs.04.html
Answer:
Here is the recursive code to execute the scan until LastEvaluatedKey
is available.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
var AWS = require("aws-sdk"); var creds = new AWS.Credentials('akid', 'secret', 'session'); AWS.config.update({ region: "us-west-2", endpoint: "http://localhost:8000", credentials : creds }); var docClient = new AWS.DynamoDB.DocumentClient(); var params = { TableName: "Movies" }; console.log("Scanning Movies table."); docClient.scan(params, onScan); var count = 0; function onScan(err, data) { if (err) { console.error("Unable to scan the table. Error JSON:", JSON.stringify(err, null, 2)); } else { // print all the movies console.log("Scan succeeded."); data.Items.forEach(function(movie) { console.log("Item :", ++count,JSON.stringify(movie)); }); // continue scanning if we have more movies if (typeof data.LastEvaluatedKey != "undefined") { console.log("Scanning for more..."); params.ExclusiveStartKey = data.LastEvaluatedKey; docClient.scan(params, onScan); } } } |