UNPKG

plugin-postgresql-connector

Version:

NocoBase plugin for connecting to external PostgreSQL databases

185 lines 6.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useConnection = void 0; const react_1 = require("react"); const client_1 = require("@nocobase/client"); const antd_1 = require("antd"); const useConnection = () => { const [selectedConnection, setSelectedConnection] = (0, react_1.useState)(''); const [connections, setConnections] = (0, react_1.useState)([]); // Fetch all connections const { data: connectionsData, loading: loadingConnections, run: refreshConnections, } = (0, client_1.useRequest)({ url: '/postgresql-connections', }); // Test connection const { run: testConnection, loading: testingConnection } = (0, client_1.useRequest)((config) => ({ url: '/postgresql-connections/test', method: 'POST', data: config, }), { manual: true, onSuccess: () => { antd_1.message.success('Kết nối thành công!'); }, onError: (error) => { antd_1.message.error(`Kết nối thất bại: ${error.message}`); throw error; }, }); // Create new connection const { run: createConnection, loading: creatingConnection } = (0, client_1.useRequest)((config) => ({ url: '/postgresql-connections', method: 'POST', data: config, }), { manual: true, onSuccess: (data) => { antd_1.message.success('Tạo kết nối thành công!'); refreshConnections(); return data; }, onError: (error) => { antd_1.message.error(`Tạo kết nối thất bại: ${error.message}`); throw error; }, }); // Update connection const { run: updateConnection, loading: updatingConnection } = (0, client_1.useRequest)(({ id, config }) => ({ url: `/postgresql-connections/${id}`, method: 'PUT', data: config, }), { manual: true, onSuccess: (data) => { antd_1.message.success('Cập nhật kết nối thành công!'); refreshConnections(); return data; }, onError: (error) => { antd_1.message.error(`Cập nhật kết nối thất bại: ${error.message}`); throw error; }, }); // Delete connection const { run: deleteConnection, loading: deletingConnection } = (0, client_1.useRequest)((id) => ({ url: `/postgresql-connections/${id}`, method: 'DELETE', }), { manual: true, onSuccess: () => { antd_1.message.success('Xóa kết nối thành công!'); if (selectedConnection === id) { setSelectedConnection(''); } refreshConnections(); }, onError: (error) => { antd_1.message.error(`Xóa kết nối thất bại: ${error.message}`); throw error; }, }); // Get connection by ID const getConnectionById = (0, react_1.useCallback)((id) => { return connections.find(conn => conn.id === id); }, [connections]); // Get active connections const getActiveConnections = (0, react_1.useCallback)(() => { return connections.filter(conn => conn.isActive); }, [connections]); // Check if connection exists const connectionExists = (0, react_1.useCallback)((name) => { return connections.some(conn => conn.name === name && conn.isActive); }, [connections]); // Validate connection config const validateConnectionConfig = (0, react_1.useCallback)((config) => { const errors = []; if (!config.name?.trim()) { errors.push('Tên kết nối không được để trống'); } else if (config.name.length < 3) { errors.push('Tên kết nối phải có ít nhất 3 ký tự'); } if (!config.host?.trim()) { errors.push('Host không được để trống'); } if (!config.port || config.port < 1 || config.port > 65535) { errors.push('Port phải từ 1 đến 65535'); } if (!config.database?.trim()) { errors.push('Tên database không được để trống'); } if (!config.username?.trim()) { errors.push('Username không được để trống'); } if (!config.password?.trim()) { errors.push('Password không được để trống'); } return errors; }, []); // Test and create connection const testAndCreateConnection = (0, react_1.useCallback)(async (config) => { try { // First test the connection await testConnection(config); // If test successful, create the connection const result = await createConnection(config); return result; } catch (error) { throw error; } }, [testConnection, createConnection]); // Test and update connection const testAndUpdateConnection = (0, react_1.useCallback)(async (id, config) => { try { // First test the connection await testConnection(config); // If test successful, update the connection const result = await updateConnection({ id, config }); return result; } catch (error) { throw error; } }, [testConnection, updateConnection]); // Update connections list when data changes (0, react_1.useEffect)(() => { if (connectionsData?.data) { setConnections(connectionsData.data); } }, [connectionsData]); // Auto-select first connection if none selected (0, react_1.useEffect)(() => { if (!selectedConnection && connections.length > 0) { setSelectedConnection(connections[0].id); } }, [connections, selectedConnection]); return { // State connections, selectedConnection, setSelectedConnection, // Loading states loadingConnections, testingConnection, creatingConnection, updatingConnection, deletingConnection, // Actions refreshConnections, testConnection, createConnection, updateConnection, deleteConnection, testAndCreateConnection, testAndUpdateConnection, // Utilities getConnectionById, getActiveConnections, connectionExists, validateConnectionConfig, }; }; exports.useConnection = useConnection; exports.default = exports.useConnection; //# sourceMappingURL=useConnection.js.map