UNPKG

@cn-shell/postgresql

Version:

A Cloud Native Shell extension for PostgreSQL

163 lines (162 loc) 4.44 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; }, }); } : function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; }); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); } : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function(mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function(mod) { return mod && mod.__esModule ? mod : { default: mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CNPostgreSqlConn = exports.CNPostgreSql = void 0; // imports here const cn_shell_1 = __importDefault(require("cn-shell")); const postgresql_conn_1 = require("./postgresql-conn"); Object.defineProperty(exports, "CNPostgreSqlConn", { enumerable: true, get: function() { return postgresql_conn_1.CNPostgreSqlConn; }, }); const pg = __importStar(require("pg")); // Postgres config consts here const CFG_PG_USER = "PG_USER"; const CFG_PG_DB = "PG_DB"; const CFG_PG_PASSWORD = "PG_PASSWORD"; const CFG_PG_HOST = "PG_HOST"; const CFG_PG_PORT = "PG_PORT"; const CFG_PG_SSL = "PG_SSL"; const CFG_PG_OLD = "PG_OLD"; const DEFAULT_HOST = "localhost"; const DEFAULT_PORT = "5432"; const DEFAULT_SSL = "N"; const DEFAULT_OLD = "N"; // Class CNPostgreSQL here class CNPostgreSql extends cn_shell_1.default { // Constructor here constructor(name) { super(name); let user = this.getRequiredCfg(CFG_PG_USER); let database = this.getRequiredCfg(CFG_PG_DB); let password = this.getRequiredCfg(CFG_PG_PASSWORD); let host = this.getCfg(CFG_PG_HOST, DEFAULT_HOST); let port = parseInt(this.getCfg(CFG_PG_PORT, DEFAULT_PORT), 10); let ssl = this.getCfg(CFG_PG_SSL, DEFAULT_SSL).toUpperCase(); let old = this.getCfg(CFG_PG_OLD, DEFAULT_OLD).toUpperCase(); if (old === "N") { this._pool = new pg.Pool({ user, database, password, host, port, ssl: { rejectUnauthorized: ssl === "N" ? false : true, }, }); } else { this._pool = new pg.Pool({ user, database, password, host, port, ssl: ssl === "N" ? false : true, }); } this._pool.on("error", e => { this.error(e); }); } // Methods here async start() { this.info("Starting ..."); return new Promise((resolve, reject) => { this.isServerReady(resolve, reject); }); } isServerReady(resolve, reject) { // Lets check if we can query the time const sql = "SELECT now();"; this._pool .query(sql) .then(() => { this.info("PostgreSQL DB ready"); this.info("Started!"); resolve(true); }) .catch(e => { // If we get an "ECONNREFUSED" that means the DB has not started if (e.code === "ECONNREFUSED") { setTimeout(() => { this.isServerReady(resolve, reject); }, 5000); } else { this.error("DB returned the following error: (%s)", e); reject(false); } }); } async stop() { this.info("Stopping ..."); this.info("Closing the pool ..."); await this._pool.end().catch(e => { this.error(e.message); return; }); this.info("Pool closed!"); this.info("Stopped!"); } async healthCheck() { // Lets check if we can query the time const sql = "SELECT now();"; let e; await this._pool.query(sql).catch(err => { e = err; this.error(err); }); if (e === undefined) { return true; } else { return false; } } connection(name) { return new postgresql_conn_1.CNPostgreSqlConn(name, this._pool); } } exports.CNPostgreSql = CNPostgreSql;