build with purpose

Simple Setup: DynamoDB with NodeJS

DynamoDB is a NoSQL Document Key-Value Database hosted by AWS. They offer to serve their database as a scalable cloud solution offloading the work of maintaining a database from developers. DynamoDB also features many integrations with other elements of the AWS stack like triggering Lambda functions and analyzing data in Elastic MapReduce. They support downloading and installing a local version of the database for development which allows developers to test without worrying about being charged.

Requirements

Java, NodeJS, and NPM. I’m using Java(1.8.0_101), NodeJS(6.4.0), and NPM(3.10.3)

Setup

  1. Download a copy of DynamoDB from AWS

  2. Extract the contents of the compressed file you downloaded using your favorite archive unpacker. My favorite is 7-Zip

  3. Get a command line instance up and running and enter the directory that was just extracted. Now we just need to run the .jar with Java given some options:

    java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb
    
  4. Now we have a DynamoDB instance running in our shell. Let’s create a new NPM project and install the aws-sdk:

    mkdir dynamodb-test
    cd dynamodb-test
    npm init -f
    npm install --save aws-sdk
    
  5. Let’s pull in the dependencies we need and setup an initial configuration

    var AWS = require('aws-sdk');
    AWS.config.update({
        endpoint: "http://localhost:8000",
        region: "us-west-2",
        accessKeyId: "myKeyId",
        secretAccessKey: "secretKey"
    });
    var dyn = new AWS.DynamoDB();
    var docClient = new AWS.DynamoDB.DocumentClient();
    

    This sets us up with a document client connected to our new database and we’re all ready to start playing with DynamoDB. The AWS.DynamoDB function contains an API to list and manipulate tables and the database. The DocumentClient contains an API to manipulate documents in tables.

  6. Now to write some code to actually get the database doing something

    dyn.listTables(function (err, data) {
      if (data.TableNames.indexOf('Entries') === -1) {
        var params = {
          TableName : "Entries",
          KeySchema: [
            { AttributeName: "userId", KeyType: "HASH" },  //Partition key
            { AttributeName: "id", KeyType: "RANGE"}  //Sort key
          ],
          AttributeDefinitions: [
            { AttributeName: "userId", AttributeType: "S" }, // S -> String
            { AttributeName: "id", AttributeType: "N" } // N -> Number
          ],
          ProvisionedThroughput: {
            ReadCapacityUnits: 2, // scalable with cost
            WriteCapacityUnits: 2
          }
        };
        dyn.createTable(params, function(err, data) {
          if (err) {
            console.log("Unable to create table. Error JSON:", JSON.stringify(err, null, 2));
          }
        });
      } else {
        ...
    

    So far we’ve looked up all the tables in our database and checked to see if any of them matched “Entries”. If not, we define the table’s parameters and create the table. Note the syntax when creating Keys and Definitions. This is definitely something to look into later.

  7. Now that we have a table created, let’s put some documents in it

        var reqParams = {
            TableName: "Entries",
            Key: {
                "userId":  'BEN',
                "id": 2
            }
        };
        docClient.get(reqParams, function(err, data) {
          if (err) {
            console.log("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
          } else {
            if (Object.keys(data).length === 0) {
              var docParams = {
                  TableName: "Entries",
                  Item: {
                      "userId":  'BEN',
                      "id": 2,
                      "data": 'Slurpie'
                  },
              };
              docClient.put(docParams, function(err, data) {
                if (err) {
                  console.log("Unable to add item. Error JSON:", JSON.stringify(err, null, 2));
                }
              });
            } else {
              console.log("Item insertion succeeded:", JSON.stringify(data, null, 2));
            }
          }
        });
      }});
    

    First we specify some request parameters to request for and use DocumentClient.get() to request from the newly created Entries table. We check for data and if we can’t find any we’ll construct a document to insert into the table. After running this file about three times it will have created the database, inserted a document, and then printed the stored document.

  8. (Optional) If we ever want to deploy something to production, we’ll need to change our configuration to actually make requests to AWS. To do this you’ll just need to update the configuration with your credentials like so:

    AWS.config.update({
        //endpoint: "http://localhost:8000",
        region: "us-west-2",
        accessKeyId: "<yourKey>",
        secretAccessKey: "<yourSecretKey>"
    });
    

This is the simplest way I’ve found to get up and going with DynamoDB. It cuts out a lot of work to just go right to production, but it’s sometimes nice to be able to test things before then. In production, I like to use their console that allows you to spin up tables with a UI. That completely eliminates most of my need to use the AWS.DynamoDB API as I’ll use the DocumentClient for most everything.

If you’re interested in downloading the source code for this example, I’ve created a Github gist containing the code I wrote above.

Resources

Q/A

Where do I get my secret access key again?

If I had a nickel… This can be found in IAM. Here’s a fancy link.

Make my day and share this post:

Other posts to peak your interest:

  • Understanding Visual Testing
  • The Redux Saga Black Box
  • What my college degree gave me
  • Technical Leaders Enabling Stronger Teams
  • Finding Your Gateway to Learning Vue
  • Why Write Server Rendered Frontend Apps
  • comments powered by Disqus
    © 2019. All rights reserved.