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.
91 lines • 3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PostgreSQLConnectionManager = void 0;
const pg_1 = require("pg");
class PostgreSQLConnectionManager {
constructor(config) {
this.client = null;
this.config = config;
}
async connect() {
try {
this.client = new pg_1.Client({
host: this.config.host,
port: this.config.port,
user: this.config.username,
password: this.config.password,
database: this.config.database,
});
await this.client.connect();
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
throw new Error(`PostgreSQL connection failed: ${errorMessage}`);
}
}
isConnected() {
return this.client !== null;
}
async disconnect() {
if (this.client) {
await this.client.end();
this.client = null;
}
}
async query(sql, parameters = []) {
if (!this.client) {
throw new Error('No PostgreSQL connection established');
}
try {
const result = await this.client.query(sql, parameters);
return {
rows: result.rows,
count: result.rowCount || 0,
query: sql,
parameters,
};
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
throw new Error(`PostgreSQL query failed: ${errorMessage}`);
}
}
async beginTransaction() {
if (!this.client) {
throw new Error('No PostgreSQL connection established');
}
try {
await this.client.query('BEGIN');
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
throw new Error(`Begin transaction failed: ${errorMessage}`);
}
}
async commit() {
if (!this.client) {
throw new Error('No PostgreSQL connection established');
}
try {
await this.client.query('COMMIT');
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
throw new Error(`Commit failed: ${errorMessage}`);
}
}
async rollback() {
if (!this.client) {
throw new Error('No PostgreSQL connection established');
}
try {
await this.client.query('ROLLBACK');
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
throw new Error(`Rollback failed: ${errorMessage}`);
}
}
}
exports.PostgreSQLConnectionManager = PostgreSQLConnectionManager;
//# sourceMappingURL=PostgreSQLConnectionManager.js.map