solidworks-mcp-server
Version:
Clean Architecture SolidWorks MCP Server - Production-ready with SOLID principles
132 lines • 4.03 kB
JavaScript
/**
* Database Connection Manager for Design Tables
* Supports SQL Server and PostgreSQL
*/
import pg from 'pg';
import mssql from 'mssql';
import { logger } from '../utils/logger.js';
export class PostgreSQLConnection {
client;
constructor(connectionString) {
this.client = new pg.Client({ connectionString });
}
async connect() {
await this.client.connect();
logger.info('Connected to PostgreSQL database');
}
async query(sql, params) {
const result = await this.client.query(sql, params);
return result.rows;
}
async close() {
await this.client.end();
logger.info('Disconnected from PostgreSQL database');
}
}
export class SQLServerConnection {
pool = null;
config;
constructor(connectionString) {
// Parse connection string for SQL Server
const url = new URL(connectionString);
this.config = {
server: url.hostname,
port: parseInt(url.port) || 1433,
database: url.pathname.substring(1),
user: url.username,
password: url.password,
options: {
encrypt: true,
trustServerCertificate: true
}
};
}
async connect() {
this.pool = await mssql.connect(this.config);
logger.info('Connected to SQL Server database');
}
async query(sql, params) {
if (!this.pool) {
await this.connect();
}
const request = this.pool.request();
// Add parameters if provided
if (params) {
params.forEach((param, index) => {
request.input(`param${index}`, param);
});
}
const result = await request.query(sql);
return result.recordset;
}
async close() {
if (this.pool) {
await this.pool.close();
logger.info('Disconnected from SQL Server database');
}
}
}
export class DatabaseManager {
connections = new Map();
/**
* Create a database connection
*/
async createConnection(name, connectionString) {
// Close existing connection if any
if (this.connections.has(name)) {
await this.closeConnection(name);
}
let connection;
if (connectionString.startsWith('postgresql://') || connectionString.startsWith('postgres://')) {
connection = new PostgreSQLConnection(connectionString);
await connection.connect();
}
else if (connectionString.startsWith('mssql://') || connectionString.startsWith('sqlserver://')) {
connection = new SQLServerConnection(connectionString);
await connection.connect();
}
else {
throw new Error(`Unsupported database type in connection string: ${connectionString}`);
}
this.connections.set(name, connection);
return connection;
}
/**
* Get an existing connection
*/
getConnection(name) {
return this.connections.get(name);
}
/**
* Close a connection
*/
async closeConnection(name) {
const connection = this.connections.get(name);
if (connection) {
await connection.close();
this.connections.delete(name);
}
}
/**
* Close all connections
*/
async closeAll() {
for (const [, connection] of this.connections) {
await connection.close();
}
this.connections.clear();
}
/**
* Execute a query on a named connection
*/
async query(connectionName, sql, params) {
const connection = this.connections.get(connectionName);
if (!connection) {
throw new Error(`No connection found with name: ${connectionName}`);
}
return await connection.query(sql, params);
}
}
// Export singleton instance
export const dbManager = new DatabaseManager();
//# sourceMappingURL=connection.js.map