API reference / @backpack/amazon-dynamodb
@backpack/amazon-dynamodb
️ 🗃
A lightweight utility package for working with Amazon DynamoDB.
Installing
First, ensure you have set up Nexus as private registry needed to use Backpack.
Then, install the package using npm:
bash
npm install @backpack/amazon-dynamodb
Make sure to also install the following AWS SDK libraries:
bash
npm install @aws-sdk/client-dynamodb @aws-sdk/lib-dynamodb
DynamoDbRepository
An implementation of the repository pattern, designed to abstract away direct DynamoDB interactions in your codebase.
Creating a repository
ts
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
import { DynamoDbRepository } from "@backpack/amazon-dynamodb";
const client = DynamoDBDocumentClient.from(new DynamoDBClient());
const booksRepository = new DynamoDbRepository<Book>(
client,
{
tableName: 'Books', // required
// optionally, pass your Powertools Logger instance:
logger: MyLogger,
// optionally, pass a mapper to add read/write mapping functions:
itemMapper: {
// e.g. use Zod to validate data coming from DynamoDB
readItem: (item) => Book.parse(item),
// you can also use Zod for writing the data,
// to strip unknown fields and/or to have another validation guard:
writeItem: (item) => Book.parse(item),
}
}
);
Usage patterns
You can use DynamoDbRepository
in multiple ways depending on your application structure:
- Direct usage: use the repository directly in your domain layer for simple use cases.
- Composition: wrap the repository in your own repository class to customize or limit access to certain methods.
- Inheritance: extend the repository to add custom queries or override default behavior.