๐Ÿ“Œ NodeJs

Getting started with AWS DynamoDB NodeJS CRUD operations

#node#database#dynamodb
January 19, 2022

DynamoDB is a well-known NoSQL database from Amazon Web Services(AWS). It offers a wide variety of features for free in its free tier program.

One can get started with DynamoDB in just a few minutes. But before that, you need to signup for an AWS account. Signing up for an AWS account can be done in a few simple steps, directly from the aws.com site.

Note you need to provide a credit or debit card to complete the signup. You will not be charged any amount until you cross their free tier service, so nothing to worry about ๐Ÿ˜Š. 

In the AWS free tier, you will get tonnes of features to use, of which some are limited for the first year and some are free even rest of the years until a certain threshold.

DynamoDB comes under always free service with 

  • 25 GB of Storage
  • 25 provisioned Write Capacity Units (WCU)
  • 25 provisioned Read Capacity Units (RCU)

and is said to handle up to 200M requests per month. If you go beyond that you will be charged the amount as per the extra usage. 

1 WCU = 1 write of up to 1 KB/s.

2 WCUs = 1 transactional write request (one write per second) for items up to 1 KB.

1 RCU =  1  read of up to 4 KB/s 

2 RCUs = 1 transactional read request (one read per second) for items up to 4 KB.

To understand more about the read and write capacities check these aws docs.

Once the AWS account is ready, you can set up the credentials locally for using the AWS services. Linux and Windows users can create .aws directory in the user folder and add the credentials file without any extension with the following details.

[default]
aws_access_key_id = <YOUR_ACCESS_KEY_ID>
aws_secret_access_key = <YOUR_SECRET_ACCESS_KEY>

If you need more details for setting up the AWS credentials, follow up this doc.

Now let's get started with the DynamoDB NodeJS crud operations. 

DynamoDB NodeJS CRUD

Let's create a new directory dynamo-crud and initialize node project(you need to have node installed already). 

mkdir dynamo-crud
cd dynamo-crud
npm init -y

To get started with using AWS services we need aws-sdk package and we can install it using npm. You can learn the npm commands from here.

npm i aws-sdk

For having unique IDs for our records or items, we can use nanoid package.

npm i nanoid

Next create the index.js file in the root project directory.

After that configure AWS with your desired region and create DynamoDB instance. Keep in mind that we have already configured our credentials through a local file inside .aws directory.

// Inside index.js
import AWS from 'aws-sdk';
import { nanoid } from 'nanoid';

AWS.config.update({ region: 'ap-south-1' });  // you can change region as per your preference

const DynamoDB = new AWS.DynamoDB(); // instance of DynamoDB

Creating table in DynamoDB NodeJS

Now let's create a table named Posts. While creating the table we need to mention the unique key for querying the data.

In DynamoDB there are two key parameters, one is partition key and the other is sort key.

The primary key can be created using only the partition key or the combination of partition and sort key, known as composite primary key.

Having a sort key helps in sorting the data based on that sort key.

In DynamoDB, tables, items, and attributes are the core components to work with. A table is a collection of items, and each item is a collection of attributes. DynamoDB uses primary keys to uniquely identify each item in a table and secondary indexes to provide more querying flexibility.

We'll make a function for each operation so that it will be easier to use or reuse it and keep all related lines of code together. 

// Creating the table with parition key as the primary key
const createTable = () => {
  const params = {
    TableName: 'Posts',
    KeySchema: [{ AttributeName: 'postId', KeyType: 'HASH' }], // using partition key only
    AttributeDefinitions: [{ AttributeName: 'postId', AttributeType: 'S' }], // S for string
    ProvisionedThroughput: {
      ReadCapacityUnits: 5,
      WriteCapacityUnits: 5,
    },
  };

  DynamoDB.createTable(params, function (err, data) {
    if (err) {
      console.error('Unable to create table', err);
    } else {
      console.log('Created table', data);
    }
  });
};

In the above code, we have created a table with name Posts and primary key postId. We have provisioned the capacity units 5 each RCU and WCU. 

In DynamoDB instance by using createTable method we can pass the params and a callback with err and data arguments to create the table.

Calling the creatTable() function will run all the code inside it and will create the Posts table in the AWS DynamoDB space.

Creating items/records in DynamoDB

We'll create a function named insertPost that takes postId, title and content as arguments for creating the item with three key-value pairs.

