Commit 7ec2253f by silencezhang Committed by GitHub

新增数据源配置:可以直接执行sql,支持MySQL,PG数据库 (#2958)

parent 6522cc7d
......@@ -3,11 +3,14 @@
"version": "1.0.0",
"type": "module",
"dependencies": {
"@types/pg": "^8.6.6",
"axios": "^1.5.1",
"duck-duck-scrape": "^2.2.5",
"echarts": "5.4.1",
"expr-eval": "^2.0.2",
"lodash": "^4.17.21",
"mysql2": "^3.11.3",
"pg": "^8.10.0",
"wikijs": "^6.4.1"
},
"devDependencies": {
......
......@@ -25,7 +25,8 @@ const packagePluginList = [
'duckduckgo/searchVideo',
'drawing',
'drawing/baseChart',
'wiki'
'wiki',
'databaseConnection'
];
export const list = [...staticPluginList, ...packagePluginList];
......
import { Client as PgClient } from 'pg'; // PostgreSQL 客户端
import mysql from 'mysql2/promise'; // MySQL 客户端
type Props = {
databaseType: string;
host: string;
port: string;
databaseName: string;
user: string;
password: string;
sql: string;
};
type Response = Promise<{
result: any; // 根据你的 SQL 查询结果类型调整
}>;
const main = async ({
databaseType,
host,
port,
databaseName,
user,
password,
sql
}: Props): Response => {
let result;
try {
if (databaseType === 'PostgreSQL') {
const client = new PgClient({
host,
port: parseInt(port, 10),
database: databaseName,
user,
password
});
await client.connect();
const res = await client.query(sql);
result = res.rows;
await client.end();
} else if (databaseType === 'MySQL') {
const connection = await mysql.createConnection({
host,
port: parseInt(port, 10),
database: databaseName,
user,
password
});
const [rows] = await connection.execute(sql);
result = rows;
await connection.end();
}
return {
result
};
} catch (error: unknown) {
// 使用类型断言来处理错误
if (error instanceof Error) {
console.error('Database query error:', error.message);
throw new Error(error.message);
} else {
console.error('Database query error:', error);
throw new Error('An unknown error occurred');
}
}
};
export default main;
......@@ -229,6 +229,8 @@ export const iconPaths = {
'core/workflow/template/extractJson': () =>
import('./icons/core/workflow/template/extractJson.svg'),
'core/workflow/template/wiki': () => import('./icons/core/workflow/template/wiki.svg'),
'core/workflow/template/datasource': () =>
import('./icons/core/workflow/template/datasource.svg'),
'core/workflow/template/fetchUrl': () => import('./icons/core/workflow/template/fetchUrl.svg'),
'core/workflow/template/formInput': () => import('./icons/core/workflow/template/formInput.svg'),
'core/workflow/template/getTime': () => import('./icons/core/workflow/template/getTime.svg'),
......
<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1729242018825" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4362" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><path d="M514.174587 1024a457.891171 192.489282 0 1 0 0-384.978564 457.891171 192.489282 0 1 0 0 384.978564Z" fill="#2F82F5" p-id="4363"></path><path d="M56.385749 623.773747H126.381852v212.086144H56.385749zM902.171988 624.234248H972.168091v212.341977h-69.996103z" fill="#2F82F5" p-id="4364"></path><path d="M514.225753 860.419927a457.891171 192.489282 0 1 0 0-384.978564 457.891171 192.489282 0 1 0 0 384.978564Z" fill="#92BDFA" p-id="4365"></path><path d="M377.150052 920.387348s63.395593 11.563684 142.90871 10.642682a862.875931 862.875931 0 0 0 172.073752-18.420027c61.24659-12.894019 116.660171-36.891221 116.660171-36.89122v23.997202s-43.747564 24.764703-116.660171 37.454054c-72.912607 12.689352-122.493179 19.443362-172.073752 18.420027-62.474592-1.279169-142.908709-9.772848-142.90871-9.772847v-25.429871z" fill="#FFFFFF" p-id="4366"></path><path d="M161.328736 891.222306a30.751212 29.165043 90 1 0 58.330086 0 30.751212 29.165043 90 1 0-58.330086 0Z" fill="#FFFFFF" p-id="4367"></path><path d="M514.225753 817.542198a457.891171 193.666117 0 1 0 0-387.332234 457.891171 193.666117 0 1 0 0 387.332234Z" fill="#0166F3" p-id="4368"></path><path d="M56.385749 406.468595H126.381852v214.337481H56.385749zM902.171988 402.375256H972.168091v225.952331h-69.996103z" fill="#0166F3" p-id="4369"></path><path d="M514.225753 646.696447a457.891171 193.666117 0 1 0 0-387.332234 457.891171 193.666117 0 1 0 0 387.332234Z" fill="#92BDFA" p-id="4370"></path><path d="M377.150052 711.473542s63.395593 11.61485 142.90871 10.642682a862.927097 862.927097 0 0 0 172.073752-18.420027c61.24659-12.894019 116.660171-36.840054 116.660171-36.840053v23.946035s-43.747564 24.764703-116.660171 37.505221c-72.912607 12.689352-122.493179 19.443362-172.073752 18.420027-62.474592-1.330335-142.908709-9.824014-142.90871-9.824014v-25.429871z" fill="#FFFFFF" p-id="4371"></path><path d="M161.328736 676.066157a30.751212 29.165043 90 1 0 58.330086 0 30.751212 29.165043 90 1 0-58.330086 0Z" fill="#FFFFFF" p-id="4372"></path><path d="M514.225753 603.460551a457.891171 201.136461 0 1 0 0-402.272923 457.891171 201.136461 0 1 0 0 402.272923Z" fill="#939FAE" p-id="4373"></path><path d="M56.385749 209.067306H126.381852v193.256783H56.385749zM902.223155 215.207315h69.996102v172.073752h-69.996102z" fill="#939FAE" p-id="4374"></path><path d="M514.27692 402.272923a457.891171 201.136461 0 1 0 0-402.272923 457.891171 201.136461 0 1 0 0 402.272923Z" fill="#D5DBE4" p-id="4375"></path><path d="M377.252386 486.135212s63.395593 11.563684 142.908709 10.642682a862.927097 862.927097 0 0 0 172.073752-18.420027c61.24659-12.945186 116.660171-36.891221 116.660171-36.89122v23.946035s-43.747564 24.81587-116.660171 37.505221c-72.912607 12.689352-122.493179 19.443362-172.073752 18.420027-62.474592-1.330335-142.908709-9.824014-142.908709-9.824014V486.186379z" fill="#FFFFFF" p-id="4376"></path><path d="M161.328736 461.012342a30.751212 29.165043 90 1 0 58.330086 0 30.751212 29.165043 90 1 0-58.330086 0Z" fill="#FFFFFF" p-id="4377"></path><path d="M514.225753 603.460551a457.891171 201.136461 0 1 0 0-402.272923 457.891171 201.136461 0 1 0 0 402.272923Z" fill="#2F82F5" p-id="4378"></path><path d="M56.385749 209.067306H126.381852v193.256783H56.385749zM902.223155 215.207315h69.996102v172.073752h-69.996102z" fill="#2F82F5" p-id="4379"></path><path d="M514.27692 402.272923a457.891171 201.136461 0 1 0 0-402.272923 457.891171 201.136461 0 1 0 0 402.272923Z" fill="#92BDFA" p-id="4380"></path><path d="M514.328087 299.632439a224.161495 98.444811 0 1 0 0-196.889622 224.161495 98.444811 0 1 0 0 196.889622Z" fill="#FFFFFF" p-id="4381"></path><path d="M377.252386 486.135212s63.395593 11.563684 142.908709 10.642682a862.927097 862.927097 0 0 0 172.073752-18.420027c61.24659-12.945186 116.660171-36.891221 116.660171-36.89122v23.946035s-43.747564 24.81587-116.660171 37.505221c-72.912607 12.689352-122.493179 19.443362-172.073752 18.420027-62.474592-1.330335-142.908709-9.824014-142.908709-9.824014V486.186379z" fill="#FFFFFF" p-id="4382"></path><path d="M161.328736 461.012342a30.751212 29.165043 90 1 0 58.330086 0 30.751212 29.165043 90 1 0-58.330086 0Z" fill="#FFFFFF" p-id="4383"></path></svg>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment