This commit is contained in:
Benjamin Toby
2023-09-21 17:51:08 +01:00
parent 44b013147e
commit 069db84289
17 changed files with 414 additions and 91 deletions
+10 -3
View File
@@ -3,6 +3,7 @@
* Imports
* ==============================================================================
*/
const http = require("http");
const https = require("https");
/** ****************************************************************************** */
@@ -35,6 +36,10 @@ const https = require("https");
* @returns { Promise<FunctionReturn> } - Image Url
*/
async function uploadImage({ key, url }) {
const scheme = process.env.DSQL_HTTP_SCHEME;
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
try {
/**
* Make https request
@@ -44,7 +49,9 @@ async function uploadImage({ key, url }) {
const httpResponse = await new Promise((resolve, reject) => {
const reqPayload = JSON.stringify({ url: url });
const httpsRequest = https.request(
const httpsRequest = (
scheme?.match(/^http$/i) ? http : https
).request(
{
method: "POST",
headers: {
@@ -52,8 +59,8 @@ async function uploadImage({ key, url }) {
"Content-Length": Buffer.from(reqPayload).length,
Authorization: key,
},
port: 443,
hostname: "datasquirel.com",
port: localHostPort || 443,
hostname: localHost || "datasquirel.com",
path: `/api/query/delete-file`,
},
+11 -4
View File
@@ -5,6 +5,7 @@
* Imports
* ==============================================================================
*/
const http = require("http");
const https = require("https");
/** ****************************************************************************** */
@@ -32,13 +33,17 @@ const https = require("https");
* @returns { Promise<GetSchemaReturn> } - Return Object
*/
async function getSchema({ key, database }) {
const scheme = process.env.DSQL_HTTP_SCHEME;
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
/**
* Make https request
*
* @description make a request to datasquirel.com
*/
const httpResponse = await new Promise((resolve, reject) => {
https
(scheme?.match(/^http$/i) ? http : https)
.request(
{
method: "GET",
@@ -46,9 +51,11 @@ async function getSchema({ key, database }) {
"Content-Type": "application/json",
Authorization: key,
},
port: 443,
hostname: "datasquirel.com",
path: "/api/query/get-schema" + (database ? `?database=${database}` : ""),
port: localHostPort || 443,
hostname: localHost || "datasquirel.com",
path:
"/api/query/get-schema" +
(database ? `?database=${database}` : ""),
},
/**
+30 -7
View File
@@ -5,6 +5,7 @@
* Imports
* ==============================================================================
*/
const http = require("http");
const https = require("https");
const path = require("path");
const fs = require("fs");
@@ -40,19 +41,39 @@ const localGet = require("../engine/query/get");
* @returns { Promise<GetReturn> } - Return Object
*/
async function get({ key, db, query, queryValues, tableName }) {
const scheme = process.env.DSQL_HTTP_SCHEME;
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
/**
* 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;
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(/./)) {
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");
const localDbSchemaPath = path.resolve(
process.cwd(),
"dsql.schema.json"
);
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
} catch (error) {}
@@ -80,10 +101,12 @@ async function get({ key, db, query, queryValues, tableName }) {
.replace(/ /g, "+")}`;
if (queryValues) {
path += `&queryValues=${JSON.stringify(queryValues)}${tableName ? `&tableName=${tableName}` : ""}`;
path += `&queryValues=${JSON.stringify(queryValues)}${
tableName ? `&tableName=${tableName}` : ""
}`;
}
https
(scheme?.match(/^http$/i) ? http : https)
.request(
{
method: "GET",
@@ -91,8 +114,8 @@ async function get({ key, db, query, queryValues, tableName }) {
"Content-Type": "application/json",
Authorization: key,
},
port: 443,
hostname: "datasquirel.com",
port: localHostPort || 443,
hostname: localHost || "datasquirel.com",
path: path,
},
+27 -6
View File
@@ -3,6 +3,7 @@
/**
* Imports
*/
const http = require("http");
const https = require("https");
const path = require("path");
const fs = require("fs");
@@ -52,19 +53,39 @@ const localPost = require("../engine/query/post");
* @returns { Promise<PostReturn> } - Return Object
*/
async function post({ key, query, queryValues, database, tableName }) {
const scheme = process.env.DSQL_HTTP_SCHEME;
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
/**
* 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;
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(/./)) {
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");
const localDbSchemaPath = path.resolve(
process.cwd(),
"dsql.schema.json"
);
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
} catch (error) {}
@@ -108,7 +129,7 @@ async function post({ key, query, queryValues, database, tableName }) {
const reqPayload = reqPayloadString;
const httpsRequest = https.request(
const httpsRequest = (scheme?.match(/^http$/i) ? http : https).request(
{
method: "POST",
headers: {
@@ -116,8 +137,8 @@ async function post({ key, query, queryValues, database, tableName }) {
"Content-Length": Buffer.from(reqPayload).length,
Authorization: key,
},
port: 443,
hostname: "datasquirel.com",
port: localHostPort || 443,
hostname: localHost || "datasquirel.com",
path: `/api/query/post`,
},
+13 -4
View File
@@ -1,8 +1,11 @@
// @ts-check
/**
* ==============================================================================
* Imports
* ==============================================================================
*/
const http = require("http");
const https = require("https");
/** ****************************************************************************** */
@@ -17,7 +20,7 @@ const https = require("https");
* @property {boolean} success - Did the function run successfully?
* @property {{
* urlPath: string,
* }} payload - Payload containing the url for the image and its thumbnail
* } | null} payload - Payload containing the url for the image and its thumbnail
* @property {string} [msg] - An optional message
*/
@@ -40,6 +43,10 @@ const https = require("https");
* @returns { Promise<FunctionReturn> } - Return Object
*/
async function uploadImage({ key, payload }) {
const scheme = process.env.DSQL_HTTP_SCHEME;
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
try {
/**
* Make https request
@@ -49,7 +56,9 @@ async function uploadImage({ key, payload }) {
const httpResponse = await new Promise((resolve, reject) => {
const reqPayload = JSON.stringify(payload);
const httpsRequest = https.request(
const httpsRequest = (
scheme?.match(/^http$/i) ? http : https
).request(
{
method: "POST",
headers: {
@@ -57,8 +66,8 @@ async function uploadImage({ key, payload }) {
"Content-Length": Buffer.from(reqPayload).length,
Authorization: key,
},
port: 443,
hostname: "datasquirel.com",
port: localHostPort || 443,
hostname: localHost || "datasquirel.com",
path: `/api/query/add-file`,
},
+13 -4
View File
@@ -1,8 +1,11 @@
// @ts-check
/**
* ==============================================================================
* Imports
* ==============================================================================
*/
const http = require("http");
const https = require("https");
/** ****************************************************************************** */
@@ -18,7 +21,7 @@ const https = require("https");
* @property {{
* urlPath: string,
* urlThumbnailPath: string
* }} payload - Payload containing the url for the image and its thumbnail
* } | null} payload - Payload containing the url for the image and its thumbnail
* @property {string} [msg] - An optional message
*/
@@ -42,6 +45,10 @@ const https = require("https");
* @returns { Promise<FunctionReturn> } - Return Object
*/
async function uploadImage({ key, payload }) {
const scheme = process.env.DSQL_HTTP_SCHEME;
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
try {
/**
* Make https request
@@ -51,7 +58,9 @@ async function uploadImage({ key, payload }) {
const httpResponse = await new Promise((resolve, reject) => {
const reqPayload = JSON.stringify(payload);
const httpsRequest = https.request(
const httpsRequest = (
scheme?.match(/^http$/i) ? http : https
).request(
{
method: "POST",
headers: {
@@ -59,8 +68,8 @@ async function uploadImage({ key, payload }) {
"Content-Length": Buffer.from(reqPayload).length,
Authorization: key,
},
port: 443,
hostname: "datasquirel.com",
port: localHostPort || 443,
hostname: localHost || "datasquirel.com",
path: `/api/query/add-image`,
},