feat: ✨ seaorm
This commit is contained in:
parent
36c72cee4a
commit
40149b372d
20 changed files with 236 additions and 87 deletions
|
@ -1,46 +1,31 @@
|
|||
pub mod interaction;
|
||||
pub mod message;
|
||||
pub mod post;
|
||||
pub mod user;
|
||||
|
||||
pub type SurrealUserReturn = Result<Option<User>, surrealdb::Error>;
|
||||
pub type SurrealCountReturn = Result<Option<u128>, surrealdb::Error>;
|
||||
use std::time::Duration;
|
||||
|
||||
use std::{sync::LazyLock, time::Duration};
|
||||
|
||||
use surrealdb::{
|
||||
engine::remote::ws::{Client, Ws},
|
||||
opt::auth::Root,
|
||||
Surreal,
|
||||
};
|
||||
use sea_orm::{Database, DatabaseConnection};
|
||||
use tokio::time::sleep;
|
||||
|
||||
use crate::{feature::user::User, DatabaseConfig};
|
||||
use crate::DatabaseConfig;
|
||||
|
||||
static DB: LazyLock<Surreal<Client>> = LazyLock::new(Surreal::init);
|
||||
|
||||
pub async fn establish_connection() {
|
||||
pub async fn establish_connection() -> DatabaseConnection {
|
||||
let database_config = DatabaseConfig::default();
|
||||
|
||||
DB.connect::<Ws>(database_config.address).await.unwrap();
|
||||
DB.signin(Root {
|
||||
username: &database_config.username,
|
||||
password: &database_config.password,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
DB.use_ns(database_config.namespace).await.unwrap();
|
||||
DB.use_db(database_config.database).await.unwrap();
|
||||
DB.query("DEFINE INDEX email ON TABLE user FIELDS email UNIQUE;").await.unwrap();
|
||||
let connection_string = format!(
|
||||
"{}://{}:{}@{}/{}",
|
||||
database_config.backend,
|
||||
database_config.username,
|
||||
database_config.password,
|
||||
database_config.address,
|
||||
database_config.database
|
||||
);
|
||||
Database::connect(connection_string).await.unwrap()
|
||||
}
|
||||
|
||||
pub async fn is_alive() -> bool {
|
||||
tokio::select! {
|
||||
alive_status = DB.health() => {
|
||||
match alive_status {
|
||||
Ok(_) => true,
|
||||
Err(_) => false,
|
||||
}
|
||||
},
|
||||
_ = sleep(Duration::from_secs(1)) => false
|
||||
|
||||
_ = sleep(Duration::from_secs(1)) => false,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue