dynamodel
This library provides a derive macro to implement conversions between
your object and HashMap<String, AttributeValue>.
Derive macro Dynamodel
The Dynamodel derive macro implements these three traits to use
aws-sdk-dynamodb more comfortably.
Into<HashMap<String, AttributeValue>>TryFrom<HashMap<String, AttributeValue>>- The
AttributeValueConvertibletrait enables the types that implement it to be converted from and toAttributeValue.
#[derive(Dynamodel)] Convertible
struct YourStruct { ... } <===========> HashMap<String, AttributeValue>
#[derive(Dynamodel)] Convertible
enum YourEnum { ... } <===========> HashMap<String, AttributeValue>
Requirements to use Dynamodel
To use the Dynamodel macro, all types of your object's fields must implement
the AttributeValueConvertible trait.
By default, these types automatically implement the AttributeValueConvertible
trait, so no additional code is required when using these types.
| Type | AttributeValue variant |
|---|---|
String |
AttributeValue::S("...") |
u8, u16, u32, u64, u128, usizei8, i16, i32, i64, i128, isizef32, f64 |
AttributeValue::N("...") |
bool |
AttributeValue::Bool(...) |
Vec of any types that implement AttributeValueConvertible |
AttributeValue::L([...]) |
Any types that implement Dynamodel macro |
AttributeValue::M({ ... }) |
The last row of the above table shows that once you apply the Dynamodel macro to your object,
it also implements the AttributeValueConvertible trait for your object.
So, you can create nested structures of objects that apply the Dynamodel macro.
If you want to use additional types, you need to implement the AttributeValueConvertible
trait for your type.
Usage
use Dynamodel;
use HashMap;
use AttributeValue;
let person = Person ;
let item: = .into;
// Convert from Person into HashMap<String, AttributeValue>.
let converted: = person.clone.into;
assert_eq!;
// Convert from HashMap<String, AttributeValue> into Person.
// This conversion uses std::convert::TryFrom trait, so this returns a Result.
let converted: Person = item.try_into.unwrap;
assert_eq!;
Modifying the default behavior
Like the Serde crate, you can modify the default behavior
through attributes like this.
use ;
use HashMap;
use ;
// Vec<u8> is converted to AttributeValue::L by default,
// but this case, the `data` field is converted to AttributeValue::B.
The function definition must meet these conditions.
| Field attribute | Argument | Return |
|---|---|---|
#[dynamodel(into = "...")] |
field type |
AttributeValue |
#[dynamodel(try_from = "...")] |
&AttributeValue |
Result<field type, ConvertError> |
Example
Single-table design
The following diagram shows that both Video and VideoStats are stored in the same table.

And suppose you want to add a VideoComment object that is sortable by timestamp, like this.

More features
For more features, refer to this wiki.
License
This software is released under the MIT License.