dynamo-plus
TypeScript icon, indicating that this package has built-in type declarations

2.0.2 • Public • Published

Dynamo Plus

npm version Types, Tests and Linting Issues

Extend and supercharge your DynamoDB DocumentClient with infinite batching.

Installation

npm i dynamo-plus
import { DynamoPlus } from 'dynamo-plus'
const dynamo = new DynamoPlus({
  region: 'eu-west-1',
})

const regularDynamoParams = {
  TableName: 'myTable',
  Key: {
    myKey: '1337'
  }
}
const data = await dynamo.get(regularDynamoParams)

Features

Optional return types

On methods that return documents you can have them explicitly typed from the get-go:

interface User {
  id: string
  name: string
  age: number
}
const user = await dynamo.get<User>({
  TableName: 'users',
  Key: { id: 'agent47' },
})
// user is now either User or undefined

const users = await dynamo.getAll<User>({
  TableName: 'users',
  Keys: [
    { id: 'eagle-1' },
    { id: 'pelican-1' }
  ],
})
// users is now Array<User>

const moreUsers = await dynamo.scanAll<User>({ TableName: 'users' })
// Array<User>

New method for performing batchGet requests in chunks

The built-in batchRead method can be used for fetching multiple documents by their primary keys, but it requires you to handle chunking and unprocessed items yourself. DynamoPlus adds a getAll method that does the heavy lifting for you.

getAll(params)

It's like batchGet, but with the simple syntax of get.

  • params Object
    • TableName
    • Keys - An array of key objects equivalent to Key in get().
    • BatchSize - Optional custom batch size. Defaults to 100 which the maximum permitted value by DynamoDB.

getAll() returns

const params = {
  TableName: 'users',
  Keys: [{ userId: '1' }, { userId: '2' }, /* ... */ { userId: '999' }]
}

const response = await dynamo.getAll(params)
// response now contains ALL documents, not just the first 100

New methods for performing batchWrite requests in chunks

batchWrite is neat for inserting multiple documents at once, but it requires you to handle chunking and unprocessed items yourself, while also using it's own somewhat unique syntax. We've added deleteAll() and putAll() to do the heavy lifting for you.

deleteAll(params)

batchWrite deletions, but with the simple syntax of delete.

  • params Object
    • TableName
    • Keys - An array of key objects equivalent to Key in delete().
    • BatchSize - Optional custom batch size. Defaults to 25 which the maximum permitted value by DynamoDB.

deleteAll() does not return any data once it resolves.

const params = {
  TableName: 'Woop woop!',
  Keys: [{ userId: '123' }, { userId: 'abc' }]
}
await dynamo.deleteAll(params)

putAll(params)

batchWrite upserts, but with the simple syntax of put.

  • params Object
    • TableName
    • Items - An array of documents equivalent to Item in put().
    • BatchSize - Optional custom batch size. Defaults to 25 which the maximum permitted value by DynamoDB.

putAll() does not return any data once it resolves.

const params = {
  TableName: 'Woop woop!',
  Items: [{ a: 'b' }, { c: 'd' }]
}
await dynamo.putAll(params)

New methods for query()

Query has new sibling methods that automatically paginate through resultsets for you.

queryAll(params[, pageSize])

Resolves with the entire array of matching items.

const params = {
  TableName : 'items',
  IndexName: 'articleNo-index',
  KeyConditionExpression: 'articleNo = :val',
  ExpressionAttributeValues: { ':val': articleNo }
}
const response = await dynamo.queryAll(params)
// response now contains ALL items with the articleNo, not just the first 1MB

queryIterator(params[, pageSize])

Like scanIterator, but for queries.


New methods for scan()

We've supercharged scan() for those times when you want to recurse through entire tables.

scanAll(params[, pageSize])

Resolves with the entire array of matching items.

const params = {
  TableName : 'MyTable',
  FilterExpression : 'Year = :this_year',
  ExpressionAttributeValues : {':this_year' : 2015}
}
const response = await documentClient.scanAll(params)
// response now contains ALL documents from 2015, not just the first 1MB

scanIterator(params[, pageSize[, parallelScanSegments]])

An async generator-driven approach to recursing your tables. This is a powerful tool when you have datasets that are too large to keep in memory all at once.

To spread out the workload across your table partitions you can define a number of parallelScanSegments. DynamoPlus will launch concurrent scans and yield results on the fly.

  • params - AWS.DynamoDB.DocumentClient.scan() parameters
  • pageSize - integer (Default: 100) How many items to retrieve per API call. (DynamoPlus automatically fetches all the pages regardless)
  • parallelScans - integer (Default: 1) Amount of segments to split the scan operation into. It also accepts an array of individual segment options such as LastEvaluatedKey, the length of the array then decides the amount of segments.
const usersIterator = dynamoPlus.scanIterator({ TableName: 'users' })
for await (const user of usersIterator) {
  await sendNewsletter(user.email)
}

FAQ

Can I interact with the DocumentClient directly?

Yes, its accessible via .client:

import { DescribeTableCommand } from '@aws-sdk/client-dynamodb'
import { DynamoPlus } from 'dynamo-plus'
const dynamo = new DynamoPlus()

const command = new DescribeTableCommand({ TableName: 'my-table' })
dynamo.client.send(command)

How do I upgrade from v1 to v2?

DynamoPlus 2.0.0 introduces a significant rewrite. Changes include:

  • Rewritten from the ground up using modern tools and syntax, natively producing correct type definitions

  • aws-sdk v2 has been replaced with @aws-sdk/* v3 and are now explicit dependencies

  • DynamoPlus is now a class that you instantiate with new, similar to the AWS clients

    const dynamo = new DynamoPlus({ region: 'eu-west-1' })
  • queryStream and queryStreamSync have been replaced with queryIterator

    const params = {
      TableName: 'users',
      IndexName: 'orgs-index',
      KeyConditionExpression: "organisationId = :val",
      ExpressionAttributeValues: { ":val": organisationId },
    }
    const queryResults = dynamo.queryIterator(params)
    for await (const user of queryResults) {
      console.log(user)
    }
  • scanStream and scanStreamSync have been replaced with scanIterator

    const params = {
      TableName: 'users',
    }
    const scanResults = dynamo.scanIterator(params)
    for await (const user of scanResults) {
      console.log(user)
    }

Package Sidebar

Install

npm i dynamo-plus

Weekly Downloads

10

Version

2.0.2

License

MIT

Unpacked Size

29.6 kB

Total Files

6

Last publish

Collaborators

  • sleavely