datasquirel/utils/get-schema.js

107 lines
4.0 KiB
JavaScript
Raw Normal View History

2023-09-21 14:00:04 +00:00
// @ts-check
/**
* ==============================================================================
* Imports
* ==============================================================================
*/
2023-09-21 16:51:08 +00:00
const http = require("http");
2023-09-21 14:00:04 +00:00
const https = require("https");
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/**
* @typedef {Object} GetSchemaReturn
* @property {boolean} success - Did the function run successfully?
2024-10-22 17:32:02 +00:00
* @property {import("../package-shared/types").DSQL_DatabaseSchemaType | import("../package-shared/types").DSQL_TableSchemaType | import("../package-shared/types").DSQL_FieldSchemaType | null} payload - Response payload
2023-09-21 14:00:04 +00:00
*/
/**
2024-10-22 17:17:59 +00:00
* # Get Schema for Database, table, or field *
* @param {import("../package-shared/types").GetSchemaAPIParam} params
2023-09-21 14:00:04 +00:00
*
* @returns { Promise<GetSchemaReturn> } - Return Object
*/
2024-10-22 17:17:59 +00:00
async function getSchema({ key, database, field, table }) {
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;
2024-11-06 06:37:42 +00:00
const remoteHost = process.env.DSQL_API_REMOTE_HOST?.match(/.*\..*/)
? process.env.DSQL_API_REMOTE_HOST
: undefined;
const remoteHostPort = process.env.DSQL_API_REMOTE_HOST_PORT?.match(/./)
? process.env.DSQL_API_REMOTE_HOST_PORT
: undefined;
2023-09-21 16:51:08 +00:00
2023-09-21 14:00:04 +00:00
/**
* Make https request
*
* @description make a request to datasquirel.com
*/
const httpResponse = await new Promise((resolve, reject) => {
2024-10-22 17:17:59 +00:00
/** @type {import("../package-shared/types").GetSchemaRequestQuery} */
const queryObject = { database, field, table };
let query = Object.keys(queryObject)
// @ts-ignore
.filter((k) => queryObject[k])
// @ts-ignore
.map((k) => `${k}=${queryObject[k]}`)
.join("&");
2023-09-21 16:51:08 +00:00
(scheme?.match(/^http$/i) ? http : https)
2023-09-21 14:00:04 +00:00
.request(
{
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: key,
},
2024-11-06 06:37:42 +00:00
port: remoteHostPort || localHostPort || 443,
hostname: remoteHost || localHost || "datasquirel.com",
2023-09-21 16:51:08 +00:00
path:
"/api/query/get-schema" +
2024-10-22 17:17:59 +00:00
(query?.match(/./) ? `?${query}` : ""),
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 () {
resolve(JSON.parse(str));
});
response.on("error", (err) => {
reject(err);
});
}
)
.end();
});
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
return httpResponse;
}
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
module.exports = getSchema;