Skip to Content
Learn
DynamoDB
Refactor Session Attributes

When we save session attributes to our database table, we should always store them in a well-structured and descriptive format. Currently, our session attributes aren’t specific, and while we could write several more session attribute fields to cover the information we want to save, we would find a lot of redundancies.

DynamoDB is designed to support key:value storage, with the name of our attribute being the key and what we’ve set it to becoming its value. Use bracket notation:

this.attributes['yourAttribute'] = 'value';

Or dot notation:

this.attributes.yourAttribute = 'value';

For our purposes, we want our skill to remember the language we’re currently working on. For each language, we want to remember the flashcard that we’re currently on and the number of flashcards we’ve gotten correct. Because of the information we want to remember from session to session, we need to create a more complex session attribute structure that allows us to work with more data.

We can extend the key:value functionality to create a session attribute object that contains nested key:value pairs. This nested structure allows us to easily read, write, and store even more information. Since we set the session attributes value to an object, DynamoDB interprets these objects as JSON.

However, we can refactor our session attributes value into an intuitive, nested object. This structure would allow us to better maintain, persist, and remember the different states, such as our progress in each language. We want to refactor the three session attributes currently in the code so that they are now part of one flashcards object.

Instructions

1.

In the LaunchRequest function, delete the current session attributes and create a new flashcards session attribute object. Be sure to use dot syntax instead of brackets.

2.

Inside the object, create a 'currentLanguage' key and set its value to an empty string.

3.

Inside the flashcards object but below the 'currentLanguage' key, create an empty 'languages' object.

4.

Inside the languages object, create objects for ruby, python, and javascript. Inside each one of those objects, add keys for numberCorrect and currentFlashcardIndex. Set their values to 0.

Folder Icon

Take this course for free

Already have an account?