/** * @fileoverview Chahua Database Manager - Database Operations Module * @description โมดูลสำหรับการดำเนินการฐานข้อมูลผ่าน REST API * คุณสมบัติ: Connection testing, Query execution, Database structure retrieval * @author ทีมพัฒนา Chahua * @version 1.0.0 * @since 2024-01-01 * @calledBy app.js, UI components * @dependencies * - fetch API: HTTP request handling * - database-server.js: Backend API endpoints * @features * - Database connection testing and validation * - SQL query execution with error handling * - Database structure discovery * - Connection management operations * - HTTP error handling and retries * @security * - Input sanitization for queries * - Error message filtering * - Connection parameter validation * @example * // Test database connection * const dbOps = new DatabaseOperations(); * const result = await dbOps.testConnection(connectionConfig); * * // Execute SQL query * const queryResult = await dbOps.executeQuery(connectionId, 'SELECT * FROM users'); */ // Database operations module /** * Database Operations - Class for handling database operations via REST API * @class DatabaseOperations * @description Provides methods for database connection testing, query execution, and structure discovery * @features * - Connection management and testing * - SQL query execution with error handling * - Database structure discovery * - HTTP-based communication with backend server */ class DatabaseOperations { /** * Create a DatabaseOperations instance * @constructor * @description Initializes the database operations with default API base URL */ constructor() { this.baseUrl = 'http://localhost:3001/api'; } /** * Test database connection * @async * @function testConnection * @param {Object} connectionData - Database connection configuration * @param {string} connectionData.type - Database type (postgresql, mysql, mongodb, redis) * @param {string} connectionData.host - Database host * @param {number} connectionData.port - Database port * @param {string} connectionData.database - Database name * @param {string} connectionData.username - Username for authentication * @param {string} connectionData.password - Password for authentication * @returns {Promise} Connection test result * @throws {Error} Network or connection errors */ async testConnection(connectionData) { try { const response = await fetch(`${this.baseUrl}/connections`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(connectionData) }); return await response.json(); } catch (error) { return { success: false, error: error.message }; } } /** * Get all saved connections * @async * @function getConnections * @returns {Promise} List of saved database connections * @throws {Error} Network or server errors */ async getConnections() { try { const response = await fetch(`${this.baseUrl}/connections`); return await response.json(); } catch (error) { return { success: false, error: error.message }; } } /** * Execute SQL query on specified connection * @async * @function executeQuery * @param {string} connectionId - Unique identifier for the database connection * @param {string} query - SQL query to execute * @returns {Promise} Query execution result with data or error * @throws {Error} Network, query syntax, or database errors */ async executeQuery(connectionId, query) { try { const response = await fetch(`${this.baseUrl}/connections/${connectionId}/query`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ query }) }); return await response.json(); } catch (error) { return { success: false, error: error.message }; } } /** * Get database structure (tables, columns, relationships) * @async * @function getDatabaseStructure * @param {string} connectionId - Unique identifier for the database connection * @returns {Promise} Database structure information * @throws {Error} Network or database access errors */ async getDatabaseStructure(connectionId) { try { const response = await fetch(`${this.baseUrl}/connections/${connectionId}/structure`); return await response.json(); } catch (error) { return { success: false, error: error.message }; } } async getTableData(connectionId, tableName, page = 1, limit = 50) { try { const response = await fetch(`${this.baseUrl}/connections/${connectionId}/tables/${tableName}/data?page=${page}&limit=${limit}`); return await response.json(); } catch (error) { return { success: false, error: error.message }; } } } // Make it available globally if (typeof window !== 'undefined') { window.DatabaseOperations = DatabaseOperations; }