/* Reference: http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchGetItem.html */
/* Scenario: Query one table multiple times and a second table one time in the same call. */
/*
AWS DYNAMO
*/
var AWS = require("aws-sdk");
var batClient = new AWS.DynamoDB();
var params = {};
var result1 = "", result2 = "";
        
exports.handler = function(event, context) {
    /* Create batchGetItem Query */
    params = {
        "RequestItems": {
            "UserAccountServiceChoicesMain": { // Table 1
                "Keys": [
                    {
                        "UserAccountServiceChoicesID": { "N":"1" } // Column and value to match
                    },
                    {
                        "UserAccountServiceChoicesID": { "N":"2" } // Column and value to match
                    }
                ],
                "ProjectionExpression": "UserAccountServiceChoicesID,ChoicePrice" // Column values to return
            },
            "UserAccountServiceChoicesExtended1": { // Table 2
                "Keys": [
                    {
                        "UserAccountServiceChoicesID": { "N":"1" } // Column and value to match
                    }
                ],
                "ProjectionExpression": "UserAccountServiceChoicesID,IsDeleted" // Column values to return
            }
        }
    };
    /* Perform Dynamo Call */
    batClient.batchGetItem(params, function(err, data) {
        if (err) {
            /* DynamoDB error */
            context.done(
                null,
                {
                    "Hubub": "Error JSON: " + JSON.stringify(err, null, 2)
                }
            );
        } else {
            /* Get results of querying Table 1 */
            data.Responses.UserAccountServiceChoicesMain.map(function(row) {
                /* Each match is a row of results; in this case there will be 2 rows */
                var tmp_UserAccountServiceChoicesID = row.UserAccountServiceChoicesID.N;
                var tmp_ChoicePrice = row.ChoicePrice.N;
                /* Put into string */
                result1 = result1 + "UserAccountServiceChoicesID = " + tmp_UserAccountServiceChoicesID + ", ChoicePrice = " + tmp_ChoicePrice + "...";
            });
            /* Get results of querying Table 2 */
            data.Responses.UserAccountServiceChoicesExtended1.map(function(row) {
                /* Each match is a row of results; in this case there will be 1 row */
                var tmp_UserAccountServiceChoicesID = row.UserAccountServiceChoicesID.N;
                var tmp_IsDeleted = row.IsDeleted.N;
                /* Put into string */
                result2 = result2 + "UserAccountServiceChoicesID = " + tmp_UserAccountServiceChoicesID + ", IsDeleted = " + tmp_IsDeleted + "...";
            });
            /* Generate Response */
            context.done(
                null,
                {
                    "result1": result1,
                    "result2": result2
                }
            );
        }
    });
};
