feat(db): add diesel and sqlite3 as dependencies
This commit is contained in:
14
src/db.rs
Normal file
14
src/db.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
use diesel::prelude::*;
|
||||
use diesel::sqlite::SqliteConnection;
|
||||
use dotenv::dotenv;
|
||||
use std::env;
|
||||
|
||||
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))
|
||||
}
|
||||
@@ -1,4 +1,10 @@
|
||||
#[macro_use]
|
||||
extern crate diesel;
|
||||
|
||||
pub mod db;
|
||||
pub mod game;
|
||||
pub mod models;
|
||||
pub mod schema;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
8
src/models.rs
Normal file
8
src/models.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Queryable)]
|
||||
pub struct GameFile {
|
||||
pub id: i32,
|
||||
pub original_path: PathBuf,
|
||||
pub file_hash: u64,
|
||||
}
|
||||
23
src/schema.rs
Normal file
23
src/schema.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
table! {
|
||||
game_file (id) {
|
||||
id -> Nullable<Integer>,
|
||||
original_path -> Binary,
|
||||
file_hash -> Integer,
|
||||
game_save_id -> Integer,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
game_save_location (id) {
|
||||
id -> Nullable<Integer>,
|
||||
friendly_name -> Text,
|
||||
uuid -> Binary,
|
||||
}
|
||||
}
|
||||
|
||||
joinable!(game_file -> game_save_location (game_save_id));
|
||||
|
||||
allow_tables_to_appear_in_same_query!(
|
||||
game_file,
|
||||
game_save_location,
|
||||
);
|
||||
Reference in New Issue
Block a user