Using Dynamite Libraries to Simplify CRUD Operations in DynamoDB

Dynamite brings the explosive power of DynamoDB in an easy to use library so you can focus on building your Alexa Skill

Mandy Chan
A Cloud Guru
6 min readSep 5, 2016

--

Join me at the NYC Amazon Alexa Meet-Up

After trying out the AWS SDK for my last Alexa skill, I decided to use a third-party library called Dynamite. Dynamite is a promise-based client and it allows you to query for specific attributes easily with very little code. Lets examine how Dynamite can simplify CRUD operations.

Prerequisites

To get started, first you need to get the security keys from Amazon. For more information on how to get these keys, see “Managing AWS Access Keys” by Amazon.

To use Dynamite, you’ll need to supply the security keys by creating an object that has the region, accessKeyId and secretAccessKey (shown below).

After you created this options object for Dynamite, you will need to create a new instance of Dynamite.Client. Once you finish setting up Dynamite.Client, you can begin using it.

TIP: If you did not configure the options object properly, you’ll encounter the following error message: “The security token included in the request is invalid”.

Background of New Alexa Skill Using Dynamite

I used the AWS SDK for my last Alexa skill. In this post, we will use a new skill to demonstrate the power of Dynamite. Going forward, we will be using the table below.

This new Alexa skill called Message Wall allows users to leave an anonymous message for other users to hear publicly. It’s important to note that there will only be one message available for all users to hear.

In this table, the threadId is the partition key which is always equal to zero. We do this because we are only getting the latest message from the “public thread”. In order to get the latest message, we must provide a sort key (createdOn) for the thread. This allows us to easily find the most recent message, since all messages are pre-sorted by createdOn.

To learn more about the two different types of primary keys in DynamoDB. See Introduction to DynamoDB Concepts by Amazon

Basic CRUD operations using Dynamite

Before I demonstrate the CRUD operations, it is important to note that Dynamite uses a promise library called Kew for all of its methods. Each method returns a promise object instead of using callbacks as in the AWS SDK. If you haven’t read my first post, I recommend it so you can see how DynamoDB in the AWS SDK uses callbacks.

Now let’s look at Dynamite CRUD operations starting with creating records.

C for Create

In Dynamite, in order to create a new record, we use the putItem method on the Dynamite client as shown on line 38. The putItem method takes two parameters. First being the table name which we want to create the item in and second being the item you want to create.

In this example, the object includes the partition key “threadId”, the sort key “createdOn” as well as the message we’re creating. Notice, that on line 39, we’re returning the Kew promise vs using callbacks.

R for Read

Dynamite has two ways for accessing data; getItem and a query builder. They are very similar, except that the query builder provides more functionality in how we access data. In this scenario, we need to retrieve the latest message and limit our results to one record.

When calling the newQueryBuilder method, we need to provide the name of the table. In this example, on line 43, we provide “Wall” as the name. Next we call setHashKey ( ) to specify the partition key. In order to get the most recent record, we must do a scanBackward( ) which sorts the numeric in descending order and setLimit ( ) which sets the maximum number of records returned to one.

See Dynamite library to learn about the query builder and other methods.

U for Update

The signature of Dynamite’s update method is very similar to the query builder. Notice there are a few additional methods being called. For example, on line 53, we’re calling setRangeKey( )so we can narrow down the exact record that we wish to update. In this example, the range key is our sort key createdOn. You’ll also notice that we call enableUpsert( ) on line 54. This is very useful in scenarios where a record may exist. If it already exists, then it’ll be updated; otherwise it’s inserted.

Finally, on line 55, we tell Dynamite what attributes we want to update.

TIP: If you want to delete an attribute only, call deleteAttribute (<attribute name>) on the update builder.

D for Delete

Now let’s see how Dynamite can delete a record. At a glance, you’ll see the method deleteItem is almost like the update builder with the exception we only provide the table name, partition key and sort key. This is required to narrow down what records we want to delete.

Conclusion

Dynamite brings the explosive power of DynamoDB in an easy to use library. Instead of dealing with callbacks, constructing complex query expressions and fighting a library, you can now focus on building your Alexa Skill.

Mandy Chan is a passionate JavaScript developer who enjoys spending her weekends writing code at local coffee shops or attending hackathons to transform her ideas into code. At the moment, she is building a set of rich APIs that sit on top of the Citi Bike and New York City MTA systems. Her goal is to help digitalize New York City.

Mandy has developed more than a dozen Alexa skills and won three hackathons for Alexa skills: Best Use of Alexa at TechCrunch New York, Grand Prize Winner at Manhattan Angel Hack, and HackItTogether.

Mandy continues to share her passion for Alexa and voice design by publishing open source projects such as SSML-Builder npm package and Alexa-Hackathon-Quick-Starter. She is also a co-organizer of the NYC Amazon Alexa Meetup. You can follow her on Twitter: @MandyChanNYC.

--

--

Mandy Chan
A Cloud Guru

| ☆ Creator of SSML Builder | Google Assistant Developer Advocate | I write about Actions on Google | Voiceholic on http://bit.ly/gcp-mandy