import { DrizzleAdapter } from '@nextlyhq/adapter-drizzle'; import { BaseAdapterConfig } from '@nextlyhq/adapter-drizzle/types'; /** * @fileoverview Database adapter factory with environment-based selection * * This module provides factory functions for creating database adapters based on * environment configuration. It supports PostgreSQL, MySQL, and SQLite with * automatic adapter selection via DB_DIALECT environment variable or DATABASE_URL * protocol detection. * * Key features: * - Tree-shakeable: Only bundles the adapter you use (via dynamic imports) * - Environment-first: Auto-detects from DB_DIALECT or DATABASE_URL * - Type-safe: Full TypeScript support with adapter capabilities * - Zero-config: Works with just environment variables * * @example * ```typescript * // Auto-detect from environment * const adapter = await createAdapterFromEnv(); * * // Explicit configuration * const adapter = await createAdapter({ * type: 'postgresql', * url: 'postgres://localhost:5432/mydb', * }); * ``` * * @module database/factory */ /** * Supported database adapter types * * @public */ type AdapterType = "postgresql" | "mysql" | "sqlite"; /** * Configuration for creating a database adapter * * Extends BaseAdapterConfig with an optional type field for explicit * adapter selection. If type is not specified, the factory will detect * it from environment variables. * * @public */ interface AdapterConfig extends BaseAdapterConfig { /** * Database adapter type * * If not specified, will be detected from DB_DIALECT environment variable * or DATABASE_URL protocol. */ type?: AdapterType; } /** * Create a database adapter based on configuration * * This is the main factory function that creates and connects the appropriate * database adapter. If type is not specified in config, it will be detected * from environment variables. * * Dynamic imports are used to enable tree-shaking - only the adapter you use * will be bundled in production. * * @param config - Optional adapter configuration. If omitted, uses environment variables. * @returns Connected DrizzleAdapter instance * @throws {Error} If DATABASE_URL is missing for PostgreSQL/MySQL * @throws {Error} If unsupported database type is specified * * @example * ```typescript * // Auto-detect from environment * const adapter = await createAdapter(); * * // Explicit PostgreSQL * const adapter = await createAdapter({ * type: 'postgresql', * url: 'postgres://localhost:5432/mydb', * }); * * // SQLite with custom path * const adapter = await createAdapter({ * type: 'sqlite', * url: 'file:./data/production.db', * }); * ``` * * @public */ declare function createAdapter(config?: AdapterConfig): Promise; /** * Create adapter from environment variables only * * Convenience wrapper that creates an adapter using only environment * configuration. Equivalent to calling createAdapter() with no arguments. * * @returns Connected DrizzleAdapter instance * @throws {Error} If environment configuration is invalid * * @example * ```typescript * // In your .env file: * // DB_DIALECT=postgres * // DATABASE_URL=postgres://localhost:5432/mydb * * const adapter = await createAdapterFromEnv(); * ``` * * @public */ declare function createAdapterFromEnv(): Promise; /** * Validate database environment configuration * * Checks that environment variables are properly configured before * attempting to create an adapter. This allows catching configuration * errors early in the application startup. * * @returns Validation result with errors if any * * @example * ```typescript * const validation = validateDatabaseEnv(); * if (!validation.valid) { * console.error('Database configuration errors:'); * validation.errors.forEach(err => console.error(` - ${err}`)); * process.exit(1); * } * ``` * * @public */ declare function validateDatabaseEnv(): { valid: boolean; errors: string[]; }; /** * Health check for database adapter * * Tests database connectivity and returns health status with connection * statistics. This function connects to the database if not already connected, * executes a simple test query, and reports the results. * * @param adapter - Database adapter to check * @returns Health check result with connection status and statistics * * @example * ```typescript * const adapter = await createAdapterFromEnv(); * const health = await checkAdapterHealth(adapter); * * if (health.healthy) { * console.log(`Database ${health.dialect} is healthy`); * console.log('Pool stats:', health.poolStats); * } else { * console.error(`Database error: ${health.error}`); * } * ``` * * @public */ declare function checkAdapterHealth(adapter: DrizzleAdapter): Promise<{ healthy: boolean; dialect: string; connected: boolean; error?: string; poolStats?: { total: number; idle: number; waiting: number; active: number; } | null; }>; export { createAdapter as b, checkAdapterHealth as c, createAdapterFromEnv as d, validateDatabaseEnv as v }; export type { AdapterConfig as A, AdapterType as a };