use leptos::*; use crate::models::queries::{{ query.output_type }}; #[server({{ endpoint_name }}, "/api")] pub async fn {{ function_name }}( {%- for param in query.params %} {{ param.name }}: {{ param.type_ }}, {%- if loop.last %} {% endif -%} {%- endfor -%} ) -> Result<{{ query.output_type }}, ServerFnError> { use surrealdb::{engine::remote::ws::Ws, Surreal, opt::auth::Root}; use crate::db::queries::{{ query.short_name }}::{{ query.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 = {{ query.name }}({{ [['&db'], query.params | map(attribute="name")] | flatten | join(", ") }}) .await .map_err(|_| ServerFnError::ServerError("Cannot apply query {{ query.short_name }}".to_string()))?; Ok(result) }