summaryrefslogtreecommitdiff
path: root/src/database/mod.rs
blob: d66b72bf56e017e6b7020f06420a08c6cb32293a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
pub mod song;
pub mod album;
pub mod artist;
pub mod search_results;

use serde::{Deserialize, Serialize};
use sqlx::mysql::{MySqlPool, MySqlPoolOptions};
use std::env;

#[derive(Serialize, Deserialize)]
pub struct Delete {
    pub id: Option<String>,
}

pub struct DatabaseWrapper {
    db_pool: MySqlPool,
}

impl DatabaseWrapper {
    pub async fn new() -> Result<DatabaseWrapper, sqlx::Error> {
        let pool: MySqlPool = MySqlPoolOptions::new()
            .max_connections(10)
            .connect(
                env::var("DATABASE_URL")
                    .expect("Environment variable DATABASE_URL is *probably not setted up!!")
                    .as_str(),
            )
            .await
            .unwrap(); /* This will break in case of error. It's intended. */

        Ok(DatabaseWrapper { db_pool: pool })
    }
}