26 lines
791 B
Rust
26 lines
791 B
Rust
use diesel::prelude::*;
|
|
use diesel::sqlite::SqliteConnection;
|
|
use dotenv::dotenv;
|
|
use std::env;
|
|
|
|
/// Establishes a DB Connection with a Sqlite Database
|
|
///
|
|
/// Will **panic** if:
|
|
/// * `$DATABASE_URL` is not set
|
|
/// * Save Sync fails to connect to the database at `$DATABASE_URL`
|
|
///
|
|
/// # Examples
|
|
/// ```
|
|
/// # use save_sync::db::establish_connection;
|
|
/// let connection = establish_connection();
|
|
/// ```
|
|
pub fn establish_connection() -> SqliteConnection {
|
|
dotenv().ok();
|
|
|
|
// TODO: Consider whether it is best practice to panic here
|
|
// or have establish_connection return a Result with a thiserror enum
|
|
|
|
let db_url = env::var("DATABASE_URL").expect("$DATABASE_URL was not set");
|
|
SqliteConnection::establish(&db_url).expect(&format!("Error connecting to {}", db_url))
|
|
}
|