import { PostgreSQLClient } from './client'; import { postgresConfig } from '@stock-bot/config'; import type { PostgreSQLClientConfig, PostgreSQLConnectionOptions } from './types'; /** * Factory function to create a PostgreSQL client instance */ export function createPostgreSQLClient( config?: Partial, options?: PostgreSQLConnectionOptions ): PostgreSQLClient { return new PostgreSQLClient(config, options); } /** * Create a PostgreSQL client with default configuration */ export function createDefaultPostgreSQLClient(): PostgreSQLClient { const config: Partial = { host: postgresConfig.POSTGRES_HOST, port: postgresConfig.POSTGRES_PORT, database: postgresConfig.POSTGRES_DATABASE, username: postgresConfig.POSTGRES_USERNAME, password: postgresConfig.POSTGRES_PASSWORD }; return new PostgreSQLClient(config); } /** * Singleton PostgreSQL client instance */ let defaultClient: PostgreSQLClient | null = null; /** * Get or create the default PostgreSQL client instance */ export function getPostgreSQLClient(): PostgreSQLClient { if (!defaultClient) { defaultClient = createDefaultPostgreSQLClient(); } return defaultClient; } /** * Connect to PostgreSQL using the default client */ export async function connectPostgreSQL(): Promise { const client = getPostgreSQLClient(); if (!client.connected) { await client.connect(); } return client; } /** * Disconnect from PostgreSQL */ export async function disconnectPostgreSQL(): Promise { if (defaultClient) { await defaultClient.disconnect(); defaultClient = null; } }