日本語

A database without SQL

A database read through an SDK, such as DynamoDB, is used the same way as one read with SQL: the client goes in the router's state, and the handler awaits its calls.

The client in the router's state

A database with an SDK in place of SQL takes the same shape as one with SQL: the client goes in the router's state and the handler awaits its calls. The sample below reads DynamoDB with aws-sdk-dynamodb, AWS's own crate, and needs these two crates:

Cargo.toml
[dependencies]
aws-config = "1.13"
aws-sdk-dynamodb = "1.134"
use aws_config::BehaviorVersion;
use aws_sdk_dynamodb::Client;
use aws_sdk_dynamodb::types::AttributeValue;

#[derive(Clone)]
struct AppState {
    dynamo: Client,
}

#[derive(Debug)]
struct DbError(String);

impl IntoResponse for DbError {
    fn into_response(self) -> Response {
        eprintln!("DynamoDB: {}", self.0);
        let message = "The list could not be read. Try again.";
        (StatusCode::INTERNAL_SERVER_ERROR, message).into_response()
    }
}

/// The titles of the todos under the partition key `list = "home"`.
async fn titles(state: &AppState) -> Result<Vec<String>, DbError> {
    let output = state
        .dynamo
        .query()
        .table_name("todos")
        .key_condition_expression("list = :list")
        .expression_attribute_values(":list", AttributeValue::S("home".to_owned()))
        .send()
        .await
        .map_err(|error| DbError(error.to_string()))?;
    let titles = output.items().iter().filter_map(|item| item.get("title")?.as_s().ok());
    Ok(titles.cloned().collect())
}

async fn index(state: AppState, (): ()) -> Result<Page<Todos>, DbError> {
    let titles = titles(&state).await?;
    let count = i32::try_from(titles.len()).unwrap_or(i32::MAX);
    let list = ul().children(titles.into_iter().map(|title| li().text(title)));
    Ok(Page::new("Todos", Todos { count }).child(list))
}

hypersig::routes! {
    fn pages(Router<AppState>);
    const INDEX: Route = "/" => index;
}

/// The region and the credentials come from the environment, as the AWS
/// CLI reads them.
async fn state() -> AppState {
    let config = aws_config::defaults(BehaviorVersion::latest()).load().await;
    AppState {
        dynamo: Client::new(&config),
    }
}

aws_config::defaults(..).load() reads the region and the credentials from the environment, as the AWS CLI does, so they stay on the server. A Client is Clone, and its clones share one client, so the state holds it without an Arc, and the server builds it once when it starts. A call that fails, such as one without credentials, returns an error that DbError logs and answers with a 500, as the SQL sample does.

What the types leave unchecked

The table name, the key condition, and the attribute names are strings that the compiler does not read. A wrong table name or condition fails when send runs, and the visitor sees the 500. An item without a title, or one whose title is not a string, is a value as_s refuses, and the sample's filter_map leaves it out of the list without a message. Where a missing attribute is a mistake to hear about, log it in place of skipping it.

Firestore

For Firestore, Google has published no crate for reading and writing documents as of October 2026, only google-cloud-firestore-admin-v1 for the admin API, and the community's firestore crate is the one in common use.

Next

  • Storing data covers the connection in the state, what a failed query answers, and updates across processes.
  • The database each host can reach covers the hosts that reach a database only over the network or through a binding.