Package Exports
- dynamodb-helpers
- dynamodb-helpers/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (dynamodb-helpers) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
dynamodb-helpers
Installation
npm install dynamodb-helpers @aws-sdk/client-dynamodb @aws-sdk/lib-dynamodb
[!NOTE]
@aws-sdk/client-dynamodb
and@aws-sdk/lib-dynamodb
are peer dependencies.
Setup
Create a file (I use lib/dynamodb.ts
). All the helper functions will be exported from this file. Paste the following code in the file:
export {
dynamodb,
getItem,
putItem,
queryStartsWith,
batchWriteAll,
queryAllItems,
scanAll,
batchGetItem,
updateItem,
client,
ddbDocClient,
} = createDynamoDBHelpers(dbConfig, translateConfig);
dbConfig
and translateConfig
come from the DyanmoDB Docs. Here is an example of how to set them up:
// lib/dynamodb.ts
import createDynamoDBHelpers from 'dynamodb-helpers';
const marshallOptions = {
// Whether to automatically convert empty strings, blobs, and sets to `null`.
convertEmptyValues: false, // false, by default.
// Whether to remove undefined values while marshalling.
removeUndefinedValues: true, // false, by default.
// Whether to convert typeof object to map attribute.
convertClassInstanceToMap: false, // false, by default.
};
const unmarshallOptions = {
// Whether to return numbers as a string instead of converting them to native JavaScript numbers.
wrapNumbers: false, // false, by default.
};
const translateConfig = { marshallOptions, unmarshallOptions };
export const dbConfig: DynamoDBClientConfig = {
region: process.env.REGION,
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!,
},
};
export const {
dynamodb,
getItem,
putItem,
queryStartsWith,
batchWriteAll,
queryAllItems,
scanAll,
batchGetItem,
updateItem,
client,
ddbDocClient,
} = createDynamoDBHelpers(dbConfig, translateConfig);
Now simply import a function to use it!
import { updateItem } from '@/lib/dynamodb';
await updateItem(process.env.TABLE_NAME!, { pk: '1', sk: 'John Doe' }, {
set: {
date: '2021-01-01',
name: 'John Doe',
},
setIfNotExists: {
age: 30,
dateOfBirth: '1990-01-01',
}
});
Types
Import types from the package like so:
import { UpdateItemParams } from 'dynamodb-helpers';