2023-09-21 14:00:04 +00:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ==============================================================================
|
|
|
|
* Imports
|
|
|
|
* ==============================================================================
|
|
|
|
*/
|
2024-07-13 11:19:48 +00:00
|
|
|
const http = require("node:http");
|
|
|
|
const https = require("node:https");
|
2023-09-21 14:00:04 +00:00
|
|
|
const path = require("path");
|
|
|
|
const fs = require("fs");
|
|
|
|
const localGet = require("../engine/query/get");
|
2024-10-18 04:49:04 +00:00
|
|
|
const serializeQuery = require("./functions/serialize-query");
|
2023-09-21 14:00:04 +00:00
|
|
|
|
|
|
|
/** ****************************************************************************** */
|
|
|
|
/** ****************************************************************************** */
|
|
|
|
/** ****************************************************************************** */
|
|
|
|
/** ****************************************************************************** */
|
|
|
|
/** ****************************************************************************** */
|
|
|
|
/** ****************************************************************************** */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make a get request to Datasquirel API
|
|
|
|
* ==============================================================================
|
|
|
|
* @async
|
|
|
|
*
|
|
|
|
* @param {Object} params - Single object passed
|
|
|
|
* @param {string} [params.key] - API Key
|
|
|
|
* @param {string} [params.db] - Database Name
|
|
|
|
* @param {string} params.query - SQL Query
|
|
|
|
* @param {string[]} [params.queryValues] - An array of query values if using "?" placeholders
|
|
|
|
* @param {string} [params.tableName] - Name of the table to query
|
|
|
|
*
|
2024-10-19 16:45:42 +00:00
|
|
|
* @returns { Promise<import("../package-shared/types").GetReturn> } - Return Object
|
2023-09-21 14:00:04 +00:00
|
|
|
*/
|
|
|
|
async function get({ key, db, query, queryValues, tableName }) {
|
2023-09-21 16:51:08 +00:00
|
|
|
const scheme = process.env.DSQL_HTTP_SCHEME;
|
|
|
|
const localHost = process.env.DSQL_LOCAL_HOST;
|
|
|
|
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
|
|
|
|
|
2023-09-21 14:00:04 +00:00
|
|
|
/**
|
|
|
|
* Check for local DB settings
|
|
|
|
*
|
|
|
|
* @description Look for local db settings in `.env` file and by pass the http request if available
|
|
|
|
*/
|
2023-09-21 16:51:08 +00:00
|
|
|
const {
|
2024-11-06 06:26:23 +00:00
|
|
|
DSQL_DB_HOST,
|
|
|
|
DSQL_DB_USERNAME,
|
|
|
|
DSQL_MARIADB_ROOT_PASSWORD,
|
2023-09-21 16:51:08 +00:00
|
|
|
DSQL_DB_NAME,
|
|
|
|
} = process.env;
|
2023-09-21 14:00:04 +00:00
|
|
|
|
2023-09-21 16:51:08 +00:00
|
|
|
if (
|
2024-11-06 06:26:23 +00:00
|
|
|
DSQL_DB_HOST?.match(/./) &&
|
|
|
|
DSQL_DB_USERNAME?.match(/./) &&
|
|
|
|
DSQL_MARIADB_ROOT_PASSWORD?.match(/./) &&
|
2023-09-21 16:51:08 +00:00
|
|
|
DSQL_DB_NAME?.match(/./)
|
|
|
|
) {
|
2024-10-19 16:45:42 +00:00
|
|
|
/** @type {import("../package-shared/types").DSQL_DatabaseSchemaType | undefined} */
|
2023-09-21 14:00:04 +00:00
|
|
|
let dbSchema;
|
|
|
|
|
|
|
|
try {
|
2023-09-21 16:51:08 +00:00
|
|
|
const localDbSchemaPath = path.resolve(
|
|
|
|
process.cwd(),
|
|
|
|
"dsql.schema.json"
|
|
|
|
);
|
2023-09-21 14:00:04 +00:00
|
|
|
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
|
|
|
|
} catch (error) {}
|
|
|
|
|
|
|
|
return await localGet({
|
|
|
|
dbSchema: dbSchema,
|
|
|
|
options: {
|
|
|
|
query: query,
|
|
|
|
queryValues: queryValues,
|
|
|
|
tableName: tableName,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make https request
|
|
|
|
*
|
|
|
|
* @description make a request to datasquirel.com
|
|
|
|
*/
|
|
|
|
const httpResponse = await new Promise((resolve, reject) => {
|
2024-10-19 16:45:42 +00:00
|
|
|
/** @type {import("../package-shared/types").GetReqQueryObject} */
|
2024-10-18 04:49:04 +00:00
|
|
|
const queryObject = {
|
|
|
|
db: String(db),
|
|
|
|
query: String(
|
|
|
|
query
|
|
|
|
.replace(/\n|\r|\n\r/g, "")
|
|
|
|
.replace(/ {2,}/g, " ")
|
|
|
|
.replace(/ /g, "+")
|
|
|
|
),
|
|
|
|
queryValues: queryValues ? JSON.stringify(queryValues) : undefined,
|
|
|
|
tableName,
|
|
|
|
};
|
|
|
|
|
|
|
|
const queryString = serializeQuery({ query: queryObject });
|
|
|
|
|
|
|
|
let path = `/api/query/get${queryString}`;
|
2023-09-21 14:00:04 +00:00
|
|
|
|
2024-07-13 10:53:35 +00:00
|
|
|
/** @type {https.RequestOptions} */
|
|
|
|
const requestObject = {
|
|
|
|
method: "GET",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: key,
|
|
|
|
},
|
|
|
|
port: localHostPort || 443,
|
|
|
|
hostname: localHost || "datasquirel.com",
|
2024-07-13 11:19:48 +00:00
|
|
|
path: encodeURI(path),
|
2024-07-13 10:53:35 +00:00
|
|
|
};
|
|
|
|
|
2023-09-21 16:51:08 +00:00
|
|
|
(scheme?.match(/^http$/i) ? http : https)
|
2023-09-21 14:00:04 +00:00
|
|
|
.request(
|
2024-07-13 10:53:35 +00:00
|
|
|
requestObject,
|
2023-09-21 14:00:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback Function
|
|
|
|
*
|
|
|
|
* @description https request callback
|
|
|
|
*/
|
|
|
|
(response) => {
|
|
|
|
var str = "";
|
|
|
|
|
|
|
|
response.on("data", function (chunk) {
|
|
|
|
str += chunk;
|
|
|
|
});
|
|
|
|
|
|
|
|
response.on("end", function () {
|
2024-07-13 11:01:15 +00:00
|
|
|
try {
|
|
|
|
resolve(JSON.parse(str));
|
2024-07-24 07:18:04 +00:00
|
|
|
} catch (/** @type {any} */ error) {
|
2024-07-13 11:01:15 +00:00
|
|
|
reject({
|
|
|
|
error: error.message,
|
|
|
|
result: str,
|
|
|
|
});
|
|
|
|
}
|
2023-09-21 14:00:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
response.on("error", (err) => {
|
2024-07-13 10:50:15 +00:00
|
|
|
console.log("DSQL get Error,", err.message);
|
2023-09-21 14:00:04 +00:00
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
/** ********************************************** */
|
|
|
|
/** ********************************************** */
|
|
|
|
/** ********************************************** */
|
|
|
|
|
|
|
|
return httpResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** ********************************************** */
|
|
|
|
/** ********************************************** */
|
|
|
|
/** ********************************************** */
|
|
|
|
|
|
|
|
module.exports = get;
|