use leptos::*; use crate::models::mutations::{{ mutation.output_type }}; #[server({{ endpoint_name }}, "/api")] pub async fn {{ function_name }}( {%- for param in mutation.params %} {{ param.name }}: {{ param.type_ }}, {%- if loop.last %} {% endif -%} {%- endfor -%} ) -> Result<{{ mutation.output_type }}, ServerFnError> { use surrealdb::{engine::remote::ws::Ws, Surreal, opt::auth::Root}; use crate::db::mutations::{{ mutation.short_name }}::{{ mutation.name }}; let db = Surreal::new::("localhost:8000") .await .map_err(|_| ServerFnError::ServerError("Cannot open connection to SurrealDB".to_string()))?; db .signin(Root { username: "root", password: "root", }) .await .map_err(|_| ServerFnError::ServerError("Cannot signin to SurrealDB".to_string()))?; db .use_ns("test") .use_db("test") .await .map_err(|_| ServerFnError::ServerError("Cannot use namespace and database".to_string()))?; let result = {{ mutation.name }}({{ [['&db'], mutation.params | map(attribute="name")] | flatten | join(", ") }}) .await .map_err(|_| ServerFnError::ServerError("Cannot apply mutation {{ mutation.short_name }}".to_string()))?; Ok(result) }