Skip to main content

dynamo_table/
lib.rs

1//! # DynamoDB Table Abstraction
2//!
3//! A high-level, type-safe DynamoDB table abstraction for Rust with support for:
4//! - Batch operations (get, write, delete)
5//! - Pagination and streaming
6//! - Global Secondary Indexes (GSI)
7//! - Conditional expressions
8//! - Optimistic locking
9//! - Automatic retry with exponential backoff
10//!
11//! ## Features
12//!
13//! - **Type-safe**: Leverage Rust's type system with `serde` for automatic serialization
14//! - **Async-first**: Built on `tokio` and `aws-sdk-dynamodb`
15//! - **Batch operations**: Efficiently process multiple items with automatic batching
16//! - **Streaming**: Handle large result sets with async streams
17//! - **Reserved word validation**: Debug-mode checks for DynamoDB reserved words
18//! - **GSI support**: Query and scan Global Secondary Indexes
19//!
20//! ## Quick Start
21//!
22//! ```rust,no_run
23//! use dynamo_table::{DynamoTable, Error};
24//! use serde::{Deserialize, Serialize};
25//!
26//! #[derive(Debug, Clone, Serialize, Deserialize)]
27//! struct User {
28//!     user_id: String,
29//!     email: String,
30//!     name: String,
31//! }
32//!
33//! impl DynamoTable for User {
34//!     type PK = String;
35//!     type SK = String;
36//!
37//!     const TABLE: &'static str = "users";
38//!     const PARTITION_KEY: &'static str = "user_id";
39//!     const SORT_KEY: Option<&'static str> = None;
40//!
41//!     fn partition_key(&self) -> Self::PK {
42//!         self.user_id.clone()
43//!     }
44//! }
45//!
46//! #[tokio::main]
47//! async fn main() -> Result<(), Error> {
48//!     // Initialize the global DynamoDB client
49//!     let config = aws_config::defaults(aws_config::BehaviorVersion::latest()).load().await;
50//!     dynamo_table::init(&config).await;
51//!
52//!     // Put an item
53//!     let user = User {
54//!         user_id: "123".to_string(),
55//!         email: "user@example.com".to_string(),
56//!         name: "John Doe".to_string(),
57//!     };
58//!     user.add_item().await?;
59//!
60//!     // Get an item
61//!     let retrieved = User::get_item(&"123".to_string(), None).await?;
62//!
63//!     // Query items
64//!     let result = User::query_items(&"123".to_string(), None, None, None).await?;
65//!
66//!     Ok(())
67//! }
68//! ```
69#![deny(
70    warnings,
71    bad_style,
72    dead_code,
73    improper_ctypes,
74    non_shorthand_field_patterns,
75    no_mangle_generic_items,
76    overflowing_literals,
77    path_statements,
78    patterns_in_fns_without_body,
79    unconditional_recursion,
80    unused,
81    unused_allocation,
82    unused_comparisons,
83    unused_parens,
84    while_true,
85    missing_debug_implementations,
86    missing_docs,
87    trivial_casts,
88    trivial_numeric_casts,
89    unreachable_pub,
90    unused_extern_crates,
91    unused_import_braces,
92    unused_qualifications,
93    unused_results,
94    deprecated,
95    unknown_lints,
96    unreachable_code,
97    unused_mut
98)]
99
100mod error;
101pub use error::Error;
102
103/// Generic table module
104pub mod table;
105
106/// Methods of Generic table
107pub mod methods;
108
109/// Table setup utilities for testing
110pub mod setup;
111
112// Re-export main types for convenience
113pub use methods::DynamoTableMethods;
114pub use table::{CompositeKey, DynamoTable, GSITable};
115
116// Re-export aws-config types for configuration
117pub use aws_config::{
118    BehaviorVersion, Region, SdkConfig, defaults,
119    meta::region::{ProvideRegion, RegionProviderChain},
120    retry::{RetryConfig, RetryMode},
121    timeout::TimeoutConfig,
122};
123
124// Re-export aws-types for advanced configuration
125pub use aws_types::sdk_config::Builder as SdkConfigBuilder;
126
127use aws_sdk_dynamodb::Client as DynamoDbClient;
128use tokio::sync::OnceCell;
129
130/// Global DynamoDB client instance
131static GLOBAL_CLIENT: OnceCell<DynamoDbClient> = OnceCell::const_new();
132
133/// Initialize the global DynamoDB client with default sensible settings
134///
135/// This is called automatically by `dynamodb_client()` if not already initialized.
136/// It configures:
137/// - Adaptive retry mode with 3 max attempts
138/// - Exponential backoff starting at 1 second
139/// - Connect timeout: 3 seconds
140/// - Read timeout: 20 seconds
141/// - Operation timeout: 60 seconds
142/// - LocalStack support via AWS_PROFILE=localstack
143///
144/// Note: This function is internal. Use `init()` or `init_with_client()` for
145/// custom configuration, or let `dynamodb_client()` auto-initialize with defaults.
146async fn aws_config_defaults() -> SdkConfig {
147    use aws_config::BehaviorVersion;
148    use aws_types::sdk_config::{RetryConfig, TimeoutConfig};
149    use std::time::Duration;
150
151    let timeout_config = TimeoutConfig::builder()
152        .connect_timeout(Duration::from_secs(3))
153        .read_timeout(Duration::from_secs(20))
154        .operation_timeout(Duration::from_secs(60))
155        .build();
156
157    let mut loader = defaults(BehaviorVersion::latest())
158        .retry_config(
159            RetryConfig::adaptive()
160                .with_max_attempts(3)
161                .with_initial_backoff(Duration::from_secs(1)),
162        )
163        .timeout_config(timeout_config);
164
165    // Support LocalStack via AWS_PROFILE=localstack
166    if std::env::var("AWS_PROFILE").unwrap_or_default() == "localstack" {
167        loader = loader.endpoint_url("http://127.0.0.1:4566");
168    }
169
170    loader.load().await
171}
172
173/// Initialize the global DynamoDB client with a custom AWS config
174///
175/// Use this when you need custom AWS configuration beyond the defaults.
176///
177/// # Example
178///
179/// ```rust,no_run
180/// #[tokio::main]
181/// async fn main() {
182///     let config = aws_config::defaults(aws_config::BehaviorVersion::latest())
183///         .region(aws_config::Region::new("us-west-2"))
184///         .load()
185///         .await;
186///     dynamo_table::init(&config).await;
187///
188///     // Now you can use table operations
189/// }
190/// ```
191pub async fn init(config: &SdkConfig) {
192    let _ = GLOBAL_CLIENT
193        .get_or_init(|| async { DynamoDbClient::new(config) })
194        .await;
195}
196
197/// Initialize the global DynamoDB client with a custom client instance
198///
199/// Useful for testing or when you need fine-grained control over client configuration.
200///
201/// # Example
202///
203/// ```rust,no_run
204/// use aws_sdk_dynamodb::Client;
205///
206/// #[tokio::main]
207/// async fn main() {
208///     let config = aws_config::load_from_env().await;
209///     let client = Client::new(&config);
210///     dynamo_table::init_with_client(client).await;
211/// }
212/// ```
213pub async fn init_with_client(client: DynamoDbClient) {
214    let _ = GLOBAL_CLIENT.get_or_init(|| async { client }).await;
215}
216
217/// Get a reference to the global DynamoDB client
218///
219/// Automatically initializes the client with sensible defaults if not already initialized.
220/// For custom configuration, call [`init`] or [`init_with_client`] before using this function.
221///
222/// # Auto-Initialization
223///
224/// If not explicitly initialized, this function will automatically configure:
225/// - Adaptive retry mode with 3 max attempts
226/// - Exponential backoff starting at 1 second
227/// - Connect timeout: 3 seconds
228/// - Read timeout: 20 seconds
229/// - Operation timeout: 60 seconds
230/// - LocalStack support via AWS_PROFILE=localstack
231///
232/// # Example
233///
234/// ```rust,no_run
235/// # async fn example() {
236/// // Client auto-initializes with defaults on first use
237/// let client = dynamo_table::dynamodb_client().await;
238/// // Use client for custom operations
239/// # }
240/// ```
241///
242/// # Custom Configuration Example
243///
244/// ```rust,no_run
245/// # async fn example() {
246/// // Initialize with custom config before first use
247/// let config = dynamo_table::defaults(dynamo_table::BehaviorVersion::latest())
248///     .region(dynamo_table::Region::new("us-west-2"))
249///     .load()
250///     .await;
251/// dynamo_table::init(&config).await;
252///
253/// // Now uses custom configuration
254/// let client = dynamo_table::dynamodb_client().await;
255/// # }
256/// ```
257pub async fn dynamodb_client() -> &'static DynamoDbClient {
258    // Safe to unwrap because init_defaults() always sets it
259    GLOBAL_CLIENT
260        .get_or_init(|| async {
261            let config = aws_config_defaults().await;
262            DynamoDbClient::new(&config)
263        })
264        .await
265}
266
267#[cfg(debug_assertions)]
268pub(crate) fn assert_not_reserved_key(key: &str) {
269    // https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ReservedWords.html
270    #[rustfmt::skip]
271    const KEYS: [&str; 573] = [
272"abort", "absolute", "action", "add", "after", "agent", "aggregate", "all", "allocate", "alter", "analyze", "and", "any", "archive", "are", "array", "as", "asc", "ascii", "asensitive", "assertion", "asymmetric", "at", "atomic", "attach", "attribute", "auth", "authorization", "authorize", "auto", "avg", "back", "backup", "base", "batch", "before", "begin", "between", "bigint", "binary", "bit", "blob", "block", "boolean", "both", "breadth", "bucket", "bulk", "by", "byte", "call", "called", "calling", "capacity", "cascade", "cascaded", "case", "cast", "catalog", "char", "character", "check", "class", "clob", "close", "cluster", "clustered", "clustering", "clusters", "coalesce", "collate", "collation", "collection", "column", "columns", "combine", "comment", "commit", "compact", "compile", "compress", "condition", "conflict", "connect", "connection", "consistency", "consistent", "constraint", "constraints", "constructor", "consumed", "continue", "convert", "copy", "corresponding", "count", "counter", "create", "cross", "cube", "current", "cursor", "cycle", "data", "database", "date", "datetime", "day", "deallocate", "dec", "decimal", "declare", "default", "deferrable", "deferred", "define", "defined", "definition", "delete", "delimited", "depth", "deref", "desc", "describe", "descriptor", "detach", "deterministic", "diagnostics", "directories", "disable", "disconnect", "distinct", "distribute", "do", "domain", "double", "drop", "dump", "duration", "dynamic", "each", "element", "else", "elseif", "empty", "enable", "end", "equal", "equals", "error", "escape", "escaped", "eval", "evaluate", "exceeded", "except", "exception", "exceptions", "exclusive", "exec", "execute", "exists", "exit", "explain", "explode", "export", "expression", "extended", "external", "extract", "fail", "false", "family", "fetch", "fields", "file", "filter", "filtering", "final", "finish", "first", "fixed", "flattern", "float", "for", "force", "foreign", "format", "forward", "found", "free", "from", "full", "function", "functions", "general", "generate", "get", "glob", "global", "go", "goto", "grant", "greater", "group", "grouping", "handler", "hash", "have", "having", "heap", "hidden", "hold", "hour", "identified", "identity", "if", "ignore", "immediate", "import", "in", "including", "inclusive", "increment", "incremental", "index", "indexed", "indexes", "indicator", "infinite", "initially", "inline", "inner", "innter", "inout", "input", "insensitive", "insert", "instead", "int", "integer", "intersect", "interval", "into", "invalidate", "is", "isolation", "item", "items", "iterate", "join", "key", "keys", "lag", "language", "large", "last", "lateral", "lead", "leading", "leave", "left", "length", "less", "level", "like", "limit", "limited", "lines", "list", "load", "local", "localtime", "localtimestamp", "location", "locator", "lock", "locks", "log", "loged", "long", "loop", "lower", "map", "match", "materialized", "max", "maxlen", "member", "merge", "method", "metrics", "min", "minus", "minute", "missing", "mod", "mode", "modifies", "modify", "module", "month", "multi", "multiset", "name", "names", "national", "natural", "nchar", "nclob", "new", "next", "no", "none", "not", "null", "nullif", "number", "numeric", "object", "of", "offline", "offset", "old", "on", "online", "only", "opaque", "open", "operator", "option", "or", "order", "ordinality", "other", "others", "out", "outer", "output", "over", "overlaps", "override", "owner", "pad", "parallel", "parameter", "parameters", "partial", "partition", "partitioned", "partitions", "path", "percent", "percentile", "permission", "permissions", "pipe", "pipelined", "plan", "pool", "position", "precision", "prepare", "preserve", "primary", "prior", "private", "privileges", "procedure", "processed", "project", "projection", "property", "provisioning", "public", "put", "query", "quit", "quorum", "raise", "random", "range", "rank", "raw", "read", "reads", "real", "rebuild", "record", "recursive", "reduce", "ref", "reference", "references", "referencing", "regexp", "region", "reindex", "relative", "release", "remainder", "rename", "repeat", "replace", "request", "reset", "resignal", "resource", "response", "restore", "restrict", "result", "return", "returning", "returns", "reverse", "revoke", "right", "role", "roles", "rollback", "rollup", "routine", "row", "rows", "rule", "rules", "sample", "satisfies", "save", "savepoint", "scan", "schema", "scope", "scroll", "search", "second", "section", "segment", "segments", "select", "self", "semi", "sensitive", "separate", "sequence", "serializable", "session", "set", "sets", "shard", "share", "shared", "short", "show", "signal", "similar", "size", "skewed", "smallint", "snapshot", "some", "source", "space", "spaces", "sparse", "specific", "specifictype", "split", "sql", "sqlcode", "sqlerror", "sqlexception", "sqlstate", "sqlwarning", "start", "state", "static", "status", "storage", "store", "stored", "stream", "string", "struct", "style", "sub", "submultiset", "subpartition", "substring", "subtype", "sum", "super", "symmetric", "synonym", "system", "table", "tablesample", "temp", "temporary", "terminated", "text", "than", "then", "throughput", "time", "timestamp", "timezone", "tinyint", "to", "token", "total", "touch", "trailing", "transaction", "transform", "translate", "translation", "treat", "trigger", "trim", "true", "truncate", "ttl", "tuple", "type", "under", "undo", "union", "unique", "unit", "unknown", "unlogged", "unnest", "unprocessed", "unsigned", "until", "update", "upper", "url", "usage", "use", "user", "users", "using", "uuid", "vacuum", "value", "valued", "values", "varchar", "variable", "variance", "varint", "varying", "view", "views", "virtual", "void", "wait", "when", "whenever", "where", "while", "window", "with", "within", "without", "work", "wrapped", "write", "year", "zone "
273];
274
275    debug_assert!(!KEYS.contains(&key), "Reserved key: {key}");
276}
277
278#[cfg(test)]
279mod tests {
280    use super::*;
281
282    #[test]
283    fn test_composite_key_tuple() {
284        let key: CompositeKey<String, String> =
285            ("user123".to_string(), Some("order456".to_string()));
286        assert_eq!(key.0, "user123");
287        assert_eq!(key.1, Some("order456".to_string()));
288    }
289
290    #[test]
291    fn test_composite_key_no_sort_key() {
292        let key: CompositeKey<String, String> = ("user123".to_string(), None);
293        assert_eq!(key.0, "user123");
294        assert_eq!(key.1, None);
295    }
296
297    #[cfg(debug_assertions)]
298    #[test]
299    #[should_panic(expected = "Reserved key: user")]
300    fn test_assert_reserved_key_panics() {
301        assert_not_reserved_key("user");
302    }
303
304    #[cfg(debug_assertions)]
305    #[test]
306    fn test_assert_not_reserved_key_ok() {
307        assert_not_reserved_key("user_id");
308        assert_not_reserved_key("custom_field");
309    }
310}