-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: sqlite db pool connect and error handle
- Loading branch information
Showing
11 changed files
with
180 additions
and
93 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,39 @@ | ||
use entity::todos; | ||
use sea_orm::DbErr; | ||
use sea_orm::DbConn; | ||
use tauri::State; | ||
|
||
use crate::service::{Mutation, Query}; | ||
use crate::{ | ||
error::AppError, | ||
service::{Mutation, Query}, | ||
}; | ||
|
||
#[tauri::command] | ||
pub async fn add_todo_item(title: String) -> Result<bool, String> { | ||
let result = Mutation::create_todo_item(title).await; | ||
pub async fn add_todo_item(db: State<'_, DbConn>, title: String) -> Result<bool, String> { | ||
let result = Mutation::create_todo_item(db.inner(), title).await; | ||
Ok(result.is_ok()) | ||
} | ||
|
||
#[tauri::command] | ||
pub async fn query_list_by_page(page: u64) -> Result<Vec<todos::Model>, String> { | ||
match Query::query_todo_list(page).await { | ||
pub async fn query_list_by_page( | ||
db: State<'_, DbConn>, | ||
page: u64, | ||
) -> Result<Vec<todos::Model>, String> { | ||
match Query::query_todo_list(db.inner(), page).await { | ||
Ok(result) => Ok(result), | ||
Err(err) => Err(err.to_string()), | ||
} | ||
} | ||
|
||
#[tauri::command] | ||
pub async fn delete_item_by_id(id: i32) -> Result<bool, String> { | ||
let result = Mutation::delete_item_by_id(id).await; | ||
Ok(result.is_ok()) | ||
pub async fn delete_item_by_id(db: State<'_, DbConn>, id: i32) -> Result<bool, AppError> { | ||
Mutation::delete_item_by_id(&db, id).await | ||
} | ||
|
||
#[tauri::command] | ||
pub async fn switch_item_status(id: i32, is_done: bool) -> Result<bool, String> { | ||
match Mutation::switch_item_status(id, is_done).await { | ||
Ok(result) => Ok(result), | ||
Err(err) => Err(err.to_string()), | ||
} | ||
} | ||
pub async fn switch_item_status( | ||
db: State<'_, DbConn>, | ||
id: i32, | ||
is_done: bool, | ||
) -> Result<bool, AppError> { | ||
Mutation::switch_item_status(db.inner(), id, is_done).await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use sea_orm::DbErr; | ||
use serde::Serialize; | ||
use thiserror::Error; | ||
|
||
#[derive(Debug, Error)] | ||
pub enum AppError { | ||
#[error("The item with a specified id is not found")] | ||
RowNotFound, | ||
#[error("Call command parameter is not valid")] | ||
ParamsError, | ||
#[error(transparent)] | ||
DatabaseError(DbErr), | ||
#[error(transparent)] | ||
Unexpected(anyhow::Error), | ||
} | ||
|
||
impl From<anyhow::Error> for AppError { | ||
fn from(err: anyhow::Error) -> Self { | ||
AppError::Unexpected(err) | ||
} | ||
} | ||
|
||
impl From<DbErr> for AppError { | ||
fn from(err: DbErr) -> Self { | ||
AppError::DatabaseError(err) | ||
} | ||
} | ||
|
||
impl Serialize for AppError { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: serde::Serializer, | ||
{ | ||
serializer.serialize_str(self.to_string().as_ref()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use std::env; | ||
|
||
use migration::{Migrator, MigratorTrait}; | ||
use sea_orm::{DatabaseConnection, SqlxSqliteConnector}; | ||
use sqlx::SqlitePool; | ||
use tauri::{api::path::app_data_dir, App, Config, Manager}; | ||
|
||
pub fn setup(app: &mut App) -> Result<(), Box<dyn std::error::Error>> { | ||
let handler = app.handle(); | ||
tokio::spawn(async move { | ||
let db_conn = get_database_pool(&handler.config()).await; | ||
handler.manage(db_conn); | ||
}); | ||
|
||
Ok(()) | ||
} | ||
|
||
pub async fn get_database_pool(config: &Config) -> DatabaseConnection { | ||
let database_url = get_db_path(config); | ||
|
||
let pool = SqlitePool::connect(&database_url) | ||
.await | ||
.expect("Failed to connect database"); | ||
|
||
let db_conn = SqlxSqliteConnector::from_sqlx_sqlite_pool(pool); | ||
|
||
Migrator::up(&db_conn, None) | ||
.await | ||
.expect("Failed to migrate"); | ||
|
||
db_conn | ||
} | ||
|
||
pub fn get_db_path(config: &Config) -> String { | ||
if cfg!(debug_assertions) { | ||
env::var("DATABASE_URL").expect("DATABASE_URL must be set") | ||
} else { | ||
let app_data_dir = app_data_dir(config).expect("DATABASE_URL must be"); | ||
std::fs::create_dir_all(&app_data_dir).unwrap(); | ||
|
||
format!( | ||
"sqlite://{}?mode=rwc", | ||
app_data_dir.join("db.sqlite").to_string_lossy() | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.