Import source: src/db/index
Import the existing local project snapshot. This commit organizes existing files; it does not represent newly developed functionality.
This commit is contained in:
@@ -0,0 +1,55 @@
|
|||||||
|
import Database from "@tauri-apps/plugin-sql";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Thin typed data layer over the Tauri SQL plugin (SQLite).
|
||||||
|
* All persistence goes through here — components never touch SQL directly.
|
||||||
|
*/
|
||||||
|
|
||||||
|
let db: Database | null = null;
|
||||||
|
|
||||||
|
export async function getDb(): Promise<Database> {
|
||||||
|
if (!db) {
|
||||||
|
// Migrations (src-tauri/migrations) run automatically on first load.
|
||||||
|
db = await Database.load("sqlite:workspace.db");
|
||||||
|
}
|
||||||
|
return db;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface WorkspaceMeta {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
theme: "light" | "dark" | "system";
|
||||||
|
accent: string;
|
||||||
|
onboarded: number;
|
||||||
|
last_page_id: string | null;
|
||||||
|
settings: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getWorkspaceMeta(): Promise<WorkspaceMeta> {
|
||||||
|
const d = await getDb();
|
||||||
|
const rows = await d.select<WorkspaceMeta[]>(
|
||||||
|
"SELECT * FROM workspace_meta WHERE id = 1",
|
||||||
|
);
|
||||||
|
return rows[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function updateWorkspaceMeta(
|
||||||
|
patch: Partial<Omit<WorkspaceMeta, "id">>,
|
||||||
|
): Promise<void> {
|
||||||
|
const d = await getDb();
|
||||||
|
const keys = Object.keys(patch) as (keyof typeof patch)[];
|
||||||
|
if (keys.length === 0) return;
|
||||||
|
const sets = keys.map((k, i) => `${k} = $${i + 1}`).join(", ");
|
||||||
|
await d.execute(
|
||||||
|
`UPDATE workspace_meta SET ${sets} WHERE id = 1`,
|
||||||
|
keys.map((k) => patch[k]),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function newId(): string {
|
||||||
|
return crypto.randomUUID();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function now(): number {
|
||||||
|
return Date.now();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user