You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.8 KiB

11 months ago
  1. const { SystemSettings } = require("../../../../../../models/systemSettings");
  2. const { safeJsonParse } = require("../../../../../http");
  3. /**
  4. * @typedef {('postgresql'|'mysql'|'sql-server')} SQLEngine
  5. */
  6. /**
  7. * @typedef {Object} QueryResult
  8. * @property {[number]} rows - The query result rows
  9. * @property {number} count - Number of rows the query returned/changed
  10. * @property {string|null} error - Error string if there was an issue
  11. */
  12. /**
  13. * A valid database SQL connection object
  14. * @typedef {Object} SQLConnection
  15. * @property {string} database_id - Unique identifier of the database connection
  16. * @property {SQLEngine} engine - Engine used by connection
  17. * @property {string} connectionString - RFC connection string for db
  18. */
  19. /**
  20. * @param {SQLEngine} identifier
  21. * @param {object} connectionConfig
  22. * @returns Database Connection Engine Class for SQLAgent or throws error
  23. */
  24. function getDBClient(identifier = "", connectionConfig = {}) {
  25. switch (identifier) {
  26. case "mysql":
  27. const { MySQLConnector } = require("./MySQL");
  28. return new MySQLConnector(connectionConfig);
  29. case "postgresql":
  30. const { PostgresSQLConnector } = require("./Postgresql");
  31. return new PostgresSQLConnector(connectionConfig);
  32. case "sql-server":
  33. const { MSSQLConnector } = require("./MSSQL");
  34. return new MSSQLConnector(connectionConfig);
  35. default:
  36. throw new Error(
  37. `There is no supported database connector for ${identifier}`
  38. );
  39. }
  40. }
  41. /**
  42. * Lists all of the known database connection that can be used by the agent.
  43. * @returns {Promise<[SQLConnection]>}
  44. */
  45. async function listSQLConnections() {
  46. return safeJsonParse(
  47. (await SystemSettings.get({ label: "agent_sql_connections" }))?.value,
  48. []
  49. );
  50. }
  51. module.exports = {
  52. getDBClient,
  53. listSQLConnections,
  54. };