Files
Codewalkers/apps/server/db/index.ts
Lukas May 34578d39c6 refactor: Restructure monorepo to apps/server/ and apps/web/ layout
Move src/ → apps/server/ and packages/web/ → apps/web/ to adopt
standard monorepo conventions (apps/ for runnable apps, packages/
for reusable libraries). Update all config files, shared package
imports, test fixtures, and documentation to reflect new paths.

Key fixes:
- Update workspace config to ["apps/*", "packages/*"]
- Update tsconfig.json rootDir/include for apps/server/
- Add apps/web/** to vitest exclude list
- Update drizzle.config.ts schema path
- Fix ensure-schema.ts migration path detection (3 levels up in dev,
  2 levels up in dist)
- Fix tests/integration/cli-server.test.ts import paths
- Update packages/shared imports to apps/server/ paths
- Update all docs/ files with new paths
2026-03-03 11:22:53 +01:00

53 lines
1.6 KiB
TypeScript

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';