From 201a37e8d3d0ef4c59b184c0707ec8ddd301a25a Mon Sep 17 00:00:00 2001 From: "Adam T. Carpenter" Date: Fri, 9 Apr 2021 18:50:55 -0400 Subject: spruced up templates, some repo impl --- src/repo/constants.rs | 9 --------- src/repo/mod.rs | 28 ++++++++++++++++++++++++---- 2 files changed, 24 insertions(+), 13 deletions(-) (limited to 'src/repo') diff --git a/src/repo/constants.rs b/src/repo/constants.rs index e168a77..6da641f 100644 --- a/src/repo/constants.rs +++ b/src/repo/constants.rs @@ -1,11 +1,2 @@ -use once_cell::sync::Lazy; -use sled::{Config, Db}; - pub const PARTS_TREE: &str = "parts"; pub const CARS_TREE: &str = "cars"; -pub static REPO_INSTANCE: Lazy = Lazy::new(|| { - Config::default() - .path("/tmp/twinh") - .open() - .expect("Couldn't open DB!") -}); diff --git a/src/repo/mod.rs b/src/repo/mod.rs index d4c5e44..f0bb95c 100644 --- a/src/repo/mod.rs +++ b/src/repo/mod.rs @@ -1,16 +1,36 @@ +use crate::config::CONFIG_INSTANCE; use crate::error::TwinHError; use crate::models::Car; use crate::models::Part; use bincode::deserialize; use constants::*; +use once_cell::sync::Lazy; +use sled::{Config, Db}; mod constants; +static REPO_INSTANCE: Lazy = Lazy::new(|| { + Config::default() + .path(&CONFIG_INSTANCE.db_uri) + .temporary(true) + .open() + .expect("Couldn't open DB!") +}); + +pub fn create_demo_db() -> Result<(), TwinHError> { + let db = sled::Config::default() + .path(&CONFIG_INSTANCE.db_uri) + .create_new(true) + .open()?; + let cars_tree = db.open_tree(CARS_TREE)?; + Ok(()) +} + pub fn create_new_db() -> Result<(), TwinHError> { - let config = sled::Config::default() - .path("/var/db/twinh") - .create_new(true); - let db = config.open()?; + sled::Config::default() + .path(&CONFIG_INSTANCE.db_uri) + .create_new(true) + .open()?; Ok(()) } -- cgit v1.2.3