converted full sql from onboarding flow to prisma

This commit is contained in:
Ren Amamiya
2023-04-03 15:03:07 +07:00
parent 33000979ed
commit 3f87d510ab
14 changed files with 314 additions and 74 deletions

View File

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

View File

@@ -0,0 +1,73 @@
-- CreateTable
CREATE TABLE "Account" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"pubkey" TEXT NOT NULL,
"privkey" TEXT NOT NULL,
"active" BOOLEAN NOT NULL DEFAULT false,
"metadata" TEXT NOT NULL
);
-- CreateTable
CREATE TABLE "Follow" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"pubkey" TEXT NOT NULL,
"kind" INTEGER NOT NULL,
"metadata" TEXT NOT NULL,
"accountId" INTEGER NOT NULL,
CONSTRAINT "Follow_accountId_fkey" FOREIGN KEY ("accountId") REFERENCES "Account" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);
-- CreateTable
CREATE TABLE "Note" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"eventId" TEXT NOT NULL,
"pubkey" TEXT NOT NULL,
"kind" INTEGER NOT NULL,
"tags" TEXT NOT NULL,
"content" TEXT NOT NULL,
"parent_id" TEXT NOT NULL,
"parent_comment_id" TEXT NOT NULL,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"accountId" INTEGER NOT NULL,
CONSTRAINT "Note_accountId_fkey" FOREIGN KEY ("accountId") REFERENCES "Account" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);
-- CreateTable
CREATE TABLE "Message" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"pubkey" TEXT NOT NULL,
"content" TEXT NOT NULL,
"tags" TEXT NOT NULL,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"accountId" INTEGER NOT NULL,
CONSTRAINT "Message_accountId_fkey" FOREIGN KEY ("accountId") REFERENCES "Account" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);
-- CreateTable
CREATE TABLE "Relay" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"url" TEXT NOT NULL,
"active" BOOLEAN NOT NULL DEFAULT true
);
-- CreateTable
CREATE TABLE "Setting" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"key" TEXT NOT NULL,
"value" TEXT NOT NULL
);
-- CreateIndex
CREATE UNIQUE INDEX "Account_privkey_key" ON "Account"("privkey");
-- CreateIndex
CREATE INDEX "Account_pubkey_idx" ON "Account"("pubkey");
-- CreateIndex
CREATE UNIQUE INDEX "Note_eventId_key" ON "Note"("eventId");
-- CreateIndex
CREATE INDEX "Note_eventId_idx" ON "Note"("eventId");
-- CreateIndex
CREATE INDEX "Message_pubkey_idx" ON "Message"("pubkey");

View File

@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "sqlite"

View File

@@ -1,6 +1,6 @@
datasource db {
provider = "sqlite"
url = "file:../lume.db"
url = "file:../../lume.db"
}
generator client {
@@ -37,6 +37,7 @@ model Follow {
model Note {
id Int @id @default(autoincrement())
eventId String @unique
pubkey String
kind Int
tags String
@@ -47,6 +48,8 @@ model Note {
Account Account @relation(fields: [accountId], references: [id])
accountId Int
@@index([eventId])
}
model Message {

View File

@@ -7,6 +7,7 @@
#[macro_use]
extern crate objc;
use prisma_client_rust::raw;
use tauri::{Manager, WindowEvent};
#[cfg(target_os = "macos")]
use window_ext::WindowExt;
@@ -31,6 +32,11 @@ struct CreateAccountData {
metadata: String,
}
#[derive(Deserialize, Type)]
struct GetFollowData {
account_id: i32,
}
#[derive(Deserialize, Type)]
struct CreateFollowData {
pubkey: String,
@@ -39,6 +45,18 @@ struct CreateFollowData {
account_id: i32,
}
#[derive(Deserialize, Type)]
struct CreateNoteData {
event_id: String,
pubkey: String,
kind: i32,
tags: String,
content: String,
parent_id: String,
parent_comment_id: String,
account_id: i32,
}
#[tauri::command]
#[specta::specta]
async fn get_account(db: DbState<'_>) -> Result<Vec<account::Data>, ()> {
@@ -59,6 +77,16 @@ async fn create_account(db: DbState<'_>, data: CreateAccountData) -> Result<acco
.map_err(|_| ())
}
#[tauri::command]
#[specta::specta]
async fn get_follows(db: DbState<'_>, data: GetFollowData) -> Result<Vec<follow::Data>, ()> {
db.follow()
.find_many(vec![follow::account_id::equals(data.account_id)])
.exec()
.await
.map_err(|_| ())
}
#[tauri::command]
#[specta::specta]
async fn create_follow(db: DbState<'_>, data: CreateFollowData) -> Result<follow::Data, ()> {
@@ -75,13 +103,68 @@ async fn create_follow(db: DbState<'_>, data: CreateFollowData) -> Result<follow
.map_err(|_| ())
}
#[tauri::command]
#[specta::specta]
async fn create_note(db: DbState<'_>, data: CreateNoteData) -> Result<note::Data, ()> {
let event_id = data.event_id.clone();
let content = data.content.clone();
db.note()
.upsert(
note::event_id::equals(event_id),
note::create(
data.event_id,
data.pubkey,
data.kind,
data.tags,
data.content,
data.parent_id,
data.parent_comment_id,
account::id::equals(data.account_id),
vec![],
),
vec![note::content::set(content)],
)
.exec()
.await
.map_err(|_| ())
}
#[tauri::command]
#[specta::specta]
async fn get_notes(db: DbState<'_>) -> Result<Vec<note::Data>, ()> {
db._query_raw(raw!("SELECT * FROM Note"))
.exec()
.await
.map_err(|_| ())
}
#[tauri::command]
#[specta::specta]
async fn check_note(db: DbState<'_>) -> Result<Vec<note::Data>, ()> {
db.note()
.find_many(vec![])
.take(5)
.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],
collect_types![
get_account,
create_account,
get_follows,
create_follow,
create_note,
get_notes,
check_note
],
"../src/utils/bindings.ts",
)
.unwrap();
@@ -115,7 +198,11 @@ async fn main() {
.invoke_handler(tauri::generate_handler![
get_account,
create_account,
create_follow
get_follows,
create_follow,
create_note,
get_notes,
check_note
])
.manage(Arc::new(db))
.run(tauri::generate_context!())