api-beep-onboarding
Version:
Oracle OCI FaaS for Api Beep Onboarding library
238 lines (200 loc) • 8.15 kB
JavaScript
/**
*
* BeePay 2.023-2.024 - Oracle OCI FaaS for Api Beep Onboarding
* Autonomous Data Warehouse database connection
*
* Changes:
* jlugo: 2024-jan-10. File creation
* jlugo: 2024-jan-19. Add ADWError class
* jlugo: 2024-apr-25. Moved to a library
*/
/* RGVzYXJyb2xsYWRvIHBvciBKb25hdGhhbiBMdWdv */
import process from "node:process";
import oracledb from "oracledb";
/**
* Autonomous Dataware House errors
*/
export class ADWError {
#error;
/**
*
* @param {String} code - Exception code
* @param {String} error - Exception message
*/
constructor(code, error) {
//--- Clean up oracledb node js message errors
// eg: ORA-20001: Se genero el siguiente error: -1 -ERROR- ORA-00001: unique constraint (DWH_LAFISE_QA.PK_DT_CLIENT) violated
// ORA-06512: at "DWH_LAFISE_QA.PR_INSERTA_CLIENTE", line 69
// ORA-06512: at line 1
// Help: https://docs.oracle.com/error-help/db/ora-20001/
// --> msg=ORA-00001: unique constraint violated
let a = error?.split("\n")?.[0];
let b = a?.split("-ERROR-")?.[1];
let msg = b?.replaceAll(/\s*\(.*?\)\s*/g, " ");
this.#error = `APIOB-${code}. ${msg ? msg?.trim() : error}`;
}
get Error() {
return this.#error;
}
}
/**
* OCI Autonomous Dataware House operations
*/
export class ADWConnection {
#pool = null;
m_dbConfig;
m_debug;
m_connection;
/**
*
* @param {Object} dbConfig - {
* "wallet": "wallet password",
* "walletLocation": "wallet path=TNS_ADMIN",
* "username": "database username",
* "password": "database user password",
* "service": "database TNS_NAME service"
* }
* @param {Object} debug - Node.js console object
*/
constructor(dbConfig, debug) {
this.m_dbConfig = dbConfig;
this.m_debug = debug;
this.m_connection = null;
}
/**
* Create ADW connection pool
*/
async connectAsync() {
if (this.#pool !== null) {
return;
}
const start = process.hrtime.bigint();
try {
this.#pool = await oracledb.createPool({
user: this.m_dbConfig.username,
password: this.m_dbConfig.password,
connectionString: this.m_dbConfig.service,
configDir: this.m_dbConfig.walletLocation,
walletLocation: this.m_dbConfig.walletLocation,
walletPassword: this.m_dbConfig.wallet,
poolIncrement: 0,
poolMin: 4,
poolMax: 4
});
}
catch(ex) {
this.#pool = null;
this.m_debug?.log(`APIOB-ADW01. ${ex.message}`);
}
const end = process.hrtime.bigint()
this.m_debug?.log(`ADW pool created. ${(end - start) / 1000000n}ms`);
}
/**
* Close ADW connection pool
*/
async disconnectAsync() {
const start = process.hrtime.bigint();
if (this.#pool) {
try {
await this.#pool?.close();
this.#pool = null;
}
catch (ex) {
this.m_debug?.log(`APIOB-ADW02. ${ex.message}`);
}
}
const end = process.hrtime.bigint();
this.m_debug?.log(`ADW pool closed. ${(end - start) / 1000000n}ms`);
}
/**
* Get a new database connection from pool
*/
async #openAsync() {
const start = process.hrtime.bigint();
this.m_connection = await this.#pool?.getConnection();
const end = process.hrtime.bigint();
this.m_debug?.log(`ADW connected v${this.m_connection?.oracleServerVersionString}. ${(end - start) / 1000000n}ms`);
}
/**
* Close the database connection and get back to pool
*/
async #closeAsync() {
const start = process.hrtime.bigint();
await this.m_connection?.close();
const end = process.hrtime.bigint();
this.m_debug?.log(`ADW connection closed. ${(end - start) / 1000000n}ms`);
}
/**
* Use for test database connection
*
* @returns {Promise<Object>} - {
* "metaData": [
* {
* "name": "FECHA",
* "dbType": {
* "num": 2001,
* "name": "DB_TYPE_VARCHAR",
* "columnTypeName": "VARCHAR2",
* "_bufferSizeFactor": 4,
* "_oraTypeNum": 1,
* "_csfrm": 1
* },
* "nullable": true,
* "byteSize": 10,
* "dbTypeName": "VARCHAR2",
* "fetchType": {
* "num": 2001,
* "name": "DB_TYPE_VARCHAR",
* "columnTypeName": "VARCHAR2",
* "_bufferSizeFactor": 4,
* "_oraTypeNum": 1,
* "_csfrm": 1
* }
* }
* ],
* "rows": [
* {
* "FECHA": "DD-MM-YYYY"
* }
* ]
* }
*/
async testAsync() {
const start = process.hrtime.bigint();
let result = null;
try {
await this.#openAsync();
result = await this.m_connection?.execute(`SELECT TO_CHAR(SYSDATE, 'DD-MM-YYYY') AS FECHA FROM DUAL`, // sql
[], // bind params
{ outFormat: oracledb.OUT_FORMAT_OBJECT }); // options
}
catch (ex) {
this.m_debug?.log(`APIOB-ADW03. ${ex.message}`);
}
finally {
await this.#closeAsync();
}
const end = process.hrtime.bigint();
this.m_debug?.log(`ADW test connection: ${(end - start) / 1000000n}ms`);
return result;
}
/**
*
* @param {String} sql
* @param {Object} bindParams
* @param {Object} options
* @returns
*/
async executeAsync(sql, bindParams, options = {}) {
let result ;
try {
await this.#openAsync();
result = await this.m_connection.execute(sql, bindParams, options);
}
finally {
await this.#closeAsync();
}
return result ;
}
}
export default ADWConnection;