타입스크립트 에러수정
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
import * as mysql from 'mysql2/promise';
|
||||
import { DatabaseConnector, ConnectionConfig, QueryResult } from '../interfaces/DatabaseConnector';
|
||||
import { ConnectionTestResult, TableInfo } from '../types/externalDbTypes';
|
||||
import * as mysql from "mysql2/promise";
|
||||
import {
|
||||
DatabaseConnector,
|
||||
ConnectionConfig,
|
||||
QueryResult,
|
||||
} from "../interfaces/DatabaseConnector";
|
||||
import { ConnectionTestResult, TableInfo } from "../types/externalDbTypes";
|
||||
|
||||
export class MySQLConnector implements DatabaseConnector {
|
||||
private connection: mysql.Connection | null = null;
|
||||
@@ -12,17 +16,17 @@ export class MySQLConnector implements DatabaseConnector {
|
||||
|
||||
async connect(): Promise<void> {
|
||||
if (this.connection) {
|
||||
throw new Error('이미 연결되어 있습니다.');
|
||||
throw new Error("이미 연결되어 있습니다.");
|
||||
}
|
||||
|
||||
this.connection = await mysql.createConnection({
|
||||
host: this.config.host,
|
||||
port: this.config.port,
|
||||
database: this.config.database,
|
||||
user: this.config.username,
|
||||
user: this.config.user,
|
||||
password: this.config.password,
|
||||
connectTimeout: this.config.connectionTimeout || 30000,
|
||||
ssl: this.config.sslEnabled ? { rejectUnauthorized: false } : undefined,
|
||||
connectTimeout: this.config.connectionTimeoutMillis || 30000,
|
||||
ssl: this.config.ssl ? { rejectUnauthorized: false } : undefined,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -37,30 +41,34 @@ export class MySQLConnector implements DatabaseConnector {
|
||||
const startTime = Date.now();
|
||||
try {
|
||||
await this.connect();
|
||||
|
||||
const [versionResult] = await this.connection!.query('SELECT VERSION() as version');
|
||||
const [sizeResult] = await this.connection!.query(
|
||||
'SELECT SUM(data_length + index_length) as size FROM information_schema.tables WHERE table_schema = DATABASE()'
|
||||
|
||||
const [versionResult] = await this.connection!.query(
|
||||
"SELECT VERSION() as version"
|
||||
);
|
||||
|
||||
const [sizeResult] = await this.connection!.query(
|
||||
"SELECT SUM(data_length + index_length) as size FROM information_schema.tables WHERE table_schema = DATABASE()"
|
||||
);
|
||||
|
||||
const responseTime = Date.now() - startTime;
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: 'MySQL 연결이 성공했습니다.',
|
||||
message: "MySQL 연결이 성공했습니다.",
|
||||
details: {
|
||||
response_time: responseTime,
|
||||
server_version: (versionResult as any)[0]?.version || '알 수 없음',
|
||||
database_size: this.formatBytes(parseInt((sizeResult as any)[0]?.size || '0')),
|
||||
server_version: (versionResult as any)[0]?.version || "알 수 없음",
|
||||
database_size: this.formatBytes(
|
||||
parseInt((sizeResult as any)[0]?.size || "0")
|
||||
),
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
message: 'MySQL 연결에 실패했습니다.',
|
||||
message: "MySQL 연결에 실패했습니다.",
|
||||
error: {
|
||||
code: 'CONNECTION_FAILED',
|
||||
details: error instanceof Error ? error.message : '알 수 없는 오류',
|
||||
code: "CONNECTION_FAILED",
|
||||
details: error instanceof Error ? error.message : "알 수 없는 오류",
|
||||
},
|
||||
};
|
||||
} finally {
|
||||
@@ -78,9 +86,9 @@ export class MySQLConnector implements DatabaseConnector {
|
||||
return {
|
||||
rows: rows as any[],
|
||||
rowCount: Array.isArray(rows) ? rows.length : 0,
|
||||
fields: (fields as mysql.FieldPacket[]).map(field => ({
|
||||
fields: (fields as mysql.FieldPacket[]).map((field) => ({
|
||||
name: field.name,
|
||||
dataType: field.type.toString(),
|
||||
dataType: field.type?.toString() || "unknown",
|
||||
})),
|
||||
};
|
||||
} finally {
|
||||
@@ -106,7 +114,8 @@ export class MySQLConnector implements DatabaseConnector {
|
||||
const result: TableInfo[] = [];
|
||||
|
||||
for (const table of tables as any[]) {
|
||||
const [columns] = await this.connection!.query(`
|
||||
const [columns] = await this.connection!.query(
|
||||
`
|
||||
SELECT
|
||||
COLUMN_NAME as column_name,
|
||||
DATA_TYPE as data_type,
|
||||
@@ -116,7 +125,9 @@ export class MySQLConnector implements DatabaseConnector {
|
||||
WHERE TABLE_SCHEMA = DATABASE()
|
||||
AND TABLE_NAME = ?
|
||||
ORDER BY ORDINAL_POSITION
|
||||
`, [table.table_name]);
|
||||
`,
|
||||
[table.table_name]
|
||||
);
|
||||
|
||||
result.push({
|
||||
table_name: table.table_name,
|
||||
@@ -131,18 +142,21 @@ export class MySQLConnector implements DatabaseConnector {
|
||||
}
|
||||
}
|
||||
|
||||
async getColumns(tableName: string): Promise<Array<{
|
||||
name: string;
|
||||
dataType: string;
|
||||
isNullable: boolean;
|
||||
defaultValue?: string;
|
||||
}>> {
|
||||
async getColumns(tableName: string): Promise<
|
||||
Array<{
|
||||
name: string;
|
||||
dataType: string;
|
||||
isNullable: boolean;
|
||||
defaultValue?: string;
|
||||
}>
|
||||
> {
|
||||
if (!this.connection) {
|
||||
await this.connect();
|
||||
}
|
||||
|
||||
try {
|
||||
const [columns] = await this.connection!.query(`
|
||||
const [columns] = await this.connection!.query(
|
||||
`
|
||||
SELECT
|
||||
COLUMN_NAME as name,
|
||||
DATA_TYPE as dataType,
|
||||
@@ -152,9 +166,11 @@ export class MySQLConnector implements DatabaseConnector {
|
||||
WHERE TABLE_SCHEMA = DATABASE()
|
||||
AND TABLE_NAME = ?
|
||||
ORDER BY ORDINAL_POSITION
|
||||
`, [tableName]);
|
||||
`,
|
||||
[tableName]
|
||||
);
|
||||
|
||||
return (columns as any[]).map(col => ({
|
||||
return (columns as any[]).map((col) => ({
|
||||
name: col.name,
|
||||
dataType: col.dataType,
|
||||
isNullable: col.isNullable,
|
||||
@@ -166,10 +182,10 @@ export class MySQLConnector implements DatabaseConnector {
|
||||
}
|
||||
|
||||
private formatBytes(bytes: number): string {
|
||||
if (bytes === 0) return '0 Bytes';
|
||||
if (bytes === 0) return "0 Bytes";
|
||||
const k = 1024;
|
||||
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
|
||||
const sizes = ["Bytes", "KB", "MB", "GB", "TB"];
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
|
||||
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + " " + sizes[i];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user