Module serde_dynamo::aws_sdk_dynamodb_0_14
source · Available on crate feature
aws-sdk-dynamodb+0_14
only.Expand description
Support for aws-sdk-dynamodb version 0.14
Because aws-sdk-dynamodb has not yet reached version 1.0, a feature is required to enable support. Add the following to your dependencies.
[dependencies]
aws-config = "0.44"
aws-sdk-dynamodb = "0.14"
serde_dynamo = { version = "4", features = ["aws-sdk-dynamodb+0_14"] }
§Parsing items as strongly-typed data structures.
Items received from a aws-sdk-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 result = client.scan().table_name("user").send().await?;
// And deserialize them as strongly-typed data structures
if let Some(items) = result.items().map(|slice| slice.to_vec()) {
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.
#[derive(Serialize, Deserialize)]
pub struct User {
id: String,
name: String,
age: u8,
};
// Get documents from DynamoDB
let result = client.scan().table_name("user").send().await?;
// And deserialize them as strongly-typed data structures
for item in result.items().map(|slice| slice.to_vec()).unwrap() {
let user: User = from_item(item)?;
println!("{} is {}", user.name, user.age);
}
§Creating items by serializing data structures
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 aws-sdk-dynamodb understands
let item = to_item(user)?;
// And write it!
client.put_item().table_name("users").set_item(Some(item)).send().await?;
§Using to_attribute_value for more control
In some circumstances, building aws_sdk_dynamodb::model::AttributeValues directly is required.
For example, when generating a key to supply to get_item.
use serde_dynamo::to_attribute_value;
// Create the unique key of the record in DynamoDB in a way rusoto understands
let key = HashMap::from([
(String::from("id"), to_attribute_value(&user.id)?),
]);
// And get the record
client.get_item().table_name("users").set_key(Some(key)).send().await?;
Or when generating attribute values in a query call.
use serde_dynamo::to_attribute_value;
// Declare all of the expression inputs for a query call
let expression_attribute_values = HashMap::from([
(String::from(":user_type"), to_attribute_value(user_type)?),
(String::from(":last_login"), to_attribute_value(yesterday)?),
]);
client.query()
.table_name("users")
.index_name("by_type_and_last_login")
.key_condition_expression("user_type = :user_type AND last_login > :last_login")
.set_expression_attribute_values(Some(expression_attribute_values))
.send()
.await?;
Functions§
- A version of
crate::from_attribute_value
where theAV
generic is tied toaws-sdk-dynamodb::model::AttributeValue
. - A version of
crate::from_item
where theAV
generic is tied toaws-sdk-dynamodb::model::AttributeValue
. - A version of
crate::from_items
where theAV
generic is tied toaws-sdk-dynamodb::model::AttributeValue
. - A version of
crate::to_attribute_value
where theAV
generic is tied toaws-sdk-dynamodb::model::AttributeValue
. - A version of
crate::to_item
where theAV
generic is tied toaws-sdk-dynamodb::model::AttributeValue
.