DEV Community

Cover image for Working With DynamoDB, AWS NoSQL Workbench, and Node.JS
Josue Bustos
Josue Bustos

Posted on

Working With DynamoDB, AWS NoSQL Workbench, and Node.JS

Introduction

In this tutorial, I will show you how to set up a locally-hosted DynamoDB server, use the AWS NoSQL Workbench to insert data into DynamoDB, and finally, create a simple Node.JS project that will query sample data provided by AWS NoSQL Workbench.

Prerequisites

This tutorial assumes you're comfortable executing terminal commands, navigating, and modifying the macOS and Ubuntu Linux file system.

My setup is a hybrid environment consisting of a Host, macOS Catalina, and a Guest Ubuntu VM instance. But You can set up everything on one host, macOS, Ubuntu, or Windows.

To follow along, you will need the following already installed:

On Ubuntu (Guest) or Host:

On MacOS (Host):

Okay, let's get started! :)

Install AWS NoSQL Workbench

Let's get the simple stuff out of the way. Navigate to the AWS DynamoDB Developer page and download the AWS NoSQL Workbench for your Operating System.

download

After downloading the installation file, follow the onscreen instructions to install NoSQL Workbench.

Install DynamoDB

Docker and Docker Compose will create and manage the DynamoDB server container and install all the required software packages and dependencies.

Open your preferred terminal app and type the following command to create a folder, change directories and create a YAML file in one swoop, like so:


 bash
$ mkdir ~/dynamodblocal && cd ~/dynamodblocal && touch docker-compose.yml


Enter fullscreen mode Exit fullscreen mode

Next, copy-paste the following YAML code into the docker-compose.yml file.




# docker-compose.yml

version: '3.8'
services:
  dynamodb-local:
    command: "-jar DynamoDBLocal.jar -sharedDb -optimizeDbBeforeStartup -dbPath ./data"
    image: "amazon/dynamodb-local:latest"
    container_name: dynamodb-local
    ports:
      - "8000:8000"
    volumes:
      - "./docker/dynamodb:/home/dynamodblocal/data"
    working_dir: /home/dynamodblocal


Enter fullscreen mode Exit fullscreen mode

Finally, launch the DynamoBD Server by typing the following command in the terminal:


 bash
$ docker-compose up


Enter fullscreen mode Exit fullscreen mode

The first time you execute this command, it will take a few minutes to get the server up and running. The next time after that, it should be instant.

Now, if you see an output similar to the one below. That means you have successfully installed and launched a DynamoDB Server!



dynamodb-local    | Initializing DynamoDB Local with the following configuration:
dynamodb-local    | Port:       8000
dynamodb-local    | InMemory:   false
dynamodb-local    | DbPath:     ./data
dynamodb-local    | SharedDb:   true
dynamodb-local    | shouldDelayTransientStatuses:       false
dynamodb-local    | CorsParams: *
dynamodb-local    | 


Enter fullscreen mode Exit fullscreen mode

To quickly shut down or stop the server, press:



Ctrl + C 


Enter fullscreen mode Exit fullscreen mode

Insert Data Into Your DynamoDB Server

Before we can perform requests to the database from inside Node.js, we need to insert data. To do that, we're going to leverage AWS NoSQL Workbench GUI and connect to our recently launched DynamobDB server.

Choose a Model

  • First, open/launch the AWS NoSQL Workbench.
  • On the left sidebar, locate and click on Visualizer.
  • Click on the dropdown menu and select AWS Discussion Forum Data Model.

aws workbench

This model includes three tables for you to explore: Forum, Reply, and Thread.

  • Next, click on the Commit to Amazon DynamoDB button

connect

  • When the popup opens, click on the tab furthest to the right that says Add a new DynamoDB local connection.

  • Then, click on the Commit to connect to your local database.

  • You can choose whatever name you want for Connection name field.

local connection

This step does three things. First, it creates a connection, inserts all three tables into your database, and opens a new window or view, called Operation builder, that displays all your databases.

  • Finally, Click the Open button and VoilΓ !

Open

Now we're ready to make requests to the database in Node.js!

Query Data With Node.js

To query data from DynamoDB in your Node.js project, you use the AWS JavaScript SDK for Node.js. So, Let's get to it...

Open your preferred terminal app, create a directory, change directories, and initialize it to install npm packages. We can do this in one swoop like so:



$ cd ~/ && mkdir my-project && cd my-project && npm init -y


Enter fullscreen mode Exit fullscreen mode

While we're here, install DynamoDB specific packages by typing the following command:



$ npm i @aws-sdk/client-dynamodb


Enter fullscreen mode Exit fullscreen mode

Next, create a file called index.js :



$ touch index.js


Enter fullscreen mode Exit fullscreen mode

Copy-paste the following code into the index.js file:



// index.js
const { QueryCommand } = require('@aws-sdk/client-dynamodb');
const REGION = "eu-west-2"; //e.g. "us-east-1"

// Create an Amazon DynamoDB service client object.
const ddbClient = new DynamoDBClient({
    region: REGION,
    endpoint: 'http://localhost:8000'
});
// Query Parameter to retrieve data from DynamoDB
const params = {
    TableName: "Forum",
    ScanIndexForward: true,
    ConsistentRead: false,
    KeyConditionExpression: "#0b290 = :0b290",
    ExpressionAttributeValues: {
        // Attribute Name
        ":0b290": {
            "S": "AWS Data Pipeline"
        }
    },
    "ExpressionAttributeNames": {
        // Partition key
        "#0b290": "ForumName" 
    }
};

// a "run" function 
const run = async () => {
    try {
        const data = await ddbClient.send(new QueryCommand(params));
        data.Items.forEach(function (element, index, array) {
            // Display data in the console
            console.log(element);            
            return data;
        });
    } catch (err) {
        console.error(err);
    }
};

run();



Enter fullscreen mode Exit fullscreen mode

Now, let's see if this works. First, open a terminal and type the following command to execute the run() function, like so:



$ node index.js


Enter fullscreen mode Exit fullscreen mode

Your output should look like this:



{
  Threads: { N: '19' },
  Category: { S: 'Amazon Web Services' },
  Messages: { N: '9' },
  Views: { N: '500' },
  ForumName: { S: 'AWS Data Pipeline' }
}


Enter fullscreen mode Exit fullscreen mode

Phew! If you've gotten this far, you're a champ! AND you're pretty serious about AWS Cloud Engineering and Development. Hats off to you!

There was a lot to unpack in such a short time. However, you can deep dive into some of these practical solutions by jumping on over to the AWS Developer documentation.

Thank you for sticking around and holding on to the end.

Until next time!

Top comments (0)