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

This commit is contained in:
silencezhang
2024-10-21 14:26:33 +08:00
committed by GitHub
parent 6522cc7dfa
commit 7ec2253f0a
6 changed files with 838 additions and 1 deletions

View File

@@ -3,11 +3,14 @@
"version": "1.0.0", "version": "1.0.0",
"type": "module", "type": "module",
"dependencies": { "dependencies": {
"@types/pg": "^8.6.6",
"axios": "^1.5.1", "axios": "^1.5.1",
"duck-duck-scrape": "^2.2.5", "duck-duck-scrape": "^2.2.5",
"echarts": "5.4.1", "echarts": "5.4.1",
"expr-eval": "^2.0.2", "expr-eval": "^2.0.2",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"mysql2": "^3.11.3",
"pg": "^8.10.0",
"wikijs": "^6.4.1" "wikijs": "^6.4.1"
}, },
"devDependencies": { "devDependencies": {

View File

@@ -25,7 +25,8 @@ const packagePluginList = [
'duckduckgo/searchVideo', 'duckduckgo/searchVideo',
'drawing', 'drawing',
'drawing/baseChart', 'drawing/baseChart',
'wiki' 'wiki',
'databaseConnection'
]; ];
export const list = [...staticPluginList, ...packagePluginList]; export const list = [...staticPluginList, ...packagePluginList];

View File

@@ -0,0 +1,71 @@
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;

View File

@@ -0,0 +1,759 @@
{
"author": "",
"version": "4811",
"name": "数据源配置",
"avatar": "core/workflow/template/datasource",
"intro": "可连接常用数据库并执行sql",
"showStatus": true,
"weight": 10,
"isTool": true,
"templateType": "tools",
"workflow": {
"nodes": [
{
"nodeId": "pluginInput",
"name": "workflow:template.plugin_start",
"intro": "workflow:intro_plugin_input",
"avatar": "core/workflow/template/workflowStart",
"flowNodeType": "pluginInput",
"showStatus": false,
"position": {
"x": 335.8310044079668,
"y": -260.8285440670886
},
"version": "481",
"inputs": [
{
"renderTypeList": [
"select"
],
"selectedTypeIndex": 0,
"valueType": "string",
"canEdit": true,
"key": "databaseType",
"label": "databaseType",
"description": "数据库的类型",
"defaultValue": "",
"list": [
{
"label": "MySQL",
"value": "MySQL"
},
{
"label": "PostgreSQL",
"value": "PostgreSQL"
}
],
"required": true
},
{
"renderTypeList": [
"input"
],
"selectedTypeIndex": 0,
"valueType": "string",
"canEdit": true,
"key": "host",
"label": "host",
"description": "数据库连接host",
"defaultValue": "",
"required": true,
"list": [
{
"label": "",
"value": ""
}
]
},
{
"renderTypeList": [
"input"
],
"selectedTypeIndex": 0,
"valueType": "string",
"canEdit": true,
"key": "port",
"label": "port",
"description": "数据库连接端口号",
"defaultValue": "",
"required": true,
"list": [
{
"label": "",
"value": ""
}
]
},
{
"renderTypeList": [
"input"
],
"selectedTypeIndex": 0,
"valueType": "string",
"canEdit": true,
"key": "databaseName",
"label": "databaseName",
"description": "数据库名称",
"defaultValue": "",
"required": true,
"list": [
{
"label": "",
"value": ""
}
]
},
{
"renderTypeList": [
"input"
],
"selectedTypeIndex": 0,
"valueType": "string",
"canEdit": true,
"key": "password",
"label": "password",
"description": "数据库密码",
"defaultValue": "",
"list": [
{
"label": "",
"value": ""
}
],
"required": true
},
{
"renderTypeList": [
"input"
],
"selectedTypeIndex": 0,
"valueType": "string",
"canEdit": true,
"key": "user",
"label": "user",
"description": "数据库账号",
"defaultValue": "",
"list": [
{
"label": "",
"value": ""
}
],
"required": true
},
{
"renderTypeList": [
"reference"
],
"selectedTypeIndex": 0,
"valueType": "string",
"canEdit": true,
"key": "sql",
"label": "sql",
"description": "sql语句可以传入sql语句直接执行",
"defaultValue": "",
"list": [
{
"label": "",
"value": ""
}
],
"required": true,
"toolDescription": "sql语句可以传入sql语句直接执行"
}
],
"outputs": [
{
"id": "databaseType",
"valueType": "string",
"key": "databaseType",
"label": "databaseType",
"type": "hidden"
},
{
"id": "host",
"valueType": "string",
"key": "host",
"label": "host",
"type": "hidden"
},
{
"id": "port",
"valueType": "string",
"key": "port",
"label": "port",
"type": "hidden"
},
{
"id": "dataBaseName",
"valueType": "string",
"key": "databaseName",
"label": "databaseName",
"type": "hidden"
},
{
"id": "dataBasePwd",
"valueType": "string",
"key": "password",
"label": "password",
"type": "hidden"
},
{
"id": "user",
"valueType": "string",
"key": "user",
"label": "user",
"type": "hidden"
},
{
"id": "sql",
"valueType": "string",
"key": "sql",
"label": "sql",
"type": "hidden"
}
]
},
{
"nodeId": "pluginOutput",
"name": "common:core.module.template.self_output",
"intro": "workflow:intro_custom_plugin_output",
"avatar": "core/workflow/template/pluginOutput",
"flowNodeType": "pluginOutput",
"showStatus": false,
"position": {
"x": 1788.4723692358186,
"y": -153.2313912808486
},
"version": "481",
"inputs": [
{
"renderTypeList": [
"reference"
],
"valueType": "string",
"canEdit": true,
"key": "result",
"label": "result",
"isToolOutput": true,
"description": "数据库连接结果",
"value": [
"zBeXy7YZEiXe",
"httpRawResponse"
]
}
],
"outputs": []
},
{
"nodeId": "pluginConfig",
"name": "common:core.module.template.system_config",
"intro": "",
"avatar": "core/workflow/template/systemConfig",
"flowNodeType": "pluginConfig",
"position": {
"x": -133.25818142678844,
"y": -200.98784849888733
},
"version": "4811",
"inputs": [],
"outputs": []
},
{
"nodeId": "zBeXy7YZEiXe",
"name": "数据库连接",
"intro": "可以发出一个 HTTP 请求,实现更为复杂的操作(联网搜索、数据库查询等)",
"avatar": "core/workflow/template/httpRequest",
"flowNodeType": "httpRequest468",
"showStatus": true,
"position": {
"x": 1035.92763304296,
"y": -498.57137296107504
},
"version": "481",
"inputs": [
{
"key": "system_addInputParam",
"renderTypeList": [
"addInputParam"
],
"valueType": "dynamic",
"label": "",
"required": false,
"description": "common:core.module.input.description.HTTP Dynamic Input",
"customInputConfig": {
"selectValueTypeList": [
"string",
"number",
"boolean",
"object",
"arrayString",
"arrayNumber",
"arrayBoolean",
"arrayObject",
"arrayAny",
"any",
"chatHistory",
"datasetQuote",
"dynamic",
"selectApp",
"selectDataset"
],
"showDescription": false,
"showDefaultValue": true
},
"valueDesc": "",
"debugLabel": "",
"toolDescription": ""
},
{
"key": "system_httpMethod",
"renderTypeList": [
"custom"
],
"valueType": "string",
"label": "",
"value": "POST",
"required": true,
"valueDesc": "",
"description": "",
"debugLabel": "",
"toolDescription": ""
},
{
"key": "system_httpTimeout",
"renderTypeList": [
"custom"
],
"valueType": "number",
"label": "",
"value": 30,
"min": 5,
"max": 600,
"required": true,
"valueDesc": "",
"description": "",
"debugLabel": "",
"toolDescription": ""
},
{
"key": "system_httpReqUrl",
"renderTypeList": [
"hidden"
],
"valueType": "string",
"label": "",
"description": "common:core.module.input.description.Http Request Url",
"placeholder": "https://api.ai.com/getInventory",
"required": false,
"value": "databaseConnection",
"valueDesc": "",
"debugLabel": "",
"toolDescription": ""
},
{
"key": "system_httpHeader",
"renderTypeList": [
"custom"
],
"valueType": "any",
"value": [],
"label": "",
"description": "common:core.module.input.description.Http Request Header",
"placeholder": "common:core.module.input.description.Http Request Header",
"required": false,
"valueDesc": "",
"debugLabel": "",
"toolDescription": ""
},
{
"key": "system_httpParams",
"renderTypeList": [
"hidden"
],
"valueType": "any",
"value": [],
"label": "",
"required": false,
"valueDesc": "",
"description": "",
"debugLabel": "",
"toolDescription": ""
},
{
"key": "system_httpJsonBody",
"renderTypeList": [
"hidden"
],
"valueType": "any",
"value": "{\r\n \"databaseType\":\"{{databaseType-H}}\",\r\n \"host\":\"{{host-H}}\",\r\n \"port\":\"{{port-H}}\",\r\n \"databaseName\":\"{{databaseName-H}}\",\r\n \"user\":\"{{databaseUser-H}}\",\r\n \"password\":\"{{databasePwd-H}}\",\r\n \"sql\":\"{{sql-H}}\"\r\n}",
"label": "",
"required": false,
"valueDesc": "",
"description": "",
"debugLabel": "",
"toolDescription": ""
},
{
"key": "system_httpFormBody",
"renderTypeList": [
"hidden"
],
"valueType": "any",
"value": [],
"label": "",
"required": false,
"valueDesc": "",
"description": "",
"debugLabel": "",
"toolDescription": ""
},
{
"key": "system_httpContentType",
"renderTypeList": [
"hidden"
],
"valueType": "string",
"value": "json",
"label": "",
"required": false,
"valueDesc": "",
"description": "",
"debugLabel": "",
"toolDescription": ""
},
{
"renderTypeList": [
"reference"
],
"valueType": "string",
"canEdit": true,
"key": "databaseType-H",
"label": "databaseType-H",
"customInputConfig": {
"selectValueTypeList": [
"string",
"number",
"boolean",
"object",
"arrayString",
"arrayNumber",
"arrayBoolean",
"arrayObject",
"arrayAny",
"any",
"chatHistory",
"datasetQuote",
"dynamic",
"selectApp",
"selectDataset"
],
"showDescription": false,
"showDefaultValue": true
},
"required": true,
"value": [
"pluginInput",
"databaseType"
]
},
{
"renderTypeList": [
"reference"
],
"valueType": "string",
"canEdit": true,
"key": "host-H",
"label": "host-H",
"customInputConfig": {
"selectValueTypeList": [
"string",
"number",
"boolean",
"object",
"arrayString",
"arrayNumber",
"arrayBoolean",
"arrayObject",
"arrayAny",
"any",
"chatHistory",
"datasetQuote",
"dynamic",
"selectApp",
"selectDataset"
],
"showDescription": false,
"showDefaultValue": true
},
"required": true,
"value": [
"pluginInput",
"host"
]
},
{
"renderTypeList": [
"reference"
],
"valueType": "string",
"canEdit": true,
"key": "port-H",
"label": "port-H",
"customInputConfig": {
"selectValueTypeList": [
"string",
"number",
"boolean",
"object",
"arrayString",
"arrayNumber",
"arrayBoolean",
"arrayObject",
"arrayAny",
"any",
"chatHistory",
"datasetQuote",
"dynamic",
"selectApp",
"selectDataset"
],
"showDescription": false,
"showDefaultValue": true
},
"required": true,
"value": [
"pluginInput",
"port"
]
},
{
"renderTypeList": [
"reference"
],
"valueType": "string",
"canEdit": true,
"key": "databaseName-H",
"label": "databaseName-H",
"customInputConfig": {
"selectValueTypeList": [
"string",
"number",
"boolean",
"object",
"arrayString",
"arrayNumber",
"arrayBoolean",
"arrayObject",
"arrayAny",
"any",
"chatHistory",
"datasetQuote",
"dynamic",
"selectApp",
"selectDataset"
],
"showDescription": false,
"showDefaultValue": true
},
"required": true,
"value": [
"pluginInput",
"dataBaseName"
]
},
{
"renderTypeList": [
"reference"
],
"valueType": "string",
"canEdit": true,
"key": "databasePwd-H",
"label": "databasePwd-H",
"customInputConfig": {
"selectValueTypeList": [
"string",
"number",
"boolean",
"object",
"arrayString",
"arrayNumber",
"arrayBoolean",
"arrayObject",
"arrayAny",
"any",
"chatHistory",
"datasetQuote",
"dynamic",
"selectApp",
"selectDataset"
],
"showDescription": false,
"showDefaultValue": true
},
"required": true,
"value": [
"pluginInput",
"dataBasePwd"
]
},
{
"renderTypeList": [
"reference"
],
"valueType": "string",
"canEdit": true,
"key": "databaseUser-H",
"label": "databaseUser-H",
"customInputConfig": {
"selectValueTypeList": [
"string",
"number",
"boolean",
"object",
"arrayString",
"arrayNumber",
"arrayBoolean",
"arrayObject",
"arrayAny",
"any",
"chatHistory",
"datasetQuote",
"dynamic",
"selectApp",
"selectDataset"
],
"showDescription": false,
"showDefaultValue": true
},
"required": true,
"value": [
"pluginInput",
"user"
]
},
{
"renderTypeList": [
"reference"
],
"valueType": "string",
"canEdit": true,
"key": "sql-H",
"label": "sql-H",
"customInputConfig": {
"selectValueTypeList": [
"string",
"number",
"boolean",
"object",
"arrayString",
"arrayNumber",
"arrayBoolean",
"arrayObject",
"arrayAny",
"any",
"chatHistory",
"datasetQuote",
"dynamic",
"selectApp",
"selectDataset"
],
"showDescription": false,
"showDefaultValue": true
},
"required": true,
"value": [
"pluginInput",
"sql"
]
}
],
"outputs": [
{
"id": "error",
"key": "error",
"label": "workflow:request_error",
"description": "HTTP请求错误信息成功时返回空",
"valueType": "object",
"type": "static"
},
{
"id": "httpRawResponse",
"key": "httpRawResponse",
"required": true,
"label": "workflow:raw_response",
"description": "HTTP请求的原始响应。只能接受字符串或JSON类型响应数据。",
"valueType": "any",
"type": "static"
},
{
"id": "system_addOutputParam",
"key": "system_addOutputParam",
"type": "dynamic",
"valueType": "dynamic",
"label": "",
"customFieldConfig": {
"selectValueTypeList": [
"string",
"number",
"boolean",
"object",
"arrayString",
"arrayNumber",
"arrayBoolean",
"arrayObject",
"arrayAny",
"any",
"chatHistory",
"datasetQuote",
"dynamic",
"selectApp",
"selectDataset"
],
"showDescription": false,
"showDefaultValue": false
},
"valueDesc": "",
"description": ""
}
]
}
],
"edges": [
{
"source": "pluginInput",
"target": "zBeXy7YZEiXe",
"sourceHandle": "pluginInput-source-right",
"targetHandle": "zBeXy7YZEiXe-target-left"
},
{
"source": "zBeXy7YZEiXe",
"target": "pluginOutput",
"sourceHandle": "zBeXy7YZEiXe-source-right",
"targetHandle": "pluginOutput-target-left"
}
],
"chatConfig": {
"welcomeText": "",
"variables": [],
"questionGuide": false,
"ttsConfig": {
"type": "web"
},
"whisperConfig": {
"open": false,
"autoSend": false,
"autoTTSResponse": false
},
"chatInputGuide": {
"open": false,
"textList": [],
"customUrl": ""
},
"instruction": "数据源配置,支持主流数据库配置",
"_id": "670a23b31957c5b9899b4a4d"
}
}
}

View File

@@ -229,6 +229,8 @@ export const iconPaths = {
'core/workflow/template/extractJson': () => 'core/workflow/template/extractJson': () =>
import('./icons/core/workflow/template/extractJson.svg'), import('./icons/core/workflow/template/extractJson.svg'),
'core/workflow/template/wiki': () => import('./icons/core/workflow/template/wiki.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/fetchUrl': () => import('./icons/core/workflow/template/fetchUrl.svg'),
'core/workflow/template/formInput': () => import('./icons/core/workflow/template/formInput.svg'), 'core/workflow/template/formInput': () => import('./icons/core/workflow/template/formInput.svg'),
'core/workflow/template/getTime': () => import('./icons/core/workflow/template/getTime.svg'), 'core/workflow/template/getTime': () => import('./icons/core/workflow/template/getTime.svg'),

View File

@@ -0,0 +1 @@
<?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>

After

Width:  |  Height:  |  Size: 4.4 KiB