an absurd amount of tomfoolery

This commit is contained in:
2026-02-06 12:50:39 -08:00
parent 7b8ccb0e83
commit b26bec726f
5 changed files with 18 additions and 4 deletions

View File

@@ -9,17 +9,26 @@ import { browser } from "$app/environment";
* Exported to allow query helpers to accept the specific type without
* creating circular type references.
*/
export type ClientDb = ReturnType<typeof drizzle>;
// this is kinda mid
export const sqlocal = browser ? await initDb() : null;
export const db = sqlocal ? drizzle(sqlocal.driver, sqlocal.batchDriver) : null;
// wacky typecasting, but it works to give type hints for db queries
export const db = sqlocal
? drizzle(sqlocal.driver, sqlocal.batchDriver)
: ((await serverDb()) as unknown as ReturnType<typeof drizzle>);
export type ClientDb = typeof db;
async function initDb() {
const { SQLocalDrizzle } = await import("sqlocal/drizzle");
return new SQLocalDrizzle("database.sqlite3");
}
async function serverDb() {
const { db } = await import("..");
return db;
}
export function getClientDb() {
if (!sqlocal || !db) throw "Client DB can only be accessed from the browser";
return {

View File

@@ -41,6 +41,7 @@ function ensureBrowser() {
export async function ensureSeeded(
opts: { version?: number; force?: boolean; fetch?: typeof fetch } = {},
): Promise<void> {
if (!browser) return;
ensureBrowser();
const version = opts.version ?? AMQ_DB_SEED_VERSION;

View File

@@ -1,4 +1,4 @@
// import 'dotenv/config';
import { drizzle } from "drizzle-orm/bun-sqlite";
import "dotenv/config";
import { drizzle } from "drizzle-orm/better-sqlite3";
export const db = drizzle(process.env.DB_FILE_NAME);