dynamo-plus
npm install dynamo-plus  
> Extend and supercharge your DynamoDB DocumentClient with infinite batching.
``shell`
npm i dynamo-plus
`js
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)
`
- Simplified stack traces that dont bury errors in AWS SDK internals
- Optionally define the expected return type of items when using Typescript
- get() returns the document or undefined
- New methods for batching unlimited amounts of data
- getAll(params)
- deleteAll(params)
- putAll(params)
- queryAll(params)
- [queryIterator(params[, pageSize])](#methods-queryiterator)
- scanAll(params)
- [scanIterator(params[, pageSize[, parallelScans]])](#methods-scaniterator)
On methods that return documents you can have them explicitly typed from the get-go:
`typescript
interface User {
id: string
name: string
age: number
}
const user = await dynamo.get
TableName: 'users',
Key: { id: 'agent47' },
})
// user is now either User or undefined
const users = await dynamo.getAll
TableName: 'users',
Keys: [
{ id: 'eagle-1' },
{ id: 'pelican-1' }
],
})
// users is now Array
const moreUsers = await dynamo.scanAll
// Array
`
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.
It's like _batchGet_, but with the simple syntax of _get_.
- params ObjectKey
- TableName
- Keys - An array of key objects equivalent to in _get()_.
- BatchSize - Optional custom batch size. Defaults to 100 which the maximum permitted value by DynamoDB.
_getAll()_ returns
`js
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
`
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.
_batchWrite_ deletions, but with the simple syntax of _delete_.
- params ObjectKey
- TableName
- Keys - An array of key objects equivalent to 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.
`js`
const params = {
TableName: 'Woop woop!',
Keys: [{ userId: '123' }, { userId: 'abc' }]
}
await dynamo.deleteAll(params)
---
_batchWrite_ upserts, but with the simple syntax of _put_.
- params ObjectItem
- TableName
- Items - An array of documents equivalent to 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.
`js`
const params = {
TableName: 'Woop woop!',
Items: [{ a: 'b' }, { c: 'd' }]
}
await dynamo.putAll(params)
---
Query has new sibling methods that automatically paginate through resultsets for you.
Resolves with the entire array of matching items.
- params - AWS.DynamoDB.DocumentClient.query() parameters
`js`
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
---
- params - AWS.DynamoDB.DocumentClient.query() parameters
Like scanIterator, but for queries.
---
We've supercharged _scan()_ for those times when you want to recurse through entire tables.
Resolves with the entire array of matching items.
- params - AWS.DynamoDB.DocumentClient.scan() parameters
`js`
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
---
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.
`js`
const usersIterator = dynamoPlus.scanIterator({ TableName: 'users' })
for await (const user of usersIterator) {
await sendNewsletter(user.email)
}
---
Yes, its accessible via .client:
`js
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)
`
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`
ts`
const dynamo = new DynamoPlus({ region: 'eu-west-1' })
* queryStream and queryStreamSync have been replaced with queryIterator`
js`
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`
js``
const params = {
TableName: 'users',
}
const scanResults = dynamo.scanIterator(params)
for await (const user of scanResults) {
console.log(user)
}