This commit is contained in:
Benjamin Toby
2025-02-16 17:12:40 +01:00
parent e9761cc971
commit e95f4d1087
628 changed files with 3091 additions and 1073 deletions
+9 -4
View File
@@ -11,6 +11,7 @@ import {
GetReturn,
} from "../types";
import apiGetGrabQueryAndValues from "../utils/grab-query-and-values";
import debugLog from "../utils/logging/debug-log";
type Param<T extends { [k: string]: any } = { [k: string]: any }> = {
key?: string;
@@ -43,6 +44,10 @@ export default async function get<
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
function debugFn(log: any, label?: string) {
debugLog({ log, addTime: true, title: "apiGet", label });
}
/**
* Check for local DB settings
*
@@ -62,7 +67,7 @@ export default async function get<
} catch (error) {}
if (debug) {
console.log("apiGet:Running Locally ...");
debugFn("Running Locally ...");
}
return await apiGet({
@@ -96,13 +101,13 @@ export default async function get<
};
if (debug) {
console.log("apiGet:queryObject", queryObject);
debugFn(queryObject, "queryObject");
}
const queryString = serializeQuery({ ...queryObject });
if (debug) {
console.log("apiGet:queryString", queryString);
debugFn(queryString, "queryString");
}
let path = `/api/query/${
@@ -110,7 +115,7 @@ export default async function get<
}/get${queryString}`;
if (debug) {
console.log("apiGet:path", path);
debugFn(path, "path");
}
const requestObject: https.RequestOptions = {
@@ -11,6 +11,7 @@ import {
DSQL_DatabaseSchemaType,
PackageUserLoginRequestBody,
} from "../../types";
import debugLog from "../../utils/logging/debug-log";
type Param = {
key?: string;
@@ -73,6 +74,10 @@ export default async function loginUser({
const finalEncryptionSalt =
encryptionSalt || process.env.DSQL_ENCRYPTION_SALT;
function debugFn(log: any, label?: string) {
debugLog({ log, addTime: true, title: "loginUser", label });
}
if (!finalEncryptionKey?.match(/.{8,}/)) {
console.log("Encryption key is invalid");
return {
@@ -81,6 +86,7 @@ export default async function loginUser({
msg: "Encryption key is invalid",
};
}
if (!finalEncryptionSalt?.match(/.{8,}/)) {
console.log("Encryption salt is invalid");
return {
@@ -210,7 +216,7 @@ export default async function loginUser({
}
if (debug) {
console.log(`loginUser:httpResponse:`, httpResponse);
debugFn(httpResponse, "httpResponse");
}
if (httpResponse?.success) {
@@ -244,9 +250,9 @@ export default async function loginUser({
const csrfName = cookieNames.csrfCookieName;
if (debug) {
console.log(`loginUser:authKeyName:`, authKeyName);
console.log(`loginUser:csrfName:`, csrfName);
console.log(`loginUser:encryptedPayload:`, encryptedPayload);
debugFn(authKeyName, "authKeyName");
debugFn(csrfName, "csrfName");
debugFn(encryptedPayload, "encryptedPayload");
}
response?.setHeader("Set-Cookie", [
@@ -255,7 +261,7 @@ export default async function loginUser({
]);
if (debug) {
console.log(`loginUser:Response Sent!`);
debugFn("Response Sent!");
}
}
@@ -6,6 +6,7 @@ import { deleteAuthFile } from "../../functions/backend/auth/write-auth-files";
import parseCookies from "../../utils/backend/parseCookies";
import { DATASQUIREL_LoggedInUser } from "../../types";
import grabHostNames from "../../utils/grab-host-names";
import debugLog from "../../utils/logging/debug-log";
type Param = {
encryptedUserString?: string;
@@ -48,8 +49,12 @@ export default function logoutUser({
userId: user_id,
});
function debugFn(log: any, label?: string) {
debugLog({ log, addTime: true, title: "logoutUser", label });
}
if (debug) {
console.log("logoutUser:cookieNames", cookieNames);
debugFn(cookieNames, "cookieNames");
}
const authKeyName = cookieNames.keyCookieName;
@@ -84,7 +89,7 @@ export default function logoutUser({
})();
if (debug) {
console.log("logoutUser:decryptedUserJSON", decryptedUserJSON);
debugFn(decryptedUserJSON, "decryptedUserJSON");
}
if (!decryptedUserJSON) throw new Error("Invalid User");
@@ -0,0 +1,44 @@
import getQueue from "./get-queue";
import {
DSQL_DATASQUIREL_PROCESS_QUEUE,
DsqlTables,
} from "../../../types/dsql";
import dsqlCrud from "../../../utils/data-fetching/crud";
import numberfy from "../../../utils/numberfy";
type Param = {
queue: DSQL_DATASQUIREL_PROCESS_QUEUE;
userId: string | number;
dummy?: boolean;
};
export default async function addQueue({ queue, userId, dummy }: Param) {
const tableName: (typeof DsqlTables)[number] = "process_queue";
const existingQueueRes = dummy
? undefined
: ((await getQueue({
query: {
query: {
user_id: {
value: String(userId),
},
job_type: {
value: String(queue.job_type),
},
},
},
})) as DSQL_DATASQUIREL_PROCESS_QUEUE[] | undefined);
const existingQueue = existingQueueRes?.[0];
if (existingQueue?.id && !dummy) return undefined;
const addQueueRes = await dsqlCrud<DSQL_DATASQUIREL_PROCESS_QUEUE>({
action: "insert",
table: tableName,
data: { ...queue, user_id: numberfy(userId) },
});
return addQueueRes;
}
@@ -0,0 +1,29 @@
import dsqlCrud from "../../../utils/data-fetching/crud";
import getQueue from "./get-queue";
import {
DSQL_DATASQUIREL_PROCESS_QUEUE,
DsqlTables,
} from "../../../types/dsql";
type Param = {
queueId: string | number;
userId: string | number;
};
export default async function deleteQueue({ queueId, userId }: Param) {
const tableName: (typeof DsqlTables)[number] = "process_queue";
const existingQueue = (await getQueue({ userId, queueId })) as
| DSQL_DATASQUIREL_PROCESS_QUEUE
| undefined;
if (!existingQueue?.id) return false;
const deleteQueueRes = await dsqlCrud<DSQL_DATASQUIREL_PROCESS_QUEUE>({
action: "delete",
table: tableName,
targetId: existingQueue.id,
});
return Boolean(deleteQueueRes?.success);
}
@@ -0,0 +1,53 @@
import {
DSQL_DATASQUIREL_PROCESS_QUEUE,
DsqlTables,
} from "../../../types/dsql";
import dsqlCrud from "../../../utils/data-fetching/crud";
import { DsqlCrudQueryObject, ServerQueryQueryObject } from "../../../types";
type Param = {
queueId?: string | number;
userId?: string | number;
query?: DsqlCrudQueryObject<DSQL_DATASQUIREL_PROCESS_QUEUE>;
single?: boolean;
};
export default async function getQueue({
queueId,
userId,
query,
single,
}: Param) {
const tableName: (typeof DsqlTables)[number] = "process_queue";
let queryQuery: ServerQueryQueryObject<DSQL_DATASQUIREL_PROCESS_QUEUE> = {};
if (queueId) {
queryQuery = { ...queryQuery, ...{ id: { value: String(queueId) } } };
}
if (userId) {
queryQuery = {
...queryQuery,
...{ user_id: { value: String(userId) } },
};
}
const getQueue = await dsqlCrud<DSQL_DATASQUIREL_PROCESS_QUEUE>({
action: "get",
table: tableName,
query: {
...query,
query: {
...query?.query,
...queryQuery,
},
},
});
const queuePayload = getQueue?.payload as
| DSQL_DATASQUIREL_PROCESS_QUEUE[]
| undefined;
return queueId || single ? queuePayload?.[0] : queuePayload;
}
@@ -0,0 +1,23 @@
import dsqlCrud from "../../../utils/data-fetching/crud";
import {
DSQL_DATASQUIREL_PROCESS_QUEUE,
DsqlTables,
} from "../../../types/dsql";
type Param = {
queueId: string | number;
queue: DSQL_DATASQUIREL_PROCESS_QUEUE;
};
export default async function updateQueue({ queueId, queue }: Param) {
const tableName: (typeof DsqlTables)[number] = "process_queue";
const updateQueueRes = await dsqlCrud<DSQL_DATASQUIREL_PROCESS_QUEUE>({
action: "update",
table: tableName,
targetId: queueId,
data: queue,
});
return Boolean(updateQueueRes?.success);
}
@@ -2,7 +2,7 @@ import varDatabaseDbHandler from "../utils/varDatabaseDbHandler";
import { DSQL_DatabaseSchemaType, PostInsertReturn } from "../../types";
import { DSQL_DATASQUIREL_USER_DATABASES } from "../../types/dsql";
import numberfy from "../../utils/numberfy";
import addDbEntry from "@/package-shared/functions/backend/db/addDbEntry";
import addDbEntry from "../../functions/backend/db/addDbEntry";
type Param = {
userId?: number | string | null;
@@ -11,8 +11,8 @@ import {
DsqlTables,
} from "../../types/dsql";
import sqlGenerator from "../../functions/dsql/sql/sql-generator";
import numberfy from "@/package-shared/utils/numberfy";
import addDbEntry from "@/package-shared/functions/backend/db/addDbEntry";
import numberfy from "../../utils/numberfy";
import addDbEntry from "../../functions/backend/db/addDbEntry";
type Param = {
userId?: number | string | null;
@@ -26,7 +26,7 @@ export default async function createDbFromSchema({
userId,
targetDatabase,
dbSchemaData,
}: Param) {
}: Param): Promise<boolean> {
const { userSchemaMainJSONFilePath, mainShemaJSONFilePath } = grabDirNames({
userId,
});
@@ -41,7 +41,7 @@ export default async function createDbFromSchema({
if (!dbSchema) {
console.log("Schema Not Found!");
return;
return false;
}
for (let i = 0; i < dbSchema.length; i++) {
@@ -228,4 +228,6 @@ export default async function createDbFromSchema({
}
}
}
return true;
}
+21
View File
@@ -15,6 +15,7 @@ export const DsqlTables = [
"docs_page_extra_links",
"deleted_api_keys",
"servers",
"process_queue",
] as const
export type DSQL_DATASQUIREL_USERS = {
@@ -320,4 +321,24 @@ export type DSQL_DATASQUIREL_SERVERS = {
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
export type DSQL_DATASQUIREL_PROCESS_QUEUE = {
id?: number;
uuid?: string;
user_id?: number;
title?: string;
job_type?: string;
data?: string;
running?: number;
server_id?: number;
error?: number;
error_message?: string;
success?: number;
date_created?: string;
date_created_code?: number;
date_created_timestamp?: string;
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
@@ -1,6 +1,6 @@
import datasquirel from "@moduletrace/datasquirel";
import { execSync, ExecSyncOptions } from "child_process";
import os from "os";
import connDbHandler from "../db/conn-db-handler";
export type ExportMariaDBDatabaseParam = {
dbFullName: string;
@@ -27,7 +27,7 @@ export default async function importMariadbDatabase({
const finalMariadbHost = mariadbHost || process.env.DSQL_DB_HOST;
const finalMariadbPass = mariadbPass || process.env.DSQL_DB_PASSWORD;
await datasquirel.utils.connDbHandler(
await connDbHandler(
global.DSQL_DB_CONN,
`CREATE DATABASE IF NOT EXISTS ${dbFullName}`
);
@@ -62,6 +62,11 @@ export default function grabDirNames(param?: Param) {
? path.join(userPrivateSQLExportsDir, userPrivateDbImportZipFileName)
: undefined;
const dbNginxLoadBalancerConfigFile = path.join(
appDir,
"docker/mariadb/load-balancer/config/template/nginx.conf"
);
return {
schemasDir,
userDirPath,
@@ -80,5 +85,6 @@ export default function grabDirNames(param?: Param) {
userPrivateDbExportZipFilePath,
userPrivateDbImportZipFileName,
userPrivateDbImportZipFilePath,
dbNginxLoadBalancerConfigFile,
};
}
@@ -0,0 +1,32 @@
const consoleColors = {
Reset: "\x1b[0m",
Bright: "\x1b[1m",
Dim: "\x1b[2m",
Underscore: "\x1b[4m",
Blink: "\x1b[5m",
Reverse: "\x1b[7m",
Hidden: "\x1b[8m",
FgBlack: "\x1b[30m",
FgRed: "\x1b[31m",
FgGreen: "\x1b[32m",
FgYellow: "\x1b[33m",
FgBlue: "\x1b[34m",
FgMagenta: "\x1b[35m",
FgCyan: "\x1b[36m",
FgWhite: "\x1b[37m",
FgGray: "\x1b[90m",
BgBlack: "\x1b[40m",
BgRed: "\x1b[41m",
BgGreen: "\x1b[42m",
BgYellow: "\x1b[43m",
BgBlue: "\x1b[44m",
BgMagenta: "\x1b[45m",
BgCyan: "\x1b[46m",
BgWhite: "\x1b[47m",
BgGray: "\x1b[100m",
};
export default consoleColors;
export const ccol = consoleColors;
@@ -0,0 +1,60 @@
import { ccol } from "../console-colors";
const LogTypes = ["error", "warning"] as const;
type Param = {
/**
* data to be logged.
*/
log: any;
/**
* Log Title. Could be name of function or name of variable
*/
title?: string;
/**
* Label for the log
*/
label?: string;
/**
* Log type. `error` or `warning` or default
*/
type?: (typeof LogTypes)[number];
/**
* Whether to add a time stamp
*/
addTime?: boolean;
};
export default function debugLog({ log, label, title, type, addTime }: Param) {
const logType = (() => {
switch (type) {
case "error":
return ccol.FgRed;
case "warning":
return ccol.FgYellow;
default:
return ccol.FgGreen;
}
})();
let logTxt = `${logType}DEBUG${ccol.Reset}:::`;
const date = new Date();
const time = date.toLocaleTimeString("en-US", {
hour: "numeric",
minute: "numeric",
second: "numeric",
hour12: true,
});
const logTime = `${date.toLocaleDateString()}][${time}`;
if (addTime) logTxt = `${ccol.BgWhite}[${logTime}]${ccol.Reset} ` + logTxt;
if (title) logTxt += `${ccol.FgBlue}${title}${ccol.Reset}::`;
if (label)
logTxt += `${ccol.FgWhite}${ccol.Bright}${label}${ccol.Reset} =>`;
console.log(logTxt, log);
}