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,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1 @@
|
|||
async fn create_interaction() {}
|
||||
async fn read_interaction() {}
|
||||
async fn update_interaction() {}
|
||||
async fn delete_interaction() {}
|
||||
async fn count_interactions_of_a_user() {}
|
||||
async fn count_interactions_of_a_post() {}
|
||||
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
|
|
@ -1,5 +1 @@
|
|||
async fn create_post() {}
|
||||
async fn read_post() {}
|
||||
async fn update_post() {}
|
||||
async fn delete_post() {}
|
||||
async fn count_posts_of_a_user() {}
|
||||
|
||||
|
|
|
@ -1,29 +1 @@
|
|||
|
||||
use crate::feature::user::User;
|
||||
|
||||
use super::{SurrealCountReturn, SurrealUserReturn, DB};
|
||||
|
||||
|
||||
pub async fn create_user(user:User) -> SurrealUserReturn {
|
||||
DB.create("user").content(user).await
|
||||
}
|
||||
|
||||
pub async fn read_user(email: &String) -> SurrealUserReturn{
|
||||
DB.select(("user", email)).await
|
||||
}
|
||||
|
||||
pub async fn update_user(target_user_email: &String, user: User) -> SurrealUserReturn{
|
||||
DB.update(("user", target_user_email)).content(user).await
|
||||
}
|
||||
pub async fn delete_user(email: &String) -> SurrealUserReturn {
|
||||
DB.delete(("user", email)).await
|
||||
}
|
||||
pub async fn count_users() -> SurrealCountReturn{
|
||||
DB.query("SELECT count() FROM user GROUP BY count;").await?.take("count")
|
||||
}
|
||||
pub async fn count_male_users() -> SurrealCountReturn{
|
||||
DB.query("SELECT count() FROM user WHERE gender = true GROUP BY count;").await?.take("count")
|
||||
}
|
||||
pub async fn count_female_users() -> SurrealCountReturn{
|
||||
DB.query("SELECT count() FROM user WHERE gender = false GROUP BY count;").await?.take("count")
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
pub mod user;
|
||||
pub mod interaction;
|
||||
pub mod message;
|
||||
pub mod post;
|
||||
pub mod interaction;
|
||||
pub mod user;
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
enum InteractionType {
|
||||
Like,
|
||||
Dislike,
|
||||
|
@ -8,9 +11,12 @@ enum InteractionType {
|
|||
Rocket,
|
||||
Smile,
|
||||
Laugh,
|
||||
Sad,
|
||||
Shrug,
|
||||
}
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct Interaction {
|
||||
post_id:String,
|
||||
user_id:String,
|
||||
post_id: String,
|
||||
user_id: String,
|
||||
interaction_type: InteractionType,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,15 +1,21 @@
|
|||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Message {
|
||||
pub sender_email: String,
|
||||
pub receiver_email: String,
|
||||
pub message: String,
|
||||
pub execution_time: DateTime<Utc>,
|
||||
}
|
||||
|
||||
impl Message {
|
||||
pub async fn new(sender_email: String, receiver_email: String, message: String) -> Self{
|
||||
Message {
|
||||
pub async fn new(sender_email: String, receiver_email: String, message: String) -> Self {
|
||||
Self {
|
||||
sender_email,
|
||||
receiver_email,
|
||||
message,
|
||||
execution_time: Utc::now(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Post {
|
||||
pub poster_email: String,
|
||||
pub post: String,
|
||||
}
|
||||
|
||||
impl Post {
|
||||
pub async fn new(poster_email: String, post: String) -> Self {
|
||||
Self { poster_email, post }
|
||||
}
|
||||
}
|
|
@ -24,7 +24,13 @@ pub struct User {
|
|||
}
|
||||
|
||||
impl User {
|
||||
pub async fn new(name: Vec<String>, surname: Vec<String>, gender: bool, birth_date: NaiveDate, email: String) -> Self {
|
||||
pub async fn new(
|
||||
name: Vec<String>,
|
||||
surname: Vec<String>,
|
||||
gender: bool,
|
||||
birth_date: NaiveDate,
|
||||
email: String,
|
||||
) -> Self {
|
||||
Self {
|
||||
name,
|
||||
surname,
|
||||
|
@ -34,4 +40,4 @@ impl User {
|
|||
role: Role::User,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
12
src/lib.rs
12
src/lib.rs
|
@ -1,7 +1,8 @@
|
|||
pub mod feature;
|
||||
pub mod database;
|
||||
pub mod feature;
|
||||
pub mod utils;
|
||||
|
||||
use sea_orm::DatabaseConnection;
|
||||
use utils::naive_toml_parser;
|
||||
|
||||
const DATABASE_CONFIG_FILE_LOCATION: &str = "./configs/database_config.toml";
|
||||
|
@ -12,8 +13,8 @@ pub struct DatabaseConfig {
|
|||
pub address: String,
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
pub namespace: String,
|
||||
pub database: String,
|
||||
pub backend: String,
|
||||
}
|
||||
impl Default for DatabaseConfig {
|
||||
fn default() -> Self {
|
||||
|
@ -21,8 +22,8 @@ impl Default for DatabaseConfig {
|
|||
|
||||
if header == "[database_config]" {
|
||||
Self {
|
||||
backend: database_configs.pop().unwrap(),
|
||||
database: database_configs.pop().unwrap(),
|
||||
namespace: database_configs.pop().unwrap(),
|
||||
password: database_configs.pop().unwrap(),
|
||||
username: database_configs.pop().unwrap(),
|
||||
address: database_configs.pop().unwrap(),
|
||||
|
@ -51,3 +52,8 @@ impl Default for ServerConfig {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AppState {
|
||||
pub database_connection: DatabaseConnection,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue