connection.ts•1.51 kB
import { drizzle } from 'drizzle-orm/libsql';
import { createClient } from '@libsql/client';
import { ConfigManager } from '../config/manager';
import * as schema from './schema';
import { logger } from '../utils/logger';
let db: ReturnType<typeof drizzle> | null = null;
export async function getDatabase() {
if (db) {
return db;
}
const configManager = new ConfigManager();
const dbPath = configManager.getDatabasePath();
try {
// Use libsql with local file
const client = createClient({
url: `file:${dbPath}`,
});
db = drizzle(client, { schema });
// Test the connection by creating the table if it doesn't exist
// This is a simple way to initialize the database
try {
await db.select().from(schema.llmRequests).limit(1).execute();
} catch {
// Table might not exist yet, that's fine
logger.log('Database initialized, will create tables on first migration');
}
logger.log('Database connected using @libsql/client');
return db;
} catch (error) {
throw new Error(`Failed to connect to database: ${error instanceof Error ? error.message : String(error)}`);
}
}
export function isDatabaseConnected(): boolean {
return db !== null;
}
// Close database connection
export async function closeDatabase(): Promise<void> {
if (db) {
try {
// libsql client doesn't need explicit closing
db = null;
} catch (error) {
logger.warn('Error closing database:', error);
}
}
}