Simplify db code by removing r2d2
This commit is contained in:
parent
d18ce42938
commit
c502f87fa3
|
|
@ -11,7 +11,6 @@ axum = "0.7"
|
|||
askama = "0.12"
|
||||
askama_axum = "0.4"
|
||||
tower-http = { version = "0.5", features = ["fs"] }
|
||||
r2d2_postgres = "0.18"
|
||||
deadpool-r2d2 = {version = "0.3", features = ["rt_tokio_1"]}
|
||||
deadpool = "0.10"
|
||||
deadpool-postgres = { version = "0.12", features = ["rt_tokio_1"] }
|
||||
tokio-postgres = { version = "0.7", features = ["with-chrono-0_4"] }
|
||||
thiserror = "1.0"
|
||||
38
src/db.rs
38
src/db.rs
|
|
@ -1,47 +1,31 @@
|
|||
use {
|
||||
deadpool, deadpool_r2d2::Runtime, thiserror::Error,
|
||||
deadpool_postgres::{CreatePoolError, Pool, Runtime},
|
||||
thiserror::Error,
|
||||
tokio_postgres::NoTls,
|
||||
};
|
||||
|
||||
type PgManager = deadpool_r2d2::Manager<
|
||||
r2d2_postgres::PostgresConnectionManager<r2d2_postgres::postgres::NoTls>,
|
||||
>;
|
||||
type PgPool = deadpool_r2d2::Pool<PgManager>;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("Could not initialize DB connection pool.")]
|
||||
ConnectionPoolError(#[from] deadpool::managed::BuildError),
|
||||
#[error("Error with Postgres database")]
|
||||
PostrgesError(#[from] r2d2_postgres::postgres::Error)
|
||||
ConnectionPoolError(#[from] CreatePoolError),
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Database {
|
||||
pg_pool: PgPool,
|
||||
pg_pool: Pool,
|
||||
}
|
||||
|
||||
impl Database {
|
||||
pub fn create_pool(connection_url: &str, max_size: usize) -> Result<Database, Error> {
|
||||
let pg_config: r2d2_postgres::postgres::Config = connection_url.parse()?;
|
||||
let r2d2_manager = r2d2_postgres::PostgresConnectionManager::new(
|
||||
pg_config,
|
||||
r2d2_postgres::postgres::NoTls,
|
||||
);
|
||||
let manager = PgManager::new(r2d2_manager, Runtime::Tokio1);
|
||||
let pg_pool = PgPool::builder(manager).max_size(max_size).build()?;
|
||||
pub fn create_pool(connection_url: &str) -> Result<Database, Error> {
|
||||
let mut config = deadpool_postgres::Config::new();
|
||||
config.url = Some(connection_url.to_string());
|
||||
let pg_pool = config.create_pool(Some(Runtime::Tokio1), NoTls)?;
|
||||
Ok(Database { pg_pool })
|
||||
}
|
||||
|
||||
pub async fn log_num_users(&self) -> usize {
|
||||
self.pg_pool
|
||||
.get()
|
||||
.await
|
||||
.unwrap()
|
||||
.interact(|conn| {
|
||||
let results = conn.query("SELECT * FROM users;", &[]).unwrap();
|
||||
let client = self.pg_pool.get().await.unwrap();
|
||||
let results = client.query("SELECT * FROM users;", &[]).await.unwrap();
|
||||
results.len()
|
||||
})
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ fn main() {
|
|||
async fn localhub_main() -> Result<(), Error> {
|
||||
let config = get_config()?;
|
||||
|
||||
let db_pool = Database::create_pool(&config.database_url, 2)?;
|
||||
let db_pool = Database::create_pool(&config.database_url)?;
|
||||
|
||||
let app = app::routes()
|
||||
.with_state(db_pool)
|
||||
|
|
|
|||
Loading…
Reference in New Issue