We need documentClient instance for doing CRUD operations in the Dynamo database. With the following code snippet, we can create the instance of the documentClient.

const documentClient = new AWS.DynamoDB.DocumentClient();

Now initialize the function insertPost for inserting the items. We are going to use put method of documentClient for inserting the items.

// Function to insert the post into Posts table/document
const insertPost = async (postId, title, content) => {
  const params = {
    TableName: 'Posts',
    Item: {
      postId,
      title,
      content,
    },
  };
  try {
    const data = await documentClient.put(params).promise();
    console.log('Post inserted', data);
  } catch (err) {
    console.log(err);
  }
};

So we can now insert posts by calling the insertPost function with the data. 

// Inserting two posts
insertPost(
  nanoid(),
  'How to do CRUD operations in Dynamo?',
  'It is not that hard to do CRUD operations in DynamoDB.'
);
insertPost(
  nanoid(),
  'Sample title that can be deleted',
  'Sample description for the sample post'
);

Reading items/records in DynamoDB

For reading the items we're going to create two functions, getAllPosts and getSinglePost functions.

scan method of documentClient is used to get all the items in the table. get method is used to get a single item by using the primary key, nothing but the postId here.

// Function to get all the posts
const getAllPosts = async (table) => {
  const params = {
    TableName: table,
  };
  try {
    const data = await documentClient.scan(params).promise();
    console.log(data);
    return data;
  } catch (err) {
    console.log(err);
  }
};

getAllPosts('Posts');

// Output:
// {
//   Items: [
//     {
//       content: 'Sample description for the sample post',
//       postId: 'FPbCFAvNoKWinPadsNBoW',
//       title: 'Sample title that can be deleted'
//     },
//     {
//       content: 'Follow this to learn about DyanamoDB',
//       postId: 'j6uv_RKuZg5XZll1AkAWx',
//       title: 'Learn to use DyanamoDB'
//     }
//   ],
//   Count: 2,
//   ScannedCount: 2
// }

By calling getAllPosts('Posts') with the table name we get all the posts. 

// Function to get single post from the table/document
const getSinglePost = async (table, keyObj) => {
  const params = {
    TableName: table,
    Key: keyObj,
  };
  try {
    const data = await documentClient.get(params).promise();
    console.log(data);
    return data;
  } catch (err) {
    console.log(err);
  }
};

getSinglePost('Posts', { postId: 'FPbCFAvNoKWinPadsNBoW' }); // gets single post

By calling getSinglePost(...) with the table name and keyObj gets the single post. 

Updating items in DynamoDB 

For updating the items we're going to create updatePost function.

put method of documentClient is used for inserting and updating the item based on the primary key. If there's an item already existing then it will update else insert the item into the table.

// Function to update the post based on postId
const updatePost = async (postId, title, content) => {
  const params = {
    TableName: 'Posts',
    Item: {
      postId,
      title,
      content,
    }
  };
  try {
    const data = await documentClient.put(params).promise();
    console.log('Post updated', data);
  } catch (err) {
    console.log(err);
  }
};
updatePost(
  'FPbCFAvNoKWinPadsNBoW',
  'Update sample post title',
  'Updated content'
);

By calling updatePost(...) with postId, title and content will update that particular post. 

Deleting item in DynamoDB

For deleting the item we're going to create deletePost function.

delete method of documentClient is used for deleting the item based on the primary key. 

// Function to delete the post from posts table/document by postId
const deletePost = async (postId) => {
  const params = {
    TableName: 'Posts',
    Key: {
      postId,
    }
  };
  try {
    const data = await documentClient.delete(params).promise();
    console.log('Post deleted', data);
  } catch (err) {
    console.log(err);
  }
};
deletePost('FPbCFAvNoKWinPadsNBoW');

By calling deletePost(...) function with postId, deletes the item from the Posts table.

That's the basic CRUD(Create, Read, Update, Delete) in DynamoDB using NodeJS. There are other methods and ways for querying data based on different conditions in AWS dynamodb. You can find the detailed documentation in these docs.

You can find all the above-discussed codes in this dynamodb-crud git repository.

If you like receiving regular tips, tricks related to web development and technology then do follow on devapt-twitter @dev_apt
devapt-github
devapt-twitterdevapt-facebookdevapt-whatsappdevapt-redditdevapt-mail