an absurd amount of tomfoolery
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user