UNPKG

@szegedsw/lib-node

Version:

A little framework published by Szeged Software Zrt. in order to enhance api endpoint security and create reuseable code. Email module, Logging system, and much more. Further improvements are expected.

147 lines 6.09 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.deleteQuickRes = exports.postQuickRes = exports.getQuickRes = exports.getMultRes = exports.getFullRes = exports.sqlParams = exports.oracleExecute = void 0; const oracledb_1 = __importDefault(require("oracledb")); const config_1 = require("../config/config"); const http_codes_1 = require("../config/http-codes"); const request_1 = require("../express/request"); const try_1 = require("../functions/try"); const logger_1 = require("../logger/logger"); const sql_handler_1 = require("./sql-handler"); function oracleExecute(sql, bindParams = [], options = {}) { // eslint-disable-next-line no-async-promise-executor return new Promise(async (resolve, reject) => { let conn; options.outFormat = oracledb_1.default.OUT_FORMAT_OBJECT; options.autoCommit = true; try { conn = await oracledb_1.default.getConnection(); logger_1.Logger.trace(sql); logger_1.Logger.trace(JSON.stringify(bindParams)); const result = await conn.execute(sql, bindParams, options); resolve(result); } catch (err) { reject(new Error(`${err.message}\n\n${sql}\n\n${JSON.stringify(bindParams)}\n`)); } finally { if (conn) { try { await conn.close(); } catch (err) { logger_1.Logger.error(err); } } } }); } exports.oracleExecute = oracleExecute; function sqlParams(res, sql, modSql) { res.locals.sql = sql; res.locals.modSql = modSql; } exports.sqlParams = sqlParams; async function getFullRes(req, res, key) { await try_1._try(res, async () => { const controller = String(new Error().stack) .split("\n") .find((line) => line.includes("controller")); const defKey = key || (controller ? (process.platform === "win32" ? controller.split("\\") : controller.split("/")).reverse()[0].split(".")[0] : "rows"); const { from, to } = request_1.requestParams(req); const { select, count } = res.locals.sql; const { join, where, countries } = res.locals.modSql; let { bind, bindCount } = res.locals.modSql; // sql injection protection with using oracle built-in query-builder bind = bind || {}; bindCount = bindCount || {}; Object.keys(where || {}).forEach((whereKey, index) => { if (where && sql_handler_1.isWhere(where, whereKey) && bind && bindCount) { bind[index] = where[whereKey]; bindCount[index] = where[whereKey]; where[whereKey] = `:${index}`; } }); const values = await Promise.all([ oracleExecute(sql_handler_1.rownumSql(sql_handler_1.countriesSql(countries || {}, sql_handler_1.whereSql(where || {}, sql_handler_1.joinSql(join || {}, select))), to), bind), oracleExecute(sql_handler_1.countriesSql(countries || {}, sql_handler_1.whereSql(where || {}, sql_handler_1.joinSql(join || {}, count))), bindCount), ]); if (values[0].rows && values[1].rows && values[0].rows.length > 0) { const response = { count: Math.min(values[1].rows[0].COUNT, config_1.env.databaseRownum) }; response[defKey] = to && to !== -1 ? values[0].rows.slice(from - 1, to) : values[0].rows; res.status(http_codes_1.HttpCodes.ok).json(response).end(); } else { res.status(http_codes_1.HttpCodes.notFound).end(); } }, { "no countries": { code: "expectationFailed", body: "No countries specified!" }, }); } exports.getFullRes = getFullRes; async function getMultRes(res, exes, keys, limits) { await try_1._try(res, async () => { const response = {}; const values = await Promise.all(exes); values.forEach((value, index) => { if (value.rows && value.rows.length > 0) { const limit = limits === null || limits === void 0 ? void 0 : limits.find((l) => l.key === keys[index]); response[keys[index]] = limit ? value.rows.slice(limit.from - 1, limit.to) : value.rows; } }); if (Object.keys(response).length) { res.status(http_codes_1.HttpCodes.ok).json(response).end(); } else { res.status(http_codes_1.HttpCodes.notFound).end(); } }); } exports.getMultRes = getMultRes; async function getQuickRes(res, exe, key) { await getMultRes(res, [exe], [key]); } exports.getQuickRes = getQuickRes; async function postQuickRes(res, sql, bindParams, callback) { await try_1._try(res, async () => { const result = await oracleExecute(sql, bindParams); if (Boolean(result.rowsAffected) || result.outBinds) { if (callback) { await callback(); } res.status(http_codes_1.HttpCodes.ok) .json(result.outBinds || {}) .end(); } else { throw new Error("post conflict"); } }, { "post conflict": { code: "conflict", body: "Post conflict happened." }, }); } exports.postQuickRes = postQuickRes; async function deleteQuickRes(res, sql, bindParams, callback) { await try_1._try(res, async () => { const result = await oracleExecute(sql, bindParams); if (result.rowsAffected) { if (callback) { await callback(); } res.status(http_codes_1.HttpCodes.ok).end(); } else { throw new Error("not found"); } }, { "not found": { code: "notFound", body: "Delete did not find resource." }, }); } exports.deleteQuickRes = deleteQuickRes; //# sourceMappingURL=oracle-execute.js.map