1use sqlx::postgres::{PgPool, PgPoolOptions};
2use std::time::Duration;
3
4#[derive(Debug, Clone)]
5pub struct ConnectionPool {
6 pool: PgPool,
7}
8
9impl ConnectionPool {
10 pub async fn new(database_url: &str) -> Result<Self, sqlx::Error> {
12 let pool = PgPoolOptions::new()
13 .max_connections(5)
14 .acquire_timeout(Duration::from_secs(30))
15 .connect(database_url)
16 .await?;
17
18 Ok(Self { pool })
19 }
20
21 pub fn pool(&self) -> &PgPool {
23 &self.pool
24 }
25
26 pub async fn test_connection(&self) -> Result<(), sqlx::Error> {
28 sqlx::query("SELECT 1").fetch_one(&self.pool).await?;
29
30 Ok(())
31 }
32}