datasquirel/engine/query/get.js

109 lines
3.3 KiB
JavaScript
Raw Normal View History

2023-09-21 14:00:04 +00:00
/** # MODULE TRACE
======================================================================
* No imports found for this Module
==== MODULE TRACE END ==== */
2024-11-06 06:26:23 +00:00
const runQuery = require("../../package-shared/functions/backend/db/runQuery");
2023-09-21 14:00:04 +00:00
2024-11-06 06:26:23 +00:00
// @ts-check
2023-09-21 14:00:04 +00:00
/**
* @typedef {Object} LocalGetReturn
* @property {boolean} success - Did the function run successfully?
* @property {*} [payload] - GET request results
* @property {string} [msg] - Message
* @property {string} [error] - Error Message
*/
/**
* @typedef {Object} LocalQueryObject
* @property {string} query - Table Name
* @property {string} [tableName] - Table Name
* @property {string[]} [queryValues] - GET request results
*/
/**
* Make a get request to Datasquirel API
* ==============================================================================
* @async
*
* @param {Object} params - Single object passed
* @param {LocalQueryObject} params.options - SQL Query
2024-10-19 16:45:42 +00:00
* @param {import("../../package-shared/types").DSQL_DatabaseSchemaType | undefined} [params.dbSchema] - Name of the table to query
2023-09-21 14:00:04 +00:00
*
* @returns { Promise<LocalGetReturn> } - Return Object
*/
async function localGet({ options, dbSchema }) {
try {
const { query, queryValues } = options;
/** @type {string | undefined | any } */
const tableName = options?.tableName ? options.tableName : undefined;
const dbFullName = process.env.DSQL_DB_NAME || "";
/**
* Input Validation
*
* @description Input Validation
*/
2024-10-14 06:49:01 +00:00
if (
typeof query == "string" &&
(query.match(
/^alter|^delete|information_schema|databases|^create/i
) ||
2024-11-18 09:25:28 +00:00
!query.match(/^select|^\( ?select/i))
2024-10-14 06:49:01 +00:00
) {
2023-09-21 14:00:04 +00:00
return { success: false, msg: "Wrong Input" };
}
/**
* Create new user folder and file
*
* @description Create new user folder and file
*/
let results;
try {
let { result, error } = await runQuery({
dbFullName: dbFullName,
query: query,
queryValuesArray: queryValues,
dbSchema,
tableName,
2024-11-06 06:26:23 +00:00
local: true,
2023-09-21 14:00:04 +00:00
});
if (error) throw error;
2024-10-14 06:49:01 +00:00
if (!result)
throw new Error("No Result received for query => " + query);
2023-09-21 14:00:04 +00:00
if (result?.error) throw new Error(result.error);
results = result;
return { success: true, payload: results };
////////////////////////////////////////
} catch (/** @type {*} */ error) {
////////////////////////////////////////
console.log("Error in local get Request =>", error.message);
return {
success: false,
payload: null,
error: error.message,
};
}
////////////////////////////////////////
} catch (/** @type {*} */ error) {
////////////////////////////////////////
console.log("Error in local get Request =>", error.message);
return { success: false, msg: "Something went wrong!" };
////////////////////////////////////////
}
}
module.exports = localGet;