2024-01-14 01:07:23 +00:00
|
|
|
// @ts-check
|
|
|
|
import Database from "better-sqlite3";
|
|
|
|
import { drizzle } from "drizzle-orm/better-sqlite3";
|
2023-12-30 00:49:32 +00:00
|
|
|
import { DB_PATH } from "./drizzle.config.js";
|
|
|
|
import * as schema from "./schema.js";
|
2024-01-24 23:13:20 +00:00
|
|
|
import { migrateDb } from "./migrate.js";
|
2023-12-30 00:49:32 +00:00
|
|
|
|
2024-01-24 23:13:20 +00:00
|
|
|
/** @type {null | import("drizzle-orm/better-sqlite3").BetterSQLite3Database<schema>} */
|
|
|
|
let db = null;
|
|
|
|
export function getDb() {
|
2024-04-05 01:59:46 +00:00
|
|
|
if (db) return db;
|
2024-01-24 23:13:20 +00:00
|
|
|
const sqlite = new Database(DB_PATH);
|
2024-07-13 18:49:52 +00:00
|
|
|
sqlite.pragma(`journal_mode = WAL`);
|
|
|
|
sqlite.pragma(`busy_timeout = 15000`);
|
|
|
|
sqlite.pragma(`foreign_keys = true`);
|
2024-02-07 02:44:16 +00:00
|
|
|
db = drizzle(sqlite, { schema, logger: true });
|
2024-01-24 23:13:20 +00:00
|
|
|
migrateDb(db);
|
|
|
|
return db;
|
|
|
|
}
|