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

express-dynamodb

v1.0.7

Published

express-dynamodb is a wrapper package for DynamoDB that simplifies its usage. It provides convenient functions for common operations such as inserting or updating items, deleting items, and querying items based on different criteria.

Downloads

108

Readme

express-dynamodb

express-dynamodb is a wrapper package for DynamoDB that simplifies its usage. It provides convenient functions for common operations such as inserting or updating items, deleting items, and querying items based on different criteria.

Installation

npm install express-dynamodb

Usage

import { insertOrUpdate, deleteOne, findOne, findMany, findManyByField, findManyPlain, findOperated, findWithin, findAll, setRegion } from 'express-dynamodb';

// Set the AWS region
setRegion('us-east-1');

// Set the AWS Secret
setAccessKey({accessKeyId: 'xxxxxx', secretAccessKey: 'xxxxxx'});

// Set config (secret and region (opt) together)
setConfig({accessKeyId: 'xxxxxx', secretAccessKey: 'xxxxxx'}, 'us-east-1');

// Insert or update an item
await insertOrUpdate('tableName', 'pk', item, 'sk');

// Delete an item by partion key
await deleteOne('tableName', 'pk', 'pkValue');

// Delete an item by partion key and sort key
await deleteOne('tableName', 'pk', 'pkValue', 'sk', 'skValue');

// Find one item by partition key
const result = await findOne('tableName', 'pk', 'pkValue');

// Find one item by partition key and sort key
const result = await findOne('tableName', 'pk', 'pkValue', 'sk', 'skValue');

// Find many items by partition key
const results = await findMany('tableName', 'pk', 'pkValue');

// Find many items by field
const resultsByField = await findManyByField('tableName', 'field', 'value');

// Find many items using plain DynamoDB expression
const resultsPlain = await findManyPlain('tableName', 'filterExpression', {':u': 'value'});

// Find items using operated filters
const operatedResults = await findOperated('tableName', { attribute: value }, { attribute: '<' });

// Find items within a range
const resultsWithinRange = await findWithin('tableName', 'key', start, end);

// Find all items in the table
const batchSize = 100;
let res;
let iterator = findAll('tableName', batchSize);
while ((res = await iterator.next()) && !res.done) {
  for (const val of res.value) {
    console.log(val);
  }
}

API

setRegion(region: string)

Sets the AWS region for DynamoDB operations.

  • region: The AWS region (e.g., 'us-east-1').

insertOrUpdate(tableName: string, pk: string, item: any, sk?: string): Promise<void>

Inserts or updates an item in the table.

  • tableName: The name of the table.
  • pk: The partition key of the item.
  • item: The item to insert or update.
  • sk (optional): The sort key of the item.

deleteOne(tableName: string, pk: string, pkValue: string, sk: string, skValue: string): Promise<void>

Deletes one item by partition key.

  • tableName: The name of the table.
  • pk: The partition key of the item to be deleted.
  • pkValue: The value of the partition key.
  • sk (optional): The sort key of the item.
  • skValue (optional): The value of the sort key.

findOne(tableName: string, pk: string, pkValue: string, sk: string, skValue: string): Promise<any | null>

Finds one item by partition key.

  • tableName: The name of the table.
  • pk: The partition key of the item to be found.
  • pkValue: The value of the partition key.
  • sk (optional): The sort key of the item.
  • skValue (optional): The value of the sort key.

Returns null if the item is not found.

findMany(tableName: string, pk: string, pkValue: string): Promise<any[]>

Finds many items by partition key.

  • tableName: The name of the table.
  • pk: The partition key of the items to be found.
  • pkValue: The value of the partition key.

Returns an array of items.

findManyByField(tableName: string, field: string, value: any): Promise<any[]>

Finds many items by field key.

  • tableName: The name of the table.
  • field: The field key of the items to be found.
  • value: The value of the field.

Returns an array of items.

findManyPlain(tableName: string, filterExpression: string, expressionAttributeValues: Record<string, AttributeValue>): Promise<any[]>

Finds many items using a plain DynamoDB expression.

  • tableName: The name of the table.
  • filterExpression: The filter expression.
  • expressionAttributeValues: The expression attribute values.

Returns an array of items.

findOperated(tableName: string, filter: any, operators: { [key: string]: '<' | '<=' | '=' | '>=' | '>' }): Promise<any[]>

Finds items using operated filters.

  • tableName: The name of the table.
  • filter: The attributes to be searched and their values in an object.
  • operators: The operators for each attribute in an object.

Returns an array of items.

findWithin(tableName: string, key: string, start: number, end: number): Promise<any[]>

Finds items within a range.

  • tableName: The name of the table.
  • key: The key to filter within.
  • start: The start value of the range.
  • end: The end value of the range.

Returns an array of items.

findAll(tableName: string, limit: number = 10): AsyncGenerator<Record<string, any>[], void, unknown>

Findsall items in the table.

  • tableName: The name of the table.
  • limit: The maximum number of items to retrieve per batch (default is 10).

Returns an async iterable that yields batches of items. Each batch is an array of items.

Conclusion

express-dynamodb is a useful package for simplifying the usage of DynamoDB in an Express.js application. It provides convenient functions for common operations and helps abstract away some of the complexity of working with DynamoDB. By using this package, you can easily perform CRUD operations on your DynamoDB tables and retrieve data based on different criteria.

Author: @rrrokhtar