Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Update DynamoDB Items With Node.js

DZone's Guide to

Update DynamoDB Items With Node.js

In this post, we take a look at how to update items in DynamoDB using Node.js including some examples. Read on to find out more!

· Database Zone ·
Free Resource

Running out of memory? Learn how Redis Enterprise enables large dataset analysis with the highest throughput and lowest latency while reducing costs over 75%! 

In a previous post, we inserted items into DynamoDB using Node.js. DynamoDB also supports updating items.

We will use the Login table for the update examples.

When issuing an update, you must specify the primary key of the item you want to update.

var updateName = function(email,fullName,callback) {

var docClient = new AWS.DynamoDB.DocumentClient();

var params = {
TableName:"Users",
Key: {
email : email
},
UpdateExpression: "set fullname = :fullname",
    ExpressionAttributeValues:{
        ":fullname":fullName
    },
    ReturnValues:"UPDATED_NEW"
};

docClient.update(params,callback);
}


We can proceed on more advanced statements using conditional updates. Conditional updates can help us in many cases, such as handling concurrent updates. In our case, we will update an item’s full name only if it starts with a certain prefix.

var updateConditionally = function(email,fullName,prefix,callback) {

var docClient = new AWS.DynamoDB.DocumentClient();

var params = {
TableName:"Users",
Key: {
email : email
},
UpdateExpression: "set fullname = :fullname",
ConditionExpression: "begins_with(fullname,:prefix)",
ExpressionAttributeValues:{
        ":fullname":fullName,
        ":prefix":prefix
    },
    ReturnValues:"UPDATED_NEW"
};

docClient.update(params,callback);
}


Another feature is atomic counters. We can issue updates to a DynamoDB item and increase the attribute values. We will add an extra field called count. Also, we will add another update function, which, once called, will update the field specified, but will also increase the counter attribute. Thus, the counter attribute will represent how many times an update was performed on a specific item.

var addUpdateCounter = function(email,callback) {

var docClient = new AWS.DynamoDB.DocumentClient();

var params = {
TableName:"Users",
Key: {
email : email
},
UpdateExpression: "set #counter = :counter",
ExpressionAttributeNames:{
        "#counter":"counter"
    },
ExpressionAttributeValues:{
        ":counter":0
    },
ReturnValues:"UPDATED_NEW"
};

docClient.update(params,callback);
}

var updateAndIncreaseCounter = function(email,fullName,callback) {

var docClient = new AWS.DynamoDB.DocumentClient();

var params = {
TableName:"Users",
Key: {
email : email
},
UpdateExpression: "set fullname = :fullname ADD #counter :incva",
ExpressionAttributeNames:{
        "#counter":"counter"
    },
ExpressionAttributeValues:{
        ":fullname":fullName,
        ":incva":1
    },
    ReturnValues:"UPDATED_NEW"
};

docClient.update(params,callback);
}


And that's it! You can now update items in DynamoDB using Node.js As usual, you can find the source code on GitHub.

Related Refcard:

Running out of memory? Never run out of memory with Redis Enterprise databaseStart your free trial today.

Topics:
examples ,update ,node.js ,dynamodb ,database

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}