preciazo/db-datos/db.js

23 lines
789 B
JavaScript
Raw Permalink Normal View History

// @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-04-12 23:32:35 +00:00
sqlite.exec(`PRAGMA journal_mode = WAL;`);
sqlite.exec(`PRAGMA busy_timeout = 15000;`);
sqlite.exec(`PRAGMA synchronous = NORMAL;`);
sqlite.exec(`PRAGMA cache_size = 1000000000;`);
sqlite.exec(`PRAGMA foreign_keys = true;`);
sqlite.exec(`PRAGMA temp_store = memory;`);
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;
}