|
- use diesel::r2d2::{self, ConnectionManager};
- use diesel::PgConnection;
- use std::env;
- use dotenvy::dotenv;
-
- pub type DbPool = r2d2::Pool<ConnectionManager<PgConnection>>;
- pub type DbConn = r2d2::PooledConnection<ConnectionManager<PgConnection>>;
-
- pub fn establish_connection() -> Result<DbPool, Box<dyn std::error::Error>> {
- dotenv().ok();
-
- let database_url = env::var("DATABASE_URL")
- .map_err(|_| "DATABASE_URL must be set")?;
-
- let manager = ConnectionManager::<PgConnection>::new(database_url);
- let pool = r2d2::Pool::builder()
- .build(manager)
- .map_err(|e| format!("Failed to create pool: {}", e))?;
-
- Ok(pool)
- }
-
- pub fn get_connection() -> Result<DbConn, Box<dyn std::error::Error>> {
- let pool = establish_connection()?;
- pool.get().map_err(|e| -> Box<dyn std::error::Error> { Box::new(e) })
- }
|