datasquirel/utils/get.js

135 lines
4.5 KiB
JavaScript
Raw Normal View History

2023-08-12 13:36:18 +00:00
// @ts-check
2023-05-03 06:16:29 +00:00
/**
* ==============================================================================
* Imports
* ==============================================================================
*/
const https = require("https");
2023-08-12 13:36:18 +00:00
const path = require("path");
const fs = require("fs");
2023-08-12 13:47:43 +00:00
const localGet = require("../engine/query/get");
2023-05-03 06:16:29 +00:00
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
2023-05-18 08:22:05 +00:00
/**
* @typedef {Object} GetReturn
* @property {boolean} success - Did the function run successfully?
2023-08-12 13:36:18 +00:00
* @property {(Object[]|string|null|object)} [payload] - GET request results
* @property {string} [msg] - Message
* @property {string} [error] - Error Message
2023-05-18 08:22:05 +00:00
*/
2023-05-03 06:16:29 +00:00
/**
2023-07-24 13:37:08 +00:00
* Make a get request to Datasquirel API
2023-05-03 06:16:29 +00:00
* ==============================================================================
2023-05-18 08:22:05 +00:00
* @async
*
* @param {Object} params - Single object passed
* @param {string} params.key - API Key
* @param {string} params.db - Database Name
2023-07-26 04:04:02 +00:00
* @param {string} params.query - SQL Query
* @param {string[]} [params.queryValues] - An array of query values if using "?" placeholders
2023-08-08 14:55:27 +00:00
* @param {string} [params.tableName] - Name of the table to query
2023-05-18 08:22:05 +00:00
*
2023-05-22 07:51:33 +00:00
* @returns { Promise<GetReturn> } - Return Object
2023-05-03 06:16:29 +00:00
*/
2023-08-08 14:55:27 +00:00
async function get({ key, db, query, queryValues, tableName }) {
2023-08-12 13:36:18 +00:00
/**
* Check for local DB settings
*
* @description Look for local db settings in `.env` file and by pass the http request if available
*/
const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME, DSQL_KEY, DSQL_REF_DB_NAME, DSQL_FULL_SYNC } = process.env;
if (DSQL_HOST?.match(/./) && DSQL_USER?.match(/./) && DSQL_PASS?.match(/./) && DSQL_DB_NAME?.match(/./)) {
/** @type {import("../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */
let dbSchema;
try {
const localDbSchemaPath = path.resolve(process.cwd(), "dsql.schema.json");
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
} catch (error) {}
console.log("Reading from local database ...");
return await localGet({
dbSchema: dbSchema,
options: {
query: query,
queryValues: queryValues,
tableName: tableName,
},
});
}
2023-05-03 06:16:29 +00:00
/**
* Make https request
*
* @description make a request to datasquirel.com
*/
const httpResponse = await new Promise((resolve, reject) => {
2023-07-26 04:04:02 +00:00
let path = `/api/query/get?db=${db}&query=${query
.replace(/\n|\r|\n\r/g, "")
.replace(/ {2,}/g, " ")
.replace(/ /g, "+")}`;
if (queryValues) {
2023-08-08 15:20:28 +00:00
path += `&queryValues=${JSON.stringify(queryValues)}${tableName ? `&tableName=${tableName}` : ""}`;
2023-07-26 04:04:02 +00:00
}
2023-05-03 06:16:29 +00:00
https
.request(
{
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: key,
},
port: 443,
hostname: "datasquirel.com",
2023-07-26 04:04:02 +00:00
path: path,
2023-05-03 06:16:29 +00:00
},
/**
* Callback Function
*
* @description https request callback
*/
(response) => {
var str = "";
response.on("data", function (chunk) {
str += chunk;
});
response.on("end", function () {
resolve(JSON.parse(str));
});
response.on("error", (err) => {
reject(err);
});
}
)
.end();
});
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
return httpResponse;
2023-07-07 19:13:13 +00:00
}
2023-05-03 06:16:29 +00:00
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
2023-07-07 19:13:13 +00:00
module.exports = get;