@vasoyaprince14/sql-analyzer
Version:
🚀 Enhanced SQL database analyzer with AI-powered insights, comprehensive security analysis, RLS policy auditing, and beautiful HTML reports
229 lines • 7.31 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.configPresets = exports.ConfigManager = exports.defaultConfig = void 0;
exports.defaultConfig = {
database: {
host: 'localhost',
port: 5432,
ssl: false
},
ai: {
enabled: false,
provider: 'openai',
model: 'gpt-4',
maxTokens: 2000,
temperature: 0.1
},
analysis: {
includeSchema: true,
includeTriggers: true,
includeProcedures: true,
includeRLS: true,
includeCostAnalysis: true,
includeAIInsights: false,
performanceThreshold: 1000,
securityLevel: 'standard'
},
reporting: {
format: 'html',
outputPath: './reports',
includeCharts: true,
includeBeforeAfter: true,
includeImplementationGuide: true
},
advanced: {
concurrentAnalysis: true,
cacheResults: false,
cacheTTL: 300,
retryAttempts: 3,
timeout: 30000,
verbose: false,
skipLargeObjects: false,
maxTableSize: 1000000000 // 1GB
}
};
class ConfigManager {
constructor(userConfig) {
this.config = this.mergeConfig(exports.defaultConfig, userConfig || {});
}
getConfig() {
return this.config;
}
updateConfig(updates) {
this.config = this.mergeConfig(this.config, updates);
}
mergeConfig(base, override) {
return {
database: { ...base.database, ...override.database },
ai: { ...base.ai, ...override.ai },
analysis: { ...base.analysis, ...override.analysis },
reporting: { ...base.reporting, ...override.reporting },
advanced: { ...base.advanced, ...override.advanced }
};
}
validateConfig() {
const errors = [];
// Validate AI settings
if (this.config.ai?.enabled && !this.config.ai?.apiKey) {
errors.push('AI is enabled but no API key provided');
}
// Validate database settings
if (!this.config.database?.connectionString &&
(!this.config.database?.host || !this.config.database?.database)) {
errors.push('Either connectionString or host+database must be provided');
}
// Validate output path
if (this.config.reporting?.outputPath &&
!this.isValidPath(this.config.reporting.outputPath)) {
errors.push('Invalid output path specified');
}
return {
valid: errors.length === 0,
errors
};
}
isValidPath(path) {
// Basic path validation
return typeof path === 'string' && path.length > 0;
}
/**
* Load configuration from environment variables
*/
static fromEnvironment() {
const envConfig = {};
// Database settings from env
if (process.env.DATABASE_URL) {
envConfig.database = { connectionString: process.env.DATABASE_URL };
}
else {
envConfig.database = {
host: process.env.DB_HOST,
port: process.env.DB_PORT ? parseInt(process.env.DB_PORT) : undefined,
database: process.env.DB_NAME,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
ssl: process.env.DB_SSL === 'true'
};
}
// AI settings from env
if (process.env.OPENAI_API_KEY) {
envConfig.ai = {
enabled: true,
provider: 'openai',
apiKey: process.env.OPENAI_API_KEY,
model: process.env.OPENAI_MODEL || 'gpt-4'
};
}
// Analysis settings from env
envConfig.analysis = {
includeAIInsights: process.env.ENABLE_AI_INSIGHTS === 'true',
securityLevel: process.env.SECURITY_LEVEL || 'standard'
};
// Reporting settings from env
envConfig.reporting = {
format: process.env.REPORT_FORMAT || 'html',
outputPath: process.env.OUTPUT_PATH || './reports'
};
return new ConfigManager(envConfig);
}
/**
* Load configuration from file
*/
static async fromFile(filePath) {
try {
const fs = await Promise.resolve().then(() => __importStar(require('fs/promises')));
const configData = await fs.readFile(filePath, 'utf-8');
const userConfig = JSON.parse(configData);
return new ConfigManager(userConfig);
}
catch (error) {
throw new Error(`Failed to load config from ${filePath}: ${error}`);
}
}
/**
* Save configuration to file
*/
async saveToFile(filePath) {
try {
const fs = await Promise.resolve().then(() => __importStar(require('fs/promises')));
await fs.writeFile(filePath, JSON.stringify(this.config, null, 2));
}
catch (error) {
throw new Error(`Failed to save config to ${filePath}: ${error}`);
}
}
}
exports.ConfigManager = ConfigManager;
// Configuration presets for common scenarios
exports.configPresets = {
development: {
advanced: {
verbose: true,
cacheResults: false
},
analysis: {
securityLevel: 'basic'
}
},
production: {
advanced: {
verbose: false,
cacheResults: true,
cacheTTL: 600
},
analysis: {
securityLevel: 'strict',
includeAIInsights: true
}
},
ci: {
reporting: {
format: 'json'
},
analysis: {
includeAIInsights: false
},
advanced: {
timeout: 60000
}
},
comprehensive: {
analysis: {
includeSchema: true,
includeTriggers: true,
includeProcedures: true,
includeRLS: true,
includeCostAnalysis: true,
includeAIInsights: true,
securityLevel: 'strict'
},
ai: {
enabled: true,
maxTokens: 4000
}
}
};
//# sourceMappingURL=config.js.map