Skip to content

Commit

Permalink
refactor(back): reorganize commands
Browse files Browse the repository at this point in the history
  • Loading branch information
kareemmahlees committed Dec 10, 2023
1 parent 30698e5 commit 657eef5
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 88 deletions.
38 changes: 0 additions & 38 deletions src-tauri/src/common.rs

This file was deleted.

69 changes: 69 additions & 0 deletions src-tauri/src/connection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use crate::utils::{read_from_connections_file, write_into_connections_file, Drivers};
use sqlx::{AnyConnection, Connection};

#[tauri::command]
pub async fn test_connection(conn_string: String) -> Result<String, String> {
sqlx::any::install_default_drivers();
let mut con = AnyConnection::connect(conn_string.as_str())
.await
.map_err(|_| "Couldn't connect to DB".to_string())?;
let _ = con
.ping()
.await
.map_err(|_| "DB not responding to Pings".to_string())?;

let _ = con.close().await;

Ok("Connection is healthy".to_string())
}

#[tauri::command]
pub fn create_connection_record(
app: tauri::AppHandle,
conn_string: String,
conn_name: String,
) -> Result<(), String> {
write_into_connections_file(
app.path_resolver().app_config_dir(),
Drivers::SQLITE,
conn_string,
conn_name,
);
Ok(())
}

#[tauri::command]
pub fn connections_exist(app: tauri::AppHandle) -> Result<bool, String> {
let (_, connections) = read_from_connections_file(app.path_resolver().app_config_dir());
match connections {
Ok(connections) => {
if connections.as_object().unwrap().len() > 0 {
Ok(true)
} else {
Ok(false)
}
}
Err(err) => Err(err.to_string()),
}
}

#[tauri::command]
pub fn get_connections(app: tauri::AppHandle) -> Result<serde_json::Value, String> {
let (_, connections) = read_from_connections_file(app.path_resolver().app_config_dir());
match connections {
Ok(conns) => Ok(conns),
Err(err) => Err(err.to_string()),
}
}

#[tauri::command]
pub fn get_connection_details(
app: tauri::AppHandle,
conn_id: String,
) -> Result<serde_json::Value, String> {
let (_, connections) = read_from_connections_file(app.path_resolver().app_config_dir());
match connections {
Ok(conns) => Ok(conns.get(conn_id).unwrap().to_owned()),
Err(err) => Err(err.to_string()),
}
}
15 changes: 9 additions & 6 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

mod common;
mod connection;
mod sqlite;
mod utils;

use common::{connections_exist, get_connection_details, get_connections};
use connection::{
connections_exist, create_connection_record, get_connection_details, get_connections,
test_connection,
};
use sqlite::{
connect_sqlite, create_row, create_sqlite_connection, delete_row, get_columns,
get_columns_definition, get_rows, get_tables, test_sqlite_conn, update_row,
connect_sqlite, create_row, delete_row, get_columns, get_columns_definition, get_rows,
get_tables, update_row,
};
use sqlx::Pool;
use tokio::sync::Mutex;
Expand All @@ -24,8 +27,8 @@ fn main() {
pool: Default::default(),
})
.invoke_handler(tauri::generate_handler![
test_sqlite_conn,
create_sqlite_connection,
test_connection,
create_connection_record,
connections_exist,
get_connections,
get_connection_details,
Expand Down
37 changes: 2 additions & 35 deletions src-tauri/src/sqlite.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,12 @@
use crate::{utils, DbInstance};
use serde_json::Map;
use serde_json::Value as JsonValue;
use sqlx::{AnyConnection, Column, Connection, Row};
use sqlx::{Column, Row};
use std::collections::HashMap;
use std::iter::Iterator;
use std::result::Result::Ok;
use std::vec;
use tauri::{Runtime, State};

use crate::utils::{write_into_connections_file, Drivers};

#[tauri::command]
pub async fn test_sqlite_conn(conn_string: String) -> Result<String, String> {
sqlx::any::install_default_drivers();
let mut con = AnyConnection::connect(conn_string.as_str())
.await
.map_err(|_| "Couldn't connect to DB".to_string())?;
let _ = con
.ping()
.await
.map_err(|_| "DB not responding to Pings".to_string())?;

let _ = con.close().await;

Ok("Connection is healthy".to_string())
}

#[tauri::command]
pub fn create_sqlite_connection<R: Runtime>(
app: tauri::AppHandle<R>,
conn_string: String,
conn_name: String,
) -> Result<(), String> {
write_into_connections_file(
app.path_resolver().app_config_dir(),
Drivers::SQLITE,
conn_string,
conn_name,
);
Ok(())
}
use tauri::State;

#[tauri::command]
pub async fn connect_sqlite(
Expand Down
6 changes: 3 additions & 3 deletions src/app/connect/_components/sqlite-connection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { useRouter } from "next/navigation"
import { useState, type FC } from "react"
import { useForm } from "react-hook-form"
import { z } from "zod"
import { connectSQLite, testSQLiteConnection } from "../actions"
import { createConnectionRecord, testConnection } from "../actions"

const SqliteConnection = () => {
const [selectedPath, setSelectedPath] = useState<string | null>(null)
Expand Down Expand Up @@ -59,7 +59,7 @@ const ConnectionForm: FC<ConnectionFormProps> = ({ selectedPath }) => {
resolver: zodResolver(formSchema)
})
const onSubmit = (values: z.infer<typeof formSchema>) => {
connectSQLite(values.connName, selectedPath as string)
createConnectionRecord(values.connName, `sqlite:${selectedPath}`)
router.push("/connections")
}
return (
Expand Down Expand Up @@ -90,7 +90,7 @@ const ConnectionForm: FC<ConnectionFormProps> = ({ selectedPath }) => {
<Button
type="button"
className="bg-green-500 hover:bg-green-700 w-[100px]"
onClick={() => testSQLiteConnection(selectedPath as string)}
onClick={() => testConnection(`sqlite:${selectedPath}`)}
>
Test
</Button>
Expand Down
15 changes: 9 additions & 6 deletions src/app/connect/actions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { invoke } from "@tauri-apps/api/tauri"
import toast from "react-hot-toast"

export const testSQLiteConnection = async (selectedPath: string) => {
const command = invoke<string>("test_sqlite_conn", {
connString: `sqlite:${selectedPath}`
export const testConnection = async (connString: string) => {
const command = invoke<string>("test_connection", {
connString
})
toast.promise(command, {
loading: "Loading",
Expand All @@ -12,9 +12,12 @@ export const testSQLiteConnection = async (selectedPath: string) => {
})
}

export const connectSQLite = async (connName: string, selectedPath: string) => {
await invoke("create_sqlite_connection", {
connString: `sqlite:${selectedPath}`,
export const createConnectionRecord = async (
connName: string,
connString: string
) => {
await invoke("create_connection_record", {
connString,
connName
})
}

0 comments on commit 657eef5

Please sign in to comment.