feat: add surreal db

This commit is contained in:
reya 2024-02-03 11:24:08 +07:00
parent a3a8f57bfc
commit bd2b6a3759
6 changed files with 1643 additions and 11 deletions

1582
src-tauri/Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -39,6 +39,7 @@ tauri-plugin-upload = "2.0.0-alpha"
tauri-plugin-window-state = "2.0.0-alpha"
tauri-plugin-theme = { git = "https://github.com/wyhaya/tauri-plugin-theme" }
webpage = { version = "2.0", features = ["serde"] }
surrealdb = { version = "1.1.1", features = ["kv-rocksdb"] }
[target.'cfg(not(target_os = "linux"))'.dependencies]
keyring = "2"

View File

@ -1,3 +1,4 @@
pub mod db;
pub mod folder;
pub mod opg;
pub mod secret;

View File

@ -0,0 +1,23 @@
use crate::model::*;
use crate::AppState;
use tauri::State;
#[tauri::command(async)]
pub async fn create_account(
pubkey: String,
app_state: State<'_, AppState>,
) -> Result<Vec<Record>, ()> {
let db = app_state.db.lock().await;
let created: Vec<Record> = db
.create("account")
.content(Account {
id: None,
pubkey,
is_active: true,
})
.await
.expect("Create account failed");
Ok(created)
}

View File

@ -4,16 +4,23 @@
)]
pub mod commands;
pub mod model;
pub mod nostr;
use nostr_sdk::{Client, ClientBuilder};
use nostr_sqlite::SQLiteDatabase;
use std::sync::Arc;
use surrealdb::{
engine::local::{Db, RocksDb},
Surreal,
};
use tauri::Manager;
use tauri_plugin_autostart::MacosLauncher;
use tauri_plugin_theme::ThemePlugin;
use tokio::sync::Mutex;
pub struct AppState {
pub db: Mutex<Surreal<Db>>,
pub nostr: Arc<Client>,
}
@ -25,6 +32,17 @@ fn main() {
let config_dir = app.path().app_config_dir().unwrap();
tauri::async_runtime::spawn(async move {
// Create app db connection
let db = Surreal::new::<RocksDb>(config_dir.join("lume.db"))
.await
.expect("Initialize app db failed");
// Select namespace and db
db.use_ns("lume")
.use_db("app")
.await
.expect("Open app db failed");
// Create database connection
let nostr_db = SQLiteDatabase::open(config_dir.join("nostr.db"))
.await
@ -50,6 +68,7 @@ fn main() {
// Init global state
handle.manage(AppState {
db: Mutex::new(db),
nostr: client.into(),
})
});

28
src-tauri/src/model.rs Normal file
View File

@ -0,0 +1,28 @@
use serde::{Deserialize, Serialize};
use surrealdb::sql::Thing;
#[derive(Debug, Serialize)]
pub struct Account {
pub id: Option<Thing>,
pub pubkey: String,
pub is_active: bool,
}
#[derive(Debug, Serialize)]
pub struct Column {
pub id: Option<Thing>,
pub title: String,
pub content: String,
pub kind: i32,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Record {
#[allow(dead_code)]
pub id: Thing,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct AffectedRows {
pub rows_affected: u64,
}