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 Parameter | Default type | Description |
---|---|---|
TItem | Record <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
new DynamoDbRepository<TItem, TKey>(client, options): DynamoDbRepository<TItem, TKey>;
Parameters
Parameter | Type | Description |
---|---|---|
client | DynamoDBDocumentClient | The DynamoDBDocumentClient from @aws-sdk/lib-dynamodb . |
options | DynamoDbRepositoryOptions <TItem > | Additional options for this repository. |
Returns
DynamoDbRepository
<TItem
, TKey
>
Methods
deleteItemById()
deleteItemById(key): Promise<void>;
Delete an item by its key using a DeleteCommand
.
Parameters
Parameter | Type | Description |
---|---|---|
key | TKey | The key of the item to delete. |
Returns
Promise
<void
>
Example
await booksRepository.deleteItemById({ isbn: "9789027439642" });
getAllItems()
getAllItems(tableName, scanOptions?): Promise<TItem[]>;
Parameters
Parameter | Type | Description |
---|---|---|
tableName | string | The table name of the DynamoDB table. |
scanOptions? | ScanOptions | Additional scan options. |
Returns
Promise
<TItem
[]>
Deprecated
use DynamoDbRepository#scanAllItems instead.
getItem()
getItem(key, tableName): Promise<undefined | TItem>;
Parameters
Parameter | Type | Description |
---|---|---|
key | TKey | The key of the item to retrieve. |
tableName | string | The table name of the DynamoDB table. |
Returns
Promise
<undefined
| TItem
>
Deprecated
use DynamoDbRepository#getItemById instead.
getItemById()
getItemById(key): Promise<undefined | TItem>;
Finds an item by its key using a GetCommand
.
Parameters
Parameter | Type | Description |
---|---|---|
key | TKey | The key of the item to delete. |
Returns
Promise
<undefined
| TItem
>
itemExists()
itemExists(key, tableName): Promise<boolean>;
Parameters
Parameter | Type | Description |
---|---|---|
key | TKey | The key of the item to check. |
tableName | string | The table name of the DynamoDB table. |
Returns
Promise
<boolean
>
Deprecated
use DynamoDbRepository#itemExistsById instead.
itemExistsById()
itemExistsById(key): Promise<boolean>;
Returns whether an item exists by its key using a GetCommand
.
Parameters
Parameter | Type | Description |
---|---|---|
key | TKey | The key of the item to check. |
Returns
Promise
<boolean
>
putItem()
Call Signature
putItem(item, options?): Promise<void>;
Saves an item using a PutCommand
.
Parameters
Parameter | Type | Description |
---|---|---|
item | TItem | The item to put. |
options? | PutOptions | Additional options for putting the item. |
Returns
Promise
<void
>
Call Signature
putItem(item, tableName): Promise<void>;
Saves an item using a PutCommand
.
Parameters
Parameter | Type | Description |
---|---|---|
item | TItem | The item to put. |
tableName | string | The table name of the DynamoDB table. |
Returns
Promise
<void
>
Deprecated
use DynamoDbRepository#putItem instead.
queryItems()
queryItems(queryOptions): Promise<TItem[]>;
Returns specific items using a QueryCommand
.
Parameters
Parameter | Type |
---|---|
queryOptions | QueryOptions |
Returns
Promise
<TItem
[]>
Example
const books = await booksRepository.queryItems({
KeyConditionExpression: "genre = :genre",
ExpressionAttributeValues: {
":genre": "FANTASY",
},
IndexName: "GenreIndex",
});
queryTable()
queryTable(
keyConditionExpression,
indexName?,
expressionAttributeValues?,
filterExpression?,
limit?,
scanIndexForward?): Promise<TItem[]>;
Parameters
Parameter | Type | Default value |
---|---|---|
keyConditionExpression | string | undefined |
indexName? | string | undefined |
expressionAttributeValues? | Record <string , any > | undefined |
filterExpression? | string | undefined |
limit? | number | undefined |
scanIndexForward? | boolean | true |
Returns
Promise
<TItem
[]>
Deprecated
use DynamoDbRepository#queryItems instead.
removeItem()
removeItem(key, tableName): Promise<void>;
Parameters
Parameter | Type | Description |
---|---|---|
key | TKey | The key of the item to delete. |
tableName | string | The table name of the DynamoDB table. |
Returns
Promise
<void
>
Deprecated
use DynamoDbRepository#deleteItemById instead.
scanAllItems()
scanAllItems(options?): Promise<TItem[]>;
Returns all items using a ScanCommand
.
Parameters
Parameter | Type | Description |
---|---|---|
options? | ScanOptions | Additional scan options. |
Returns
Promise
<TItem
[]>
updateItem()
updateItem(options): Promise<void>;
Updates an item using a UpdateCommand
.
Parameters
Parameter | Type |
---|---|
options | UpdateOptions |
Returns
Promise
<void
>
Example
await booksRepository.updateItem({
Key: { id: myId },
UpdateExpression: 'set foo = :foo',
ExpressionAttributeValues: {
':foo': foo,
},
});