The node.js DynamoDB client comes from the aws-sdk package. We cover how to set up the client with your AWS credentials as well as how to use the callback and promise versions of documentclient functions.
Instructor: [0:00] The Node.js DocumentClient for DynamoDB comes straight out of the AWS SDK. Make sure you install that.
[0:06] The first thing we need to do is update the AWS config with the region that our DynamoDB table is in, the access key that we'll use to access it and the secret access key as well. This allows us to instantiate a new AWS.DynamoDB.DocumentClient.
[0:21] We put the name of the table that we're going to be accessing in this table constant and follow the a fairly normal pattern for DynamoDB queries, which is we put the params in an object and then we pass the params in later. In this case, on the DocumentClient, we're using the get command which we'll cover again in another video.
[0:38] In this case, we've used the callback parameters, which give us error in data. If we run this script, we can see that get gives us back an object with an item key inside of it that gives us back the item. If we look for an item that doesn't exist, we don't get anything back.
[0:52] The DocumentClient also supports Promises. Now that we've created an additional function called runQuery(), because we can't use a single width syntax at the top level in a Node.js file -- this is a restriction of the Node.js async/await's [inaudible] and not the DocumentClient.
[1:05] We can run the script again and we'll see the same results with the new console.log, so we can use the DocumentClient with either a Callback-based approach or a Promise-based approach which will let us use Promises with .then or async/await.