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

use-dynamodb

v1.0.0

Published

A TypeScript library that provides a simplified interface for interacting with Amazon DynamoDB, using the AWS SDK v3.

Downloads

69

Readme

Use DynamoDB

A TypeScript library that provides a simplified interface for interacting with Amazon DynamoDB, using the AWS SDK v3.

🚀 Features

  • ✅ Support for CRUD operations (Create, Read, Update, Delete)
  • 🔍 Support for local and global secondary indexes
  • 📦 Batch operations (batch write and delete)
  • 🔎 Optimized queries with filtering
  • 🔒 Conditional updates
  • 📄 Pagination support
  • 🔄 Upsert support
  • ⏱️ Automatic timestamp management

📦 Installation

yarn add use-dynamodb

🛠️ Usage

Initialization

import Dynamodb from 'use-dynamodb';

const dynamodb = new Dynamodb({
	accessKeyId: 'YOUR_ACCESS_KEY',
	secretAccessKey: 'YOUR_SECRET_KEY',
	region: 'us-east-1',
	table: 'YOUR_TABLE_NAME',
	schema: { partition: 'pk', sort: 'sk' },
	indexes: [
		{
			name: 'ls-index',
			partition: 'pk',
			sort: 'lsiSk',
			type: 'S'
		},
		{
			name: 'gs-index',
			partition: 'gsiPk',
			sort: 'gsiSk',
			type: 'S'
		}
	]
});

Basic Operations

📝 Create/Update Item (Put)

const item = await dynamodb.put({
	pk: 'user#123',
	sk: 'profile',
	name: 'John Doe',
	email: '[email protected]'
});

📖 Get Item

const item = await dynamodb.get({
	pk: 'user#123',
	sk: 'profile'
});

🔄 Update Item

const updatedItem = await dynamodb.update(
	{
		pk: 'user#123',
		sk: 'profile'
	},
	{
		updateFn: item => ({
			...item,
			email: '[email protected]'
		})
	}
);

🗑️ Delete Item

const deletedItem = await dynamodb.delete({
	pk: 'user#123',
	sk: 'profile'
});

Advanced Query Operations

🔍 Fetch (Query) Items

const { items, count, lastEvaluatedKey } = await dynamodb.fetch({
	pk: 'user#123'
});

🔎 Fetch with Filter

const { items, count, lastEvaluatedKey } = await dynamodb.fetch(
	{ pk: 'user#123' },
	{
		attributeNames: { '#foo': 'foo' },
		attributeValues: { ':foo': 'foo-0' },
		filterExpression: '#foo = :foo'
	}
);

📄 Fetch with Pagination

const { items, count, lastEvaluatedKey } = await dynamodb.fetch(
	{ pk: 'user#123' },
	{
		limit: 10,
		startKey: lastEvaluatedKey // from previous query
	}
);

Batch Operations

📦 Batch Write

const items = [
	{ pk: 'user#1', sk: 'profile', name: 'User 1' },
	{ pk: 'user#2', sk: 'profile', name: 'User 2' }
];
await dynamodb.batchWrite(items);

🗑️ Batch Delete

await dynamodb.batchDelete({ pk: 'user#123' });

Using Indexes

🔍 Query using Local Secondary Index

const { items, count, lastEvaluatedKey } = await dynamodb.fetch({
	pk: 'user#123',
	lsiSk: 'lsi-value'
});

🔎 Query using Global Secondary Index

const { items, count, lastEvaluatedKey } = await dynamodb.fetch({
	gsiPk: 'gsi-partition-value',
	gsiSk: 'gsi-sort-value'
});

🧪 Testing

This library includes a comprehensive set of tests. To run the tests:

  1. Set the required environment variables:
export AWS_REGION='us-east-1'
export AWS_ACCESS_KEY='YOUR_ACCESS_KEY'
export AWS_SECRET_KEY='YOUR_SECRET_KEY'
  1. Run the tests:
yarn test

Make sure to replace 'YOUR_ACCESS_KEY' and 'YOUR_SECRET_KEY' with your actual AWS credentials.

🙏 Acknowledgements