A large part of modern app development needs a mix of robust programming languages and powerful databases.

One of the solutions that Amazon Web Services (AWS) offers is DynamoDB, a tool that can revolutionize your data management. Using it, you can quickly provision a database to handle large amounts of diverse data.

Screenshot showing console output for create table operation in DynamoDB

What Is DynamoDB?

AWS offers services for different database needs, likeAmazon RDS for relational databases, and DocumentDB fordocument databases such as MongoDB. DynamoDB is a NoSQL database for storing data in a key-value format.

DynamoDB can handle large amounts of data across distributed infrastructure without compromising performance, durability, or reliability. It offers a flexible model, letting you easily store and query data, whether it’s structured or unstructured.

Screenshot of AWS DynamoDB console showing table data

You can use DynamoDB as the database for various types of application. You can access it directly from the AWS web console and programmatically via the AWS-CLI, or from web applications using the AWS-SDK.

Getting Started With DynamoDB in Node.js

There are manytools for building backend APIs in Node.jsand you’re free to choose the database for your API when working with any of these tools. Node.js provides wide support for external services including databases like AWS DynamoDB.

All you need to access an AWS service from your Node app is the clientaws-sdkpackage for that service. For instance, to access DynamoDB, you need to install theclient-dynamodbpackage underaws-sdk.

Run this command in your project directory to install the package:

After installingaws-sdk/client-dynamodbin your Node.js project, you need to add the region of your DynamoDB table to the configuration before you interact with it. You will do this when initializing the DynamoDB client.

If you have installed and used AWS-CLI on your computer before, you probably have AWS credentials set in your environment already, and the SDK will automatically get your values from the environment.

But if you haven’t, you can head to theAWS Identity Access Management (IAM)service in your console and create a new user. After creating the user, you can get an access key ID and secret key, which are your personal credentials.

Add these credentials to your environment by running the following terminal commands for your platform:

On Unix, Linux, or macOS:

On Windows (CMD):

On Windows (PowerShell):

Then, back in your Node.js project, create a new file and name itdynamodb.js. In this file, instantiate a new AWS DynamoDB client using the following code:

Pretty simple! AWS makes sure you are not exposing any of your security credentials in your code, so while the code above tries to create the client, it first reads the access key and secret key from your environment.

The newly-createdclientenables you to carry out various operations, like creating tables and reading and writing data.

DynamoDB is schema-less just like other NoSQL databases, so you can always add new attributes (fields) to a table at any point. This is why you only need to add attributes that will serve as primary keys to a DynamoDB table when creating it.

Check out the following code which creates a new table (Customer) in DynamoDB:

TheAttributeDefinitionsfield is where you define the table’s key attributes and their types. TheEmailattribute here has typeSwhich means the field expects aStringas its value. The three available attribute types areS,N, andB(String, Number, and Binary).

You need theKeySchemato define primary keys which help to find and organize items quickly. DynamoDB expects the attributes you add when creating the table to be key attributes, so Email is the primary key here. You must add it to the KeySchema and specify itsKeyType(HASH).

The other available KeyType value isRANGEwhich is used for sort keys. Sort keys are useful in cases where you might have data with the same HASH keys in a table, and you want to group them according to some extra data such as date or color, you’re able to make the extra data a RANGE key.

The third important parameter in the above code is theProvisionedThroughput. This is where you define the number of reads and writes you want DynamoDb to allow on the table per second.

When you run the code above, you should get output that looks like this:

If you check your DynamoDB tables dashboard in the web console, you will see the table either still being provisioned or with a status ofactivealready.

Always consider your application needs when specifying theReadCapacityUnitsandWriteCapacityUnitsbecause an inappropriate value can lead to performance problems or high billing costs on your account.

Once you’re sure the table is already active, you can perform CRUD operations on it.

The following are some code examples that show you how to write and read data from theCustomertable.

Building Efficient Applications With DynamoDB

Amazon Web Services continues to thrive. It provides an accessible platform you can use to deliver efficient, secure digital solutions. DynamoDB is the perfect choice if you’re looking for a database to get running without worrying about infrastructure or security.

You are now equipped with all you need to get started with DynamoDB in Node.js, and you can confidently choose DynamoDB for your next Node.js application.