36 lines
1.1 KiB
Rust
36 lines
1.1 KiB
Rust
use sea_orm::{ConnectOptions, Database as SeaDatabase, DatabaseConnection, DbErr};
|
|
use std::time::Duration;
|
|
|
|
#[derive(Clone)]
|
|
pub struct Database {
|
|
pub connection: DatabaseConnection,
|
|
}
|
|
|
|
impl Database {
|
|
pub async fn init(dsn: &str) -> Result<Self, DbErr> {
|
|
let mut final_dsn = dsn.to_string();
|
|
if dsn.starts_with("sqlite:") && !dsn.contains('?') && dsn != "sqlite::memory:" {
|
|
final_dsn = format!("{}?mode=rwc", dsn);
|
|
}
|
|
println!("{}", final_dsn);
|
|
|
|
let mut opt = ConnectOptions::new(final_dsn);
|
|
opt.max_connections(100)
|
|
.min_connections(5)
|
|
.connect_timeout(Duration::from_secs(8))
|
|
.acquire_timeout(Duration::from_secs(8))
|
|
.sqlx_logging(true)
|
|
.sqlx_logging_level(log::LevelFilter::Debug);
|
|
|
|
let connection = SeaDatabase::connect(opt).await?;
|
|
|
|
Ok(Self { connection })
|
|
}
|
|
|
|
// Tu peux ajouter ici tes méthodes helpers si tu veux encapsuler SeaORM
|
|
// ex: pub async fn find_user(...)
|
|
pub fn get_connection(&self) -> &DatabaseConnection {
|
|
&self.connection
|
|
}
|
|
}
|