Available on crate feature rusoto_dynamodb+0_46 only.
Expand description

Support for rusoto_dynamodb version 0.46

Because rusoto_dynamodb has not yet reached version 1.0, a feature is required to enable support. Add the following to your dependencies.

[dependencies]
rusoto_core = { version = "0.46", default-features = false, features = ["rustls"] }
rusoto_dynamodb = { version = "0.46", default-features = false, features = ["rustls"] }
serde_dynamo = { version = "3", features = ["rusoto_dynamodb+0_46"] }

Parsing items as strongly-typed data structures.

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.

#[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
for item in result.items.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 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?;

Using to_attribute_value for more control

In some circumstances, building rusoto_dynamodb::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
let input = GetItemInput {
    table_name: "users".to_string(),
    key: key,
    ..GetItemInput::default()
};
client.get_item(input).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)?),
]);

// And execute the query
let input = QueryInput {
    table_name: "users".to_string(),
    index_name: Some("by_type_and_last_login".to_string()),
    key_condition_expression: Some("user_type = :user_type AND last_login > :last_login".to_string()),
    expression_attribute_values: Some(expression_attribute_values),
    ..QueryInput::default()
};
client.query(input).await?;

Functions

A version of crate::from_item where the AV generic is tied to rusoto_dynamodb::AttributeValue.
A version of crate::from_items where the AV generic is tied to rusoto_dynamodb::AttributeValue.
A version of crate::to_item where the AV generic is tied to rusoto_dynamodb::AttributeValue.