use serde::{Deserialize, Serialize}; use surrealdb::{ engine::remote::ws::{Client, Ws}, opt::auth::Root, sql::Thing, Result, Surreal, }; #[derive(Debug, Serialize, Deserialize)] pub struct Article { title: String, content: String, previous: Option, next: Option, date: Option, } #[derive(Debug, Deserialize)] pub struct ArticleMetadata { pub id: Thing, title: String, previous: Option, next: Option, date: Option, } #[derive(Debug, Deserialize)] struct Record { #[allow(dead_code)] id: Thing, } pub async fn connect() -> Result> { // Connect to the server let db = Surreal::new::("127.0.0.1:8080").await?; // Signin as a namespace, database, or root user db.signin(Root { username: "root", password: "root", }) .await?; // Select a specific namespace / database db.use_ns("test").use_db("test").await?; Ok(db) } pub async fn get_all_articles(connection: &Surreal) -> Result> { let mut response = connection .query("SELECT id, title, previous, next, date FROM article ORDER BY date DESC") .await?; let articles: Vec = response.take(0)?; Ok(articles) } pub async fn get_article(connection: &Surreal, id: String) -> Result<()> { let response: Vec
= connection.select(("article", id)).await?.expect("Hopefully something else has not gone terribly wrong"); dbg!(response); Ok(()) }