Skip to content

Commit d45d83d

Browse files
committed
update
1 parent 214e9f0 commit d45d83d

File tree

3 files changed

+40
-21
lines changed

3 files changed

+40
-21
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use parking_lot::Mutex;
55

66
use crate::{
77
config::AppConfig,
8-
storage::{db::AppSqlStorage, AppEncryptedKVStorage, AppKvStorage},
8+
storage::{db::SqlConn, AppEncryptedKVStorage, AppKvStorage},
99
};
1010

1111
#[derive(Clone)]
@@ -25,15 +25,15 @@ pub struct AppContext {
2525
pub kv_enc: Arc<Mutex<AppEncryptedKVStorage>>,
2626

2727
// sqlite3
28-
pub sql: Arc<Mutex<AppSqlStorage>>,
28+
pub sql: Arc<Mutex<SqlConn>>,
2929
}
3030

3131
impl AppContext {
3232
// 单例模式:
3333
pub async fn global() -> &'static AppContext {
3434
static DATA: OnceCell<AppContext> = OnceCell::new();
3535

36-
let sql = AppSqlStorage::default().await;
36+
let sql = SqlConn::default().await;
3737

3838
DATA.get_or_init(|| AppContext {
3939
config: Arc::new(Mutex::new(AppConfig::default())),

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,37 @@ use std::{env, str::FromStr};
33
use sqlx::{
44
migrate::Migrator,
55
sqlite::{SqliteConnectOptions, SqlitePool, SqlitePoolOptions},
6-
Acquire, ConnectOptions, Error, Executor, Pool, Sqlite,
6+
Acquire, ConnectOptions, Executor,
77
};
88
use tracing::info;
99

10-
use crate::util;
10+
use crate::{storage::todo::TodoSqlScope, util};
1111

12-
// kv存储方案:
13-
pub struct AppSqlStorage {
12+
// sql storage biz
13+
14+
pub struct AppSqlStorageBiz {
15+
pub g: SqlConn,
16+
17+
// biz units:
18+
pub todo: TodoSqlScope,
19+
}
20+
21+
impl AppSqlStorageBiz {
22+
pub async fn new() -> anyhow::Result<Self> {
23+
let g = SqlConn::default().await?;
24+
25+
let todo = TodoSqlScope::new(g.clone());
26+
27+
Ok(Self { g, todo })
28+
}
29+
}
30+
31+
// kv存储方案: SqlConn
32+
pub struct SqlConn {
1433
pub conn: SqlitePool,
1534
}
1635

17-
impl AppSqlStorage {
36+
impl SqlConn {
1837
pub async fn default() -> Self {
1938
let mut _root_dir = tauri::api::path::document_dir();
2039

@@ -142,7 +161,7 @@ mod test {
142161

143162
#[tokio::test]
144163
async fn test_sqlite() {
145-
let mut db = AppSqlStorage::default().await;
164+
let mut db = SqlConn::default().await;
146165
db.init_migrations().await;
147166

148167
// let mut conn = db.db.acquire().await.unwrap();

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
use sqlx::{Acquire, Executor, Statement};
22

3-
use crate::{proto::TodoEntity, storage::db::AppSqlStorage};
3+
use crate::{proto::TodoEntity, storage::db::SqlConn};
44

5-
pub struct TodoStorage {
6-
pub db: AppSqlStorage,
5+
pub struct TodoSqlScope {
6+
pub g: SqlConn,
77
}
88

9-
impl TodoStorage {
10-
pub fn new(db: AppSqlStorage) -> Self {
11-
Self { db }
9+
impl TodoSqlScope {
10+
pub fn new(g: SqlConn) -> Self {
11+
Self { g }
1212
}
1313

1414
pub async fn add_todo(&self, todo: &TodoEntity) -> anyhow::Result<i64> {
15-
let mut conn = self.db.conn.acquire().await?;
15+
let mut conn = self.g.conn.acquire().await?;
1616

1717
// Insert the task, then obtain the ID of this row
1818
let id = sqlx::query!(
@@ -31,7 +31,7 @@ impl TodoStorage {
3131
}
3232

3333
pub async fn list_todo(&self) -> anyhow::Result<Vec<TodoEntity>> {
34-
let mut conn = self.db.conn.acquire().await?;
34+
let mut conn = self.g.conn.acquire().await?;
3535

3636
let rows = sqlx::query!(
3737
r#"
@@ -59,7 +59,7 @@ ORDER BY id
5959
}
6060

6161
pub async fn get_todo(&self, id: i64) -> anyhow::Result<TodoEntity> {
62-
let mut conn = self.db.conn.acquire().await?;
62+
let mut conn = self.g.conn.acquire().await?;
6363

6464
let row =
6565
sqlx::query!(r#"SELECT id, title, description, completed FROM todos WHERE id = ?"#, id)
@@ -84,11 +84,11 @@ mod test {
8484

8585
use super::*;
8686

87-
async fn setup() -> TodoStorage {
88-
let mut db = AppSqlStorage::new("test.db").await;
87+
async fn setup() -> TodoSqlScope {
88+
let mut db = SqlConn::new("test.db").await;
8989
db.init_migrations().await;
9090

91-
TodoStorage::new(db)
91+
TodoSqlScope::new(db)
9292
}
9393

9494
#[tokio::test]

0 commit comments

Comments
 (0)