@bazilio-san/af-stream
Version:
Data stream from database table
55 lines • 2.15 kB
JavaScript
// @ts-ignore
import * as pg from 'pg';
import { DbBase } from './DbBase';
const postgresDefaults = {
// all valid client config options are also valid here
// in addition here are the pool specific configuration parameters:
// number of milliseconds to wait before timing out when connecting a new client
// by default this is 0 which means no timeout
connectionTimeoutMillis: 30000,
// number of milliseconds a client must sit idle in the pool and not be checked out
// before it is disconnected from the backend and discarded
// default is 10000 (10 seconds) - set to 0 to disable auto-disconnection of idle clients
idleTimeoutMillis: 10000,
// maximum number of clients the pool should contain
// by default this is set to 10.
max: 10,
statement_timeout: 30000,
query_timeout: 30000, // number of milliseconds before a query call will timeout, default is no timeout
};
export class DbPostgres extends DbBase {
constructor(options) {
super(options);
this.pool = null;
const { dbOptions, dbConfig } = options;
const postgresDbOptions = { ...postgresDefaults, ...(dbOptions || {}) };
this.cfg = { ...postgresDbOptions, ...dbConfig };
}
async getPool() {
if (this.pool) {
return this.pool;
}
this.pool = new pg.Pool(this.cfg);
return this.pool;
}
async close() {
const self = this;
return new Promise((resolve) => {
var _a;
(_a = self.pool) === null || _a === void 0 ? void 0 : _a.end().then(() => resolve(true));
});
}
async query(strSQL) {
const pool = await this.getPool();
return pool.query(strSQL);
}
async _getColumnsNames() {
const { fields } = await this.query(`${'SELECT'} * FROM ${this.schemaAndTable} LIMIT 1`);
return fields.map((field) => field.name);
}
// eslint-disable-next-line class-methods-use-this
limitIt(strSQL, limit) {
return `${strSQL} LIMIT ${limit}`;
}
}
//# sourceMappingURL=DbPostgres.js.map