182 lines
6.1 KiB
JavaScript
182 lines
6.1 KiB
JavaScript
|
"use strict";
|
||
|
exports.id = 1206;
|
||
|
exports.ids = [1206];
|
||
|
exports.modules = {
|
||
|
|
||
|
/***/ 7410:
|
||
|
/***/ ((module) => {
|
||
|
|
||
|
// @ts-check
|
||
|
/**
|
||
|
* Regular expression to match default fields
|
||
|
*
|
||
|
* @description Regular expression to match default fields
|
||
|
*/
|
||
|
const defaultFieldsRegexp = /^id$|^uuid$|^date_created$|^date_created_code$|^date_created_timestamp$|^date_updated$|^date_updated_code$|^date_updated_timestamp$/;
|
||
|
////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
module.exports = defaultFieldsRegexp;
|
||
|
|
||
|
|
||
|
/***/ }),
|
||
|
|
||
|
/***/ 7432:
|
||
|
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
|
||
|
|
||
|
// @ts-check
|
||
|
|
||
|
const decrypt = __webpack_require__(5304);
|
||
|
const defaultFieldsRegexp = __webpack_require__(7410);
|
||
|
/**
|
||
|
* Parse Database results
|
||
|
* ==============================================================================
|
||
|
* @description this function takes a database results array gotten from a DB handler
|
||
|
* function, decrypts encrypted fields, and returns an updated array with no encrypted
|
||
|
* fields
|
||
|
*
|
||
|
* @param {object} params - Single object params
|
||
|
* @param {any[]} params.unparsedResults - Array of data objects containing Fields(keys)
|
||
|
* and corresponding values of the fields(values)
|
||
|
* @param {import("../../types").DSQL_TableSchemaType} [params.tableSchema] - Table schema
|
||
|
* @returns {Promise<object[]|null>}
|
||
|
*/ module.exports = async function parseDbResults({ unparsedResults , tableSchema , }) {
|
||
|
/**
|
||
|
* Declare variables
|
||
|
*
|
||
|
* @description Declare "results" variable
|
||
|
*/ let parsedResults = [];
|
||
|
try {
|
||
|
/**
|
||
|
* Declare variables
|
||
|
*
|
||
|
* @description Declare "results" variable
|
||
|
*/ for(let pr = 0; pr < unparsedResults.length; pr++){
|
||
|
let result = unparsedResults[pr];
|
||
|
let resultFieldNames = Object.keys(result);
|
||
|
for(let i = 0; i < resultFieldNames.length; i++){
|
||
|
const resultFieldName = resultFieldNames[i];
|
||
|
let resultFieldSchema = tableSchema?.fields[i];
|
||
|
if (resultFieldName?.match(defaultFieldsRegexp)) {
|
||
|
continue;
|
||
|
}
|
||
|
let value = result[resultFieldName];
|
||
|
if (typeof value !== "number" && !value) {
|
||
|
continue;
|
||
|
}
|
||
|
if (resultFieldSchema?.encrypted) {
|
||
|
if (value?.match(/./)) {
|
||
|
result[resultFieldName] = decrypt(value);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
parsedResults.push(result);
|
||
|
}
|
||
|
/**
|
||
|
* Declare variables
|
||
|
*
|
||
|
* @description Declare "results" variable
|
||
|
*/ return parsedResults;
|
||
|
} catch (/** @type {any} */ error) {
|
||
|
console.log("ERROR in parseDbResults Function =>", error.message);
|
||
|
return unparsedResults;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
|
||
|
/***/ }),
|
||
|
|
||
|
/***/ 1206:
|
||
|
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
|
||
|
|
||
|
// @ts-check
|
||
|
|
||
|
const fs = __webpack_require__(7147);
|
||
|
const parseDbResults = __webpack_require__(7432);
|
||
|
const serverError = __webpack_require__(7023);
|
||
|
const DB_HANDLER = __webpack_require__(9395);
|
||
|
const DSQL_USER_DB_HANDLER = __webpack_require__(8682);
|
||
|
/**
|
||
|
* DB handler for specific database
|
||
|
* ==============================================================================
|
||
|
* @async
|
||
|
* @param {object} params - Single object params
|
||
|
* @param {string} params.queryString - SQL string
|
||
|
* @param {*[]} [params.queryValuesArray] - Values Array
|
||
|
* @param {string} [params.database] - Database name
|
||
|
* @param {import("../../types").DSQL_TableSchemaType} [params.tableSchema] - Table schema
|
||
|
* @returns {Promise<any>}
|
||
|
*/ module.exports = async function varDatabaseDbHandler({ queryString , queryValuesArray , database , tableSchema , }) {
|
||
|
/**
|
||
|
* Declare variables
|
||
|
*
|
||
|
* @description Declare "results" variable
|
||
|
*/ const isMaster = database?.match(/^datasquirel$/) ? true : false;
|
||
|
/** @type {any} */ const FINAL_DB_HANDLER = isMaster ? DB_HANDLER : DSQL_USER_DB_HANDLER;
|
||
|
let results;
|
||
|
/**
|
||
|
* Fetch from db
|
||
|
*
|
||
|
* @description Fetch data from db if no cache
|
||
|
*/ try {
|
||
|
if (queryString && queryValuesArray && Array.isArray(queryValuesArray) && queryValuesArray[0]) {
|
||
|
results = isMaster ? await FINAL_DB_HANDLER(queryString, queryValuesArray) : await FINAL_DB_HANDLER({
|
||
|
paradigm: "Full Access",
|
||
|
database,
|
||
|
queryString,
|
||
|
queryValues: queryValuesArray
|
||
|
});
|
||
|
} else {
|
||
|
results = isMaster ? await FINAL_DB_HANDLER(queryString) : await FINAL_DB_HANDLER({
|
||
|
paradigm: "Full Access",
|
||
|
database,
|
||
|
queryString
|
||
|
});
|
||
|
}
|
||
|
////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
} catch (/** @type {any} */ error) {
|
||
|
serverError({
|
||
|
component: "varDatabaseDbHandler/lines-29-32",
|
||
|
message: error.message
|
||
|
});
|
||
|
}
|
||
|
/**
|
||
|
* Return results
|
||
|
*
|
||
|
* @description Return results add to cache if "req" param is passed
|
||
|
*/ if (results && tableSchema) {
|
||
|
try {
|
||
|
const unparsedResults = results;
|
||
|
const parsedResults = await parseDbResults({
|
||
|
unparsedResults: unparsedResults,
|
||
|
tableSchema: tableSchema
|
||
|
});
|
||
|
return parsedResults;
|
||
|
} catch (/** @type {any} */ error1) {
|
||
|
console.log("\x1b[31mvarDatabaseDbHandler ERROR\x1b[0m =>", database, error1);
|
||
|
serverError({
|
||
|
component: "varDatabaseDbHandler/lines-52-53",
|
||
|
message: error1.message
|
||
|
});
|
||
|
return null;
|
||
|
}
|
||
|
////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
} else if (results) {
|
||
|
return results;
|
||
|
////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
} else {
|
||
|
return null;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
|
||
|
/***/ })
|
||
|
|
||
|
};
|
||
|
;
|