diff --git a/crates/artifact/src/methods.rs b/crates/artifact/src/methods.rs index 76683cf..204b61f 100644 --- a/crates/artifact/src/methods.rs +++ b/crates/artifact/src/methods.rs @@ -104,11 +104,7 @@ where Id: core_types::CoreId, T: serde::Serialize + for<'a> serde::Deserialize<'a> + Clone, { - let client = clients::surreal::SurrealRootClient::new() - .await - .wrap_err("Failed to create surreal client")?; - - client.use_ns("main").use_db("main").await?; + let client = clients::surreal::SurrealRootClient::new().await?; let pushed_artifact: Vec = client .create(Id::TABLE) @@ -129,11 +125,8 @@ where Id: core_types::CoreId, T: serde::Serialize + for<'a> serde::Deserialize<'a> + Clone, { - let client = clients::surreal::SurrealRootClient::new() - .await - .wrap_err("Failed to create surreal client")?; + let client = clients::surreal::SurrealRootClient::new().await?; - client.use_ns("main").use_db("main").await?; let artifact: Option = client .select(id) .await diff --git a/crates/auth/src/lib.rs b/crates/auth/src/lib.rs index 60b38b6..6c775d6 100644 --- a/crates/auth/src/lib.rs +++ b/crates/auth/src/lib.rs @@ -144,14 +144,9 @@ pub async fn build_auth_layer() -> Result< tower_sessions_surrealdb_store::SurrealSessionStore, >, > { - let session_store_surreal_client = - clients::surreal::SurrealRootClient::new().await?; - session_store_surreal_client - .use_ns("main") - .use_db("main") - .await?; + let surreal_client = clients::surreal::SurrealRootClient::new().await?; let session_store = tower_sessions_surrealdb_store::SurrealSessionStore::new( - session_store_surreal_client.into_inner(), + surreal_client.into_inner(), "user_session".to_string(), ); let session_manager_layer = diff --git a/crates/bl/src/fetch.rs b/crates/bl/src/fetch.rs index 0f0a576..db35375 100644 --- a/crates/bl/src/fetch.rs +++ b/crates/bl/src/fetch.rs @@ -14,14 +14,7 @@ pub async fn fetch_user_owned_photo_groups( use core_types::CoreId; async move { - let client = SurrealRootClient::new() - .await - .wrap_err("Failed to create surreal client")?; - client - .use_ns("main") - .use_db("main") - .await - .wrap_err("Failed to use surreal namespace/database")?; + let client = SurrealRootClient::new().await?; let mut result = client .query(format!( @@ -60,14 +53,7 @@ pub async fn fetch_photo_group( use crate::model_ext::ModelExt; async move { - let client = SurrealRootClient::new() - .await - .wrap_err("Failed to create surreal client")?; - client - .use_ns("main") - .use_db("main") - .await - .wrap_err("Failed to use surreal namespace/database")?; + let client = SurrealRootClient::new().await?; let group = core_types::PhotoGroup::fetch(photo_group_id, &client) .await @@ -97,14 +83,7 @@ pub async fn fetch_user( use crate::model_ext::ModelExt; async move { - let client = SurrealRootClient::new() - .await - .wrap_err("Failed to create surreal client")?; - client - .use_ns("main") - .use_db("main") - .await - .wrap_err("Failed to use surreal namespace/database")?; + let client = SurrealRootClient::new().await?; core_types::User::fetch(user_id, &client) .await @@ -136,11 +115,6 @@ pub async fn fetch_photo_thumbnail( async move { // prep the surreal client let client = SurrealRootClient::new().await?; - client - .use_ns("main") - .use_db("main") - .await - .wrap_err("Failed to start surreal client")?; let photo = core_types::Photo::fetch(photo_id, &client) .await diff --git a/crates/bl/src/upload/mod.rs b/crates/bl/src/upload/mod.rs index d95b330..4ad6998 100644 --- a/crates/bl/src/upload/mod.rs +++ b/crates/bl/src/upload/mod.rs @@ -43,7 +43,7 @@ pub async fn upload_photo_group( let Some(user) = leptos::use_context::().and_then(|u| u.0) else { - return Err(PhotoUploadError::Unauthenticated.into()); + Err(PhotoUploadError::Unauthenticated)? }; let (tx, mut rx) = mpsc::channel(params.photos.len()); @@ -103,17 +103,11 @@ pub async fn upload_photo_group( let client = clients::surreal::SurrealRootClient::new() .await - .map_err(|_| { - PhotoUploadError::InternalError( - "Failed to create surreal client".to_string(), - ) + .map_err(|e| { + let error = e.to_string(); + tracing::error!("Failed to create surreal client: {}", error); + PhotoUploadError::InternalError(error) })?; - client.use_ns("main").use_db("main").await.map_err(|e| { - PhotoUploadError::InternalError(format!( - "Failed to use surreal namespace/database: {}", - e - )) - })?; // create photo group group.create(&client).await.map_err(|e| { @@ -224,14 +218,7 @@ async fn create_photo(img: image::DynamicImage) -> Result { meta: Default::default(), }; - let client = clients::surreal::SurrealRootClient::new() - .await - .wrap_err("Failed to create surreal client")?; - client - .use_ns("main") - .use_db("main") - .await - .wrap_err("Failed to use surreal namespace/database")?; + let client = clients::surreal::SurrealRootClient::new().await?; photo .create(&client) diff --git a/crates/clients/src/surreal.rs b/crates/clients/src/surreal.rs index 4447a53..13d5bee 100644 --- a/crates/clients/src/surreal.rs +++ b/crates/clients/src/surreal.rs @@ -34,6 +34,7 @@ impl SurrealRootClient { let client = Self { client }; client.sign_in_as_root().await?; + client.use_main().await?; Ok(client) } @@ -55,6 +56,16 @@ impl SurrealRootClient { /// Consumes the client and returns the inner client. pub fn into_inner(self) -> surrealdb::Surreal { self.client } + + /// Uses the main namespace and database. + pub async fn use_main(&self) -> Result<()> { + self + .client + .use_ns("main") + .use_db("main") + .await + .wrap_err("Failed to use main namespace/database") + } } impl Deref for SurrealRootClient {