exercises

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

mod.rs (646B)


      1 use anyhow::Result;
      2 use sqlx::{postgres::PgPoolOptions, PgPool};
      3 
      4 pub mod users;
      5 
      6 pub use users::*;
      7 
      8 #[derive(Clone)]
      9 pub struct Database {
     10     pool: PgPool,
     11 }
     12 
     13 impl Database {
     14     pub async fn new(database_url: &str, max_connections: u32) -> Result<Self> {
     15         let pool = PgPoolOptions::new()
     16             .max_connections(max_connections)
     17             .connect(database_url)
     18             .await?;
     19 
     20         // Set search path to users schema
     21         sqlx::query("SET search_path TO users, public")
     22             .execute(&pool)
     23             .await?;
     24 
     25         Ok(Self { pool })
     26     }
     27 
     28     pub fn pool(&self) -> &PgPool {
     29         &self.pool
     30     }
     31 }