Skip to main content

Crate autumn_plugin_surrealdb

Crate autumn_plugin_surrealdb 

Source
Expand description

Autumn plugin for SurrealDB.

Add SurrealDbPlugin to the app. Then use the SurrealDb extractor in a handler.

use autumn_plugin_surrealdb::{SurrealDb, SurrealDbPlugin, SurrealDbResultExt as _};
use autumn_web::prelude::*;

#[derive(serde::Deserialize, serde::Serialize)]
struct Profile {
    name: String,
}

#[get("/profiles/{id}")]
async fn profile(db: SurrealDb, Path(id): Path<String>) -> AutumnResult<Json<serde_json::Value>> {
    let profile = db
        .select_record("profile", &id)
        .await
        .or_http()?
        .map(Json)
        .ok_or_else(|| AutumnError::not_found_msg("no profile"));
    profile
}

autumn_web::app()
    .plugin(
        SurrealDbPlugin::new().configure(|c| {
            c.endpoint = "127.0.0.1:8000".into();
            c.namespace = "app".into();
            c.database = "app".into();
        }),
    )
    .routes(routes![profile])
    .run()
    .await;

The plugin reads [surrealdb] in autumn.toml. See config for the keys.

§What the plugin gives

  • SurrealDb: document CRUD (create_record, select_record, select_all, update_record, delete_record) with serde types, raw SurrealQL with bound variables (query_json), and version().
  • SurrealDbPlugin: connects at startup, signs in, selects the namespace and database, and puts the handle in the app state.
  • A readiness check that runs the version() query.

§Limits

  • The plugin talks to a SurrealDB server over the network. It never embeds a database: the surrealdb dependency enables the remote protocols only.
  • Each call has a timeout (timeout_ms). A timed-out call returns an error.
  • Logs, error text and Debug output never show the password or the token.

Re-exports§

pub use config::AuthScope;
pub use config::ConfigError;
pub use config::DEFAULT_SECTION;
pub use config::Protocol;
pub use config::SurrealDbConfig;
pub use surrealdb;

Modules§

config
The [surrealdb] section of autumn.toml.

Structs§

SurrealDb
The SurrealDB handle. Clone it freely: clones share the connection.
SurrealDbCheck
Checks the SurrealDB connection with a version round trip.
SurrealDbPlugin
Installs a SurrealDb handle in an Autumn app.

Enums§

ErrorKind
The kind of a SurrealDB error.
SurrealDbError
An error from the plugin.

Constants§

PLUGIN_NAME
The plugin name in Autumn diagnostics.

Traits§

SurrealDbResultExt
Adds or_http to Result<T, SurrealDbError>.