1.0.0 โ€ข Published 8 months ago

use-dynamodb v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

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: 'john@example.com'
});

๐Ÿ“– 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: 'newemail@example.com'
		})
	}
);

๐Ÿ—‘๏ธ 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