SPB Git forge

spb/toveli

Public
14commits 1branches 0releases
1.1 MBsize
maindefault branch
yesterdaylast push
TypeScript 50.6% JavaScript 46.1% SQL 2.1% Shell 0.6% CSS 0.5%
1.3 KB · 29 lines typescript
Raw Blame History
1import Database from "better-sqlite3";2import { existsSync, statSync } from "node:fs";3import path from "node:path";45// Base canonique Toveli, ouverte en lecture seule. Le fichier est remplacé atomiquement chaque nuit6// par scripts/sync.ts : on rouvre le handle si l'inode a changé (sinon on lirait l'ancien fichier).7const DB_PATH = process.env.TOVELI_DB ?? path.join(process.cwd(), "data", "toveli.db");8let handle: Database.Database | null = null;9let inode = -1;1011export function dbAvailable(): boolean { return existsSync(/*turbopackIgnore: true*/ DB_PATH); }1213export function getDb(): Database.Database {14  if (!existsSync(/*turbopackIgnore: true*/ DB_PATH)) throw new Error(`Base Toveli absente : ${DB_PATH} — exécuter « pnpm sync »`);15  const ino = statSync(/*turbopackIgnore: true*/ DB_PATH).ino;16  if (!handle || ino !== inode) {17    handle?.close();18    handle = new Database(DB_PATH, { readonly: true, fileMustExist: true });19    handle.pragma("cache_size = -262144"); // 256 Mo20    handle.pragma("mmap_size = 2147483648");21    inode = ino;22  }23  return handle;24}2526export function meta(key: string): string | null {27  try { const r = getDb().prepare("SELECT value FROM meta WHERE key = ?").get(key) as { value: string } | undefined; return r?.value ?? null; } catch { return null; }28}29