Getting Started

The most advanced bridge between AWS DynamoDB Document Client and Real World usage.

Introduction

Dynatron provides a number of abstractions, tools, utility functions designed to make dealing with Amazon DynamoDB easier and more natural for developers.

Dynatron is based on the latest AWS SDK version 3 and works equally good for node and browser.

Dynatron provides utilities for automatically submitting arbitrarily-sized batches of reads and writes to DynamoDB using well-formed BatchGetItem and BatchWriteItem operations, correct and reliable Scan and Query operations.

Dynatron has built-in defaults and mechanisms based on real-world applications making the requests to Amazon DynamoDB more reliable, adding exponential reties for applicable requests, optimizing API calls, and more...

Installation

Install Dynatron

Dynatron can be installed from npm

npm install dynatron

Install Peer Dependencies

Dynatron uses the AWS SDK DynamoDB Client but does not distribute it. Therefore, for Dynatron to work you need to install its peer dependencies.

npm install @aws-sdk/client-dynamodb

Usage

DynatronClient Initialization

const dynatronClient = new DynatronClient({
  region: "us-east-1",
});

Dynatron Initialization

const dynatron = new Dynatron(dynatronClient);

More Complete Approach

We suggest initializing the Dynatron class in a separate function where you could have a logic to check with which mode it should run. In serverless applications, you can pass in env variables and do the configuration based on them. You could create a file called dynatron-client.ts or similar which will also allow caching the initialized dynatron instance.

Dynatron also provides a special function called loadProfileCredentials. It can load the AWS credentials from the disk.

import { Dynatron, DynatronClient, DynatronClientConfig } from "./dynatron";

const IS_OFFLINE = process.env.IS_OFFLINE;
const IS_DIRECT = process.env.IS_DIRECT;
const PROFILE = process.env.PROFILE;
const REGION = process.env.REGION;
const ACCESS_KEY_ID = "accessKeyId";
const SECRET_ACCESS_KEY = "secretAccessKey";

let dynatron: Dynatron;

export const getDynatron = (): Dynatron => {
  if (dynatron) {
    return dynatron;
  }

  let config: DynatronClientConfig = {};

  if (IS_OFFLINE) {
    config = {
      region: "local",
      endpoint: "http://localhost:8080",
      credentials: {
        accessKeyId: "local-access-key-id",
        secretAccessKey: "local-secret-access-key",
      },
    };
  } else if (IS_DIRECT) {
    config = PROFILE
      ? {
          region: REGION,
          credentials: Dynatron.loadProfileCredentials(PROFILE),
        }
      : {
          region: REGION,
          credentials: {
            accessKeyId: ACCESS_KEY_ID,
            secretAccessKey: SECRET_ACCESS_KEY,
          },
        };
  }

  dynatron = new Dynatron(new DynatronClient(config));
  return dynatron;
};

Last updated