npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dynamodb-service

v1.6.2

Published

Managing records saved in Dynamodb

Downloads

7

Readme

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 = require('dynamodb-service');
// profile = 'not-default' // => this is the aws credentials found in `.aws/credentials` file
let dynamoDBService = new DynamoDBService(tableName, primaryKeyName, profile);

dynamoDBService.create(data) // => @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_key
aws_secret_access_key = your_secret_key

[not-default]
aws_access_key_id = your_access_key
aws_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 = require('../index.js');
let tableName = 'scheduling-configuration',
    primaryKeyName = 'configurationId';
let dynamodbService = new DynamodbService(tableName, primaryKeyName);

let data = {
  recurrence: 'string',
  time: 'string',
  timeZone: 'string',
  mediumType: 'SLACK', // SLACK, GMAIL, ...
  meta: { channelId: 'string', slackBotId: 'string', slackBotName: 'string' }
};

dynamoDBService.create(data)
.then(createdRecord => console.log(createdRecord))
.catch(e => console.error(e.message || 'error', e));
  • 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 = require('./src/services/DynamoDBService');
module.exports = DynamoDBService;
  • Create DynamoDBService file
# ./src/services/DynamoDBService.js
  • Add initialisation logic
// we are using es6 classes
// ./src/services/DynamoDBService.js

...
  constructor(tableName, primaryKeyName, profile) {
    this.tableName = tableName;
    this.primaryKeyName = primaryKeyName;

    let credentials = new AWS.SharedIniFileCredentials({profile});
    AWS.config.credentials = credentials;

    this.DYNAMO_DB = new AWS.DynamoDB.DocumentClient();

  }
...
  • Add logic for create method
// ./src/services/DynamoDBService.js

...
    let params = {
      TableName: this.tableName,
      Item: data
    };
    return this.dynamoDb.put(params).promise()
    .then((/* success */) => this.read(data[this.keyName]));
...
  • Add logic for read method
// ./src/services/DynamoDBService.js

...
    let Key = {};
    Key[this.keyName] = id;
    let params = {
      TableName: this.tableName,
      Key
    };
    return this.dynamoDb.get(params).promise()
    .then(function (response) {
      return response.Item;
    });
...

resource