Jonathan Pirca
Jonathan Pirca

Reputation: 31

How update (using Nodejs) a second table (DynamoDB) into a lambda function using some values from 2nd table?

I'm writing a lambda function that update a second table when there is an insert on the first table. Both tables are in DynamoDB. When an insert occurs in the first table a lambda function is triggered to update another table based on some values from the new record.

The lambda function is on Nodejs

I do not know exactly how to write the update statement since I have to use the value store in the second table and sum with the new value and update. What I want to do is something like this:

update table1 set campo1 = campo1 + newValue

I am not sure how to do it in NodeJs + DynamoDB. This is the function:

const AWS = require('aws-sdk');
const documentClient = new AWS.DynamoDB.DocumentClient(); 

exports.getCalculos = function(event, context, callback){
    let columnName = "";
    let numPersonas = 0;
    let domainIn = "";

    event.Records.forEach((record) => {
        // I just one the new records inserted
        if (record.eventName == 'INSERT') {
            // I read all the values and storage into newRecord
            const newRecord = record.dynamodb.NewImage;

            // I get the key from the new record to be
            // used in the second table
            domainIn = newRecord.Domain;

            // I check some conditions from the new record
            // to see which column I will update in the second table
            // columns to update are alwayd different
            if((newRecord.puerta1.S == "pass") && (newRecord.puerta2.S == "pass")){
                columnName = "escape";
            }

            if((newRecord.puerta1.S == "close") && (newRecord.puerta2.S == "close")){
                columnName = "atrapado";
            }

            // I get the number from the new record
            numPersonas = newRecord.count;


            // Then base on an ID from the new record,
            // I want to update another field into another table
            // It means, sum both values
            const params = {
                TableName :"SummaryByDomain",
                Key:{
                    "Domain": domainIn
                },
                UpdateExpression: "set #field2 = :numPersonas",
                ExpressionAttributeNames:{
                    "#field2":columnName
                },
                ExpressionAttributeValues: {
                    ":numPersonas": numPersonas
                },
                ReturnValues:"UPDATED_NEW"
            };

            documentClient.update(params, function(err, data) {
                if (err) {
                    console.error("Unable to update item. Error JSON:", JSON.stringify(err, null, 2));
                } else {
                    console.log("UpdateItem succeeded:", JSON.stringify(data, null, 2));
                }
            });
        }
    });
};

Upvotes: 0

Views: 624

Answers (1)

Jonathan Pirca
Jonathan Pirca

Reputation: 31

Thanks @LostJon for your answer. callback wasn't the issue. I just need how to update the filed using the same value.

I found it and solved it.

I used: UpdateExpression: "ADD #field2 :numPersonas",

In this case the ADD works as a math operator if values are numeric.

now it's working good

Upvotes: 1

Related Questions