ERP-node/frontend/lib/api/externalDbConnection.ts

273 lines
7.4 KiB
TypeScript
Raw Normal View History

2025-09-18 09:32:50 +09:00
// 외부 DB 연결 API 클라이언트
2025-09-19 12:15:14 +09:00
// 작성일: 2024-12-19
2025-09-18 09:32:50 +09:00
2025-09-19 12:15:14 +09:00
import { apiClient } from "./client";
2025-09-18 09:32:50 +09:00
export interface ExternalDbConnection {
id?: number;
connection_name: string;
description?: string;
db_type: "mysql" | "postgresql" | "oracle" | "mssql" | "sqlite";
host: string;
port: number;
database_name: string;
username: string;
password: string;
connection_timeout?: number;
query_timeout?: number;
max_connections?: number;
ssl_enabled?: string;
ssl_cert_path?: string;
connection_options?: Record<string, unknown>;
company_code: string;
is_active: string;
created_date?: Date;
created_by?: string;
updated_date?: Date;
updated_by?: string;
}
export interface ExternalDbConnectionFilter {
db_type?: string;
is_active?: string;
company_code?: string;
search?: string;
}
export interface ApiResponse<T> {
success: boolean;
data?: T;
message?: string;
2025-09-19 12:15:14 +09:00
error?: {
code?: string;
details?: string;
};
2025-09-18 09:32:50 +09:00
}
2025-09-19 12:15:14 +09:00
// 연결 테스트 관련 타입
export interface ConnectionTestRequest {
db_type: string;
host: string;
port: number;
database_name: string;
username: string;
password: string;
connection_timeout?: number;
ssl_enabled?: string;
}
2025-09-18 09:32:50 +09:00
2025-09-19 12:15:14 +09:00
export interface ConnectionTestResult {
success: boolean;
message: string;
details?: {
response_time?: number;
server_version?: string;
database_size?: string;
};
error?: {
code?: string;
details?: string;
};
}
2025-09-18 09:32:50 +09:00
2025-09-19 12:15:14 +09:00
export class ExternalDbConnectionAPI {
private static readonly BASE_PATH = "/external-db-connections";
2025-09-18 09:32:50 +09:00
/**
* DB
*/
2025-09-19 12:15:14 +09:00
static async getConnections(filter: ExternalDbConnectionFilter = {}): Promise<ExternalDbConnection[]> {
2025-09-18 09:32:50 +09:00
try {
const params = new URLSearchParams();
2025-09-19 12:15:14 +09:00
if (filter.db_type) params.append("db_type", filter.db_type);
if (filter.is_active) params.append("is_active", filter.is_active);
if (filter.company_code) params.append("company_code", filter.company_code);
if (filter.search) params.append("search", filter.search);
2025-09-18 09:32:50 +09:00
2025-09-19 12:15:14 +09:00
const response = await apiClient.get<ApiResponse<ExternalDbConnection[]>>(
`${this.BASE_PATH}?${params.toString()}`,
);
2025-09-18 09:32:50 +09:00
2025-09-19 12:15:14 +09:00
if (!response.data.success) {
throw new Error(response.data.message || "연결 목록 조회에 실패했습니다.");
}
2025-09-18 09:32:50 +09:00
2025-09-19 12:15:14 +09:00
return response.data.data || [];
2025-09-18 09:32:50 +09:00
} catch (error) {
console.error("외부 DB 연결 목록 조회 오류:", error);
2025-09-19 12:15:14 +09:00
throw error;
2025-09-18 09:32:50 +09:00
}
}
/**
* DB
*/
2025-09-19 12:15:14 +09:00
static async getConnectionById(id: number): Promise<ExternalDbConnection> {
2025-09-18 09:32:50 +09:00
try {
2025-09-19 12:15:14 +09:00
const response = await apiClient.get<ApiResponse<ExternalDbConnection>>(`${this.BASE_PATH}/${id}`);
if (!response.data.success) {
throw new Error(response.data.message || "연결 정보 조회에 실패했습니다.");
}
if (!response.data.data) {
throw new Error("연결 정보를 찾을 수 없습니다.");
}
2025-09-18 09:32:50 +09:00
2025-09-19 12:15:14 +09:00
return response.data.data;
2025-09-18 09:32:50 +09:00
} catch (error) {
console.error("외부 DB 연결 조회 오류:", error);
2025-09-19 12:15:14 +09:00
throw error;
2025-09-18 09:32:50 +09:00
}
}
/**
* DB
*/
2025-09-19 12:15:14 +09:00
static async createConnection(data: ExternalDbConnection): Promise<ExternalDbConnection> {
2025-09-18 09:32:50 +09:00
try {
2025-09-19 12:15:14 +09:00
const response = await apiClient.post<ApiResponse<ExternalDbConnection>>(this.BASE_PATH, data);
if (!response.data.success) {
throw new Error(response.data.message || "연결 생성에 실패했습니다.");
}
2025-09-18 09:32:50 +09:00
2025-09-19 12:15:14 +09:00
if (!response.data.data) {
throw new Error("생성된 연결 정보를 받을 수 없습니다.");
}
return response.data.data;
2025-09-18 09:32:50 +09:00
} catch (error) {
console.error("외부 DB 연결 생성 오류:", error);
2025-09-19 12:15:14 +09:00
throw error;
2025-09-18 09:32:50 +09:00
}
}
/**
* DB
*/
2025-09-19 12:15:14 +09:00
static async updateConnection(id: number, data: ExternalDbConnection): Promise<ExternalDbConnection> {
2025-09-18 09:32:50 +09:00
try {
2025-09-19 12:15:14 +09:00
const response = await apiClient.put<ApiResponse<ExternalDbConnection>>(`${this.BASE_PATH}/${id}`, data);
if (!response.data.success) {
throw new Error(response.data.message || "연결 수정에 실패했습니다.");
}
if (!response.data.data) {
throw new Error("수정된 연결 정보를 받을 수 없습니다.");
}
2025-09-18 09:32:50 +09:00
2025-09-19 12:15:14 +09:00
return response.data.data;
2025-09-18 09:32:50 +09:00
} catch (error) {
console.error("외부 DB 연결 수정 오류:", error);
2025-09-19 12:15:14 +09:00
throw error;
2025-09-18 09:32:50 +09:00
}
}
/**
* DB
*/
2025-09-19 12:15:14 +09:00
static async deleteConnection(id: number): Promise<void> {
2025-09-18 09:32:50 +09:00
try {
2025-09-19 12:15:14 +09:00
const response = await apiClient.delete<ApiResponse<null>>(`${this.BASE_PATH}/${id}`);
2025-09-18 09:32:50 +09:00
2025-09-19 12:15:14 +09:00
if (!response.data.success) {
throw new Error(response.data.message || "연결 삭제에 실패했습니다.");
}
2025-09-18 09:32:50 +09:00
} catch (error) {
console.error("외부 DB 연결 삭제 오류:", error);
2025-09-19 12:15:14 +09:00
throw error;
2025-09-18 09:32:50 +09:00
}
}
/**
2025-09-19 12:15:14 +09:00
* DB
2025-09-18 09:32:50 +09:00
*/
2025-09-19 12:15:14 +09:00
static async getSupportedTypes(): Promise<Array<{ value: string; label: string }>> {
2025-09-18 09:32:50 +09:00
try {
2025-09-19 12:15:14 +09:00
const response = await apiClient.get<ApiResponse<{ types: Array<{ value: string; label: string }> }>>(
`${this.BASE_PATH}/types/supported`,
);
2025-09-18 09:32:50 +09:00
2025-09-19 12:15:14 +09:00
if (!response.data.success) {
throw new Error(response.data.message || "지원 DB 타입 조회에 실패했습니다.");
}
return response.data.data?.types || [];
2025-09-18 09:32:50 +09:00
} catch (error) {
console.error("지원 DB 타입 조회 오류:", error);
2025-09-19 12:15:14 +09:00
throw error;
}
}
/**
2025-09-22 17:28:31 +09:00
* (ID )
2025-09-19 12:15:14 +09:00
*/
2025-09-22 17:28:31 +09:00
static async testConnection(connectionId: number): Promise<ConnectionTestResult> {
2025-09-19 12:15:14 +09:00
try {
2025-09-22 17:28:31 +09:00
const response = await apiClient.post<ApiResponse<ConnectionTestResult>>(
`${this.BASE_PATH}/${connectionId}/test`
);
2025-09-19 12:15:14 +09:00
if (!response.data.success) {
2025-09-22 17:28:31 +09:00
return {
success: false,
message: response.data.message || "연결 테스트에 실패했습니다.",
error: response.data.error,
};
2025-09-19 12:15:14 +09:00
}
2025-09-22 17:28:31 +09:00
return response.data.data || {
success: true,
message: response.data.message || "연결 테스트가 완료되었습니다.",
};
2025-09-19 12:15:14 +09:00
} catch (error) {
console.error("연결 테스트 오류:", error);
2025-09-18 09:32:50 +09:00
return {
success: false,
2025-09-19 12:15:14 +09:00
message: "연결 테스트 중 오류가 발생했습니다.",
error: {
code: "NETWORK_ERROR",
details: error instanceof Error ? error.message : "알 수 없는 오류",
},
2025-09-18 09:32:50 +09:00
};
}
}
2025-09-22 17:28:31 +09:00
/**
* SQL
*/
/**
*
*/
static async getTables(connectionId: number): Promise<ApiResponse<string[]>> {
try {
const response = await apiClient.get<ApiResponse<string[]>>(
`${this.BASE_PATH}/${connectionId}/tables`
);
return response.data;
} catch (error) {
console.error("테이블 목록 조회 오류:", error);
throw error;
}
}
static async executeQuery(connectionId: number, query: string): Promise<ApiResponse<any[]>> {
try {
console.log("API 요청:", `${this.BASE_PATH}/${connectionId}/execute`, { query });
const response = await apiClient.post<ApiResponse<any[]>>(
`${this.BASE_PATH}/${connectionId}/execute`,
{ query }
);
console.log("API 응답:", response.data);
return response.data;
} catch (error) {
console.error("SQL 쿼리 실행 오류:", error);
throw error;
}
}
2025-09-18 09:32:50 +09:00
}