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

@jupiterone/dynamodb-dao

v3.0.0

Published

DynamoDB Data Access Object (DAO) helper library

Downloads

2,840

Readme

dynamodb-dao

This project contains code for a DynamoDbDao class that can be used for creating, querying, updating, and deleting from DynamoDB table. Unlike tools like dynamoose, DynamoDbDao is a lower level wrapper and aims not to abstract away too many of the DynamoDB implementation details.

Also, this module leverages TypeScript type declarations so that, when possible, methods arguments and return values are strictly typed.

Examples

Constructor:

import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb';

const ddb = new DynamoDBClient({
  apiVersion: '2012-08-10'
})

const documentClient = DynamoDBDocumentClient.from(ddb, {
  marshallOptions: {
    removeUndefinedValues: true
  }
});

// The type declaration of for the documents that we are storing
interface MyDocument {
  id: string;
  accountId: string;
  name: string;
  total?: number;
}

// Key schema should have one or two properties which correspond to
// hash key and range key.
//
// NOTE: a range key is optional and depends
// on how your DynamoDB table is configured.
interface MyDocumentKeySchema {
  // hash key
  accountId: string;

  // range key
  id: string;
}

const myDocumentDao = new DynamoDbDao<MyDocument, MyDocumentKeySchema>({
  tableName: 'my-documents',
  documentClient
});

Get query:

const myDocument = await myDocumentDao.get({ id, accountId });

Paginated query:

const { items, lastKey } = await myDocumentDao.query({
  index: 'NameIndex',
  keyConditionExpression: 'accountId = :accountId',
  startAt: cursor /* `cursor` is a previously returned `lastKey` */,
  scanIndexForward: true,
  attributeValues: {
    ':accountId': accountId,
  },
});

Count query:

const count = await myDocumentDao.count({
  index: 'NameIndex',
  keyConditionExpression: 'accountId = :accountId',
  attributeValues: {
    ':accountId': input.accountId,
  },
});

Put:

await myDocumentDao.put({
  id: 'something',
  accountId: 'abc'
  name: 'blah'
});

Delete:

await myDocumentDao.delete({ id, accountId });

Incrementing/Decrementing

NOTE: This should only be used where overcounting and undercounting can be tolerated. See the DynamoDB atomic counter documentation for more information.

If a property does not already exist, the initial value is assigned 0 and incremented/decremented from 0.

// `total` will have the value `5`
const { total } = await myDocumentDao.incr(
  // The key
  {
    id: 'abc',
    accountId: 'def',
  },
  // The `number` property to increment
  'total',
  // The number to increment by. Defaults to 1.
  5
);

// `total` will have the value `-5`
const { total } = await myDocumentDao.decr(
  // The key
  {
    id: '123',
    accountId: 'def',
  },
  // The `number` property to increment
  'total',
  // The number to decrement by. Defaults to 1.
  5
);

When multiple values must be incremented and/or decremented in the same call:

// `total` will have the value `5` and `extra` will have the value -1.
const { extra, total } = await myDocumentDao.multiIncr(
  {
    id: 'abc',
    accountId: 'def',
  },
  {
    total: 5,
    extra: -1,
  }
);

Optimistic Locking with Version Numbers

For callers who wish to enable an optimistic locking strategy there are two available toggles:

  1. Provide the attribute you wish to be used to store the version number. This will enable optimistic locking on the following operations: put, update, and delete.

    Writes for documents that do not have a version number attribute will initialize the version number to 1. All subsequent writes will need to provide the current version number. If an out-of-date version number is supplied, an error will be thrown.

    Example of Dao constructed with optimistic locking enabled.

    const dao = new DynamoDbDao<DataModel, KeySchema>({
      tableName,
      documentClient,
      {
         optimisticLockingAttribute: 'version',
         // If true, the first put or update will create and initialize
         // the 'version' attribute, otherwise it will not create it
         // This allows adopters to choose to adopt at the item level
         // or at the dao level
         autoInitiateLockingAttribute: true, // default: true
      }
    });
  2. If you wish to ignore optimistic locking for a save operation, specify ignoreOptimisticLocking: true in the options on your put, update, or delete.

NOTE: Optimistic locking is NOT supported for batchWrite or batchPut operations. Consuming those APIs for data models that do have optimistic locking enabled may clobber your version data and could produce undesirable effects for other callers.

This was modeled after the Java Dynamo client implementation.

Developing

The test setup requires that docker-compose be installed. To run the tests, first open one terminal and start the local DynamoDB docker container by running:

yarn start:containers

In a second terminal run:

yarn test

To stop containers:

yarn stop:containers

Releasing

Once you are ready to publish a new version, make sure all of your changes have been pushed and merged to the remote repository.

Next, create a new branch and run the following command:

npm version minor (or major or patch)

This will add a commit with an updated package.json, and create a new tag locally.

Then, push your branch and new tag to the remote.

git push && git push --tags

Create a pull request with the branch. Once that is merged, your new version will be published.