UNPKG

@bazilio-san/af-stream

Version:
59 lines 2.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DbPostgres = void 0; // @ts-ignore const pg = require("pg"); const DbBase_1 = require("./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 }; class DbPostgres extends DbBase_1.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}`; } } exports.DbPostgres = DbPostgres; //# sourceMappingURL=DbPostgres.js.map