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

@arcticleaf/aws-util-dynamodb

v1.2.12

Published

Utilities for using DynamoDB without having to know all the API and implementation specifics of DynamoDB

Downloads

206

Readme

aws-util-dynamodb

TLDR; Wrapper around DynamoDB to allow for more generic use of DynamoDB without having to know how to use all the APIs.

This library implements a wrapper class around the DynamoDBClient from the AWS SDK. It uses a few interfaces and base classes which you need to implement or extend on your own data models to allow the data models (Classes) to include the structural information needed to build a DynamoDB table to store objects of that Class type in.

It is most useful for the core use case of DynamoDB - fast, easy JSON object storage for objects which are referenced by ID. If you have more complex use cases with Global or Local Secondary Indexes, or need to do custom serialization/deserialization of data from your Class structure, you may need to use the DynamoDB APIs directly.

For most uses of simple objects, this wrapper should provide an easier developer user experience.

Many advanced use cases are supported via additional types in the library and the options for each function call. They are well documented to review the docs and read the DynamoDB documentation for context on them and when they are useful.

Usage

For this library, each Class you want to store in DynamoDB maps to a single DynamoDB table. The Class needs to define the two minimal DynamoDB dependencies - the Hash Key (unique ID, also called the Partition Key) and the Range Key (optional, the sort order for the table - also called the Sort key). DynamoDB maps Javascript data types into a native DynamoDB format. For each attribute (a.k.a. column or field) in your Class, DynamoDB needs to know which of it's internal data types to use to store that data.

Fortunately, there are utilities which will convert between the standard Javascript types (string, number, boolean) and DynamoDB data types - this is called 'marshalling' and 'unmarshalling' (a.k.a. serializing / deserializing).

The only two fields which must be manually mapped to DynamoDB data types are the fields used as the Hash Key and Range Key. This is because DynamoDB needs to know those data types to create the table schema before you can insert an object into it.

The dynamodb-types file contains Interfaces as well as extendable base implementations for Classes to help ease this data structuring. It's recommended you extend from DynamoDbItem, DynamoDbExpiringItem, DynamoDbVersionedItem or DynamoDbExpiringVersionedItem depending on what combination of Expiration and/or Versioning support you want to leverage for you object (or neither, in the case of DynamoDbItem).

You can read the DynamoDB documentation for more info about Expiring and Versioned items but they are basically what they sounds like. DynamoDB will automatically delete Expiring items after a specified expiry date (one of the fields on the item, TTL) and Versioned items provide write-synchronization if you want to be sure the item hasn't changed in DynamoDB between the time you read it and write to it (useful when running multiple read/write operations in parallel on the table).

Example DynamoDbItem Implementation

import { DynamoDbItem } from '@arcticleaf/aws-util-dynamodb';
export class MyDataStructure extends DynamoDbItem {

    // THIS MUST BE A STRING TYPE
    public id: string;
    // --> add any properties here that define your data structure

    /**
     * A default constructor is REQUIRED for deserialization from DynamoDB
     * (in deserialization, first the default object is created, then
     * properties are assigned)
     */
    constructor(id?: string) {
        super();
        this.id = id || '';
    }

    static BaseTableName() : string {
     return 'MyDataStructures';
    }

    // #region IDynamoDbBaseItem
    public IdFieldName() : string {
     return 'id';
    }
    public RangeFieldName() : string | undefined {
     return undefined;
    }
    public BaseTableName() : string {
     return MyClass.BaseTableName();
    }
    // #endregion IDynamoDbBaseItem

}

Example Usage

// define TABLE_PREFIX in CloudFormation as `${ProjectName}.${Environment}` to make permissions scoping easier
const TABLE_PREFIX: string = `${process.env.TABLE_PREFIX || 'localhost'}`;
// DynamoDbClientWrapper constructor takes options as well
// you probably want to set a custom Logger in it
const db: DynamoDbClientWrapper = new DynamoDbClientWrapper(TABLE_PREFIX);
const item: MyDataStructure = new MyDataStructure('12345');
await db.PutItem(
    item,
    { CreateTableIfNotExists: true, Class: MyDataStructure },
);
try {
    const retrieved: MyDataStructure | undefined = await db.GetItem(MyDataStructure, item.id);
} catch (err) { ... }
// Alternate Usage: Required - if false, undefined will be returned if the item is not found instead of throwing an exception
const retrieved: MyDataStructure | undefined = await db.GetItem(MyDataStructure, item.id, undefined, { Required: false });

Release Process

  1. Make commits that have fix: as the prefix on the commit message in order for your fix to trigger a micro version update upon release
  2. Commit changes to your branch and then create a PR to merge your branch into master
  3. Update (merge) into master
  4. Merge release => master (in order to pick the last version number deployed)
    • this may have already been done but it's a safety check
  5. Create a PR to merge master => release

Note: this project uses the semantic-release system. New releases are automatically created from the release branch ONLY if there are commit comments prefixed with fix: (micro version bump), feat: (minor version bump), perf: (major version bump)