summaryrefslogtreecommitdiffstats
path: root/src/state.rs
blob: 614688b4a220fde3a1d941825e12c095ed0607f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
use sqlx::{postgres::PgPoolOptions, Pool, Postgres};

#[derive(Debug)]
pub struct AppState {
    pub pool: Pool<Postgres>,
}

impl AppState {
    pub async fn init(database_uri: &str) -> Result<Self, sqlx::Error> {
        let pool = PgPoolOptions::new()
            .max_connections(10)
            .connect(database_uri)
            .await?;

        sqlx::migrate!().run(&pool).await?;

        Ok(Self { pool })
    }
}