deployer/src/database/client.rs
2023-04-18 12:57:43 +02:00

31 lines
819 B
Rust

use r2d2;
use diesel::pg::PgConnection;
use diesel::r2d2::ConnectionManager;
use lazy_static::lazy_static;
type Pool = r2d2::Pool<ConnectionManager<PgConnection>>;
#[allow(dead_code)]
pub type DbConnection = r2d2::PooledConnection<ConnectionManager<PgConnection>>;
lazy_static! {
static ref POOL: Pool = {
let database_url = "postgres://rust:rust@localhost:5433/dispatcher";
let manager = ConnectionManager::<PgConnection>::new(database_url);
Pool::new(manager).expect("failed to create db pool")
};
}
pub fn init_database_pool() {
lazy_static::initialize(&POOL);
}
#[allow(dead_code)]
pub fn get_connection() -> Result<DbConnection, String> {
match POOL.get() {
Ok(conn) => Ok(conn),
Err(e) => Err(format!("unable to get pool connection: {}", e)),
}
}