Skip to content

Commit 214e9f0

Browse files
committed
update
1 parent 09a81fc commit 214e9f0

File tree

6 files changed

+55
-29
lines changed

6 files changed

+55
-29
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pub mod calc;
2+
pub mod todo;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#[tauri::command]
2+
pub fn add_todo(title: String, description: String) -> bool {
3+
// AppService::new().ctx.sql.
4+
false
5+
}

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
use std::sync::Arc;
2+
3+
use once_cell::sync::OnceCell;
4+
use parking_lot::Mutex;
5+
16
use crate::{
27
config::AppConfig,
3-
storage::{AppEncryptedKVStorage, AppKvStorage},
8+
storage::{db::AppSqlStorage, AppEncryptedKVStorage, AppKvStorage},
49
};
5-
use once_cell::sync::OnceCell;
6-
use parking_lot::Mutex;
7-
use std::sync::Arc;
810

911
#[derive(Clone)]
1012
pub struct AppContext {
@@ -21,17 +23,23 @@ pub struct AppContext {
2123

2224
// kv 加密存储:
2325
pub kv_enc: Arc<Mutex<AppEncryptedKVStorage>>,
26+
27+
// sqlite3
28+
pub sql: Arc<Mutex<AppSqlStorage>>,
2429
}
2530

2631
impl AppContext {
2732
// 单例模式:
28-
pub fn global() -> &'static AppContext {
33+
pub async fn global() -> &'static AppContext {
2934
static DATA: OnceCell<AppContext> = OnceCell::new();
3035

36+
let sql = AppSqlStorage::default().await;
37+
3138
DATA.get_or_init(|| AppContext {
3239
config: Arc::new(Mutex::new(AppConfig::default())),
3340
kv: Arc::new(Mutex::new(AppKvStorage::default())),
3441
kv_enc: Arc::new(Mutex::new(AppEncryptedKVStorage::default())),
42+
sql: Arc::new(Mutex::new(sql)),
3543
})
3644
}
3745
}
@@ -40,9 +48,9 @@ impl AppContext {
4048
mod tests {
4149
use super::*;
4250

43-
#[test]
44-
fn it_works() {
45-
let ctx = AppContext::global();
51+
#[tokio::test]
52+
async fn it_works() {
53+
let ctx = AppContext::global().await;
4654

4755
let mut kv = ctx.kv.lock();
4856

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
pub struct AppService {}
1+
use crate::ctx::AppContext;
2+
3+
pub struct AppService {
4+
pub ctx: &'static AppContext,
5+
}
26

37
impl AppService {
4-
pub fn new() -> AppService {
5-
Self {}
8+
pub async fn new() -> Self {
9+
let ctx = AppContext::global().await;
10+
Self { ctx }
611
}
7-
8-
pub fn update_locale() {}
912
}

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

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

1010
use crate::util;
1111

1212
// kv存储方案:
1313
pub struct AppSqlStorage {
14-
pub db: SqlitePool,
14+
pub conn: SqlitePool,
1515
}
1616

1717
impl AppSqlStorage {
@@ -29,15 +29,15 @@ impl AppSqlStorage {
2929
.connect()
3030
.await;
3131

32-
let db = SqlitePoolOptions::new().min_connections(2).connect(&url).await;
32+
let conn = SqlitePoolOptions::new().min_connections(2).connect(&url).await;
3333

34-
match db {
35-
Ok(db) => {
36-
info!("connect to sqlite db success");
37-
Self { db }
34+
match conn {
35+
Ok(conn) => {
36+
info!("sqlite conn: {:?}", conn);
37+
Self { conn }
3838
},
3939
Err(e) => {
40-
panic!("db create error: {:?}", e);
40+
panic!("sqlite conn err: {:?}", e);
4141
},
4242
}
4343
}
@@ -62,7 +62,7 @@ impl AppSqlStorage {
6262
match db {
6363
Ok(db) => {
6464
info!("connect to sqlite db success");
65-
Self { db }
65+
Self { conn: db }
6666
},
6767
Err(e) => {
6868
panic!("db create error: {:?}", e);
@@ -76,7 +76,7 @@ impl AppSqlStorage {
7676
.await
7777
.expect("migrations failed");
7878

79-
match m.run(&self.db).await {
79+
match m.run(&self.conn).await {
8080
Ok(_) => {
8181
println!("migrations run success");
8282
},

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@ use sqlx::{Acquire, Executor, Statement};
22

33
use crate::{proto::TodoEntity, storage::db::AppSqlStorage};
44

5-
impl AppSqlStorage {
5+
pub struct TodoStorage {
6+
pub db: AppSqlStorage,
7+
}
8+
9+
impl TodoStorage {
10+
pub fn new(db: AppSqlStorage) -> Self {
11+
Self { db }
12+
}
13+
614
pub async fn add_todo(&self, todo: &TodoEntity) -> anyhow::Result<i64> {
7-
let mut conn = self.db.acquire().await?;
15+
let mut conn = self.db.conn.acquire().await?;
816

917
// Insert the task, then obtain the ID of this row
1018
let id = sqlx::query!(
@@ -23,7 +31,7 @@ impl AppSqlStorage {
2331
}
2432

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

2836
let rows = sqlx::query!(
2937
r#"
@@ -51,7 +59,7 @@ ORDER BY id
5159
}
5260

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

5664
let row =
5765
sqlx::query!(r#"SELECT id, title, description, completed FROM todos WHERE id = ?"#, id)
@@ -76,10 +84,11 @@ mod test {
7684

7785
use super::*;
7886

79-
async fn setup() -> AppSqlStorage {
87+
async fn setup() -> TodoStorage {
8088
let mut db = AppSqlStorage::new("test.db").await;
8189
db.init_migrations().await;
82-
db
90+
91+
TodoStorage::new(db)
8392
}
8493

8594
#[tokio::test]

0 commit comments

Comments
 (0)