UNPKG

queryforge

Version:

A powerful, type-safe SQL query builder for Node.js with support for MySQL, PostgreSQL, and SQLite. Features fluent API, transaction management, and connection pooling.

90 lines 3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MySQLConnectionManager = void 0; const promise_1 = require("mysql2/promise"); class MySQLConnectionManager { constructor(config) { this.connection = null; this.config = config; } async connect() { try { this.connection = await (0, promise_1.createConnection)({ host: this.config.host, port: this.config.port, user: this.config.username, password: this.config.password, database: this.config.database, }); } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; throw new Error(`MySQL connection failed: ${errorMessage}`); } } isConnected() { return this.connection !== null; } async disconnect() { if (this.connection) { await this.connection.end(); this.connection = null; } } async query(sql, parameters = []) { if (!this.connection) { throw new Error('No MySQL connection established'); } try { const [rows] = await this.connection.execute(sql, parameters); return { rows: rows, count: Array.isArray(rows) ? rows.length : 0, query: sql, parameters, }; } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; throw new Error(`MySQL query failed: ${errorMessage}`); } } async beginTransaction() { if (!this.connection) { throw new Error('No MySQL connection established'); } try { await this.connection.beginTransaction(); } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; throw new Error(`Begin transaction failed: ${errorMessage}`); } } async commit() { if (!this.connection) { throw new Error('No MySQL connection established'); } try { await this.connection.commit(); } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; throw new Error(`Commit failed: ${errorMessage}`); } } async rollback() { if (!this.connection) { throw new Error('No MySQL connection established'); } try { await this.connection.rollback(); } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; throw new Error(`Rollback failed: ${errorMessage}`); } } } exports.MySQLConnectionManager = MySQLConnectionManager; //# sourceMappingURL=MySQLConnectionManager.js.map