util-dynamodb-command-input v1.1.2
Utility functions for @aws-sdk/client-dynamodb commands
A common DynamoDB error stems from field names being reserved words. One may run into this when specifying a ProjectionExpression for getting/scanning/querying items, or UpdateExpression and ExpressionAttributeValues for updating items. These utility functions detect those reserved words, generate aliases, and format command input accordingly. Formatting, mapping, and aliasing are handled by these utilities for your convenience.
Installation
npm install util-dynamodb-command-inputFunctions
toProjectionInput(dynamoDbFields: string[]) => {
ProjectionExpression: string
ExpressionAttributeNames?: { [key: string]: string }
}Specify DynamoDB item fields that you want to get from a command.
toUpdateItemInputSET(updates: { [dynamoDbField: string]: any }) => {
UpdateExpression: string
ExpressionAttributeValues: { [key: string]: AttributeValue }
ExpressionAttributeNames?: { [key: string]: string }
}Translate a JavaScript Object of field upadates to the following UpdateItemCommandInput properties:
UpdateExpression: Update values are mapped to this comma separated string (more about update expressions).ExpressionAttributeValues: Marshalled update values. Usesmarshallfrom@aws-sdk/util-dynamodbunder the hood.ExpressionAttributeNames: Aliases for reserved words, if any.AttributeValueis imported from@aws-sdk/client-dynamodb
Usage
import { toProjectionInput, toUpdateItemInputSET } from 'util-dynamodb-command-input'
const queryResponse = await dynamoDBClient.send(new QueryCommand({
...queryCommandInput,
...toProjectionInput(['foo']),
}))
const updateResponse = await dynamoDBClient.send(new UpdateItemCommand({
...updateItemCommandInput,
...toUpdateItemInputSET({ foo: 'baz' }),
}))Development
Build with esbuild:
npm run buildRun tests with jest:
npm run testPeer dependencies
@aws-sdk/client-dynamodb
@aws-sdk/util-dynamodbIt is assumed that most projects using this already have the above dependencies. Kindly npm install --save them, if not.
TODOs
- Utilities for
REMOVE,ADD,DELETE. - Utilities for other command inputs that need formatting and alias mapping.