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) withserdetypes, raw SurrealQL with bound variables (query_json), andversion().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
surrealdbdependency enables the remote protocols only. - Each call has a timeout (
timeout_ms). A timed-out call returns an error. - Logs, error text and
Debugoutput 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 ofautumn.toml.
Structs§
- Surreal
Db - The SurrealDB handle. Clone it freely: clones share the connection.
- Surreal
DbCheck - Checks the SurrealDB connection with a
versionround trip. - Surreal
DbPlugin - Installs a
SurrealDbhandle in an Autumn app.
Enums§
- Error
Kind - The kind of a SurrealDB error.
- Surreal
DbError - An error from the plugin.
Constants§
- PLUGIN_
NAME - The plugin name in Autumn diagnostics.
Traits§
- Surreal
DbResult Ext - Adds
or_httptoResult<T, SurrealDbError>.