26 lines
702 B
Rust
26 lines
702 B
Rust
static DB: std::sync::OnceLock<sqlx::MySqlPool> = std::sync::OnceLock::new();
|
|
|
|
async fn create_pool() -> sqlx::MySqlPool {
|
|
let database_url = std::env::var("DATABASE_URL").expect("no database url specify");
|
|
let pool = sqlx::mysql::MySqlPoolOptions::new()
|
|
.max_connections(4)
|
|
.connect(database_url.as_str())
|
|
.await
|
|
.expect("could not connect to database_url");
|
|
|
|
/*sqlx::migrate!()
|
|
.run(&pool)
|
|
.await
|
|
.expect("migrations failed");*/
|
|
|
|
pool
|
|
}
|
|
|
|
pub async fn init_db() -> Result<(), sqlx::Pool<sqlx::MySql>> {
|
|
DB.set(create_pool().await)
|
|
}
|
|
|
|
pub fn get_db<'a>() -> &'a sqlx::MySqlPool {
|
|
DB.get().expect("database unitialized")
|
|
}
|