wip: convert tauri sql to prisma

This commit is contained in:
Ren Amamiya
2023-04-02 16:22:50 +07:00
parent 9b8a96c651
commit 39e7c9bf34
11 changed files with 228 additions and 162 deletions

View File

@@ -3,4 +3,5 @@
/target/
# prisma
src/prisma.rs
src/db.rs
lume.db

1
src-tauri/Cargo.lock generated
View File

@@ -2193,6 +2193,7 @@ dependencies = [
"tauri",
"tauri-build",
"tauri-specta",
"tokio",
]
[[package]]

View File

@@ -21,6 +21,7 @@ prisma-client-rust = { git = "https://github.com/Brendonovich/prisma-client-rust
prisma-client-rust-cli = { git = "https://github.com/Brendonovich/prisma-client-rust", tag = "0.6.7", default-features = false, features = ["sqlite", "migrations", "mocking", "specta"] }
specta = "1.0.0"
tauri-specta = { version = "1.0.0", features = ["typescript"] }
tokio = { version = "1.26.0", features = ["macros"] }
[target.'cfg(target_os = "macos")'.dependencies]
objc = "0.2.7"

View File

@@ -1,104 +0,0 @@
-- Add migration script here
-- create relays
CREATE TABLE
relays (
id INTEGER PRIMARY KEY,
relay_url TEXT NOT NULL,
relay_status INTEGER NOT NULL DEFAULT 1,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-- add default relays
-- relay status:
-- 0: off
-- 1: on
INSERT INTO
relays (relay_url, relay_status)
VALUES
("wss://relay.damus.io", "1"),
("wss://eden.nostr.land", "0"),
("wss://nostr-pub.wellorder.net", "1"),
("wss://nostr.bongbong.com", "1"),
("wss://nostr.zebedee.cloud", "1"),
("wss://nostr.fmt.wiz.biz", "1"),
("wss://nostr.walletofsatoshi.com", "0"),
("wss://relay.snort.social", "1"),
("wss://offchain.pub", "1"),
("wss://brb.io", "0"),
("wss://relay.current.fyi", "1"),
("wss://nostr.relayer.se", "0"),
("wss://nostr.bitcoiner.social", "1"),
("wss://relay.nostr.info", "1"),
("wss://relay.zeh.app", "0"),
("wss://nostr-01.dorafactory.org", "1"),
("wss://nostr.zhongwen.world", "1"),
("wss://nostro.cc", "1"),
("wss://relay.nostr.net.in", "1"),
("wss://nos.lol", "1");
-- create accounts
-- is_active (part of multi-account feature):
-- 0: false
-- 1: true
CREATE TABLE
accounts (
id TEXT PRIMARY KEY,
privkey TEXT NOT NULL,
npub TEXT NOT NULL,
nsec TEXT NOT NULL,
is_active INTEGER NOT NULL DEFAULT 0,
metadata TEXT
);
-- create follows
-- kind (part of multi-newsfeed feature):
-- 0: direct
-- 1: follow of follow
CREATE TABLE
follows (
id INTEGER PRIMARY KEY,
pubkey TEXT NOT NULL,
account TEXT NOT NULL,
kind INTEGER NOT NULL DEFAULT 0,
metadata TEXT
);
-- create index for pubkey in follows
CREATE UNIQUE INDEX index_pubkey_on_follows ON follows (pubkey);
-- create cache profiles
CREATE TABLE
cache_profiles (
id TEXT PRIMARY KEY,
metadata TEXT,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-- create cache notes
CREATE TABLE
cache_notes (
id TEXT PRIMARY KEY,
pubkey TEXT NOT NULL,
created_at TEXT,
kind INTEGER NOT NULL DEFAULT 1,
tags TEXT NOT NULL,
content TEXT NOT NULL,
parent_id TEXT,
parent_comment_id TEXT
);
-- create settings
CREATE TABLE
settings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
setting_key TEXT NOT NULL,
setting_value TEXT NOT NULL
);
-- add default setting
INSERT INTO
settings (setting_key, setting_value)
VALUES
("last_login", "0");

View File

@@ -1,11 +1,75 @@
datasource db {
provider = "sqlite"
url = "file:lume.db"
url = "file:../lume.db"
}
generator client {
// Corresponds to the cargo alias created earlier
provider = "cargo prisma"
provider = "cargo prisma"
// The location to generate the client. Is relative to the position of the schema
output = "../src/prisma.rs"
output = "../src/db.rs"
module_path = "db"
}
model Account {
id Int @id @default(autoincrement())
pubkey String
privkey String @unique
active Boolean @default(false)
metadata String
// related
follows Follow[]
messages Message[]
notes Note[]
@@index([pubkey])
}
model Follow {
id Int @id @default(autoincrement())
pubkey String
kind Int
metadata String
Account Account @relation(fields: [accountId], references: [id])
accountId Int
}
model Note {
id Int @id @default(autoincrement())
pubkey String
kind Int
tags String
content String
parent_id String
parent_comment_id String
createdAt DateTime @default(now())
Account Account @relation(fields: [accountId], references: [id])
accountId Int
}
model Message {
id Int @id @default(autoincrement())
pubkey String
content String
tags String
createdAt DateTime @default(now())
Account Account @relation(fields: [accountId], references: [id])
accountId Int
@@index([pubkey])
}
model Relay {
id Int @id @default(autoincrement())
url String
active Boolean @default(true)
}
model Setting {
id Int @id @default(autoincrement())
key String
value String
}

View File

@@ -8,13 +8,87 @@
extern crate objc;
use tauri::{Manager, WindowEvent};
#[cfg(target_os = "macos")]
use window_ext::WindowExt;
#[cfg(target_os = "macos")]
mod window_ext;
fn main() {
mod db;
use db::*;
use serde::Deserialize;
use specta::{collect_types, Type};
use std::sync::Arc;
use tauri::State;
use tauri_specta::ts;
type DbState<'a> = State<'a, Arc<PrismaClient>>;
#[derive(Deserialize, Type)]
struct CreateAccountData {
pubkey: String,
privkey: String,
metadata: String,
}
#[derive(Deserialize, Type)]
struct CreateFollowData {
pubkey: String,
kind: i32,
metadata: String,
account_id: i32,
}
#[tauri::command]
#[specta::specta]
async fn get_account(db: DbState<'_>) -> Result<Vec<account::Data>, ()> {
db.account()
.find_many(vec![account::active::equals(true)])
.exec()
.await
.map_err(|_| ())
}
#[tauri::command]
#[specta::specta]
async fn create_account(db: DbState<'_>, data: CreateAccountData) -> Result<account::Data, ()> {
db.account()
.create(data.pubkey, data.privkey, data.metadata, vec![])
.exec()
.await
.map_err(|_| ())
}
#[tauri::command]
#[specta::specta]
async fn create_follow(db: DbState<'_>, data: CreateFollowData) -> Result<follow::Data, ()> {
db.follow()
.create(
data.pubkey,
data.kind,
data.metadata,
account::id::equals(data.account_id),
vec![],
)
.exec()
.await
.map_err(|_| ())
}
#[tokio::main]
async fn main() {
let db = PrismaClient::_builder().build().await.unwrap();
#[cfg(debug_assertions)]
ts::export(
collect_types![get_account, create_account, create_follow],
"../src/utils/bindings.ts",
)
.unwrap();
#[cfg(debug_assertions)]
db._db_push().await.unwrap();
tauri::Builder::default()
.setup(|app| {
let main_window = app.get_window("main").unwrap();
@@ -38,6 +112,12 @@ fn main() {
_ => {}
}
})
.invoke_handler(tauri::generate_handler![
get_account,
create_account,
create_follow
])
.manage(Arc::new(db))
.run(tauri::generate_context!())
.expect("error while running tauri application");
}