revert prisma-client-rust, move back to tauri-sql

This commit is contained in:
Ren Amamiya
2023-04-18 10:24:17 +07:00
parent fc258fdcde
commit 075fbcf507
27 changed files with 811 additions and 2678 deletions

View File

@@ -3,6 +3,7 @@
import { RelayContext } from '@components/relaysProvider';
import { dateToUnix, hoursAgo } from '@utils/getDate';
import { getActiveAccount } from '@utils/storage';
import { getParentID, pubkeyArray } from '@utils/transform';
import LumeSymbol from '@assets/icons/Lume';
@@ -21,11 +22,6 @@ export default function Page() {
const eose = useRef(0);
const unsubscribe = useRef(null);
const fetchActiveAccount = useCallback(async () => {
const { getAccounts } = await import('@utils/bindings');
return await getAccounts();
}, []);
const fetchPlebsByAccount = useCallback(async (id: number, kind: number) => {
const { getPlebs } = await import('@utils/bindings');
return await getPlebs({ account_id: id, kind: kind });
@@ -47,7 +43,7 @@ export default function Page() {
}, []);
const fetchData = useCallback(
async (account, follows) => {
async (account) => {
const { createNote } = await import('@utils/bindings');
const { createChat } = await import('@utils/bindings');
const { createChannel } = await import('@utils/bindings');
@@ -67,7 +63,7 @@ export default function Page() {
}
query.push({
kinds: [1, 6],
authors: follows,
authors: account.follows,
since: since,
until: dateToUnix(now.current),
});
@@ -159,22 +155,14 @@ export default function Page() {
);
useEffect(() => {
let account;
let follows;
fetchActiveAccount()
getActiveAccount()
.then((res: any) => {
if (res.length > 0) {
account = res[0];
if (res) {
const account = res;
// update local storage
writeStorage('activeAccount', account);
// fetch plebs, kind 0 = following
fetchPlebsByAccount(account.id, 0).then((res) => {
follows = pubkeyArray(res);
writeStorage('activeAccountFollows', res);
// fetch data
fetchData(account, follows);
});
// fetch data
fetchData(account);
} else {
router.replace('/onboarding');
}
@@ -186,7 +174,7 @@ export default function Page() {
unsubscribe.current();
}
};
}, [fetchActiveAccount, fetchPlebsByAccount, totalNotes, fetchData, router]);
}, [fetchData, router]);
return (
<div className="h-screen w-screen bg-zinc-50 text-zinc-900 dark:bg-black dark:text-white">

64
src/utils/storage.tsx Normal file
View File

@@ -0,0 +1,64 @@
import Database from 'tauri-plugin-sql-api';
let db: null | Database = null;
// connect database (sqlite)
// path: tauri::api::path::BaseDirectory::App
export async function connect(): Promise<Database> {
if (db) {
return db;
}
db = await Database.load('sqlite:lume.db');
return db;
}
// get active account
export async function getActiveAccount() {
const db = await connect();
// #TODO: check is_active == true
const result = await db.select(`SELECT * FROM accounts LIMIT 1;`);
return result[0];
}
// create account
export async function createAccount(data: { pubkey: string; privkey: string; metadata: string }) {
const db = await connect();
return await db.execute('INSERT OR IGNORE INTO accounts (pubkey, privkey, metadata) VALUES (?, ?, ?);', [
data.pubkey,
data.privkey,
data.metadata,
]);
}
// update account
export async function updateAccount(column: string, value: string, pubkey: string) {
const db = await connect();
return await db.execute(`UPDATE accounts SET ${column} = "${value}" WHERE pubkey = "${pubkey}";`);
}
// create pleb
export async function createPleb(pubkey: string, metadata: string) {
const db = await connect();
return await db.execute('INSERT OR IGNORE INTO plebs (pubkey, metadata) VALUES (?, ?);', [pubkey, metadata]);
}
// count total notes
export async function countTotalNotes() {
const db = await connect();
const result = await db.select('SELECT COUNT(*) AS "total" FROM notes;');
return result[0];
}
// get all notes
export async function getNotes(time: string, limit: number, offset: number) {
const db = await connect();
return await db.select(
`SELECT * FROM notes WHERE created_at <= "${time}" ORDER BY created_at DESC LIMIT "${limit}" OFFSET "${offset}";`
);
}
// get all latest notes
export async function getLatestNotes(time) {
const db = await connect();
return await db.select(`SELECT * FROM cache_notes WHERE created_at > "${time}" ORDER BY created_at DESC;`);
}

View File

@@ -1,12 +0,0 @@
export const truncate = (fullStr: string, strLen: number, separator: string | unknown[]) => {
if (fullStr.length <= strLen) return fullStr;
separator = separator || '...';
const sepLen = separator.length,
charsToShow = strLen - sepLen,
frontChars = Math.ceil(charsToShow / 2),
backChars = Math.floor(charsToShow / 2);
return fullStr.substr(0, frontChars) + separator + fullStr.substr(fullStr.length - backChars);
};