All files / src/db index.ts

0% Statements 0/6
0% Branches 0/2
0% Functions 0/1
0% Lines 0/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53                                                                                                         
import Database from 'better-sqlite3';
import { drizzle } from 'drizzle-orm/better-sqlite3';
import type { BetterSQLite3Database } from 'drizzle-orm/better-sqlite3';
 
import { getDbPath, ensureDbDirectory } from './config.js';
import * as schema from './schema.js';
 
export type DrizzleDatabase = BetterSQLite3Database<typeof schema>;
 
/**
 * Create a new database connection.
 *
 * This is a factory function (not a singleton) to allow multiple instances
 * for testing with isolated databases.
 *
 * @param path - Optional path override. Defaults to getDbPath().
 *               Use ':memory:' for in-memory testing database.
 * @returns Drizzle database instance with schema
 */
export function createDatabase(path?: string): DrizzleDatabase {
  const dbPath = path ?? getDbPath();
 
  // Ensure directory exists for file-based databases
  ensureDbDirectory(dbPath);
 
  // Create SQLite connection
  const sqlite = new Database(dbPath);
 
  // Enable WAL mode for better concurrent read performance
  sqlite.pragma('journal_mode = WAL');
 
  // Enable foreign keys (SQLite has them disabled by default)
  sqlite.pragma('foreign_keys = ON');
 
  // Create Drizzle instance with schema
  return drizzle(sqlite, { schema });
}
 
// Re-export config utilities
export { getDbPath, ensureDbDirectory } from './config.js';
 
// Re-export schema initialization
export { ensureSchema } from './ensure-schema.js';
 
// Re-export schema and types
export * from './schema.js';
 
// Re-export repository interfaces (ports)
export * from './repositories/index.js';
 
// Re-export Drizzle adapters
export * from './repositories/drizzle/index.js';