24 lines
634 B
Rust
24 lines
634 B
Rust
|
|
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()
|
||
|
|
})
|
||
|
|
}
|