Skip to content

API reference / @backpack/amazon-dynamodb / DynamoDbRepository

Class: DynamoDbRepository<TItem, TKey>

A repository for a DynamoDB table. Provides methods for getting, putting, deleting and querying items.

Type Parameters

Type ParameterDefault typeDescription
TItemRecord<string, unknown>The type of the persisted item.
TKey extends Partial<Record<keyof TItem, unknown>>Partial<Record<keyof TItem, unknown>>The type used as (composite) key for the item.

Constructors

Constructor

ts
new DynamoDbRepository<TItem, TKey>(client, options): DynamoDbRepository<TItem, TKey>;

Parameters

ParameterTypeDescription
clientDynamoDBDocumentClientThe DynamoDBDocumentClient from @aws-sdk/lib-dynamodb.
optionsDynamoDbRepositoryOptions<TItem>Additional options for this repository.

Returns

DynamoDbRepository<TItem, TKey>

Methods

deleteItemById()

ts
deleteItemById(key): Promise<void>;

Delete an item by its key using a DeleteCommand.

Parameters

ParameterTypeDescription
keyTKeyThe key of the item to delete.

Returns

Promise<void>

Example

ts
await booksRepository.deleteItemById({ isbn: "9789027439642" });

getAllItems()

ts
getAllItems(tableName, scanOptions?): Promise<TItem[]>;

Parameters

ParameterTypeDescription
tableNamestringThe table name of the DynamoDB table.
scanOptions?ScanOptionsAdditional scan options.

Returns

Promise<TItem[]>

Deprecated

use DynamoDbRepository#scanAllItems instead.


getItem()

ts
getItem(key, tableName): Promise<undefined | TItem>;

Parameters

ParameterTypeDescription
keyTKeyThe key of the item to retrieve.
tableNamestringThe table name of the DynamoDB table.

Returns

Promise<undefined | TItem>

Deprecated

use DynamoDbRepository#getItemById instead.


getItemById()

ts
getItemById(key): Promise<undefined | TItem>;

Finds an item by its key using a GetCommand.

Parameters

ParameterTypeDescription
keyTKeyThe key of the item to delete.

Returns

Promise<undefined | TItem>


itemExists()

ts
itemExists(key, tableName): Promise<boolean>;

Parameters

ParameterTypeDescription
keyTKeyThe key of the item to check.
tableNamestringThe table name of the DynamoDB table.

Returns

Promise<boolean>

Deprecated

use DynamoDbRepository#itemExistsById instead.


itemExistsById()

ts
itemExistsById(key): Promise<boolean>;

Returns whether an item exists by its key using a GetCommand.

Parameters

ParameterTypeDescription
keyTKeyThe key of the item to check.

Returns

Promise<boolean>


putItem()

Call Signature

ts
putItem(item, options?): Promise<void>;

Saves an item using a PutCommand.

Parameters
ParameterTypeDescription
itemTItemThe item to put.
options?PutOptionsAdditional options for putting the item.
Returns

Promise<void>

Call Signature

ts
putItem(item, tableName): Promise<void>;

Saves an item using a PutCommand.

Parameters
ParameterTypeDescription
itemTItemThe item to put.
tableNamestringThe table name of the DynamoDB table.
Returns

Promise<void>

Deprecated

use DynamoDbRepository#putItem instead.


queryItems()

ts
queryItems(queryOptions): Promise<TItem[]>;

Returns specific items using a QueryCommand.

Parameters

ParameterType
queryOptionsQueryOptions

Returns

Promise<TItem[]>

Example

ts
const books = await booksRepository.queryItems({
  KeyConditionExpression: "genre = :genre",
  ExpressionAttributeValues: {
    ":genre": "FANTASY",
  },
  IndexName: "GenreIndex",
});

queryTable()

ts
queryTable(
   keyConditionExpression, 
   indexName?, 
   expressionAttributeValues?, 
   filterExpression?, 
   limit?, 
scanIndexForward?): Promise<TItem[]>;

Parameters

ParameterTypeDefault value
keyConditionExpressionstringundefined
indexName?stringundefined
expressionAttributeValues?Record<string, any>undefined
filterExpression?stringundefined
limit?numberundefined
scanIndexForward?booleantrue

Returns

Promise<TItem[]>

Deprecated

use DynamoDbRepository#queryItems instead.


removeItem()

ts
removeItem(key, tableName): Promise<void>;

Parameters

ParameterTypeDescription
keyTKeyThe key of the item to delete.
tableNamestringThe table name of the DynamoDB table.

Returns

Promise<void>

Deprecated

use DynamoDbRepository#deleteItemById instead.


scanAllItems()

ts
scanAllItems(options?): Promise<TItem[]>;

Returns all items using a ScanCommand.

Parameters

ParameterTypeDescription
options?ScanOptionsAdditional scan options.

Returns

Promise<TItem[]>


updateItem()

ts
updateItem(options): Promise<void>;

Updates an item using a UpdateCommand.

Parameters

ParameterType
optionsUpdateOptions

Returns

Promise<void>

Example

ts
await booksRepository.updateItem({
  Key: { id: myId },
  UpdateExpression: 'set foo = :foo',
  ExpressionAttributeValues: {
    ':foo': foo,
  },
});