Add the v2 data layer: typed interfaces, central DB with migration runner, per-entity CRUD, and agent-runner session DB operations. - src/log.ts: concise message-first logging API - src/types-v2.ts: AgentGroup, MessagingGroup, Session, MessageIn/Out - src/db/: connection (WAL), migration runner, 001-initial schema, CRUD for agent_groups, messaging_groups, sessions, pending_questions - container/agent-runner/src/db/: session DB connection, messages_in reads + status transitions, messages_out writes - 31 new tests, all 277 tests pass Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
34 lines
870 B
TypeScript
34 lines
870 B
TypeScript
import Database from 'better-sqlite3';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
|
|
import { log } from '../log.js';
|
|
|
|
let _db: Database.Database | null = null;
|
|
|
|
export function getDb(): Database.Database {
|
|
if (!_db) throw new Error('Database not initialized. Call initDb() first.');
|
|
return _db;
|
|
}
|
|
|
|
export function initDb(dbPath: string): Database.Database {
|
|
fs.mkdirSync(path.dirname(dbPath), { recursive: true });
|
|
_db = new Database(dbPath);
|
|
_db.pragma('journal_mode = WAL');
|
|
_db.pragma('foreign_keys = ON');
|
|
log.info('Central DB initialized', { path: dbPath });
|
|
return _db;
|
|
}
|
|
|
|
/** For tests only — creates an in-memory DB and runs migrations. */
|
|
export function initTestDb(): Database.Database {
|
|
_db = new Database(':memory:');
|
|
_db.pragma('foreign_keys = ON');
|
|
return _db;
|
|
}
|
|
|
|
export function closeDb(): void {
|
|
_db?.close();
|
|
_db = null;
|
|
}
|