Skip to content

Commit

Permalink
Merge pull request #16 from opeolluwa/dev
Browse files Browse the repository at this point in the history
fixes: fixed having multiple databases
  • Loading branch information
opeolluwa authored Aug 28, 2023
2 parents fd86f1e + c86f717 commit 1a924e6
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 11 deletions.
9 changes: 8 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
{
"cSpell.words": ["sqlx"]
"cSpell.words": [
"sqlx"
],
"workbench.colorCustomizations": {
"activityBar.background": "#462511",
"titleBar.activeBackground": "#613417",
"titleBar.activeForeground": "#FDFAF8"
}
}
40 changes: 40 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ chrono = "0.4.26"
clap = {version = "4.3.23", features = ["derive"]}
console = "0.15.7"
dialoguer = {version = "0.10.4", features = ["fuzzy-select", "completion"]}
dirs = "5.0.1"
include_dir = "0.7.3"
indicatif = "0.17.6"
lazy_static = "1.4.0"
lettre = "0.10.4"
# migration = {path = "migration"}# depends on your needs
serde = {versio = "1.0.185", features = ["derive"]}
Expand Down
5 changes: 3 additions & 2 deletions src/commands/store.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::database::StoreModel;
use crate::{database::StoreModel, style::PrintColoredText};
use clap::{Args, Subcommand};
use serde::{Deserialize, Serialize};
// let id = Uuid::new_v4();

#[derive(Args, Debug, Serialize)]
pub struct StoreCommands {
Expand Down Expand Up @@ -41,6 +40,8 @@ impl StoreCommands {
/*store the key value pair in the database after checking that the key does not exist, if the key exist prompt use to overwrite */
async fn add(key: &str, value: &str) {
StoreModel::new(key, value).save().await.unwrap();
let message = format!("{key} successfully stored");
PrintColoredText::success(&message);
}
/* accept a key and update the value of the key */
fn set(key: &str, value: &str) {
Expand Down
14 changes: 6 additions & 8 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ use serde::{Deserialize, Serialize};
use sqlx::{migrate::MigrateDatabase, FromRow, Pool, Row, Sqlite, SqlitePool};
use uuid::Uuid;

use crate::style::PrintColoredText;
const DB_URL: &str = "sqlite://utils.db";
use crate::{style::PrintColoredText, DB_URL};
pub struct Database;
#[allow(unused)]

impl Database {
pub async fn init() {
if !Sqlite::database_exists(DB_URL).await.unwrap_or(false) {
match Sqlite::create_database(DB_URL).await {
if !Sqlite::database_exists(&DB_URL).await.unwrap_or(false) {
match Sqlite::create_database(&DB_URL).await {
Ok(_) => PrintColoredText::success("Database initialized"),
Err(_error) => PrintColoredText::error("error creating utility store"),
}
Expand All @@ -26,7 +25,7 @@ impl Database {
let store_create_table =
"CREATE TABLE IF NOT EXISTS store (id VARCHAR, key VARCHAR, value TEXT, date_added TEXT, last_updated TEXT)";

let db = SqlitePool::connect(DB_URL).await.unwrap();
let db = SqlitePool::connect(&DB_URL).await.unwrap();
let _ = sqlx::query(store_create_table).execute(&db).await.unwrap();
let _ = sqlx::query(email_create_table).execute(&db).await.unwrap();

Expand All @@ -47,8 +46,7 @@ impl Database {

// return connection to the database;
pub async fn conn() -> Pool<Sqlite> {

SqlitePool::connect(DB_URL).await.unwrap()
SqlitePool::connect(&DB_URL).await.unwrap()
}

pub async fn tables() -> HashMap<usize, String> {
Expand Down Expand Up @@ -121,7 +119,7 @@ impl StoreModel {
/// find all
pub async fn find() -> Vec<Self> {
let db = Database::conn().await;

sqlx::query_as::<_, Self>("SELECT * FROM store")
.fetch_all(&db)
.await
Expand Down
17 changes: 17 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
use lazy_static::lazy_static;

use include_dir::{include_dir, Dir};

pub const SOURCE_DIR: Dir = include_dir!("src/templates");
lazy_static! {
pub static ref DB_URL: std::string::String = {
// create the database in the home directory of the system
//create "utils" directory in the home dir and / save files to $HOME/utils;
let os_default_home_dir = dirs::home_dir().unwrap();
let db_path = format!(
"{home_dir}/{upload_dir}",
home_dir = os_default_home_dir.display(),
upload_dir = "utils"
);
// create the path if not exist path if not exist
let _ = std::fs::create_dir_all(&db_path);

format!("sqlite://{db_path}/utils.db")
};
}
mod commands;
mod database;
mod parser;
Expand Down

0 comments on commit 1a924e6

Please sign in to comment.