dynamodb-service
Managing records saved in Dynamodb
Scenario
I want to save scheduling information
recurrence: string time: string timeZone: string mediumType: enum // SLACK, GMAIL, ... meta: json // ex: { channelId, slackBotId, slackBotName }
Goal
Create node package that exposes a DynamoDBService
along with CRUD methods.
example usage
const DynamoDBService = ;// profile = 'not-default' // => this is the aws credentials found in `.aws/credentials` filelet dynamoDBService = tableName primaryKeyName profile; dynamoDBService // => @return Promise
pre-requisite
- Create a credentials file at ~/.aws/credentials on Mac/Linux or C:\Users\USERNAME.aws\credentials on Windows
[default]aws_access_key_id = your_access_keyaws_secret_access_key = your_secret_key [not-default]aws_access_key_id = your_access_keyaws_secret_access_key = your_secret_key
Other ways to provide credentials
- Create a table
aws dynamodb create-table --table-name scheduling-configuration \ --attribute-definitions AttributeName=configurationId,AttributeType=S \ --key-schema AttributeName=configurationId,KeyType=HASH \ --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 \ --query TableDescription.TableArn --output text \ --region=us-east-1 --profile default# the tableName 'scheduling-configuration' can be anything # note the 'AttributeName', you can change 'configurationId' to whatever you want
Step by step
The TDD way
- Create dynamodb-service
mkdir dynamodb-service
- add initial npm packge files
cd dynamodb-service && npm init// follow the prompt and provide all required info
- Create a test folder
mkdir test
- Create and add logic to the test file
// we first create a test file for create method// it should take in data and return a Promise that resolve with // the saved record// ./test/create.test.js let DynamoDBService = ;let tableName = 'scheduling-configuration' primaryKeyName = 'configurationId';let dynamodbService = tableName primaryKeyName; let data = recurrence: 'string' time: 'string' timeZone: 'string' mediumType: 'SLACK' // SLACK, GMAIL, ... meta: channelId: 'string' slackBotId: 'string' slackBotName: 'string' ; dynamoDBService;
- Run the test
# you should get errors node test/create.test.js
- Create and add logic to the
index.js
file
// ./index.js'use strict';const DynamoDBService = ;moduleexports = DynamoDBService;
- Create
DynamoDBService
file
# ./src/services/DynamoDBService.js
- Add initialisation logic
// we are using es6 classes// ./src/services/DynamoDBService.js ... { thistableName = tableName; thisprimaryKeyName = primaryKeyName; let credentials = profile; AWSconfigcredentials = credentials; thisDYNAMO_DB = ; }...
- Add logic for create method
// ./src/services/DynamoDBService.js ... let params = TableName: thistableName Item: data ; return thisdynamoDb ;...
- Add logic for read method
// ./src/services/DynamoDBService.js ... let Key = {}; KeythiskeyName = id; let params = TableName: thistableName Key ; return thisdynamoDb ;...