import sql from 'mssql';
import { ConnectionConfig } from './types.js';
export class SqlServerConnection {
private pool: sql.ConnectionPool | null = null;
private config: ConnectionConfig;
constructor(config: ConnectionConfig) {
this.config = config;
}
async connect(): Promise<void> {
if (this.pool) {
return;
}
const sqlConfig: sql.config = {
server: this.config.server,
database: this.config.database,
user: this.config.user,
password: this.config.password,
port: this.config.port,
options: {
encrypt: this.config.encrypt,
trustServerCertificate: this.config.trustServerCertificate,
connectTimeout: this.config.connectionTimeout,
requestTimeout: this.config.requestTimeout,
},
pool: {
max: 10,
min: 0,
idleTimeoutMillis: 30000,
},
};
this.pool = new sql.ConnectionPool(sqlConfig);
await this.pool.connect();
}
async disconnect(): Promise<void> {
if (this.pool) {
await this.pool.close();
this.pool = null;
}
}
async query<T = any>(queryText: string): Promise<sql.IResult<T>> {
if (!this.pool) {
throw new Error('Database connection not established');
}
const request = this.pool.request();
return await request.query(queryText);
}
async testConnection(): Promise<boolean> {
try {
await this.connect();
const result = await this.query('SELECT 1 as test');
return result.recordset.length > 0;
} catch (error) {
return false;
}
}
isConnected(): boolean {
return this.pool !== null && this.pool.connected;
}
getConfig(): Readonly<ConnectionConfig> {
return { ...this.config };
}
}