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

@mihilmy/dynamodb-adapters

v1.0.0

Published

A simpler way to write DynamoDB expressions

Downloads

2

Readme

According to Adapter Wiki Page 🧐

An adapter is a device that converts attributes of one electrical device or system to those of an otherwise incompatible device or system. Some modify power or signal attributes, while others merely adapt the physical form of one connector to another.

In Dynamo world, an adapter is something that will adapt to a more human-readable experience using method chaining to create beautiful expressions for you. The idea is to abstract all of DynamoDB's operations into three simple functions:

  1. read() Loads data from your table
  2. write() Saves data to your table
  3. delete() Deletes data from your table

The library is smart enough to pick the best low level operation based on your API query, so if you want to load four items the adapter will do a BatchGetItems under the hood which is the most efficient operation instead of four parallel Get calls.

Human Readable Expressions

One of the biggest motivations for writing this library is to have clean expressions whether it's a conditional write, or a filter expression for reads. The goal is to write one line expression to save objects:

adapters.write(object).if($P.objectId, "DoesNotExist").orIf($P.lastUpdatedAt, ">=", Date.now());

Compare that beautiful one liner with the following expression, that would achieve the same thing:

const updateExpression = {
  Table: "Objects",
  Key: {
    objectId: 1,
    objectVersion: 'V0'
  },
  UpdateExpression: "SET #name=:name, #price=:price, #type=:type",
  ConditionalExpression: "attribute_not_exists(#objectId) OR #lastUpdatedAt >= :lastUpdatedAt",
  ExpressionAttributeNames: {
    "#objectId": "objectId",
    "#name": "name",
    "#price": "price",
    "#type": "type",
    "#lastUpdatedAt": "lastUpdatedAt",
  },
  ExpressionAttributeValues: {
    ":name": "ObjectOne",
    ":price": 700,
    ":type": "Games",
    ":lastUpdatedAt": 1609130078229
  },
};

The library attempts to provide convenience when possible for example supporting path updates which is not directly supported by DynamoDB but can be a supported client side by implementing a path level traversal algorithm. The main tenant of the library is to assist in building the expression using as least code as possible, it's not responsible for the correctness of your expression.

Infrastructure As Code

With the rise of cloudformation creating a templated way to manage infrastructure, and the increase in CDK usage to generate the cloudformation templates it has become obvious the ease of uniting our infra code, and our application layer code. DynamoDB's adapters uses the same CDK constructs to interact with your table infer your keys.

Locking

Lock items to prevent multiple writers from overwriting objects at the same time. This will use a global lock table with a heartbeat implementation, this is recommended when you have multiple writers to a map or you are doing some complex computation with a read before write.

Path Updates

DynamoDB allows you to update deeply nested JSON, however it becomes difficult to update a single path out of a given attribute due to constraints imposed by the APIs. The DynamoDB adapters provides an easy way to manipulate a single path, handling creation of paths when they do not exist.