import Database from 'better-sqlite3'; import fs from 'node:fs'; import path from 'node:path'; export type DB = Database.Database; // Amounts are stored as integer cents (分) to avoid floating point drift. // Times are stored as epoch milliseconds. const SCHEMA = ` CREATE TABLE IF NOT EXISTS members ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL UNIQUE, color TEXT NOT NULL DEFAULT '#3b82f6', sort INTEGER NOT NULL DEFAULT 0 ); CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL UNIQUE, password_hash TEXT NOT NULL, member_id INTEGER NOT NULL REFERENCES members(id) ); CREATE TABLE IF NOT EXISTS sessions ( token TEXT PRIMARY KEY, user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, expires_at INTEGER NOT NULL ); CREATE TABLE IF NOT EXISTS books ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL UNIQUE, sort INTEGER NOT NULL DEFAULT 0 ); CREATE TABLE IF NOT EXISTS categories ( id INTEGER PRIMARY KEY AUTOINCREMENT, parent_id INTEGER REFERENCES categories(id), type TEXT NOT NULL CHECK (type IN ('expense', 'income')), name TEXT NOT NULL, icon TEXT, sort INTEGER NOT NULL DEFAULT 0, hidden INTEGER NOT NULL DEFAULT 0 ); CREATE UNIQUE INDEX IF NOT EXISTS categories_unique ON categories(type, IFNULL(parent_id, 0), name); CREATE TABLE IF NOT EXISTS bills ( id INTEGER PRIMARY KEY AUTOINCREMENT, book_id INTEGER NOT NULL REFERENCES books(id), type TEXT NOT NULL CHECK (type IN ('expense', 'income')), category_id INTEGER NOT NULL REFERENCES categories(id), amount INTEGER NOT NULL CHECK (amount >= 0), time INTEGER NOT NULL, note TEXT NOT NULL DEFAULT '', payer_id INTEGER REFERENCES members(id), exclude_budget INTEGER NOT NULL DEFAULT 0, created_by INTEGER REFERENCES users(id), created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL, deleted_at INTEGER, import_key TEXT UNIQUE ); CREATE INDEX IF NOT EXISTS bills_time ON bills(book_id, time); -- Who the bill is for, and each member's share of it. CREATE TABLE IF NOT EXISTS bill_members ( bill_id INTEGER NOT NULL REFERENCES bills(id) ON DELETE CASCADE, member_id INTEGER NOT NULL REFERENCES members(id), share INTEGER NOT NULL, PRIMARY KEY (bill_id, member_id) ); -- A money transfer between members that settles shared expenses. CREATE TABLE IF NOT EXISTS settlements ( id INTEGER PRIMARY KEY AUTOINCREMENT, from_member INTEGER NOT NULL REFERENCES members(id), to_member INTEGER NOT NULL REFERENCES members(id), amount INTEGER NOT NULL CHECK (amount > 0), time INTEGER NOT NULL, note TEXT NOT NULL DEFAULT '', created_by INTEGER REFERENCES users(id), deleted_at INTEGER ); -- category_id NULL means the whole book's monthly budget. CREATE TABLE IF NOT EXISTS budgets ( id INTEGER PRIMARY KEY AUTOINCREMENT, book_id INTEGER NOT NULL REFERENCES books(id), category_id INTEGER REFERENCES categories(id), amount INTEGER NOT NULL CHECK (amount > 0) ); CREATE UNIQUE INDEX IF NOT EXISTS budgets_unique ON budgets(book_id, IFNULL(category_id, 0)); -- Records which budget thresholds were already notified for a month. CREATE TABLE IF NOT EXISTS budget_alerts ( budget_id INTEGER NOT NULL REFERENCES budgets(id) ON DELETE CASCADE, month TEXT NOT NULL, level INTEGER NOT NULL, PRIMARY KEY (budget_id, month, level) ); CREATE TABLE IF NOT EXISTS push_subscriptions ( endpoint TEXT PRIMARY KEY, user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, data TEXT NOT NULL ); CREATE TABLE IF NOT EXISTS kv ( key TEXT PRIMARY KEY, value TEXT NOT NULL ); `; export function openDb(file: string): DB { if (file !== ':memory:') fs.mkdirSync(path.dirname(file), { recursive: true }); const db = new Database(file); db.pragma('journal_mode = WAL'); db.pragma('foreign_keys = ON'); db.exec(SCHEMA); return db; } export function kvGet(db: DB, key: string): string | undefined { const row = db.prepare('SELECT value FROM kv WHERE key = ?').get(key) as { value: string } | undefined; return row?.value; } export function kvSet(db: DB, key: string, value: string) { db.prepare('INSERT INTO kv (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value').run(key, value); }