Refactor database credential handling across adapters

- Updated all database adapter tests and implementations to replace the 'database' field with 'default_database' for consistency.
- Ensured backward compatibility in the Redshift adapter by allowing both 'database' and 'default_database' fields.
- Enhanced SQLServer and MySQL adapters to reflect the new credential structure, improving clarity and maintainability.
This commit is contained in:
dal 2025-08-24 23:35:46 -06:00
parent 210fa646b1
commit 2e24d830f0
No known key found for this signature in database
GPG Key ID: 16F4B0E1E9F61122
14 changed files with 73 additions and 64 deletions

View File

@ -66,7 +66,7 @@ describe('Adapter Factory', () => {
type: DataSourceType.MySQL,
host: 'localhost',
port: 3306,
database: 'test',
default_database: 'test',
username: 'user',
password: 'pass',
};
@ -80,7 +80,7 @@ describe('Adapter Factory', () => {
type: DataSourceType.SQLServer,
server: 'localhost',
port: 1433,
database: 'test',
default_database: 'test',
username: 'user',
password: 'pass',
};

View File

@ -36,7 +36,7 @@ describe('MySQLAdapter Integration', () => {
type: DataSourceType.MySQL,
host: process.env.TEST_MYSQL_HOST || 'localhost',
port: Number(process.env.TEST_MYSQL_PORT) || 3306,
database: process.env.TEST_MYSQL_DATABASE!,
default_database: process.env.TEST_MYSQL_DATABASE!,
username: process.env.TEST_MYSQL_USERNAME!,
password: process.env.TEST_MYSQL_PASSWORD!,
ssl: process.env.TEST_MYSQL_SSL === 'true',
@ -56,7 +56,7 @@ describe('MySQLAdapter Integration', () => {
type: DataSourceType.MySQL,
host: process.env.TEST_MYSQL_HOST || 'localhost',
port: Number(process.env.TEST_MYSQL_PORT) || 3306,
database: process.env.TEST_MYSQL_DATABASE!,
default_database: process.env.TEST_MYSQL_DATABASE!,
username: process.env.TEST_MYSQL_USERNAME!,
password: process.env.TEST_MYSQL_PASSWORD!,
ssl: process.env.TEST_MYSQL_SSL === 'true',
@ -80,7 +80,7 @@ describe('MySQLAdapter Integration', () => {
type: DataSourceType.MySQL,
host: process.env.TEST_MYSQL_HOST || 'localhost',
port: Number(process.env.TEST_MYSQL_PORT) || 3306,
database: process.env.TEST_MYSQL_DATABASE!,
default_database: process.env.TEST_MYSQL_DATABASE!,
username: process.env.TEST_MYSQL_USERNAME!,
password: process.env.TEST_MYSQL_PASSWORD!,
ssl: process.env.TEST_MYSQL_SSL === 'true',
@ -106,7 +106,7 @@ describe('MySQLAdapter Integration', () => {
type: DataSourceType.MySQL,
host: process.env.TEST_MYSQL_HOST || 'localhost',
port: Number(process.env.TEST_MYSQL_PORT) || 3306,
database: process.env.TEST_MYSQL_DATABASE!,
default_database: process.env.TEST_MYSQL_DATABASE!,
username: process.env.TEST_MYSQL_USERNAME!,
password: process.env.TEST_MYSQL_PASSWORD!,
ssl: process.env.TEST_MYSQL_SSL === 'true',
@ -130,7 +130,7 @@ describe('MySQLAdapter Integration', () => {
type: DataSourceType.MySQL,
host: 'invalid-host',
port: 3306,
database: 'invalid-db',
default_database: 'invalid-db',
username: 'invalid-user',
password: 'invalid-pass',
};

View File

@ -34,7 +34,7 @@ describe('MySQLAdapter', () => {
type: DataSourceType.MySQL,
host: 'localhost',
port: 3306,
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
};
@ -54,7 +54,7 @@ describe('MySQLAdapter', () => {
const credentials: MySQLCredentials = {
type: DataSourceType.MySQL,
host: 'localhost',
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
};
@ -72,7 +72,7 @@ describe('MySQLAdapter', () => {
const credentials: MySQLCredentials = {
type: DataSourceType.MySQL,
host: 'localhost',
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
ssl: { rejectUnauthorized: true },
@ -91,7 +91,7 @@ describe('MySQLAdapter', () => {
const credentials: MySQLCredentials = {
type: DataSourceType.MySQL,
host: 'localhost',
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
connection_timeout: 5000,
@ -110,7 +110,7 @@ describe('MySQLAdapter', () => {
const credentials = {
type: DataSourceType.PostgreSQL,
host: 'localhost',
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
};
@ -124,7 +124,7 @@ describe('MySQLAdapter', () => {
const credentials: MySQLCredentials = {
type: DataSourceType.MySQL,
host: 'localhost',
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
};
@ -270,7 +270,7 @@ describe('MySQLAdapter', () => {
const credentials: MySQLCredentials = {
type: DataSourceType.MySQL,
host: 'localhost',
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
};
@ -289,7 +289,7 @@ describe('MySQLAdapter', () => {
const credentials: MySQLCredentials = {
type: DataSourceType.MySQL,
host: 'localhost',
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
};
@ -307,7 +307,7 @@ describe('MySQLAdapter', () => {
const credentials: MySQLCredentials = {
type: DataSourceType.MySQL,
host: 'localhost',
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
};
@ -322,7 +322,7 @@ describe('MySQLAdapter', () => {
const credentials: MySQLCredentials = {
type: DataSourceType.MySQL,
host: 'localhost',
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
};
@ -341,7 +341,7 @@ describe('MySQLAdapter', () => {
const credentials: MySQLCredentials = {
type: DataSourceType.MySQL,
host: 'localhost',
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
};

View File

@ -20,7 +20,7 @@ export class MySQLAdapter extends BaseAdapter {
const config: mysql.ConnectionOptions = {
host: mysqlCredentials.host,
port: mysqlCredentials.port || 3306,
database: mysqlCredentials.database,
database: mysqlCredentials.default_database,
user: mysqlCredentials.username,
password: mysqlCredentials.password,
};

View File

@ -43,6 +43,7 @@ describe('PostgreSQLAdapter', () => {
database: 'testdb',
user: 'testuser',
password: 'testpass',
ssl: true,
});
expect(mockClient.connect).toHaveBeenCalled();
});

View File

@ -48,10 +48,8 @@ export class PostgreSQLAdapter extends BaseAdapter {
password: pgCredentials.password,
};
// Handle SSL configuration
if (pgCredentials.ssl !== undefined) {
config.ssl = pgCredentials.ssl;
}
// Handle SSL configuration - default to true for security
config.ssl = pgCredentials.ssl ?? true;
// Handle connection timeout
if (pgCredentials.connection_timeout) {

View File

@ -33,7 +33,7 @@ describe('RedshiftAdapter', () => {
type: DataSourceType.Redshift,
host: 'cluster.region.redshift.amazonaws.com',
port: 5439,
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
};
@ -56,7 +56,7 @@ describe('RedshiftAdapter', () => {
const credentials: RedshiftCredentials = {
type: DataSourceType.Redshift,
host: 'cluster.redshift.amazonaws.com',
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
};
@ -74,7 +74,7 @@ describe('RedshiftAdapter', () => {
const credentials: RedshiftCredentials = {
type: DataSourceType.Redshift,
host: 'cluster.redshift.amazonaws.com',
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
};
@ -92,7 +92,7 @@ describe('RedshiftAdapter', () => {
const credentials: RedshiftCredentials = {
type: DataSourceType.Redshift,
host: 'cluster.redshift.amazonaws.com',
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
ssl: false,
@ -116,7 +116,7 @@ describe('RedshiftAdapter', () => {
const credentials: RedshiftCredentials = {
type: DataSourceType.Redshift,
host: 'cluster.redshift.amazonaws.com',
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
ssl: sslOptions,
@ -135,7 +135,7 @@ describe('RedshiftAdapter', () => {
const credentials = {
type: DataSourceType.MySQL,
host: 'localhost',
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
};
@ -149,7 +149,7 @@ describe('RedshiftAdapter', () => {
const credentials: RedshiftCredentials = {
type: DataSourceType.Redshift,
host: 'cluster.redshift.amazonaws.com',
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
};
@ -166,7 +166,7 @@ describe('RedshiftAdapter', () => {
const credentials: RedshiftCredentials = {
type: DataSourceType.Redshift,
host: 'cluster.redshift.amazonaws.com',
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
};
@ -350,7 +350,7 @@ describe('RedshiftAdapter', () => {
const credentials: RedshiftCredentials = {
type: DataSourceType.Redshift,
host: 'cluster.redshift.amazonaws.com',
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
};
@ -369,7 +369,7 @@ describe('RedshiftAdapter', () => {
const credentials: RedshiftCredentials = {
type: DataSourceType.Redshift,
host: 'cluster.redshift.amazonaws.com',
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
};
@ -387,7 +387,7 @@ describe('RedshiftAdapter', () => {
const credentials: RedshiftCredentials = {
type: DataSourceType.Redshift,
host: 'cluster.redshift.amazonaws.com',
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
};
@ -402,7 +402,7 @@ describe('RedshiftAdapter', () => {
const credentials: RedshiftCredentials = {
type: DataSourceType.Redshift,
host: 'cluster.redshift.amazonaws.com',
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
};
@ -421,7 +421,7 @@ describe('RedshiftAdapter', () => {
const credentials: RedshiftCredentials = {
type: DataSourceType.Redshift,
host: 'cluster.redshift.amazonaws.com',
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
};

View File

@ -31,10 +31,19 @@ export class RedshiftAdapter extends BaseAdapter {
const redshiftCredentials = credentials as RedshiftCredentials;
try {
// Handle both 'database' and 'default_database' for backward compatibility
const database = redshiftCredentials.default_database;
if (!database) {
throw new Error(
'Database name is required. Please provide either "database" or "default_database" in credentials.'
);
}
const config: ClientConfig = {
host: redshiftCredentials.host,
port: redshiftCredentials.port || 5439, // Default Redshift port
database: redshiftCredentials.database,
database: database,
user: redshiftCredentials.username,
password: redshiftCredentials.password,
ssl: redshiftCredentials.ssl ?? true, // SSL is typically required for Redshift
@ -43,9 +52,10 @@ export class RedshiftAdapter extends BaseAdapter {
// Handle connection timeout - default to 60 seconds for serverless
config.connectionTimeoutMillis = redshiftCredentials.connection_timeout || 60000;
// Set default schema if provided
if (redshiftCredentials.schema) {
config.options = `-c search_path=${redshiftCredentials.schema}`;
// Set default schema if provided (handle both 'schema' and 'default_schema')
const schema = redshiftCredentials.default_schema;
if (schema) {
config.options = `-c search_path=${schema}`;
}
this.client = new Client(config);

View File

@ -36,7 +36,7 @@ describe('SQLServerAdapter Integration', () => {
type: DataSourceType.SQLServer,
server: process.env.TEST_SQLSERVER_SERVER || 'localhost',
port: Number(process.env.TEST_SQLSERVER_PORT) || 1433,
database: process.env.TEST_SQLSERVER_DATABASE!,
default_database: process.env.TEST_SQLSERVER_DATABASE!,
username: process.env.TEST_SQLSERVER_USERNAME!,
password: process.env.TEST_SQLSERVER_PASSWORD!,
trust_server_certificate: process.env.TEST_SQLSERVER_TRUST_CERT === 'true',
@ -56,7 +56,7 @@ describe('SQLServerAdapter Integration', () => {
type: DataSourceType.SQLServer,
server: process.env.TEST_SQLSERVER_SERVER || 'localhost',
port: Number(process.env.TEST_SQLSERVER_PORT) || 1433,
database: process.env.TEST_SQLSERVER_DATABASE!,
default_database: process.env.TEST_SQLSERVER_DATABASE!,
username: process.env.TEST_SQLSERVER_USERNAME!,
password: process.env.TEST_SQLSERVER_PASSWORD!,
trust_server_certificate: process.env.TEST_SQLSERVER_TRUST_CERT === 'true',
@ -80,7 +80,7 @@ describe('SQLServerAdapter Integration', () => {
type: DataSourceType.SQLServer,
server: process.env.TEST_SQLSERVER_SERVER || 'localhost',
port: Number(process.env.TEST_SQLSERVER_PORT) || 1433,
database: process.env.TEST_SQLSERVER_DATABASE!,
default_database: process.env.TEST_SQLSERVER_DATABASE!,
username: process.env.TEST_SQLSERVER_USERNAME!,
password: process.env.TEST_SQLSERVER_PASSWORD!,
trust_server_certificate: process.env.TEST_SQLSERVER_TRUST_CERT === 'true',
@ -106,7 +106,7 @@ describe('SQLServerAdapter Integration', () => {
type: DataSourceType.SQLServer,
server: process.env.TEST_SQLSERVER_SERVER || 'localhost',
port: Number(process.env.TEST_SQLSERVER_PORT) || 1433,
database: process.env.TEST_SQLSERVER_DATABASE!,
default_database: process.env.TEST_SQLSERVER_DATABASE!,
username: process.env.TEST_SQLSERVER_USERNAME!,
password: process.env.TEST_SQLSERVER_PASSWORD!,
trust_server_certificate: process.env.TEST_SQLSERVER_TRUST_CERT === 'true',
@ -130,7 +130,7 @@ describe('SQLServerAdapter Integration', () => {
type: DataSourceType.SQLServer,
server: 'invalid-host',
port: 1433,
database: 'invalid-db',
default_database: 'invalid-db',
username: 'invalid-user',
password: 'invalid-pass',
};

View File

@ -51,7 +51,7 @@ describe('SQLServerAdapter', () => {
type: DataSourceType.SQLServer,
server: 'localhost',
port: 1433,
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
};
@ -76,7 +76,7 @@ describe('SQLServerAdapter', () => {
const credentials: SQLServerCredentials = {
type: DataSourceType.SQLServer,
server: 'localhost',
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
};
@ -94,7 +94,7 @@ describe('SQLServerAdapter', () => {
const credentials: SQLServerCredentials = {
type: DataSourceType.SQLServer,
server: 'localhost',
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
encrypt: false,
@ -131,7 +131,7 @@ describe('SQLServerAdapter', () => {
const credentials: SQLServerCredentials = {
type: DataSourceType.SQLServer,
server: 'localhost',
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
};
@ -148,7 +148,7 @@ describe('SQLServerAdapter', () => {
const credentials: SQLServerCredentials = {
type: DataSourceType.SQLServer,
server: 'localhost',
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
};
@ -367,7 +367,7 @@ describe('SQLServerAdapter', () => {
const credentials: SQLServerCredentials = {
type: DataSourceType.SQLServer,
server: 'localhost',
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
};
@ -414,7 +414,7 @@ describe('SQLServerAdapter', () => {
const credentials: SQLServerCredentials = {
type: DataSourceType.SQLServer,
server: 'localhost',
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
};
@ -435,7 +435,7 @@ describe('SQLServerAdapter', () => {
const credentials: SQLServerCredentials = {
type: DataSourceType.SQLServer,
server: 'localhost',
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
};
@ -453,7 +453,7 @@ describe('SQLServerAdapter', () => {
const credentials: SQLServerCredentials = {
type: DataSourceType.SQLServer,
server: 'localhost',
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
};
@ -468,7 +468,7 @@ describe('SQLServerAdapter', () => {
const credentials: SQLServerCredentials = {
type: DataSourceType.SQLServer,
server: 'localhost',
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
};
@ -487,7 +487,7 @@ describe('SQLServerAdapter', () => {
const credentials: SQLServerCredentials = {
type: DataSourceType.SQLServer,
server: 'localhost',
database: 'testdb',
default_database: 'testdb',
username: 'testuser',
password: 'testpass',
};

View File

@ -27,7 +27,7 @@ export class SQLServerAdapter extends BaseAdapter {
const config: sql.config = {
server: sqlServerCredentials.server,
port: sqlServerCredentials.port,
database: sqlServerCredentials.database,
database: sqlServerCredentials.default_database,
user: sqlServerCredentials.username,
password: sqlServerCredentials.password,
options: {

View File

@ -103,7 +103,7 @@ describe('DataSource Unit Tests', () => {
credentials: {
type: DataSourceType.MySQL,
host: 'localhost',
database: 'test',
default_database: 'test',
username: 'user',
password: 'pass',
},
@ -714,7 +714,7 @@ describe('DataSource Unit Tests', () => {
credentials: {
type: DataSourceType.MySQL,
host: 'localhost',
database: 'test',
default_database: 'test',
username: 'user',
password: 'pass',
},

View File

@ -19,7 +19,7 @@ function createSQLServerCredentials(): SQLServerCredentials {
type: DataSourceType.SQLServer,
server: testConfig.sqlserver.server,
port: testConfig.sqlserver.port,
database: testConfig.sqlserver.database,
default_database: testConfig.sqlserver.database,
username: testConfig.sqlserver.username,
password: testConfig.sqlserver.password,
encrypt: testConfig.sqlserver.encrypt,

View File

@ -119,7 +119,7 @@ export interface MySQLCredentials {
port?: number;
/** Database name */
database: string;
default_database: string;
/** Username for authentication */
username: string;
@ -158,7 +158,7 @@ export interface SQLServerCredentials {
port?: number;
/** Database name */
database: string;
default_database: string;
/** Username for authentication */
username: string;
@ -199,7 +199,7 @@ export interface RedshiftCredentials {
port?: number;
/** Database name */
database: string;
default_database: string;
/** Username for authentication */
username: string;
@ -208,7 +208,7 @@ export interface RedshiftCredentials {
password: string;
/** Default schema to use */
schema?: string;
default_schema?: string;
/** SSL configuration (required for Redshift) */
ssl?: boolean;