Function serde_dynamo::to_attribute_value
source · pub fn to_attribute_value<T, AV>(value: T) -> Result<AV>where
T: Serialize,
AV: From<AttributeValue>,
Expand description
Convert a T
into an AttributeValue
.
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?;