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

@vamship/aws-dynamodb

v1.4.19

Published

Library that provides easy abstractions to perform common operations on AWS DynamoDB

Downloads

29

Readme

@vamship/aws-dynamodb

Extensible utilities that include an entity that provides CRUD operations against DynamoDB tables with useful features such as logical deletes and optimistic concurrency locking.

The primary export from this library is a simplified entity with an opinionated implementation of CRUD operations, along with a base class that provides utility methods allowing for customized implementations.

API Documentation

API documentation can be found here.

Motivation

AWS DynamoDB is AWS' hosted NoSQL database service that works very well for several data storage applications. As with all NoSQL applications, DynamoDB trades off elasticity and flexibility for features that are provided by traditional SQL stores, such as ACID.

Applications that use NoSQL stores typically account for the lack of these features by either accepting their limitations, or by desigining some of these capabilities into the application code.

There are well understood design patterns for some of these features, such as Optimistic Concurrency Control, that can be implemented for DynamoDB. When building multiple application NoSQL based applcations, it becomes apparent that these common design patterns appear repeatedly for each table that is used within the application.

This library attempts to alleviate some of these challenges by providing an entity class that represents a single DynamoDB table, with CRUD implementations that support optimistic concurrency control and logical deletes. Methods can be overridden where necessary, or, if a completely different implementation is desired, a base Entity with useful validations and utility functions can be used to develop a custom entity class.

Installation

This library can be installed using npm:

npm install @vamship/aws-dynamodb

Usage

Using SimpleEntity to perform basic database operations

const { SimpleEntity } = require('@vamship/simple-entity');
const SelectiveCopy = require('selective-copy');

// Property called "count" can be updated, but not deleted.
// Property called "tag" can be deleted, but not updated
const UPDATE_PROPS = new SelectiveCopy(['count']);
const DELETE_PROPS = new SelectiveCopy(['tag']);

/**
 * Custom entity class for a specific dynamodb table called "my-table", with
 * a hash key column called "myHashKey" and a range key column called
 * "myRangeKey"
 *
 * The _updateCopier and _deleteCopier properties define what properties are
 * updatable and deletable on the entity record.
 */
class MyEntity extends SimpleEntity {
    constructor(options) {
        super(options);
    }

    get tableName() {
        return 'vamship-test_entity';
    }

    get hashKeyName() {
        return 'accountId';
    }

    get rangeKeyName() {
        return 'entityId';
    }

    get _updateCopier() {
        return UPDATE_PROPS;
    }

    get _deleteCopier() {
        return DELETE_PROPS;
    }
}

// See class documentation for possible values that can be included in the
// options object.
const options = {
    awsRegion: 'us-east-1'
};

// Instantiate and use the entity class.
const entity = new MyEntity(options);

// Define record keys
const keys = {
    accountId: 'my-account',
    entityId: 'my-entity-id'
};

// Define a new data record.
const props = {
    tag: 'my-tag',
    count: Math.floor(Math.random() * 100),
    list: ['apple', 'orange', 'pear']
};

// Define audit information to be included with the record (this is optional).
const audit = {
    username: 'joe'
};

// Create the record in the database.
entity
    .create(keys, props, keys)
    .then(() => {
        console.log('Record created successfully');
    })
    .then(() => {
        // Retrieve a list of records from the database
        return entity.list({ accountId: keys.accountId }).then((data) => {
            console.log('Record list retrieved successfully');
            console.log(data);
            return data.find(({ accountId, entityId }) => {
                return (
                    accountId === keys.accountId && entityId === keys.entityId
                );
            });
        });
    })
    .then((record) => {
        const updateProps = {
            count: record.count + 10 + Math.floor(Math.random() * 10)
        };

        const deleteProps = {
            tag: 'value does not matter' // The value does not matter.
        };

        // Update an existing record in the database.
        // Note that the version field is obtained by querying the record in the
        // database, and is used to enforce concurrency control
        return entity
            .update(keys, updateProps, deleteProps, record.__version)
            .then(() => {
                console.log('Record updated successfully');
            });
    })
    .then(() => {
        // Retrieve the record from the database.
        return entity.lookup(keys, audit).then((record) => {
            console.log('Record retrieved successfully');
            console.log(record);
            return record;
        });
    })
    .then((record) => {
        // Delete an existing record from the database
        entity.delete(keys, record.__version).then(() => {
            console.log('Record deleted successfully');
        });
    })
    .catch((err) => {
        console.log(err);
        console.log('Aborting due to errors');
    });

SimpleEntity provides pre defined methods for the following actions:

  • Create entity
  • Lookup entity
  • List entities
  • Update entity
  • Delete entity

Where applicable, each method supports audit logging, optmistic concurrency control and logical deletes. The child class can override any (or all) of these methods to provide different implementations as needed.