ddb-local
A thin wrapper around AWS's DynamoDBLocal to make using it in unit tests a bit simpler.
Install
npm install --save ddb-local
Usage
Example usage with Mocha
Set an env var for your DynamoDB endpoint in your package.json test script
"scripts": { "test": "NODE_ENV=test AWS_DDB_ENDPOINT=http://localhost:3547 mocha --timeout 30000" },
Start up DdbLocal before running your application tests, and shut it down after.
var DdbLocal = ;
Then wihin your application code, any time you create a new DynamoDB client, set the endpoint if the env var is set.
var dynamoParams = apiVersion: '2012-08-10' endpoint: processenvAWS_DDB_ENDPOINT ; var dynamo = dynamoParams;
Now when you run $ npm test
your application will be using DynamoDBLocal
instead of the real DynamoDB. Keep in mind that if your application code
assumes your DynamoDB tables already exist, you'll have to create them
in your test setup.
Options
ddb-local supports a few configuration options via both env var and constructor
params new DdbLocal(options)
- Download path for the DynamoDBLocal jar file. Set with
options.jarDir
or by settingDEFAULT_DOWNLOAD_PATH
env var - Port for DynamoDBLocal to listen on. Set with
options.port
orDDB_PORT
- Endpoint for DynamoDBLocal to use. Overrides the Port option. Set with
AWS_DDB_ENDPOINT
- Whether to store data in memory (default) or files. Set
options.inMemory=false
orDDB_LOCAL_IN_MEMORY=false
to use files.
Self-contained Example (no test framework)
var AWS = ;var DdbLocal = ;var assert = ; var localdb = ;localdbstart { assert; // Now use DynamoDB normally, just set the endpoint to localdb.endpoint var AWS = ; var dynamoParams = apiVersion: '2012-08-10' endpoint: localdbendpoint ; var client = dynamoParams; var tableParams = TableName: 'test' AttributeDefinitions: AttributeName: 'id' AttributeType: 'S' KeySchema: AttributeName: 'id' KeyType: 'HASH' ProvisionedThroughput: ReadCapacityUnits: 5 WriteCapacityUnits: 5 ; client; // Stop LocalDB when you're done localdb;};