inceptum
Version:
hipages take on the foundational library for enterprise-grade apps written in NodeJS
90 lines • 2.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PostgresClient = exports.PostgresTransaction = void 0;
const pg_1 = require("pg");
const DBClient_1 = require("../db/DBClient");
const DBTransaction_1 = require("../db/DBTransaction");
const LogManager_1 = require("../log/LogManager");
const LOGGER = LogManager_1.LogManager.getLogger(__filename);
class PostgresTransaction extends DBTransaction_1.DBTransaction {
runQueryInConnection(sql, bindsArr) {
return new Promise((resolve, reject) => this.connection.query(sql, bindsArr, (err, rows) => {
if (err) {
LOGGER.error(err, `SQL error for ${sql}`);
return reject(err);
}
return resolve(rows.rows);
}));
}
}
exports.PostgresTransaction = PostgresTransaction;
class PostgresConnectionFactory {
constructor(name, connectionConfig) {
this.connConfig = connectionConfig;
this.name = name;
}
async create() {
LOGGER.trace(`Creating new connection for pool ${this.name}`);
const connection = new pg_1.Client(this.connConfig);
await new Promise((resolve, reject) => connection.connect((err) => {
if (err) {
reject(err);
}
else {
resolve();
}
}));
return connection;
}
async destroy(connection) {
LOGGER.trace(`Destroying connection for pool ${this.name}`);
await new Promise((resolve, reject) => connection.end((err) => {
if (err) {
reject(err);
}
else {
resolve();
}
}));
return undefined;
}
validate(connection) {
LOGGER.trace(`Validating connection for pool ${this.name}`);
return new Promise((resolve, reject) => {
connection.query('SELECT 1', (err, results) => {
if (err) {
resolve(false);
}
resolve(true);
});
});
}
}
/**
* A MySQL client you can use to execute queries against MySQL
*/
class PostgresClient extends DBClient_1.DBClient {
async initialise() {
await super.initialise();
}
getConnectionFactory(name, connectionConfig) {
return new PostgresConnectionFactory(name, connectionConfig);
}
getNewDBTransaction(readonly, connectionPool) {
return new PostgresTransaction(this.clientConfiguration.name, readonly, connectionPool);
}
getPingQuery() {
return 'SELECT 1';
}
getDefaultConnectionConfig() {
return {
host: 'localhost',
port: 5432,
user: 'postgres',
password: '',
database: '',
};
}
}
exports.PostgresClient = PostgresClient;
//# sourceMappingURL=PostgresClient.js.map