|
|
const { SystemSettings } = require("../../../../../../models/systemSettings"); |
|
|
const { safeJsonParse } = require("../../../../../http"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getDBClient(identifier = "", connectionConfig = {}) { |
|
|
switch (identifier) { |
|
|
case "mysql": |
|
|
const { MySQLConnector } = require("./MySQL"); |
|
|
return new MySQLConnector(connectionConfig); |
|
|
case "postgresql": |
|
|
const { PostgresSQLConnector } = require("./Postgresql"); |
|
|
return new PostgresSQLConnector(connectionConfig); |
|
|
case "sql-server": |
|
|
const { MSSQLConnector } = require("./MSSQL"); |
|
|
return new MSSQLConnector(connectionConfig); |
|
|
default: |
|
|
throw new Error( |
|
|
`There is no supported database connector for ${identifier}` |
|
|
); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function listSQLConnections() { |
|
|
return safeJsonParse( |
|
|
(await SystemSettings.get({ label: "agent_sql_connections" }))?.value, |
|
|
[] |
|
|
); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function validateConnection(identifier = "", connectionConfig = {}) { |
|
|
try { |
|
|
const client = getDBClient(identifier, connectionConfig); |
|
|
return await client.validateConnection(); |
|
|
} catch (error) { |
|
|
console.log(`Failed to connect to ${identifier} database.`); |
|
|
return { |
|
|
success: false, |
|
|
error: `Unable to connect to ${identifier}. Please verify your connection details.`, |
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
module.exports = { |
|
|
getDBClient, |
|
|
listSQLConnections, |
|
|
validateConnection, |
|
|
}; |
|
|
|