UNPKG

db-conn-pgsql

Version:

Database Connecton Postgres SQL

64 lines 2.04 kB
import { PgQueryUtils } from "./PgQueryUtils.js"; import { PgSqlResultSet } from "./PgSqlResultSet.js"; import Cursor from 'pg-cursor'; import { toAnsiTypeColumns, toAnsiValueRows } from "./PgSqlUtils.js"; export class PgSqlConnection { client; oUtils; constructor(client) { this.client = client; this.oUtils = new PgQueryUtils(); } async close() { if (this.client.release) { this.client.release(); delete this.client; return; } await this.client.end(); delete this.client; } async execute(sql, params) { const retParams = this.oUtils.formatParams(sql, params); const newSql = retParams.sql; const newParams = retParams.params; return new Promise((resolve, reject) => { this.client.query(newSql, newParams, (error, result) => { if (error) { reject(error); return; } let rt = {}; rt.metadata = toAnsiTypeColumns(result.fields); rt.affectedRows = result.rowCount; rt.data = toAnsiValueRows(result.rows, rt.metadata); resolve(rt); }); }); } async executeQuery(sql, params) { const rt = await this.execute(sql, params); if (rt.data === undefined) { throw new Error("No data returned"); } return rt.data; } async executeQueryStream(sql, params) { const cursor = this.client.query(new Cursor(sql, params)); const rs = new PgSqlResultSet(cursor, this.client); await rs.init(); return rs; } async setAutoCommit(autoCommit) { if (!autoCommit) { const rt = await this.execute("begin"); } } async commit() { const rt = await this.execute("commit"); } async rollback() { const rt = await this.execute("rollback"); } } //# sourceMappingURL=PgSqlConnection.js.map