use std::{sync::LazyLock, time::Duration}; use surrealdb::{ engine::remote::ws::{Client, Ws}, Surreal, }; use tokio::time::sleep; use crate::package::Package; static DB: LazyLock> = LazyLock::new(Surreal::init); pub async fn establish_connection() -> Result<(), surrealdb::Error> { DB.connect::("localhost:8000").await } pub async fn is_alive() -> bool { tokio::select! { db_result = DB.health() => { match db_result { Ok(_) => true, Err(_) => false, } }, _ = sleep(Duration::from_secs(1)) => false } } pub async fn create_package(package: Package) -> Option { DB.create::>(package.get_name()) .content(package) .await .unwrap() }