test with kerkour architecture (https://kerkour.com/rust-web-application-clean-architecture)
This commit is contained in:
23
src/database.rs
Normal file
23
src/database.rs
Normal 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()
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user