Created
December 8, 2023 16:59
-
-
Save nwisemanII/7681b9959a5264c1673774e8ee3bcafc to your computer and use it in GitHub Desktop.
No memory?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use once_cell::sync::Lazy; | |
use crate::{ instrument, debug, Error, events::WebhookEvent }; | |
use surrealdb::engine::remote::ws::{Ws, Client}; | |
use surrealdb::opt::auth::Root; | |
use surrealdb::sql::Thing; | |
use surrealdb::Surreal; | |
const DB_ADDRESS: &'static str = "ws://127.0.0.1:8000"; | |
pub static DB: Lazy<Surreal<Client>> = Lazy::new(Surreal::init); | |
#[derive(Debug)] | |
pub struct DbClient<'a> { | |
db_creds: (&'a str, &'a str), | |
connected: bool, | |
} | |
impl<'a> DbClient<'a> { | |
pub fn new(user: &'a str, pass: &'a str) -> Self { | |
DbClient::<'a> { db_creds: (user, pass), connected: false } | |
} | |
fn check_connection(&self) -> bool { | |
self.connected | |
} | |
fn now_connected(&mut self) { | |
debug!("Connected"); | |
self.connected = true; | |
} | |
pub async fn connect(&mut self, namespace: &str, database: &str) -> Result<(), Error> { | |
let username = self.db_creds.0; | |
let password = self.db_creds.1; | |
debug!("Connecting to database"); | |
let _ = DB.connect::<Ws>(DB_ADDRESS).await?; | |
DB.signin(Root { username, password } ).await?; | |
DB.use_ns(namespace).use_db(database).await?; | |
self.now_connected(); | |
Ok(()) | |
} | |
pub async fn signin(self) -> Self { | |
todo!("Signin to database") | |
} | |
#[instrument(target="rs_jira_webhooks::storage::DbClient")] | |
pub async fn write(&mut self, table: &str, content: WebhookEvent) -> Result<(), Error> { | |
println!("Writing to database"); | |
// if let true = self.check_connection() { | |
let table: Vec<Thing> = DB.create("something").await?; | |
let created: Vec<Thing> = DB.create("test").content("hibob").await?; | |
dbg!(created); | |
Ok(()) | |
// } else { | |
// Err(Error::Message("No connection to database".to_string())) | |
// } | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment