File size: 5,584 Bytes
8eeb77a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | /**
* @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<Object>} 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<Object>} 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<Object>} 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<Object>} 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;
}
|