// src/schema.ts import { sql } from "drizzle-orm"; import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core"; var appSettings = sqliteTable("app_settings", { id: text("id").primaryKey(), show_quick_actions: integer("show_quick_actions", { mode: "boolean" }).notNull().default(true), created_at: text("created_at").notNull().default(sql`datetime('now')`), updated_at: text("updated_at").notNull().default(sql`datetime('now')`) }); var schema = { appSettings }; // src/client.ts import { dirname, join } from "path"; import { fileURLToPath } from "url"; import { readFileSync, readdirSync } from "fs"; import Database from "better-sqlite3"; import { drizzle } from "drizzle-orm/better-sqlite3"; var __dirname = dirname(fileURLToPath(import.meta.url)); function resolveDatabasePath(databaseUrl) { const url = databaseUrl.startsWith("sqlite:") ? databaseUrl.slice("sqlite:".length) : databaseUrl; return url; } function createDb(databaseUrl) { const path = resolveDatabasePath(databaseUrl); const sqlite = new Database(path); sqlite.pragma("journal_mode = WAL"); sqlite.pragma("synchronous = NORMAL"); sqlite.pragma("foreign_keys = ON"); const db = drizzle(sqlite, { schema }); return { db, sqlite }; } function createMemoryDb() { const sqlite = new Database(":memory:"); sqlite.pragma("foreign_keys = ON"); const db = drizzle(sqlite, { schema }); return { db, sqlite }; } function runMigrations(sqlite) { const migrationsDir = join(__dirname, "..", "migrations"); const files = readdirSync(migrationsDir).filter((f) => f.endsWith(".sql")).sort(); sqlite.exec( `CREATE TABLE IF NOT EXISTS _migrations ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL UNIQUE, applied_at TEXT NOT NULL DEFAULT (datetime('now')) )` ); for (const file of files) { const applied = sqlite.prepare("SELECT 1 FROM _migrations WHERE name = ?").get(file); if (applied) continue; const sql2 = readFileSync(join(migrationsDir, file), "utf-8"); sqlite.exec(sql2); sqlite.prepare("INSERT INTO _migrations (name) VALUES (?)").run(file); } } function healthCheck(sqlite) { sqlite.prepare("SELECT 1").get(); } // src/errors.ts var NotFoundError = class extends Error { constructor(message) { super(message); this.name = "NotFoundError"; } }; var ConflictError = class extends Error { constructor(message) { super(message); this.name = "ConflictError"; } }; // src/settings-repo.ts import { eq } from "drizzle-orm"; var SETTINGS_ID = "settings-main"; function toDto(row) { return { id: row.id, showQuickActions: row.show_quick_actions == null ? true : Boolean(row.show_quick_actions) }; } function ensureRow(db) { const existing = db.select().from(appSettings).where(eq(appSettings.id, SETTINGS_ID)).get(); if (existing) return existing; db.insert(appSettings).values({ id: SETTINGS_ID, show_quick_actions: true }).run(); return db.select().from(appSettings).where(eq(appSettings.id, SETTINGS_ID)).get(); } function getAppSettings(db) { return toDto(ensureRow(db)); } function updateAppSettings(db, patch) { ensureRow(db); const updates = { updated_at: (/* @__PURE__ */ new Date()).toISOString().replace("T", " ").slice(0, 19) }; if (patch.showQuickActions !== void 0) { updates.show_quick_actions = patch.showQuickActions; } db.update(appSettings).set(updates).where(eq(appSettings.id, SETTINGS_ID)).run(); return getAppSettings(db); } export { ConflictError, NotFoundError, appSettings, createDb, createMemoryDb, getAppSettings, healthCheck, resolveDatabasePath, runMigrations, schema, updateAppSettings };