cache user's metdata in db

This commit is contained in:
Ren Amamiya
2023-07-05 17:19:49 +07:00
parent ec9b54cb82
commit c5ba98e37a
6 changed files with 59 additions and 49 deletions

View File

@@ -1,3 +1,4 @@
import { NDKUserProfile } from '@nostr-dev-kit/ndk';
import Database from 'tauri-plugin-sql-api';
import { getParentID } from '@utils/transform';
@@ -426,3 +427,24 @@ export async function removeAll() {
await db.execute('DELETE FROM accounts;');
return true;
}
// create metadata
export async function createMetadata(id: string, pubkey: string, content: string) {
const db = await connect();
const now = Math.floor(Date.now() / 1000);
return await db.execute(
'INSERT OR REPLACE INTO metadata (id, pubkey, content, created_at) VALUES (?, ?, ?, ?);',
[id, pubkey, content, now]
);
}
// get metadata
export async function getUserMetadata(pubkey: string) {
const db = await connect();
const result = await db.select(`SELECT content FROM metadata WHERE id = "${pubkey}";`);
if (result[0]) {
return JSON.parse(result[0].content);
} else {
return null;
}
}