Skip to content

Commit

Permalink
Add database connection module
Browse files Browse the repository at this point in the history
  • Loading branch information
rashed091 committed Nov 20, 2023
1 parent 633944a commit 22171c6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"rust-analyzer.showUnlinkedFileNotification": false
}
55 changes: 55 additions & 0 deletions src/common/database.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use diesel::r2d2::{self, ConnectionManager, PooledConnection};
use once_cell::sync::OnceCell;

#[cfg(feature = "database_postgres")]
type DbCon = diesel::PgConnection;

#[cfg(all(feature = "database_postgres", debug_assertions))]
#[allow(dead_code)]
pub type DieselBackend = diesel::pg::Pg;


pub type Pool = r2d2::Pool<ConnectionManager<DbCon>>;
pub type Connection = PooledConnection<ConnectionManager<DbCon>>;

/// wrapper function for a database pool
#[derive(Clone)]
pub struct Database {
pub pool: &'static Pool,
}

impl Default for Database {
fn default() -> Self {
Self::new()
}
}

impl Database {
pub fn new() -> Database {
Database {
pool: Self::get_or_init_pool(),
}
}

pub fn get_connection(&self) -> Connection {
self.pool.get().unwrap()
}

fn get_or_init_pool() -> &'static Pool {
#[cfg(debug_assertions)]
crate::load_env_vars();

static POOL: OnceCell<Pool> = OnceCell::new();

POOL.get_or_init(|| {
Pool::builder()
.connection_timeout(std::time::Duration::from_secs(5))
.build(ConnectionManager::<DbCon>::new(Self::connection_url()))
.unwrap()
})
}

pub fn connection_url() -> String {
std::env::var("DATABASE_URL").expect("DATABASE_URL environment variable expected.")
}
}

0 comments on commit 22171c6

Please sign in to comment.