import { Database } from "bun:sqlite";
import { BunSQLiteDatabase, drizzle } from "drizzle-orm/bun-sqlite";
import * as schema from '../db/schema';

let instance: BunSQLiteDatabase<typeof schema>;
export default function useDatabase()
{
    if(!instance)
    {
        const database = useRuntimeConfig().database;
        const sqlite = new Database(database);
        instance = drizzle({ client: sqlite, schema, /* logger: true */ });

        instance.run("PRAGMA journal_mode = WAL;");
        instance.run("PRAGMA foreign_keys = true;");
    }

    return instance;
}