Skip to content

Commit 160c5f0

Browse files
committed
update
1 parent 9c7eccd commit 160c5f0

File tree

4 files changed

+29
-137
lines changed

4 files changed

+29
-137
lines changed

crates/rs-tauri-vue/src-tauri/Cargo.toml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ rust-version = "1.57"
1919
members = []
2020

2121

22+
################################################################################
23+
2224

2325
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
2426

@@ -44,7 +46,7 @@ once_cell = "1"
4446
parking_lot = "0"
4547

4648
# i18n
47-
rust-i18n = "0"
49+
rust-i18n = "1"
4850
sys-locale = "0"
4951

5052
# encrypted kv:
@@ -66,6 +68,13 @@ anyhow = "1"
6668
# debug tool:
6769
console-subscriber = "0"
6870

71+
################################################################################
72+
73+
#
74+
# workspace internal dependencies:
75+
#
76+
rs-pkg = { path = "../../rs-pkg" }
77+
6978

7079
################################################################################
7180

@@ -86,6 +95,7 @@ available-locales = ["en", "zh-CN", "zh-HK", "fr"]
8695
# Path for your translations YAML file, default: "locales".
8796
# load-path = "locales"
8897

98+
################################################################################
8999

90100
[features]
91101
# by default Tauri runs in production mode

crates/rs-tauri-vue/src-tauri/src/ctx/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ use std::sync::Arc;
33
use once_cell::sync::OnceCell;
44
use parking_lot::Mutex;
55

6+
use rs_pkg::x::SqliteClient;
7+
68
use crate::{
79
config::AppConfig,
8-
storage::{db::SqlConn, AppEncryptedKVStorage, AppKvStorage},
10+
storage::{AppEncryptedKVStorage, AppKvStorage},
911
};
1012

1113
#[derive(Clone)]
@@ -25,15 +27,15 @@ pub struct AppContext {
2527
pub kv_enc: Arc<Mutex<AppEncryptedKVStorage>>,
2628

2729
// sqlite3
28-
pub sql: Arc<Mutex<SqlConn>>,
30+
pub sql: Arc<Mutex<SqliteClient>>,
2931
}
3032

