How to insert or delete items from a DynamoDB table using Node.js
To get started with DynamoDb, follow these steps:
Introduction
If you are here, you probably have little experience working with DynamoDB. Be aware that DynamoDB is designed for highly scalable storage requirements, and not for traditional SQL queries or tables with low volume of data.
Maybe this is obvious for you, but here is an example of an issue I faced when I started designing application with DynamoDB:
Every column you use in a query must be a partition key or an index. Indexes require additional read capacity units and write capacity units. For example, adding a small index with 5 read capacity units and 5 write capacity units is estimated to cost you USD$2.91 a month.
If you want to write a query equivalent to
SELECT * FROM MyTable WHERE Tag = 'Foo' AND Month = 1 AND User = 'Bill'
using DynamoDB, whereTag
,Month
andUser
are not the primary key, then you will need to set three additional indexes.
You get the idea. If you have not changed you mind, continue reading to get started.
Create a DynamoDB table
First, create a DynamoDB table at https://us-west-2.console.aws.amazon.com/dynamodb . Put attention in the region where you create your database, it is in the URL. In this example, the region is us-west-2.
Create an IAM user
Then, create an IAM user at https://console.aws.amazon.com/iam/home . For simplicity, select the AmazonDynamoDBFullAccess policy. Later you can restrict the permissions of the user. Once the user is created, take note of the Access key ID and the Secret access key.
Install NPM aws-sdk package
Open a terminal/console, and install the AWS Javascript SDK:
npm install aws-sdk --save
Copy-paste Node.js examples
There are two options for Javascript developers: DynamoDB and DocumentClient. The first option has more advance features, but the second one is easier to use because it automatically does some work for you.
Using DynamoDB API
Here is an example using AWS.DynamoDB. Notice how you have to add extra information about data types, and convert all the properties into string values.
Don’t forget to replace the region, the access key ID and the access key ID when copy-pasting the example into a Javascript file.
Using DocumentClient API
Here is an example using AWS.DynamoDB.DocumentClient. Notice there is no need to marshal the primary key or properties: