Marc Hughes


Home
Blog
Twitter
LinkedIn
GitHub
about
I am a developer from a bit west of Boston.

Published my first NPM module

01 Jul 2016

I've been playing around with the serverless framework lately, and published my first NPM module to solve a problem I've been having with it.

I call it the serverless-dynamodb-fixtures-plugin, and it's a simple plugin that adds a command to the serverless utility that lets you load up a bunch of json files into your dynamodb tables.

Considering it's already been downloaded 89 times in just a few days, I guess it was a need people had.

I've done a ton a client side javascript work, but only tinkered with node before this. Actually taking some time to understand the tooling and learn some best practices is pretty rewarding.

The actual publishing of a package to npm was a lot easier than expected!

serverless-dynamodb-fixtures-plugin

This is a plugin for Serverless

It will help you load up some static data into your DynamoDB tables.

Useful for either canned data or to provide consistent test-data across environments.

Installation

npm install serverless-dynamodb-fixtures-plugin --save

Then add serverless-dynamodb-fixtures-plugin to your plugins array in s-project.json

  "plugins": [
    "serverless-dynamodb-fixtures-plugin"
  ]

Usage

Create a fixtures directory in the root of your project. Populate it with json files of the format:

{
    "tableName": "table", // name of the dynamodb table
    "entries":[
        {},  // Data to insert
        {}
    ]
}

Here's an example with some data in it.

{
  "tableName": "${SERVERLESS_STAGE}-${SERVERLESS_PROJECT}-car",
  "entries":[
    {"id":"test-car-1", "make":"Subaru Forrester", "mileage":32000, "year":2010},
    {
      "id":"test-car-2",
      "make":"Audi TT",
      "mileage":132000,
      "year":2004,
      "previous_owner": {
        "name": "Marc",
        "comments": [
          "Was fun to drive.",
          "Never in an accident"
        ]
      }

    },
    {"id":"test-car-3", "make":"Ford Mustang", "mileage":57021, "year":2016},
    {"id":"test-car-4", "make":"Delorean", "mileage":2000000, "year":1986},
    {"id":"test-car-5", "make":"Intentionally Missing Mileage", "year":2010},
    {"id":"test-car-${SERVERLESS_STAGE}", "make":"Intentionally Missing Mileage", "year":2010}
  ]
}

Then run:

sls dynamoddb load

You should see something like this:

Serverless: Loading fixtures to stage "dev" in region "us-east-1"...  
Serverless: Loading fixtures/cars.json  
Serverless: Writing 6 entries  
Serverless: Loading fixtures/cars2.json  
Serverless: Writing 25 entries  
Serverless: Writing 21 entries  

If it starts taking a long time, you're probably hitting your write throughput limit, just wait and it should finish.

Optionally specify stage and/or region

NOTE: This uses a dynamoDB batch write operation which completely overwrites the items specified. If you have records with the same keys, they will be overwritten. If you have properties in your existing data that don't exist in the JSON, they will be lost.

Limitations

Right now, only the variables SERVERLESSSTAGE, SERVERLESSREGION, and SERVERLESS_PROJECT can be used within the json.