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

@setho/dynamodb-repository

v0.9.0

Published

DynamoDB repository for hash-key and hash-key/range indexed tables. Designed for Lambda use. Handles nice-to-haves like created and updated timestamps and default id creation.

Downloads

48

Readme

dynamodb-repository

CI & Test

DynamoDB repository for key-value indexed tables. Designed for Lambda use. Handles niceties like created and updated timestamps and default id creation.

Install

$ npm install @setho/dynamodb-repository --save

Usage

TypeScript/ES6

import { KeyValueRepository } from '@setho/dynamodb-repository';

const myRepo = new KeyValueRepository({
  tableName: 'Items', // Required
  keyName: 'id', // Required
  idOptions: {
    // Optional
    prefix: 'ITEM_', // Default is empty string
  },
  documentClient, // Required - V3 DynamoDBDocumentClient from @aws-sdk/lib-dynamodb
});

JavaScript

const { KeyValueRepository } = require('@setho/dynamodb-repository');

const myRepo = new KeyValueRepository({
  tableName: 'Items', // Required
  keyName: 'id', // Required
  idOptions: {
    // Optional
    prefix: 'ITEM#', // Default is empty string
  },
  documentClient, // Required - V3 DynamoDBDocumentClient from @aws-sdk/lib-dynamodb
});

Constructor

Use the optional idOptions constructor parameter to set an optional prefix to give your ids some human-readable context. The remainder of the key is a ULID, which is both unique and lexicographically sortable. See an explanation of and motivation for ULIDs here.

Create

  • Requires action dynamodb:PutItem
  • Automatically adds a string key (this will overwrite any you may try to provide). Use constructor options to specify length and optional prefix.
  • Automatically provides a createdAt and updatedAt timestamp in ISO-8601
  • Returns what was saved; does not mutate the item passed in.
const mySavedItem = await myRepo.create(myItem);
mySavedItem.id; // itm_a4d02890b7174730b4bbbc
mySavedItem.createdAt; // 1979-11-04T09:00:00.000Z
mySavedItem.updatedAt; // 1979-11-04T09:00:00.000Z

Get by Key

  • Requires action dynamodb:GetItem.
  • Throws 404 if item not found using http-errors.
const myItem = await myRepo.get(id);

Get Many

  • Requires action dynamodb:Scan
  • Accepts optional parameter fields limit and cursor
    • limit defaults to 100
  • Returns object with items (Array) and cursor (String)
    • items will always be an array; if nothing found, it will be empty
    • cursor will be present if there are more items to fetch, otherwise it will be undefined
// Example to pull 100 at time until you have all items
const allItems = [];
const getAllItems = async ({ limit, cursor = null }) => {
  const getResult = await myRepo.getMany({ limit, cursor });
  allItems.push(...getResult.items);
  if (getResult.cursor) {
    await getAllItems({ cursor: getResult.cursor });
  }
};
await getAllItems();
// The array allItems now has all your items. Go nuts.

Remove by Key

  • Requires action dynamodb:DeleteItem
await myRepo.remove(id);

Update

  • Requires dynamodb:UpdateItem
  • Honors revision check; it will only update if the revision on disk is the one you are updating. Will return a 409 if the revision has changed underneath you.
  • Will perform a partial update if you don't pass in all properties. Think of this as a "patch" vs. a replacement update. The key and revision properties are always required.
  • Returns the entire entity, including both new and unchanged properties
const person = await myRepo.create({
  name: 'Joe',
  age: 28,
  favoriteColor: 'blue',
});
// Full item update
person.favoriteColor = 'teal';
const newPerson1 = await myRepo.update(person);
console.log(newPerson1.favoriteColor); // 'teal'

// Partial update
const partial = {
  favoriteColor: 'aquamarine',
  key: person.key,
  revision: newPerson1.revision,
};
const newPerson2 = await myRepo.update(partial);
console.log(newPerson2.favoriteColor); // 'aquamarine'
console.log(newPerson2.age); // 28

// More Coming Soon...