impl postgres pool conn

This commit is contained in:
rmanach
2023-04-18 12:06:56 +02:00
parent 0163525773
commit 64d91f4cbf
15 changed files with 1178 additions and 88 deletions
+27
View File
@@ -0,0 +1,27 @@
use r2d2;
use diesel::pg::PgConnection;
use diesel::r2d2::ConnectionManager;
use lazy_static::lazy_static;
type Pool = r2d2::Pool<ConnectionManager<PgConnection>>;
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);
}
pub fn get_connection() -> Result<DbConnection, String> {
match POOL.get() {
Ok(conn) => Ok(conn),
Err(e) => Err(format!("unable to get pool connection: {}", e)),
}
}
+3
View File
@@ -0,0 +1,3 @@
mod client;
pub use client::{get_connection, init_database_pool};