fix client db ts types

This commit is contained in:
2026-02-05 19:08:35 -08:00
parent c3d2295ed1
commit 6cac7327fa
4 changed files with 16 additions and 36 deletions

View File

@@ -11,39 +11,19 @@ import { browser } from "$app/environment";
*/ */
export type ClientDb = ReturnType<typeof drizzle>; export type ClientDb = ReturnType<typeof drizzle>;
type ClientDbHolder = { // this is kinda mid
db: ClientDb; export const sqlocal = browser ? await initDb() : null;
overwriteDatabaseFile: ( export const db = sqlocal ? drizzle(sqlocal.driver, sqlocal.batchDriver) : null;
input: ReadableStream<Uint8Array> | Blob | Uint8Array,
) => Promise<void>;
};
let holder: ClientDbHolder | null = null; async function initDb() {
/**
* 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<ClientDbHolder> {
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.
const { SQLocalDrizzle } = await import("sqlocal/drizzle"); const { SQLocalDrizzle } = await import("sqlocal/drizzle");
return new SQLocalDrizzle("database.sqlite3");
const { driver, batchDriver, overwriteDatabaseFile } = new SQLocalDrizzle( }
"database.sqlite3",
); export function getClientDb() {
if (!sqlocal || !db) throw "Client DB can only be accessed from the browser";
holder = { return {
db: drizzle(driver, batchDriver), db,
overwriteDatabaseFile, overwriteDatabaseFile: sqlocal.overwriteDatabaseFile,
}; };
return holder;
} }

View File

@@ -64,7 +64,7 @@ export async function ensureSeeded(
} }
// Prefer streaming when possible. // Prefer streaming when possible.
const { overwriteDatabaseFile } = await getClientDb(); const { overwriteDatabaseFile } = getClientDb();
if (res.body) { if (res.body) {
await overwriteDatabaseFile(res.body); await overwriteDatabaseFile(res.body);

View File

@@ -34,7 +34,7 @@
try { try {
isSearching = true; isSearching = true;
const { db } = await getClientDb(); const { db } = getClientDb();
if (!q) { if (!q) {
anime = await getAnimeList(db, 20); anime = await getAnimeList(db, 20);

View File

@@ -54,7 +54,7 @@
} }
try { try {
const { db } = await getClientDb(); const { db } = getClientDb();
await ensureSeeded(); await ensureSeeded();
const res = await getAnimeWithSongsByAnnId(db, annId); const res = await getAnimeWithSongsByAnnId(db, annId);