Dennis O'Keeffe
3 min readSep 21, 2021
Heading image

Today’s post will demonstrate usage of DynamoDB Toolbox for our single-table design.

This post will continue on from the work done yesterday in part one.

Be sure to check that out to understand where we are up to.

The final code for today’s post will be in my GitHub repository.

Prerequisites

  1. Read part one.
  2. Have the code from part one ready.

Getting started

Let’s clone our code from part one in this demonstration to keep things segregated.

At this stage, we will have all the tools we need to get started.

Using DynamoDB Toolbox to put and get customers

Inside of src/customer.ts, add the following:

In this file, we are setting up the DynamoDB.DocumentClient to point towards LocalStack, then creating a table with the toolbox based on our tableName from the stack we built yesterday, as well as our partitionKey and soretKey

Within the new Entity invocation, we are defining what our table will hold.

If it important to know that id will map to the table's partition key pk and status and date_added will combine to create the sk. For example, a status of active and a date_added of 2020-04-24 will create the sk active#2020-04-24.

While this post won’t go into the “do and don’t do” of single-table design, we will use that to illustrate how to interact with the LocalStack table.

We have two helper functions: addCustomer and scanTable. We will use these to demonstrate adding some customers (with fake date through faker) and scanning the table.

Writing the scripts

Inside of scripts/putCustomers.ts, add the following:

Inside of scripts/getCustomers.ts, add the following:

In order to be able to run these scripts, we will need to add a new script to package.json:

This will allow us to run our TypeScript files.

Running our scripts with LocalStack

At this stage, ensure that LocalStack is running and that the table has been deploy through the AWS CDK:

Once everything is up and ready, we can add our customers:

Note: If you have issues, check that you have your AWS credentials and region in your environment variables.

To confirm that we can interact with our table as expected, we can now run our scan and confirm that the table does in fact have our generated users!

When we scan the table, you will notice that the users are all mapped back to their expected values as defined in our Table.

Teardown

As per usual, once you are done we can tear down the stack:

Summary

Today’s post demonstrated how to interact with the DynamoDB table that we created on LocalStack through the DynamoDB toolbox.

Albeit simple, we generated 100 users in our table and scanned for the first ten.

Resources and further reading

  1. AWS CDK for TypeScript
  2. DynamoDB Toolbox
  3. DynamoDB with LocalStack and the AWS CDK Part One
  4. Final code

Photo credit: waldemarbrandt67w

Originally posted on my blog. To see new posts without delay, read the posts there and subscribe to my newsletter.

I write content for AWS, Kubernetes, Python, JavaScript and more. To view all the latest content, be sure to visit my blog and subscribe to my newsletter. Follow me on Twitter.

Dennis O'Keeffe
Dennis O'Keeffe

No responses yet