# @aws/dynamodb-batch-iterator

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

Latest version **0.7.1** (published 2018-07-21) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @aws/dynamodb-batch-iterator
pnpm add @aws/dynamodb-batch-iterator
yarn add @aws/dynamodb-batch-iterator
bun add @aws/dynamodb-batch-iterator
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.7.1 |
| Published | 2018-07-21 |
| First published | 2017-12-22 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 61.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 808 |
| Author | AWS SDK for JavaScript Team |
| Maintainers | amzn-oss, aws-iot-sdk, aws-sdk-bot, aws-sdk-team, awssandra, awsxray, brettstack, cjradek, gkwicker.amazon, haotianw465, haverchuck, jsq, yogiraj07 |
| Keywords | aws, dynamodb |

## Links

- npm: https://www.npmjs.com/package/@aws/dynamodb-batch-iterator
- Repository: https://github.com/awslabs/dynamodb-data-mapper-js
- Homepage: https://awslabs.github.io/dynamodb-data-mapper-js/packages/dynamodb-batch-iterator/
- Issues: https://github.com/awslabs/dynamodb-data-mapper-js/issues
- npm.io page: https://npm.io/package/@aws/dynamodb-batch-iterator

## Dependencies (2)

- [tslib](https://npm.io/package/tslib.md) ^1.8.1
- [utf8-bytes](https://npm.io/package/utf8-bytes.md) ^0.0.1

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 0.7.1 (latest) — 2018-07-21
- 0.7.0 — 2018-07-10
- 0.6.1 — 2018-06-28
- 0.6.0 — 2018-06-16
- 0.5.0 — 2018-05-17
- 0.4.1 — 2018-04-24
- 0.4.0 — 2018-01-08
- 0.3.1 — 2017-12-30
- 0.3.0 — 2017-12-22

## README

# Amazon DynamoDB Batch Iteration

[![Apache 2 License](https://img.shields.io/github/license/awslabs/dynamodb-data-mapper-js.svg?style=flat)](http://aws.amazon.com/apache-2-0/)

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](https://github.com/calvinmetcalf/async-iter-stream)'s
`wrap` method).

```typescript
import { BatchGet } from '@aws/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](https://github.com/calvinmetcalf/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](http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_WriteRequest.html#DDB-Type-WriteRequest-DeleteRequest).

```typescript
import { BatchWrite } from '@aws/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 BatchGet(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.

---
_Source: https://npm.io/package/@aws/dynamodb-batch-iterator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
