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

@block65/dynamodb-batch-iterator

v0.7.4-alpha.2

Published

Abstraction for DynamoDB batch reads and writes for that handles batch splitting and partial retries with exponential backoff

Downloads

36

Readme

Amazon DynamoDB Batch Iteration

Apache 2 License

This library provides utilities for automatically submitting arbitrarily-sized batches of reads and writes to DynamoDB using well-formed BatchGetItem and BatchWriteItem operations, respectively. Partial successes (i.e., BatchGetItem operations that return some responses and some unprocessed keys or BatchWriteItem operations that return some unprocessed items) will retry the unprocessed items automatically using exponential backoff.

Getting started

Reading batches of items

Create a BatchGet object, supplying an instantiated DynamoDB client from the AWS SDK for JavaScript and an iterable of keys that you wish to retrieve. The iterable may be synchronous (such as an array) or asynchronous (such as an object stream wrapped with async-iter-stream's wrap method).

import { BatchGet } from '@block65/dynamodb-batch-iterator';
import DynamoDB = require('aws-sdk/clients/dynamodb');

const dynamoDb = new DynamoDB({region: 'us-west-2'});
const keys = [
    ['tableName', {keyProperty: {N: '0'}}],
    ['tableName', {keyProperty: {N: '1'}}],
    ['tableName', {keyProperty: {N: '2'}}],
    // etc., continuing to count up to
    ['tableName', {keyProperty: {N: '1001'}}],
];

for await (const item of new BatchGet(dynamoDb, keys)) {
    console.log(item);
}

The above code snippet will automatically split the provided keys into BatchGetItem requests of 100 or fewer keys, and any unprocessed keys will be automatically retried until they are handled. The above code will execute at least 11 BatchGetItem operations, dependening on how many items are returned without processing due to insufficient provisioned read capacity.

Each item yielded in the for...await...of loop will be a single DynamoDB record. Iteration will stop once each key has been retrieved or an error has been encountered.

Writing batches of items

Create a BatchWrite object, supplying an instantiated DynamoDB client from the AWS SDK for JavaScript and an iterable of write requests that you wish to execute. The iterable may be synchronous (such as an array) or asynchronous (such as an object stream wrapped with async-iter-stream's wrap method).

Each write request should contain either a DeleteRequest key or a PutRequest key as described in the Amazon DynamoDB API reference.

import { BatchWrite } from '@block65/dynamodb-batch-iterator';
import DynamoDB = require('aws-sdk/clients/dynamodb');

const dynamoDb = new DynamoDB({region: 'us-west-2'});
const keys = [
    ['tableName', {DeleteRequest: {Key: {keyProperty: {N: '0'}}}}],
    ['tableName', {PutRequest: {Item: {keyProperty: {N: '1'}, otherProperty: {BOOL: false}}}}],
    ['tableName', {DeleteRequest: {Key: {keyProperty: {N: '2'}}}}],
    ['tableName', {PutRequest: {Item: {keyProperty: {N: '3'}, otherProperty: {BOOL: false}}}}],
    ['tableName', {N: '2'}],
    // etc., continuing to count up to
    ['tableName', {DeleteRequest: {Key: {keyProperty: {N: '102'}}}}],
];

for await (const item of new BatchWrite(dynamoDb, keys)) {
    console.log(item);
}

The above code snippet will automatically split the provided keys into BatchWriteItem requests of 25 or fewer write request objects, and any unprocessed request objects will be automatically retried until they are handled. The above code will execute at least 5 BatchWriteItem operations, dependening on how many items are returned without processing due to insufficient provisioned write capacity.

Each item yielded in the for...await...of loop will be a single write request that has succeeded. Iteration will stop once each request has been handled or an error has been encountered.