Skip to main content

Crate dynamo_table

Crate dynamo_table 

Source
Expand description

§DynamoDB Table Abstraction

A high-level, type-safe DynamoDB table abstraction for Rust with support for:

  • Batch operations (get, write, delete)
  • Pagination and streaming
  • Global Secondary Indexes (GSI)
  • Conditional expressions
  • Optimistic locking
  • Automatic retry with exponential backoff

§Features

  • Type-safe: Leverage Rust’s type system with serde for automatic serialization
  • Async-first: Built on tokio and aws-sdk-dynamodb
  • Batch operations: Efficiently process multiple items with automatic batching
  • Streaming: Handle large result sets with async streams
  • Reserved word validation: Debug-mode checks for DynamoDB reserved words
  • GSI support: Query and scan Global Secondary Indexes

§Quick Start

use dynamo_table::{DynamoTable, Error};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
struct User {
    user_id: String,
    email: String,
    name: String,
}

impl DynamoTable for User {
    type PK = String;
    type SK = String;

    const TABLE: &'static str = "users";
    const PARTITION_KEY: &'static str = "user_id";
    const SORT_KEY: Option<&'static str> = None;

    fn partition_key(&self) -> Self::PK {
        self.user_id.clone()
    }
}

#[tokio::main]
async fn main() -> Result<(), Error> {
    // Initialize the global DynamoDB client
    let config = aws_config::defaults(aws_config::BehaviorVersion::latest()).load().await;
    dynamo_table::init(&config).await;

    // Put an item
    let user = User {
        user_id: "123".to_string(),
        email: "user@example.com".to_string(),
        name: "John Doe".to_string(),
    };
    user.add_item().await?;

    // Get an item
    let retrieved = User::get_item(&"123".to_string(), None).await?;

    // Query items
    let result = User::query_items(&"123".to_string(), None, None, None).await?;

    Ok(())
}

Re-exports§

pub use methods::DynamoTableMethods;
pub use table::CompositeKey;
pub use table::DynamoTable;
pub use table::GSITable;

Modules§

methods
Methods of Generic table
setup
Table setup utilities for testing
table
Generic table module

Structs§

BehaviorVersion
Behavior version of the client
Region
The region to send requests to.
RegionProviderChain
Load a region by selecting the first from a series of region providers.
RetryConfig
Retry configuration for requests.
SdkConfig
AWS Shared Configuration
SdkConfigBuilder
Builder for AWS Shared Configuration
TimeoutConfig
Top-level configuration for timeouts

Enums§

Error
DynamoDB table operation error
RetryMode
Specifies how failed requests should be retried.

Traits§

ProvideRegion
Provide a Region to use with AWS requests

Functions§

defaults
Create a config loader with the defaults for the given behavior version.
dynamodb_client
Get a reference to the global DynamoDB client
init
Initialize the global DynamoDB client with a custom AWS config
init_with_client
Initialize the global DynamoDB client with a custom client instance