3133
impl AppContext {
3234
// 单例模式:
3335
pub async fn global() -> &'static AppContext {
3436
static DATA: OnceCell<AppContext> = OnceCell::new();
3537

36-
let sql = SqlConn::default().await;
38+
let sql = SqliteClient::default().await;
3739

3840
DATA.get_or_init(|| AppContext {
3941
config: Arc::new(Mutex::new(AppConfig::default())),
Lines changed: 4 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,20 @@
1-
use std::str::FromStr;
1+
use rs_pkg::x::SqliteClient;
22

3-
use sqlx::{
4-
migrate::Migrator,
5-
sqlite::{SqliteConnectOptions, SqlitePool, SqlitePoolOptions},
6-
Acquire, ConnectOptions, Executor,
7-
};
8-
use tracing::info;
9-
10-
use crate::{storage::todo::TodoSqlScope, util};
3+
use crate::storage::todo::TodoSqlScope;
114

125
// sql storage biz
136
pub struct AppSqlStorage {
14-
g: SqlConn,
7+
g: SqliteClient,
158

169
// biz units:
1710
pub todo: TodoSqlScope,
1811
}
1912

2013
impl AppSqlStorage {
2114
pub async fn new() -> Self {
22-
let g = SqlConn::default().await;
23-
24-
// g.init_migrations().await;
15+
let g = SqliteClient::default().await;
2516

2617
let todo = TodoSqlScope::new(g.clone());
2718
Self { g, todo }
2819
}
2920
}
30-
31-
// kv存储方案: SqlConn
32-
pub struct SqlConn {
33-
pub conn: SqlitePool,
34-
}
35-
36-
impl Clone for SqlConn {
37-
fn clone(&self) -> Self {
38-
Self { conn: self.conn.clone() }
39-
}
40-
}
41-
42-
impl SqlConn {
43-
pub async fn default() -> Self {
44-
Self::new("app.db").await
45-
}
46-
47-
pub async fn new(db_name: &str) -> Self {
48-
let conn = new_sql_conn(db_name).await;
49-
Self { conn }
50-
}
51-
52-
pub async fn init_migrations(&mut self) {
53-
// todo x: 自动执行 db migrations
54-
let m = Migrator::new(std::path::Path::new("./migrations"))
55-
.await
56-
.expect("migrations failed");
57-
58-
match m.run(&self.conn).await {
59-
Ok(_) => {
60-
println!("migrations run success");
61-
},
62-
Err(e) => {
63-
panic!("migrations run error: {:?}", e);
64-
},
65-
}
66-
}
67-
}
68-
69-
async fn new_sql_conn(db_name: &str) -> SqlitePool {
70-
let mut _root_dir = tauri::api::path::document_dir();
71-
72-
// sqlite:tmp/app.db
73-
// let url = env::var("").unwrap_or(util::app_cfg::get_local_sqlite_url(Some(db_name)));
74-
let url = util::app_cfg::get_local_sqlite_url(Some(db_name));
75-
println!("sqlite url: {}", url);
76-
77-
// todo x: 仅用于创建 db 文件
78-
let _ = SqliteConnectOptions::from_str(&url)
79-
.unwrap()
80-
.create_if_missing(true)
81-
.connect()
82-
.await
83-
.expect("connect to sqlite failed");
84-
85-
// connect pool
86-
let conn = SqlitePoolOptions::new().min_connections(2).connect(&url).await;
87-
88-
match conn {
89-
Ok(conn) => {
90-
// todo x: 初始化表结构
91-
sqlx::migrate!("./migrations").run(&conn).await.expect("migrate failed");
92-
93-
info!("connect to sqlite db success");
94-
conn
95-
},
96-
Err(e) => {
97-
panic!("connect to sqlite db failed: {}", e);
98-
},
99-
}
100-
}
101-
102-
#[cfg(test)]
103-
mod test {
104-
use tokio;
105-
106-
use super::*;
107-
108-
#[tokio::test]
109-
async fn test_sqlite() {
110-
let mut db = SqlConn::default().await;
111-
// db.init_migrations().await;
112-
113-
println!("test sqlite success");
114-
}
115-
116-
#[test]
117-
fn test2() {
118-
info!("hello");
119-
println!("test2 success");
120-
}
121-
}

crates/rs-tauri-vue/src-tauri/src/storage/sql/todo.rs

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
use sqlx::{Acquire, Executor, Row, Statement};
22
use tracing::info;
33

4-
use crate::{proto::TodoEntity, storage::db::SqlConn};
4+
use rs_pkg::x::SqliteClient;
5+
6+
use crate::proto::TodoEntity;
57

68
pub struct TodoSqlScope {
7-
pub g: SqlConn,
9+
pub g: SqliteClient,
810
}
911

1012
impl TodoSqlScope {
11-
pub fn new(g: SqlConn) -> Self {
13+
pub fn new(g: SqliteClient) -> Self {
1214
Self { g }
1315
}
1416

1517
pub async fn add_todo(&self, todo: &TodoEntity) -> anyhow::Result<i64> {
16-
let mut conn = self.g.conn.acquire().await?;
18+
let mut conn = self.g.cli.acquire().await?;
1719

1820
// Insert the task, then obtain the ID of this row
1921
let id = sqlx::query(
@@ -33,29 +35,8 @@ impl TodoSqlScope {
3335
Ok(id)
3436
}
3537

36-
// pub async fn add_todo2(&self, todo: &TodoEntity) -> anyhow::Result<i64> {
37-
// let mut conn = self.g.conn.acquire().await?;
38-
//
39-
// // Insert the task, then obtain the ID of this row
40-
// let id = sqlx::query!(
41-
// r#"
42-
// INSERT INTO todos ( description, title )
43-
// VALUES ( ?1, ?2 )
44-
// "#,
45-
// todo.description,
46-
// todo.title
47-
// )
48-
// .execute(&mut conn)
49-
// .await?
50-
// .last_insert_rowid();
51-
//
52-
// info!("add todo: {:?}", todo);
53-
//
54-
// Ok(id)
55-
// }
56-
5738
pub async fn list_todo(&self) -> anyhow::Result<Vec<TodoEntity>> {
58-
let mut conn = self.g.conn.acquire().await?;
39+
let mut conn = self.g.cli.acquire().await?;
5940

6041
let rows = sqlx::query(
6142
r#"
@@ -83,7 +64,7 @@ impl TodoSqlScope {
8364
}
8465

8566
pub async fn get_todo(&self, id: i64) -> anyhow::Result<TodoEntity> {
86-
let mut conn = self.g.conn.acquire().await?;
67+
let mut conn = self.g.cli.acquire().await?;
8768

8869
let row = sqlx::query(
8970
r#"SELECT id, title, description, completed FROM todos WHERE id =
@@ -112,7 +93,7 @@ mod test {
11293
use super::*;
11394

11495
async fn setup() -> TodoSqlScope {
115-
let mut db = SqlConn::new("app.db").await;
96+
let mut db = SqliteClient::new(None, None).await;
11697
// let mut db = SqlConn::default().await;
11798
// db.init_migrations().await;
11899

0 commit comments

Comments
 (0)