use sqlx::postgres::{PgPool, PgPoolOptions};
use std::env;
use std::error::Error;

pub async fn establish_connection() -> Result<PgPool, Box<dyn Error>> {
    let db_url = match env::var("DATABASE_URL") {
        Ok(s) => s,
        Err(_) => panic!("No database environment variable set"),
    };

    let pool = PgPoolOptions::new()
        .max_connections(5)
        .connect(&db_url)
        .await?;

    Ok(pool)
}
