Files
dbconnector/services/dbconnector/connectionManager.js
Maddie 4e8998d5ed Added parsing for partialMatch parameter in generateReadQuery function. To see all contents, send no params. To see exact matches, send no partialMatch param or send partialMatch: false. Send partialMatch: true to see all partial matches.
Additionally, moved parameter definitions out of paramLibrary into paramsConfig.json - you can now add/remove/change parameter data type definitions inside of this file. If you get a console error of "Unsupported Param Type" then you may need to also update paramLibrary with handling for the data type you're using- use the existing definitions in functions such as formatMSSQLParams as a guide.

Added testing for the partialMatch parameter to client.js.
2025-08-21 22:10:50 -04:00

82 lines
2.6 KiB
JavaScript

// /services/dbconnector/connectionManager.js
require('dotenv').config();
const sql = require('mssql');
const oracledb = require('oracledb');
const mysql = require('mysql2/promise');
const { Client } = require('pg');
/**
* Gets a connection based on the configured database type.
*
* @returns {Promise<Object>} A promise that resolves with the database connection.
*/
async function getConnection() {
const dbType = process.env.DB_TYPE;
const config = getDBConfig(dbType);
switch (dbType) {
case 'MSSQL':
return await sql.connect(config);
case 'ORACLE':
return await oracledb.getConnection(config);
case 'MARIADB':
return await mysql.createConnection(config);
case 'POSTGRES':
const client = new Client(config);
await client.connect();
return client;
default:
throw new Error('Unsupported database type');
}
}
/**
* Fetches the configuration for a specified database type from environment variables.
*
* @param {string} dbType - Type of database (e.g., 'MSSQL', 'ORACLE').
* @returns {Object} The database configuration object.
*/
function getDBConfig(dbType) {
switch (dbType) {
case 'MSSQL':
return {
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
server: process.env.DB_SERVER,
database: process.env.DB_DATABASE,
options: {
encrypt: true, // for secure connection
trustServerCertificate: true
}
};
case 'ORACLE':
return {
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
connectString: process.env.ORACLE_CONNECTION_STRING
};
case 'MARIADB':
return {
host: process.env.DB_SERVER,
port: parseInt(process.env.DB_PORT, 10) || 3006,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
namedPlaceholders: true
};
case 'POSTGRES':
return {
host: process.env.DB_SERVER,
port: parseInt(process.env.DB_PORT, 10) || 5432,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE
};
default:
throw new Error('Unsupported database type');
}
}
module.exports = { getConnection };