summaryrefslogtreecommitdiff
path: root/server/src/db.rs
blob: 8cc4d1f3328fa1275a2669a0cbed2dd0086b5c5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
use crate::errors::AppError;

use sqlx::postgres::PgPool;

static mut CONNECTION: Option<PgPool> = None;

pub async fn setup() -> Result<(), AppError> {
    let database_url =
        std::env::var("DATABASE_URL").expect("Define `DATABASE_URL` environment variable.");

    unsafe {
        CONNECTION = Some(PgPool::connect(&database_url).await?);
    }

    Ok(())
}

pub unsafe fn get_client() -> Option<&'static PgPool> {
    CONNECTION.as_ref()
}