액션 노드들 로직 구현
This commit is contained in:
@@ -103,15 +103,34 @@ export class OracleConnector implements DatabaseConnector {
|
||||
try {
|
||||
const startTime = Date.now();
|
||||
|
||||
// 쿼리 타입 확인 (DML인지 SELECT인지)
|
||||
// 쿼리 타입 확인
|
||||
const isDML = /^\s*(INSERT|UPDATE|DELETE|MERGE)/i.test(query);
|
||||
const isCOMMIT = /^\s*COMMIT/i.test(query);
|
||||
const isROLLBACK = /^\s*ROLLBACK/i.test(query);
|
||||
|
||||
// 🔥 COMMIT/ROLLBACK 명령은 직접 실행
|
||||
if (isCOMMIT || isROLLBACK) {
|
||||
if (isCOMMIT) {
|
||||
await this.connection!.commit();
|
||||
console.log("✅ Oracle COMMIT 실행됨");
|
||||
} else {
|
||||
await this.connection!.rollback();
|
||||
console.log("⚠️ Oracle ROLLBACK 실행됨");
|
||||
}
|
||||
return {
|
||||
rows: [],
|
||||
rowCount: 0,
|
||||
fields: [],
|
||||
affectedRows: 0,
|
||||
};
|
||||
}
|
||||
|
||||
// Oracle XE 21c 쿼리 실행 옵션
|
||||
const options: any = {
|
||||
outFormat: (oracledb as any).OUT_FORMAT_OBJECT, // OBJECT format
|
||||
maxRows: 10000, // XE 제한 고려
|
||||
fetchArraySize: 100,
|
||||
autoCommit: isDML, // ✅ DML 쿼리는 자동 커밋
|
||||
autoCommit: false, // 🔥 수동으로 COMMIT 제어하도록 변경
|
||||
};
|
||||
|
||||
console.log("Oracle 쿼리 실행:", {
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
import axios, { AxiosInstance, AxiosResponse } from 'axios';
|
||||
import { DatabaseConnector, ConnectionConfig, QueryResult } from '../interfaces/DatabaseConnector';
|
||||
import { ConnectionTestResult, TableInfo } from '../types/externalDbTypes';
|
||||
import axios, { AxiosInstance, AxiosResponse } from "axios";
|
||||
import {
|
||||
DatabaseConnector,
|
||||
ConnectionConfig,
|
||||
QueryResult,
|
||||
} from "../interfaces/DatabaseConnector";
|
||||
import { ConnectionTestResult, TableInfo } from "../types/externalDbTypes";
|
||||
|
||||
export interface RestApiConfig {
|
||||
baseUrl: string;
|
||||
@@ -20,16 +24,16 @@ export class RestApiConnector implements DatabaseConnector {
|
||||
|
||||
constructor(config: RestApiConfig) {
|
||||
this.config = config;
|
||||
|
||||
|
||||
// Axios 인스턴스 생성
|
||||
this.httpClient = axios.create({
|
||||
baseURL: config.baseUrl,
|
||||
timeout: config.timeout || 30000,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${config.apiKey}`,
|
||||
'Accept': 'application/json'
|
||||
}
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${config.apiKey}`,
|
||||
Accept: "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
// 요청/응답 인터셉터 설정
|
||||
@@ -40,11 +44,13 @@ export class RestApiConnector implements DatabaseConnector {
|
||||
// 요청 인터셉터
|
||||
this.httpClient.interceptors.request.use(
|
||||
(config) => {
|
||||
console.log(`[RestApiConnector] 요청: ${config.method?.toUpperCase()} ${config.url}`);
|
||||
console.log(
|
||||
`[RestApiConnector] 요청: ${config.method?.toUpperCase()} ${config.url}`
|
||||
);
|
||||
return config;
|
||||
},
|
||||
(error) => {
|
||||
console.error('[RestApiConnector] 요청 오류:', error);
|
||||
console.error("[RestApiConnector] 요청 오류:", error);
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
@@ -52,11 +58,17 @@ export class RestApiConnector implements DatabaseConnector {
|
||||
// 응답 인터셉터
|
||||
this.httpClient.interceptors.response.use(
|
||||
(response) => {
|
||||
console.log(`[RestApiConnector] 응답: ${response.status} ${response.statusText}`);
|
||||
console.log(
|
||||
`[RestApiConnector] 응답: ${response.status} ${response.statusText}`
|
||||
);
|
||||
return response;
|
||||
},
|
||||
(error) => {
|
||||
console.error('[RestApiConnector] 응답 오류:', error.response?.status, error.response?.statusText);
|
||||
console.error(
|
||||
"[RestApiConnector] 응답 오류:",
|
||||
error.response?.status,
|
||||
error.response?.statusText
|
||||
);
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
@@ -65,16 +77,23 @@ export class RestApiConnector implements DatabaseConnector {
|
||||
async connect(): Promise<void> {
|
||||
try {
|
||||
// 연결 테스트 - 기본 엔드포인트 호출
|
||||
await this.httpClient.get('/health', { timeout: 5000 });
|
||||
await this.httpClient.get("/health", { timeout: 5000 });
|
||||
console.log(`[RestApiConnector] 연결 성공: ${this.config.baseUrl}`);
|
||||
} catch (error) {
|
||||
// health 엔드포인트가 없을 수 있으므로 404는 정상으로 처리
|
||||
if (axios.isAxiosError(error) && error.response?.status === 404) {
|
||||
console.log(`[RestApiConnector] 연결 성공 (health 엔드포인트 없음): ${this.config.baseUrl}`);
|
||||
console.log(
|
||||
`[RestApiConnector] 연결 성공 (health 엔드포인트 없음): ${this.config.baseUrl}`
|
||||
);
|
||||
return;
|
||||
}
|
||||
console.error(`[RestApiConnector] 연결 실패: ${this.config.baseUrl}`, error);
|
||||
throw new Error(`REST API 연결 실패: ${error instanceof Error ? error.message : '알 수 없는 오류'}`);
|
||||
console.error(
|
||||
`[RestApiConnector] 연결 실패: ${this.config.baseUrl}`,
|
||||
error
|
||||
);
|
||||
throw new Error(
|
||||
`REST API 연결 실패: ${error instanceof Error ? error.message : "알 수 없는 오류"}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,39 +107,55 @@ export class RestApiConnector implements DatabaseConnector {
|
||||
await this.connect();
|
||||
return {
|
||||
success: true,
|
||||
message: 'REST API 연결이 성공했습니다.',
|
||||
message: "REST API 연결이 성공했습니다.",
|
||||
details: {
|
||||
response_time: Date.now()
|
||||
}
|
||||
response_time: Date.now(),
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
message: error instanceof Error ? error.message : 'REST API 연결에 실패했습니다.',
|
||||
message:
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: "REST API 연결에 실패했습니다.",
|
||||
details: {
|
||||
response_time: Date.now()
|
||||
}
|
||||
response_time: Date.now(),
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async executeQuery(endpoint: string, method: 'GET' | 'POST' | 'PUT' | 'DELETE' = 'GET', data?: any): Promise<QueryResult> {
|
||||
// 🔥 DatabaseConnector 인터페이스 호환용 executeQuery (사용하지 않음)
|
||||
async executeQuery(query: string, params?: any[]): Promise<QueryResult> {
|
||||
// REST API는 executeRequest를 사용해야 함
|
||||
throw new Error(
|
||||
"REST API Connector는 executeQuery를 지원하지 않습니다. executeRequest를 사용하세요."
|
||||
);
|
||||
}
|
||||
|
||||
// 🔥 실제 REST API 요청을 위한 메서드
|
||||
async executeRequest(
|
||||
endpoint: string,
|
||||
method: "GET" | "POST" | "PUT" | "DELETE" = "GET",
|
||||
data?: any
|
||||
): Promise<QueryResult> {
|
||||
try {
|
||||
const startTime = Date.now();
|
||||
let response: AxiosResponse;
|
||||
|
||||
// HTTP 메서드에 따른 요청 실행
|
||||
switch (method.toUpperCase()) {
|
||||
case 'GET':
|
||||
case "GET":
|
||||
response = await this.httpClient.get(endpoint);
|
||||
break;
|
||||
case 'POST':
|
||||
case "POST":
|
||||
response = await this.httpClient.post(endpoint, data);
|
||||
break;
|
||||
case 'PUT':
|
||||
case "PUT":
|
||||
response = await this.httpClient.put(endpoint, data);
|
||||
break;
|
||||
case 'DELETE':
|
||||
case "DELETE":
|
||||
response = await this.httpClient.delete(endpoint);
|
||||
break;
|
||||
default:
|
||||
@@ -133,21 +168,36 @@ export class RestApiConnector implements DatabaseConnector {
|
||||
console.log(`[RestApiConnector] 원본 응답 데이터:`, {
|
||||
type: typeof responseData,
|
||||
isArray: Array.isArray(responseData),
|
||||
keys: typeof responseData === 'object' ? Object.keys(responseData) : 'not object',
|
||||
responseData: responseData
|
||||
keys:
|
||||
typeof responseData === "object"
|
||||
? Object.keys(responseData)
|
||||
: "not object",
|
||||
responseData: responseData,
|
||||
});
|
||||
|
||||
// 응답 데이터 처리
|
||||
let rows: any[];
|
||||
if (Array.isArray(responseData)) {
|
||||
rows = responseData;
|
||||
} else if (responseData && responseData.data && Array.isArray(responseData.data)) {
|
||||
} else if (
|
||||
responseData &&
|
||||
responseData.data &&
|
||||
Array.isArray(responseData.data)
|
||||
) {
|
||||
// API 응답이 {success: true, data: [...]} 형태인 경우
|
||||
rows = responseData.data;
|
||||
} else if (responseData && responseData.data && typeof responseData.data === 'object') {
|
||||
} else if (
|
||||
responseData &&
|
||||
responseData.data &&
|
||||
typeof responseData.data === "object"
|
||||
) {
|
||||
// API 응답이 {success: true, data: {...}} 형태인 경우 (단일 객체)
|
||||
rows = [responseData.data];
|
||||
} else if (responseData && typeof responseData === 'object' && !Array.isArray(responseData)) {
|
||||
} else if (
|
||||
responseData &&
|
||||
typeof responseData === "object" &&
|
||||
!Array.isArray(responseData)
|
||||
) {
|
||||
// 단일 객체 응답인 경우
|
||||
rows = [responseData];
|
||||
} else {
|
||||
@@ -156,8 +206,8 @@ export class RestApiConnector implements DatabaseConnector {
|
||||
|
||||
console.log(`[RestApiConnector] 처리된 rows:`, {
|
||||
rowsLength: rows.length,
|
||||
firstRow: rows.length > 0 ? rows[0] : 'no data',
|
||||
allRows: rows
|
||||
firstRow: rows.length > 0 ? rows[0] : "no data",
|
||||
allRows: rows,
|
||||
});
|
||||
|
||||
console.log(`[RestApiConnector] API 호출 결과:`, {
|
||||
@@ -165,22 +215,32 @@ export class RestApiConnector implements DatabaseConnector {
|
||||
method,
|
||||
status: response.status,
|
||||
rowCount: rows.length,
|
||||
executionTime: `${executionTime}ms`
|
||||
executionTime: `${executionTime}ms`,
|
||||
});
|
||||
|
||||
return {
|
||||
rows: rows,
|
||||
rowCount: rows.length,
|
||||
fields: rows.length > 0 ? Object.keys(rows[0]).map(key => ({ name: key, type: 'string' })) : []
|
||||
fields:
|
||||
rows.length > 0
|
||||
? Object.keys(rows[0]).map((key) => ({ name: key, type: "string" }))
|
||||
: [],
|
||||
};
|
||||
} catch (error) {
|
||||
console.error(`[RestApiConnector] API 호출 오류 (${method} ${endpoint}):`, error);
|
||||
|
||||
console.error(
|
||||
`[RestApiConnector] API 호출 오류 (${method} ${endpoint}):`,
|
||||
error
|
||||
);
|
||||
|
||||
if (axios.isAxiosError(error)) {
|
||||
throw new Error(`REST API 호출 실패: ${error.response?.status} ${error.response?.statusText}`);
|
||||
throw new Error(
|
||||
`REST API 호출 실패: ${error.response?.status} ${error.response?.statusText}`
|
||||
);
|
||||
}
|
||||
|
||||
throw new Error(`REST API 호출 실패: ${error instanceof Error ? error.message : '알 수 없는 오류'}`);
|
||||
|
||||
throw new Error(
|
||||
`REST API 호출 실패: ${error instanceof Error ? error.message : "알 수 없는 오류"}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,20 +249,20 @@ export class RestApiConnector implements DatabaseConnector {
|
||||
// 일반적인 REST API 엔드포인트들을 반환
|
||||
return [
|
||||
{
|
||||
table_name: '/api/users',
|
||||
table_name: "/api/users",
|
||||
columns: [],
|
||||
description: '사용자 정보 API'
|
||||
description: "사용자 정보 API",
|
||||
},
|
||||
{
|
||||
table_name: '/api/data',
|
||||
table_name: "/api/data",
|
||||
columns: [],
|
||||
description: '기본 데이터 API'
|
||||
description: "기본 데이터 API",
|
||||
},
|
||||
{
|
||||
table_name: '/api/custom',
|
||||
table_name: "/api/custom",
|
||||
columns: [],
|
||||
description: '사용자 정의 엔드포인트'
|
||||
}
|
||||
description: "사용자 정의 엔드포인트",
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
@@ -213,22 +273,25 @@ export class RestApiConnector implements DatabaseConnector {
|
||||
async getColumns(endpoint: string): Promise<any[]> {
|
||||
try {
|
||||
// GET 요청으로 샘플 데이터를 가져와서 필드 구조 파악
|
||||
const result = await this.executeQuery(endpoint, 'GET');
|
||||
|
||||
const result = await this.executeRequest(endpoint, "GET");
|
||||
|
||||
if (result.rows.length > 0) {
|
||||
const sampleRow = result.rows[0];
|
||||
return Object.keys(sampleRow).map(key => ({
|
||||
return Object.keys(sampleRow).map((key) => ({
|
||||
column_name: key,
|
||||
data_type: typeof sampleRow[key],
|
||||
is_nullable: 'YES',
|
||||
is_nullable: "YES",
|
||||
column_default: null,
|
||||
description: `${key} 필드`
|
||||
description: `${key} 필드`,
|
||||
}));
|
||||
}
|
||||
|
||||
return [];
|
||||
} catch (error) {
|
||||
console.error(`[RestApiConnector] 컬럼 정보 조회 오류 (${endpoint}):`, error);
|
||||
console.error(
|
||||
`[RestApiConnector] 컬럼 정보 조회 오류 (${endpoint}):`,
|
||||
error
|
||||
);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
@@ -238,24 +301,29 @@ export class RestApiConnector implements DatabaseConnector {
|
||||
}
|
||||
|
||||
// REST API 전용 메서드들
|
||||
async getData(endpoint: string, params?: Record<string, any>): Promise<any[]> {
|
||||
const queryString = params ? '?' + new URLSearchParams(params).toString() : '';
|
||||
const result = await this.executeQuery(endpoint + queryString, 'GET');
|
||||
async getData(
|
||||
endpoint: string,
|
||||
params?: Record<string, any>
|
||||
): Promise<any[]> {
|
||||
const queryString = params
|
||||
? "?" + new URLSearchParams(params).toString()
|
||||
: "";
|
||||
const result = await this.executeRequest(endpoint + queryString, "GET");
|
||||
return result.rows;
|
||||
}
|
||||
|
||||
async postData(endpoint: string, data: any): Promise<any> {
|
||||
const result = await this.executeQuery(endpoint, 'POST', data);
|
||||
const result = await this.executeRequest(endpoint, "POST", data);
|
||||
return result.rows[0];
|
||||
}
|
||||
|
||||
async putData(endpoint: string, data: any): Promise<any> {
|
||||
const result = await this.executeQuery(endpoint, 'PUT', data);
|
||||
const result = await this.executeRequest(endpoint, "PUT", data);
|
||||
return result.rows[0];
|
||||
}
|
||||
|
||||
async deleteData(endpoint: string): Promise<any> {
|
||||
const result = await this.executeQuery(endpoint, 'DELETE');
|
||||
const result = await this.executeRequest(endpoint, "DELETE");
|
||||
return result.rows[0];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user