How to use DynamoDB with NodeJS? (The easy way😉)
Introduction
What is DynamoDB?
Amazon DynamoDB is a key-value and document database that delivers single-digit millisecond performance at any scale. It’s a fully managed, multi-region, multi-active, durable database with built-in security, backup and restores, and in-memory caching for internet-scale applications. DynamoDB can handle more than 10 trillion requests per day and can support peaks of more than 20 million requests per second. — AWS Official Documentation.
What is NodeJS?
NodeJS is an open-source server environment that can run on many platforms like Windows and macOS. It uses Javascript as the programming language and is quite performant because of its asynchronous architecture.
What are we going to do?
In this article, we will be connecting DynamoDB with NodeJS using the Dynamoose library and see different types of operations we can perform using the library.
Initial Setup
The first step is to create an AWS account and then create a new user in the AWS account with appropriate rights. (This is a very important step). Giving appropriate rights to users is very important because you don’t want someone with excessive rights to misuse the database. More information on how to add roles to IAM users is here.
Let’s GO!
First, go to the AWS console and search for DynamoDB. Inside DynamoDB, go to the tables section, and create a new table with your Database Configurations and the Primary Key. (It is important to note which region you are in).
Once the table is created, create a new user in the IAM section and give this user programmatic access. In the next step, go ahead and give this user only access to DynamoDBFullAccess and nothing else. This will not allow anyone with the access keys to use any other resources of AWS apart from DynamoDB.
(You can further refine this, but for the initial setup, this is good enough. Also, skip the Tags Section if it’s not necessary) Once the user is created, you will have Access Key ID and Secret Access Key. Note this down to use later on.
Now your AWS Setup is done. Next, we will move on to the code to understand how Dynamoose works. First, create a new NodeJS application and install the Dynamoose dependency. Here is the link to the package.
Before moving on, let’s understand some basics. Every Database works on some kind of Schema but as DynamoDB is a NoSQL Database, it is schema-less. But sometimes, we want a middle-ground, wherein, we have a Schema but it is not definitive, which means that the fields inside the Schema can change but not very often. This is the exact concept that we are going to use in this tutorial. In this tutorial, I will explain how to control DynamoDB using a simple Schema but you can apply it to any Schema you want.
Initially, you will need to add the details about your accessKeyID, secretAccessKey and region for the setup of Dynamoose. It can be done by:
dynamoose.aws.sdk.config.update({accessKeyId: process.env.AWS_ACCESS_KEY_ID,secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,region: process.env.AWS_DEFAULT_REGION,});
(Here, I have added these details in my environment variables but you can add them directly. However, I do recommend using Environment Variables for security purposes.)
First, we will create a model of our User (which is one of the tables in our database). There are two attributes of our User — name and mobile number. We will create this as follows:
const schema = new dynamoose.Schema({name: String,mobileNumber: Number,});const User = dynamoose.model("Users", schema);
Here, we have created a new User Model and that’s the setup done. Now, you can manipulate this User in your code using the amazing functions provided by Dynamoose. The first parameter in the dynamoose.model() is the name of the table to which this schema is associated.
Now you can create a new user in the following manner:
const user = new User({name: "Rushank Shah",mobileNumber: 000000,});user.save((err) => {console.log(err);});
Similarly, you can retrieve data from the Database in the following manner:
User.get(000000).then((user) => {console.log(user);});
This method and library is very simple and abstract compared to what we get in aws-sdk. The Dynamoose library has a lot more functions and the query gets much easier with the library compared to what you get from SDK provided by AWS. Therefore, I highly recommend everyone to use this library, unless it’s very necessary to use the AWS SDK.
Conclusion
In this short blog, we get a lot to know about how to work with NodeJS and DynamoDB. Here is the sample repository which I used in this demo. You can do much more with the library like Querying deep in the database.
Thank you for reading!
Happy Coding :)