#StackBounty: #javascript #node.js #amazon-web-services #nosql AWS Lambda function to update newly added DynamoDB records

Bounty: 100

Approach

All data is submitted into my DynamoDB from another Lambda > API Integration function whereas the lastUpdated row gets inserted as null and then the function below basically polls my database every 1 minute looking for new rows that have a null value & performs actions on them until the lastUpdated can then be updated (once an action is performed elsewhere)

I have the following Node.JS (runtime v8.10) executing on AWS Lambda:

const AWS = require("aws-sdk");
const game = require('game-api');
const uuid = require("uuid");

AWS.config.update({
  region: "us-east-1"
});

exports.handler = async (event, context) => {

    //set db
    var documentClient = new AWS.DynamoDB.DocumentClient();

    //find matches
    var params = {
        TableName: 'matches',
        FilterExpression:'updated_at = :updated_at',
        ExpressionAttributeValues: {
            ":updated_at": 0,
        }
    };
    var rows = await documentClient.scan(params).promise();

    //game object
    let gameAPI = new game(
        [
            "admin@game.com",
            "password"
        ]
    );

    await gameAPI.login();

    for (let match of rows.Items) {

        var player_params = {
            TableName: 'players',
            Key: { "match_id": match.id }
        };

        let player_row = await documentClient.get(player_params).promise();

        //grab stats and compare
        var stats = await gameAPI.getStatsBR(
            player_row.Item.player_1_name,
            player_row.Item.player_1_network
        );
        var new_data = compareModiified(
            match.match_type,
            player_row.Item.player_1_last_updated,
            stats
        );

        if(new_data === true) {

                //we have new data
                let kills;
                let matches;
                let kills_completed;
                let matches_completed;
                let modified;

                switch(match.match_type) {
                    case 'myself':
                        kills = stats.group.solo.kills;
                        kills_completed = (kills - player_row.Item.player_1_kills);
                        matches = stats.group.solo.matches;
                        matches_completed = (matches - player_row.Item.player_1_matches);
                        modified = stats.group.solo.lastModified;
                        break;
                    case 'solo':
                        kills = stats.group.duo.kills;
                        kills_completed = (kills - player_row.Item.player_1_kills);
                        matches = stats.group.duo.matches;
                        matches_completed = (matches - player_row.Item.player_1_matches);
                        modified = stats.group.duo.lastModified;
                        break;
                    case 'duo':
                        kills = stats.group.squad.kills;
                        kills_completed = (kills - player_row.Item.player_1_kills);
                        matches = stats.group.squad.matches;
                        matches_completed = (matches - player_row.Item.player_1_matches);
                        modified = stats.group.squad.lastModified;
                        break;
                }

                var update_params = {
                    TableName:"matches",
                    Key: { "id": match.id },
                    UpdateExpression: "SET #status = :status, updated_at = :modified",
                    ExpressionAttributeNames:{
                        "#status":"status"
                    },
                    ExpressionAttributeValues:{
                        ":status": 1,
                        ":modified": modified
                    }
                };
                await documentClient.update(update_params).promise();

                var report_params = {
                    Item: {
                        'match_id': match.id,
                        'kills': kills_completed,
                        'matches': matches_completed,
                        'completed_at': new Date().getTime()
                    },
                    TableName : 'reports'
                };
                await documentClient.put(report_params).promise();

            } else {

                //we don't have new data.
                console.log("We don't have new data, let's not do anything..");

            }

    }

    return {
        statusCode: 200
    };

};

function compareModiified(match_type, db_modifiied, stats) {
    var stats_modified;
    switch(match_type) {
        case 'myself':
            stats_modified = stats.group.solo.lastModified;
            break;
        case 'solo':
            stats_modified = stats.group.duo.lastModified;
            break;
        case 'duo':
            stats_modified = stats.group.squad.lastModified;
            break;
    }
    return (stats_modified > db_modifiied);
}
```


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.