DynamoDB is an AWS database that stores key/value and document data.
The most common way to access DynamoDB data from Rust is to use rusoto_dynamodb's get_item, put_item, and related methods.
serde_dynamo provides a way to serialize and deserialize between data stored in these items and strongly-typed Rust data structures.
See the docs for more examples.
Items received from a rusoto_dynamodb call can be run through from_items
.
#[derive(Serialize, Deserialize)]
pub struct User {
id: String,
name: String,
age: u8,
};
// Get documents from DynamoDB
let input = ScanInput {
table_name: "users".to_string(),
..ScanInput::default()
};
let result = client.scan(input).await?;
// And deserialize them as strongly-typed data structures
if let Some(items) = result.items {
let users: Vec<User> = from_items(items)?;
println!("Got {} users", users.len());
}
Alternatively, to deserialize one item at a time, from_item
can be used.
for item in result.items.unwrap() {
let user: User = from_item(item)?;
println!("{} is {}", user.name, user.age);
}
Writing an entire data structure to DynamoDB typically involves using to_item
to serialize
it.
#[derive(Serialize, Deserialize)]
pub struct User {
id: String,
name: String,
age: u8,
};
// Create a user
let user = User {
id: "fSsgVtal8TpP".to_string(),
name: "Arthur Dent".to_string(),
age: 42,
};
// Turn it into an item that rusoto understands
let item = to_item(user)?;
// And write it!
let input = PutItemInput {
table_name: "users".to_string(),
item: item,
..PutItemInput::default()
};
client.put_item(input).await?;
serde_dynamodb is an effective library for serializing and deserializing data from rusoto_dynamodb.
However, serde_dynamodb is unable to handle some of the more advanced features of serde – especially features like flattening, adjacently tagged enums, and untagged enums – that we would like to use.
We opted to create a new library instead of making changes to serde_dynamodb because making the changes to support these features would cause serde_dynamodb to be backward-incompatible. Specifically, for certain cases, serde_dynamo and serde_dynamodb make different choices on how to serialize the exact same serializable structure.