feat: ✨ sqlx
This commit is contained in:
parent
40149b372d
commit
20af44c357
16 changed files with 135 additions and 194 deletions
|
@ -5,12 +5,15 @@ pub mod user;
|
|||
|
||||
use std::time::Duration;
|
||||
|
||||
use sea_orm::{Database, DatabaseConnection};
|
||||
use sqlx::{postgres::PgPoolOptions, Connection, Pool, Postgres};
|
||||
use tokio::time::sleep;
|
||||
|
||||
use crate::DatabaseConfig;
|
||||
|
||||
pub async fn establish_connection() -> DatabaseConnection {
|
||||
pub async fn set_database_up(database_connection: &Pool<Postgres>) {
|
||||
sqlx::migrate!().run(database_connection).await.unwrap();
|
||||
}
|
||||
pub async fn establish_connection() -> Pool<Postgres> {
|
||||
let database_config = DatabaseConfig::default();
|
||||
let connection_string = format!(
|
||||
"{}://{}:{}@{}/{}",
|
||||
|
@ -20,12 +23,27 @@ pub async fn establish_connection() -> DatabaseConnection {
|
|||
database_config.address,
|
||||
database_config.database
|
||||
);
|
||||
Database::connect(connection_string).await.unwrap()
|
||||
PgPoolOptions::new()
|
||||
.max_connections(database_config.connection_pool_size)
|
||||
.test_before_acquire(false)
|
||||
.connect(&connection_string)
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub async fn is_alive() -> bool {
|
||||
pub async fn is_alive(database_connection: &Pool<Postgres>) -> bool {
|
||||
tokio::select! {
|
||||
|
||||
database_connection = database_connection.acquire() => {
|
||||
match database_connection {
|
||||
Ok(mut database_connection) => {
|
||||
match database_connection.ping().await {
|
||||
Ok(_) => true,
|
||||
Err(_) => false,
|
||||
}
|
||||
},
|
||||
Err(_) => false,
|
||||
}
|
||||
}
|
||||
_ = sleep(Duration::from_secs(1)) => false,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1 +1,73 @@
|
|||
use chrono::NaiveDate;
|
||||
use sqlx::{Pool, Postgres};
|
||||
|
||||
use crate::feature::user::{Role, User};
|
||||
|
||||
pub async fn create_user(
|
||||
name: &String,
|
||||
surname: &String,
|
||||
gender: bool,
|
||||
birth_date: &NaiveDate,
|
||||
email: &String,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<User, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
User,
|
||||
r#"
|
||||
INSERT INTO "user"(name, surname, gender, birth_date, email)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
RETURNING id, name, surname, gender, birth_date, email, role AS "role:Role"
|
||||
"#,
|
||||
name,
|
||||
surname,
|
||||
gender,
|
||||
birth_date,
|
||||
email
|
||||
)
|
||||
.fetch_one(database_connection)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn read_user(
|
||||
email: &String,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<User, sqlx::Error> {
|
||||
sqlx::query_as!(User,
|
||||
r#"
|
||||
SELECT id, name, surname, gender, birth_date, email, role AS "role:Role" FROM "user" WHERE "email" = $1
|
||||
"#,
|
||||
email
|
||||
).fetch_one(database_connection).await
|
||||
}
|
||||
|
||||
pub async fn update_user(
|
||||
id: i64,
|
||||
name: &String,
|
||||
surname: &String,
|
||||
gender: &bool,
|
||||
birth_date: &NaiveDate,
|
||||
email: &String,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<User, sqlx::Error> {
|
||||
sqlx::query_as!(User,
|
||||
r#"
|
||||
UPDATE "user" SET "name" = $1, "surname" = $2, "gender" = $3, "birth_date" = $4, "email" = $5 WHERE "id" = $6
|
||||
RETURNING id, name, surname, gender, birth_date, email, role AS "role:Role"
|
||||
"#, name, surname, gender, birth_date, email, id).fetch_one(database_connection).await
|
||||
}
|
||||
|
||||
pub async fn delete_user(
|
||||
id: i64,
|
||||
database_connection: &Pool<Postgres>,
|
||||
) -> Result<User, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
User,
|
||||
r#"
|
||||
DELETE FROM "user" where id = $1
|
||||
RETURNING id, name, surname, gender, birth_date, email, role AS "role:Role"
|
||||
"#,
|
||||
id
|
||||
)
|
||||
.fetch_one(database_connection)
|
||||
.await
|
||||
}
|
||||
|
|
|
@ -8,36 +8,20 @@ pub struct Contact {
|
|||
pub website: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[derive(Debug, Serialize, Deserialize, sqlx::Type)]
|
||||
#[sqlx(type_name = "role")]
|
||||
pub enum Role {
|
||||
User,
|
||||
Zero,
|
||||
Hero,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct User {
|
||||
pub name: Vec<String>,
|
||||
pub surname: Vec<String>,
|
||||
pub id: i64,
|
||||
pub name: String,
|
||||
pub surname: String,
|
||||
pub gender: bool,
|
||||
pub birth_date: NaiveDate,
|
||||
pub email: String,
|
||||
pub role: Role,
|
||||
}
|
||||
|
||||
impl User {
|
||||
pub async fn new(
|
||||
name: Vec<String>,
|
||||
surname: Vec<String>,
|
||||
gender: bool,
|
||||
birth_date: NaiveDate,
|
||||
email: String,
|
||||
) -> Self {
|
||||
Self {
|
||||
name,
|
||||
surname,
|
||||
gender,
|
||||
birth_date,
|
||||
email,
|
||||
role: Role::User,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ pub mod database;
|
|||
pub mod feature;
|
||||
pub mod utils;
|
||||
|
||||
use sea_orm::DatabaseConnection;
|
||||
use sqlx::{Pool, Postgres};
|
||||
use utils::naive_toml_parser;
|
||||
|
||||
const DATABASE_CONFIG_FILE_LOCATION: &str = "./configs/database_config.toml";
|
||||
|
@ -15,6 +15,7 @@ pub struct DatabaseConfig {
|
|||
pub password: String,
|
||||
pub database: String,
|
||||
pub backend: String,
|
||||
pub connection_pool_size: u32,
|
||||
}
|
||||
impl Default for DatabaseConfig {
|
||||
fn default() -> Self {
|
||||
|
@ -22,6 +23,7 @@ impl Default for DatabaseConfig {
|
|||
|
||||
if header == "[database_config]" {
|
||||
Self {
|
||||
connection_pool_size: database_configs.pop().unwrap().parse().unwrap(),
|
||||
backend: database_configs.pop().unwrap(),
|
||||
database: database_configs.pop().unwrap(),
|
||||
password: database_configs.pop().unwrap(),
|
||||
|
@ -55,5 +57,5 @@ impl Default for ServerConfig {
|
|||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AppState {
|
||||
pub database_connection: DatabaseConnection,
|
||||
pub database_connection: Pool<Postgres>,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue