From 6cac7327fa2be8c6a89b5ac40bcb0f4e0c739cd9 Mon Sep 17 00:00:00 2001 From: Yuri Tatishchev Date: Thu, 5 Feb 2026 19:08:35 -0800 Subject: [PATCH] fix client db ts types --- src/lib/db/client-db/index.ts | 46 ++++++++------------------- src/lib/db/client-db/seed.ts | 2 +- src/routes/+page.svelte | 2 +- src/routes/anime/[annId]/+page.svelte | 2 +- 4 files changed, 16 insertions(+), 36 deletions(-) diff --git a/src/lib/db/client-db/index.ts b/src/lib/db/client-db/index.ts index 4922dbe..b4355ec 100644 --- a/src/lib/db/client-db/index.ts +++ b/src/lib/db/client-db/index.ts @@ -11,39 +11,19 @@ import { browser } from "$app/environment"; */ export type ClientDb = ReturnType; -type ClientDbHolder = { - db: ClientDb; - overwriteDatabaseFile: ( - input: ReadableStream | Blob | Uint8Array, - ) => Promise; -}; +// this is kinda mid +export const sqlocal = browser ? await initDb() : null; +export const db = sqlocal ? drizzle(sqlocal.driver, sqlocal.batchDriver) : null; -let holder: ClientDbHolder | null = null; - -/** - * Lazily create the SQLocal-backed Drizzle DB. - * Must only be called in the browser (Workers SSR does not provide `BroadcastChannel`). - */ -export async function getClientDb(): Promise { - if (!browser) { - throw new Error( - "Client DB is only available in the browser (SSR is not supported).", - ); - } - - if (holder) return holder; - - // Dynamic import keeps SQLocal (and its browser-only APIs) out of SSR evaluation. +async function initDb() { const { SQLocalDrizzle } = await import("sqlocal/drizzle"); - - const { driver, batchDriver, overwriteDatabaseFile } = new SQLocalDrizzle( - "database.sqlite3", - ); - - holder = { - db: drizzle(driver, batchDriver), - overwriteDatabaseFile, - }; - - return holder; + return new SQLocalDrizzle("database.sqlite3"); +} + +export function getClientDb() { + if (!sqlocal || !db) throw "Client DB can only be accessed from the browser"; + return { + db, + overwriteDatabaseFile: sqlocal.overwriteDatabaseFile, + }; } diff --git a/src/lib/db/client-db/seed.ts b/src/lib/db/client-db/seed.ts index 2b82fa8..29a975e 100644 --- a/src/lib/db/client-db/seed.ts +++ b/src/lib/db/client-db/seed.ts @@ -64,7 +64,7 @@ export async function ensureSeeded( } // Prefer streaming when possible. - const { overwriteDatabaseFile } = await getClientDb(); + const { overwriteDatabaseFile } = getClientDb(); if (res.body) { await overwriteDatabaseFile(res.body); diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index de249df..37cab63 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -34,7 +34,7 @@ try { isSearching = true; - const { db } = await getClientDb(); + const { db } = getClientDb(); if (!q) { anime = await getAnimeList(db, 20); diff --git a/src/routes/anime/[annId]/+page.svelte b/src/routes/anime/[annId]/+page.svelte index 357e7f5..7e17fd8 100644 --- a/src/routes/anime/[annId]/+page.svelte +++ b/src/routes/anime/[annId]/+page.svelte @@ -54,7 +54,7 @@ } try { - const { db } = await getClientDb(); + const { db } = getClientDb(); await ensureSeeded(); const res = await getAnimeWithSongsByAnnId(db, annId);