backend/db/
connection.rs

1//! Utilities to set up the initial connection to the database.
2
3use diesel_async::{
4    pooled_connection::deadpool, pooled_connection::AsyncDieselConnectionManager, AsyncPgConnection,
5};
6
7/// Type renaming of [`deadpool::Pool`] using [`AsyncPgConnection`]
8pub type Pool = deadpool::Pool<AsyncPgConnection>;
9
10/// Creates an initialized pool connecting to the database.
11///
12/// # Panics
13/// If the pool is unable to open its minimum number of connections.
14#[must_use]
15pub fn init_pool(url: &str) -> Pool {
16    let manager = AsyncDieselConnectionManager::<AsyncPgConnection>::new(url);
17
18    match deadpool::Pool::builder(manager).build() {
19        Ok(pool) => pool,
20        Err(e) => panic!("Error while creating pool: {e}"),
21    }
22}