This commit is contained in:
Peter Schiwy
2024-06-20 14:50:36 +02:00
parent 1d60fc5a3f
commit 44931afbe7
23 changed files with 431 additions and 25 deletions

23
src/database.rs Normal file
View File

@@ -0,0 +1,23 @@
use std::time::Duration;
use anyhow::Error;
use sqlx::{self, postgres::PgPoolOptions, Executor, Pool, Postgres};
use crate::config;
pub type DB = Pool<Postgres>;
pub trait Queryer<'c>: Executor<'c, Database = sqlx::Postgres> {}
impl<'c> Queryer<'c> for &Pool<Postgres> {}
pub async fn connect(database: &config::Database) -> Result<DB, Error> {
PgPoolOptions::new()
.max_connections(database.pool_size)
.max_lifetime(Duration::from_secs(database.max_lifetime))
.connect(&database.url)
.await
.map_err(|err| {
tracing::error!("{}", err);
err.into()
})
}