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

New whitepaper: Database DevOps – 6 Tips for Achieving Continuous Delivery. Discover 6 tips for continuous delivery with Database DevOps in this new whitepaper from Redgate. In 9 pages, it covers version control for databases and configurations, branching and testing, automation, using NuGet packages, and advice for how to start a pioneering Database DevOps project. Also includes further research on the industry-wide state of Database DevOps, how application and database development compare, plus practical steps for bringing DevOps to your database. Read it now free.

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:

New whitepaper: Database DevOps – 6 Tips for Achieving Continuous Delivery. Discover 6 tips for continuous delivery with Database DevOps in this new whitepaper from Redgate. In 9 pages, it covers version control for databases and configurations, branching and testing, automation, using NuGet packages, and advice for how to start a pioneering Database DevOps project. Also includes further research on the industry-wide state of Database DevOps, how application and database development compare, plus practical steps for bringing DevOps to your database. Read it now free.

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 }}