1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Getting individual DynamoDB items with node.js using get and promises

    nodeNode.js
    awsAWS

    get is how we can take a primary key and get a single DynamoDB item. We cover the basics, like how to get the actual item, as well as more advanced usage with additional options that tell us how much capacity we used, and returning only specific attributes

    Code

    Code

    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: 0:00 The Node.js DocumentClient allows us to get a single item if we have the primary key. In this case, we have a table with a primary key that includes the PK and the SK. That is a partition key and a sort key. We're going to need both of these values to get back to this item.

    0:15 If we look at the item that we're getting in JSON view, we can see that it has a partition key, a sort key and a data key. The data key is some additional JSON data, such as the text to the todo and whether it's done or not.

    0:28 Back in our editor, we have a script. We set up the parameters that are going to be shared between the three queries that we're going to make. We're going to use the table name of testing. We're going to get the same item every time, that is an item with the primary key that is made up of the partition key user #2 and a sort key todo #4.

    0:45 We also include this key down here, which is returned ConsumedCapacity. We specify this as total, but we could also put in indexes or none. Let's take a look at the output.

    0:55 If we look at the first result, we can see that the query was just the parameters that we specified earlier, which is the table name, the primary key and the returned ConsumeDCapacity. That gives us back the item because we do have an item with this partition key and this sort key in our table and it gave us back the rest of the data for the item as well.

    1:12 Note that it gave us back all of the data. The other returned value it gave us back is a ConsumedCapacity key. This key will tell us things like what table were we querying against and how much capacity did we use.

    1:22 This returned value can be really useful if you're using something like Honeycomb for observability because you can attach these values to the event that you're handling, and then, when you go to query it later, you can find out if say user #2 happens to be using a lot of capacity to get all their todos back because they're doing something really weird. Otherwise, this ConsumedCapacity probably isn't something we're going to use in our application.

    1:45 If we move on to the second get, we can see that we've added one parameter, AttributesToGet. AttributesToGet lets us specify a single or multiple attributes that we want to get returned when we query for this item.

    1:56 In this case, we've asked to only return data and you'll see that item only has data in it. If we compare this to the original query, you can see that we got PK and SK back in the original query, but in the AttributesToGet query, we only get back data.

    2:10 AttributesToGet only works for top level attributes. In our case, that's PK, SK or data. If we wanted to just get done back and not any of the other values, we could use a projection expression.

    2:20 Note that the third query takes the parameters that we specified in the original query, that is table name, key and returned ConsumedCapacity. We add a projection expression. The projection expression uses a replaceable value for data because data is a reserved word in DynamoDB. Then we access the done attribute on data.

    2:37 To do the replacement of the reserved word, we specify #data and then we use expression attribute names to replace that value with the actual value that we want.

    2:46 When we run the third query, we can see that all we get back is data.done and we're missing text. If you want to get a top-level attribute, you can use data, but if you have a nested attribute, you can use a projection expression to get just that value.