util-dynamodb-command-input
TypeScript icon, indicating that this package has built-in type declarations

1.1.2 • Public • Published

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-input

Functions

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. Uses marshall from @aws-sdk/util-dynamodb under the hood.
  • ExpressionAttributeNames: Aliases for reserved words, if any. AttributeValue is 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 build

Run tests with jest:

npm run test

Peer dependencies

@aws-sdk/client-dynamodb
@aws-sdk/util-dynamodb

It 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.

Package Sidebar

Install

npm i util-dynamodb-command-input

Weekly Downloads

1

Version

1.1.2

License

ISC

Unpacked Size

94.4 kB

Total Files

15

Last publish

Collaborators

  • bobo-ross