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

@acomagu/dynamodb-dataloader

v1.0.1

Published

This library provides a [DataLoader](https://github.com/graphql/dataloader) layer for efficient fetching from DynamoDB by caching and batching.

Downloads

19

Readme

DynamoDB DataLoader

This library provides a DataLoader layer for efficient fetching from DynamoDB by caching and batching.

Features

  • Batch Loading: Combines multiple queries into fewer network requests to DynamoDB(only for get operation).
  • Unified Caching: Caches are shared across get, query, and scan operations.
    • But this shared caching is effective only in limited scenarios, such as when entries previously fetched using query or scan are accessed again using get. Also, the feature does not function when only parts of records are retrieved.

Initializing the DataLoader

Define the schema for your tables, specifying each table's name and the attribute names that form the keys used in caching.

import { DynamodbDataLoader, TableSchema } from '@acomagu/dynamodb-dataloader';

const tableSchemas: TableSchema[] = [
  { tableName: "Users", keyAttributeNames: ["userId"] },
  { tableName: "Posts", keyAttributeNames: ["userId", "postId"] }, // PK and SK
]; // Used to enable cache sharing across query, scan, and get operations.

const options = {
  dynamodbClient: new DynamoDBClient({ /* AWS SDK configuration options */ }),
  getOptions: { /* BatchGet options */ },
};

const dynamodbDataLoader = new DynamodbDataLoader(tableSchemas, options); // All arguments are optional.

Propagate with Express Context

Following best practices of DataLoader, a new instance of DynamodbDataLoader should be created per client request.

This example inserts the dataLoader to the Request object in express middleware.

const app = express();

// Middleware to initialize DataLoader and store it in AsyncLocalStorage
app.use((req) => {
  req.dataLoader = new DynamodbDataLoader();
});

app.get('/user/:id', async (req, res) => {
  const item = await req.dataLoader.getter.load({
    TableName: "Users",
    Key: { userId: req.params.id },
  });
  res.send(item);
});

Store to AsyncLocalStorage

The another way to isolate DataLoader per client request is using AsyncLocalStorage.

const app = express();

const dynamodbDataLoaderStorage = new AsyncLocalStorage();

app.use((req, res, next) => {
  dynamodbDataLoaderStorage.run(new DynamodbDataLoader(), next);
});

app.get('/user/:id', async (req, res) => {
  const item = await dynamodbDataLoaderStorage.getStore()!.getter.load({
    TableName: "Users",
    Key: { userId: req.params.id },
  });
  res.send(item);
});

Usage

Fetching Data

Get Operation

Fetch data for a specific user ID from the "Users" table using the getter DataLoader:

const getUserRequest = {
  TableName: "Users",
  Key: { userId: "12345" }
};
const item = await dynamodbDataLoader.getter.load(getUserRequest);

Query Operation

Example of querying posts for a specific user:

const queryPostsRequest = {
  TableName: "Posts",
  KeyConditionExpression: "userId = :userId",
  ExpressionAttributeValues: {
    ":userId": "12345",
  },
};
const items = await dynamodbDataLoader.querier.load(queryPostsRequest);

Scan Operation

Scanning for items with a specific filter:

const scanRequest = {
  TableName: "Posts",
  FilterExpression: "contains(content, :content)",
  ExpressionAttributeValues: {
    ":content": "DynamoDB",
  },
};
const items = await dynamodbDataLoader.scanner.load(scanRequest);

API Documentation

Documentation