Share on facebook
Share on twitter
Share on linkedin

Data Persistence and CRUD Operations using DynamoDB in the new Alexa SDK

A Cloud Guru News
A Cloud Guru News

My experience with Data Persistence and CRUD Operations using DynamoDB in the new Alexa SDK:

The new Alexa SDK for NodeJS helps developers build skills faster, and with less complications. In the past, I’ve built skills using the old SDK, and recently started to transition over to this new version.

In this post, I’ll share my experience on data persistence using DynamoDB in the new Alexa SDK, as well as how to do DynamoDB CRUD operations using the AWS SDK for long-term data persistence.

I will be going over the two areas listed below:

  • Built-in support for session persistence
  • DynamoDB CRUD operations using the AWS SDK

AWS SDK Prerequisites

Since this post is focused on my experience with AWS SDK DynamoDB, I won’t provide much details about the basic concepts of DynamoDB, Permission and Security access.

Here are some reference resources:

  1. How to Add a DynamoDB Role to your Lambda Function by Noelle LaCharite, Amazon Alexa Solution Architect
  2. Getting Started with Amazon DynamoDB by Amazon
  3. AWS Command Line Interface for DynamoDB by Amazon

For basic concepts of DynamoDB, see:

  1. DynamoDB Getting Started Guide by Amazon
  2. How it works. Core Components by Amazon

Session persistence using the new Alexa SDK

A very powerful feature of the new Alexa SDK, is the ability to save session data to DynamoDB with one line of code. But in order to activate this feature, you have to tell the new SDK the name of the session table within DynamoDB.

This is done by setting the property of the Alexa object called dynamoDBTableName as shown on line 23. You must remember to do this before calling execute on line 24.

At this point, whenever you set a session attribute, it will be saved before the response is sent to the user.

The session table has two columns as shown below.

Note: PK means “Partition Key

The advantage of persisting session data into DynamoDB, is that the data can now be accessed between usages of the skill; even after weeks or months.

DynamoDB Tips:

  • Must set the attributes before emitting the tell or ask event. If not, the data will not be saved.
  • Whenever you have a property that is a Date, it will be converted to a string when saving into the DynamoDB. Also, note when retrieving dates from the database, it will be a string not a Date object.
  • Since the session table is only used for session management, anything that is not stored within the session should have it’s own table. The session table should only have two columns; the user_id (partition key) and mapAttr column as shown below.

DynamoDB CRUD operations using the AWS SDK

Since my new skill allows the user to record and retrieve how they felt on a given day, the skill needs to save each entry forever. In order to do that, I created a second table for this purpose. Going forward, I will be referring to this table when talking about the CRUD operations.

Note: PK means “Partition Key” and SK means “Sort Key

Below is the actual table in the user interface.

Before we can go over the step-by-step guide for using AWS SDK, first we need to create an instance of AWS.DynamoDB class and specify the apiVersion as ‘2012–08–10’.

TIP: If you’re interested in playing around with DynamoDB locally, take a look at Getting Started. JS Shell by Amazon

Keep in mind, the syntax they use to query DynamoDB in the shell is different than the one used in the AWS SDK library.

Basic CRUD operation using AWS SDK

And what does it stand for?

C for Create

To learn more about PutItem, see API_PutItem by Amazon

In order to create a record using the AWS SDK, we call the method putItem .This method takes two parameters. The first being an object which can be seen above between lines 14 and 27. On line 15, there is a property called TableName which indicates the name of the table that we are creating the item in. From line 16 to 26, it describes the item we will create.

Notice, each property of the item must include the type. For example, the required partition key userId is a “string” as indicated by the letter “S”. For more details on the different attribute values, see Attribute Value by Amazon

The second parameter being the callback which will be invoked after the put operation is completed.

R for Read

To learn more about GetItem, see API_GetItem by Amazon

The getItem is very much like the putItem. One slight difference is we have to add an additional property called “ProjectionExpression” in the params object. This property allows you to specify what attributes you want to read from the table as shown on line 89. Finally, we provide the callback which will be passed the results of getItem.

U for Update

To learn more about UpdateItem, see API_UpdateItem by Amazon.

UpdateItem is also very similar to putItem, except we need to specify what properties in the item that we want to update .We do this by adding “UpdateExpression” property in the params object as shown on line 105. UpdateExpression defines the attributes to be updated, as well as the action to be performed.

There are four actions that “UpdatedExpression” accepts: SET, REMOVE, ADD and DELETE. Above is an example of SET action being demonstrated on line 105. Notice “ExpressionAttributeValues” is where the assignation of the updated value and the type S are required, see line 110.

Finally, remember to add the “ReturnValues” property as shown on line 112 and set it to “ALL_NEW” in order for all of the attributes of the new version of the item to be returned.

D for Delete

To learn more about deleteItem, see “DeleteItem” by Amazon

To delete records, I used deleteItem in the AWS SDK. Instead of using “UpdatedExpression”, I used “ConditionExpression” which specifies the required condition in order to successfully delete an attribute.

TIP: For deleteItem, we should set ReturnValues to “ALL_OLD” instead of “ALL_NEW” in order to return the content of the old item, see line 129.

In my next post Part II, I go over a third-party library called Dynamite that really simplifies doing CRUD operations in DynamoDB.

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.

Level up your cloud career

A Cloud Guru makes it easy (and awesome) to get certified and master modern tech skills — whether you’re new to cloud or a seasoned pro. Check out ACG’s current free courses or get started now with a free trial.

Get Started
Who’s going to be learning?
Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!