This commit is contained in:
Benjamin Toby
2025-02-13 08:18:46 +01:00
parent b578843c6d
commit 8b3553fcd5
48 changed files with 1642 additions and 203 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 -5
View File
@@ -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!");
}
}
+7 -2
View File
@@ -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");
@@ -36,10 +36,3 @@ export default function setUserSchemaData({
return false;
}
}
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
@@ -0,0 +1,72 @@
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 "../../functions/backend/db/addDbEntry";
type Param = {
userId?: number | string | null;
dbSchema: DSQL_DatabaseSchemaType;
};
/**
* # Create database from Schema Function
* @requires DSQL_DB_CONN - Gobal Variable for Datasquirel Database
*/
export default async function checkDbRecordCreateDbSchema({
userId,
dbSchema,
}: Param): Promise<DSQL_DATASQUIREL_USER_DATABASES | undefined> {
try {
const {
dbFullName,
dbName,
dbSlug,
dbDescription,
dbImage,
childDatabase,
childDatabaseDbFullName,
} = dbSchema;
let recordedDbEntryArray = userId
? await varDatabaseDbHandler({
queryString: `SELECT * FROM datasquirel.user_databases WHERE db_full_name = ?`,
queryValuesArray: [dbFullName],
})
: undefined;
let recordedDbEntry: DSQL_DATASQUIREL_USER_DATABASES | undefined =
recordedDbEntryArray?.[0];
if (!recordedDbEntry?.id && userId) {
const newDbEntryObj: DSQL_DATASQUIREL_USER_DATABASES = {
user_id: numberfy(userId),
db_name: dbName,
db_slug: dbSlug,
db_full_name: dbFullName,
db_description: dbDescription,
db_image: dbImage,
active_clone: childDatabase ? 1 : undefined,
active_clone_parent_db: childDatabaseDbFullName,
};
const newDbEntry = (await addDbEntry({
data: newDbEntryObj,
tableName: "user_databases",
forceLocal: true,
})) as PostInsertReturn;
if (newDbEntry.insertId) {
recordedDbEntryArray = await varDatabaseDbHandler({
queryString: `SELECT * FROM datasquirel.user_databases WHERE db_full_name = ?`,
queryValuesArray: [dbFullName],
});
recordedDbEntry = recordedDbEntryArray?.[0];
}
}
return recordedDbEntry;
} catch (error) {
return undefined;
}
}
@@ -0,0 +1,115 @@
import varDatabaseDbHandler from "../utils/varDatabaseDbHandler";
import dbHandler from "../utils/dbHandler";
import {
DSQL_DatabaseSchemaType,
DSQL_TableSchemaType,
PostInsertReturn,
} from "../../types";
import {
DSQL_DATASQUIREL_USER_DATABASE_TABLES,
DSQL_DATASQUIREL_USER_DATABASES,
DsqlTables,
} from "../../types/dsql";
import sqlGenerator from "../../functions/dsql/sql/sql-generator";
import numberfy from "../../utils/numberfy";
import addDbEntry from "../../functions/backend/db/addDbEntry";
type Param = {
userId?: number | string | null;
tableSchema?: DSQL_TableSchemaType;
dbSchema: DSQL_DatabaseSchemaType[];
dbRecord?: DSQL_DATASQUIREL_USER_DATABASES;
dbFullName: string;
};
/**
* # Create database from Schema Function
* @requires DSQL_DB_CONN - Gobal Variable for Datasquirel Database
*/
export default async function checkTableRecordCreateDbSchema({
userId,
tableSchema,
dbSchema,
dbRecord,
dbFullName,
}: Param): Promise<DSQL_DATASQUIREL_USER_DATABASE_TABLES | undefined> {
if (!tableSchema) return undefined;
try {
const queryObj = sqlGenerator<DSQL_DATASQUIREL_USER_DATABASE_TABLES>({
tableName: "user_database_tables" as (typeof DsqlTables)[number],
genObject: {
query: {
db_id: {
value: String(dbRecord?.id),
},
table_slug: {
value: tableSchema.tableName,
},
user_id: {
value: String(userId),
},
},
},
dbFullName: "datasquirel",
});
let recordedTableEntryArray = userId
? await varDatabaseDbHandler({
queryString: queryObj?.string || "",
queryValuesArray: queryObj?.values,
})
: undefined;
let recordedTableEntry:
| DSQL_DATASQUIREL_USER_DATABASE_TABLES
| undefined = recordedTableEntryArray?.[0];
if (!recordedTableEntry?.id && userId) {
const newTableInsertObject: DSQL_DATASQUIREL_USER_DATABASE_TABLES =
{
user_id: numberfy(userId),
db_id: dbRecord?.id,
db_slug: dbRecord?.db_slug,
table_name: tableSchema.tableFullName,
table_slug: tableSchema.tableName,
};
if (tableSchema?.childTable && tableSchema.childTableName) {
const parentDb = dbSchema.find(
(db) => db.dbFullName == tableSchema.childTableDbFullName
);
const parentDbTable = parentDb?.tables.find(
(tbl) => tbl.tableName == tableSchema.childTableName
);
if (parentDb && parentDbTable) {
newTableInsertObject["child_table"] = 1;
newTableInsertObject["child_table_parent_database"] =
parentDb.dbFullName;
newTableInsertObject["child_table_parent_table"] =
parentDbTable.tableName;
}
}
const newTableRecordEntry = (await addDbEntry({
data: newTableInsertObject,
tableName: "user_database_tables",
dbContext: "Master",
forceLocal: true,
})) as PostInsertReturn;
if (newTableRecordEntry.insertId) {
recordedTableEntryArray = await varDatabaseDbHandler({
queryString: queryObj?.string || "",
queryValuesArray: queryObj?.values,
});
recordedTableEntry = recordedTableEntryArray?.[0];
}
}
return recordedTableEntry;
} catch (error) {
return undefined;
}
}
@@ -0,0 +1,59 @@
import varDatabaseDbHandler from "../utils/varDatabaseDbHandler";
import { DSQL_IndexSchemaType } from "../../types";
type Param = {
tableName: string;
dbFullName: string;
indexes: DSQL_IndexSchemaType[];
};
/**
* Handle DATASQUIREL Table Indexes
* ===================================================
* @description Iterate through each datasquirel schema
* table index(if available), and perform operations
*/
export default async function handleIndexescreateDbFromSchema({
dbFullName,
tableName,
indexes,
}: Param) {
for (let g = 0; g < indexes.length; g++) {
const { indexType, indexName, indexTableFields, alias } = indexes[g];
if (!alias?.match(/./)) continue;
/**
* @description Check for existing Index in MYSQL db
*/
try {
/**
* @type {import("../../types").DSQL_MYSQL_SHOW_INDEXES_Type[]}
* @description All indexes from MYSQL db
*/ // @ts-ignore
const allExistingIndexes: import("../../types").DSQL_MYSQL_SHOW_INDEXES_Type[] =
await varDatabaseDbHandler({
queryString: `SHOW INDEXES FROM \`${dbFullName}\`.\`${tableName}\``,
});
const existingKeyInDb = allExistingIndexes.filter(
(indexObject) => indexObject.Key_name === alias
);
if (!existingKeyInDb[0])
throw new Error("This Index Does not Exist");
} catch (error) {
/**
* @description Create new index if determined that it
* doesn't exist in MYSQL db
*/
await varDatabaseDbHandler({
queryString: `CREATE${
indexType?.match(/fullText/i) ? " FULLTEXT" : ""
} INDEX \`${alias}\` ON \`${dbFullName}\`.\`${tableName}\`(${indexTableFields
?.map((nm) => nm.value)
.map((nm) => `\`${nm}\``)
.join(",")}) COMMENT 'schema_index'`,
});
}
}
}
@@ -1,18 +1,21 @@
import fs from "fs";
import noDatabaseDbHandler from "./utils/noDatabaseDbHandler";
import varDatabaseDbHandler from "./utils/varDatabaseDbHandler";
import createTable from "./utils/createTable";
import updateTable from "./utils/updateTable";
import dbHandler from "./utils/dbHandler";
import EJSON from "../utils/ejson";
import { DSQL_DatabaseSchemaType } from "../types";
import grabDirNames from "../utils/backend/names/grab-dir-names";
import noDatabaseDbHandler from "../utils/noDatabaseDbHandler";
import varDatabaseDbHandler from "../utils/varDatabaseDbHandler";
import createTable from "../utils/createTable";
import updateTable from "../utils/updateTable";
import dbHandler from "../utils/dbHandler";
import EJSON from "../../utils/ejson";
import { DSQL_DatabaseSchemaType } from "../../types";
import grabDirNames from "../../utils/backend/names/grab-dir-names";
import checkDbRecordCreateDbSchema from "./check-db-record";
import checkTableRecordCreateDbSchema from "./check-table-record";
import handleIndexescreateDbFromSchema from "./handle-indexes";
type Param = {
userId?: number | string | null;
targetDatabase?: string;
dbSchemaData?: import("../types").DSQL_DatabaseSchemaType[];
dbSchemaData?: import("../../types").DSQL_DatabaseSchemaType[];
};
/**
@@ -41,19 +44,15 @@ export default async function createDbFromSchema({
return;
}
// await createDatabasesFromSchema(dbSchema);
for (let i = 0; i < dbSchema.length; i++) {
const database: DSQL_DatabaseSchemaType = dbSchema[i];
const { dbFullName, tables, dbName, dbSlug, childrenDatabases } =
database;
const { dbFullName, tables, dbSlug, childrenDatabases } = database;
if (targetDatabase && dbFullName != targetDatabase) {
continue;
}
/** @type {any} */
const dbCheck: any = await noDatabaseDbHandler(
`SELECT SCHEMA_NAME AS dbFullName FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '${dbFullName}'`
);
@@ -64,16 +63,14 @@ export default async function createDbFromSchema({
);
}
/**
* Select all tables
* @type {any}
* @description Select All tables in target database
*/
const allTables: any = await noDatabaseDbHandler(
`SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='${dbFullName}'`
);
// let tableDropped;
let recordedDbEntry = await checkDbRecordCreateDbSchema({
dbSchema: database,
userId,
});
for (let tb = 0; tb < allTables.length; tb++) {
const { TABLE_NAME } = allTables[tb];
@@ -115,15 +112,6 @@ export default async function createDbFromSchema({
}
}
const recordedDbEntryArray = userId
? await varDatabaseDbHandler({
queryString: `SELECT * FROM datasquirel.user_databases WHERE db_full_name = ?`,
queryValuesArray: [dbFullName],
})
: undefined;
const recordedDbEntry = recordedDbEntryArray?.[0];
/**
* @description Iterate through each table and perform table actions
*/
@@ -189,11 +177,7 @@ export default async function createDbFromSchema({
});
}
}
////////////////////////////////////////
} else {
////////////////////////////////////////
/**
* @description Create new Table if table doesnt exist
*/
@@ -205,66 +189,28 @@ export default async function createDbFromSchema({
recordedDbEntry,
});
if (indexes && indexes[0]) {
/**
* Handle DATASQUIREL Table Indexes
* ===================================================
* @description Iterate through each datasquirel schema
* table index(if available), and perform operations
*/
if (indexes && indexes[0]) {
for (let g = 0; g < indexes.length; g++) {
const {
indexType,
indexName,
indexTableFields,
alias,
} = indexes[g];
if (!alias?.match(/./)) continue;
/**
* @description Check for existing Index in MYSQL db
*/
try {
/**
* @type {import("../types").DSQL_MYSQL_SHOW_INDEXES_Type[]}
* @description All indexes from MYSQL db
*/ // @ts-ignore
const allExistingIndexes: import("../types").DSQL_MYSQL_SHOW_INDEXES_Type[] =
await varDatabaseDbHandler({
queryString: `SHOW INDEXES FROM \`${dbFullName}\`.\`${tableName}\``,
});
const existingKeyInDb =
allExistingIndexes.filter(
(indexObject) =>
indexObject.Key_name === alias
);
if (!existingKeyInDb[0])
throw new Error(
"This Index Does not Exist"
);
} catch (error) {
/**
* @description Create new index if determined that it
* doesn't exist in MYSQL db
*/
await varDatabaseDbHandler({
queryString: `CREATE${
indexType?.match(/fullText/i)
? " FULLTEXT"
: ""
} INDEX \`${alias}\` ON \`${dbFullName}\`.\`${tableName}\`(${indexTableFields
?.map((nm) => nm.value)
.map((nm) => `\`${nm}\``)
.join(",")}) COMMENT 'schema_index'`,
});
}
}
}
/**
* Handle DATASQUIREL Table Indexes
* ===================================================
* @description Iterate through each datasquirel schema
* table index(if available), and perform operations
*/
if (indexes?.[0]) {
handleIndexescreateDbFromSchema({
dbFullName,
indexes,
tableName,
});
}
}
const tableRecord = await checkTableRecordCreateDbSchema({
dbFullName,
dbSchema,
tableSchema: table,
dbRecord: recordedDbEntry,
userId,
});
}
/**
+1 -1
View File
@@ -25,7 +25,7 @@ export default async function dbHandler({
} else {
results = await CONNECTION.query(query);
}
} catch (/** @type {any} */ error: any) {
} catch (error: any) {
if (process.env.FIRST_RUN) {
return null;
}
+323
View File
@@ -0,0 +1,323 @@
export const DsqlTables = [
"users",
"mariadb_users",
"api_keys",
"invitations",
"user_users",
"delegated_user_tables",
"user_databases",
"user_database_tables",
"user_media",
"delegated_users",
"unsubscribes",
"notifications",
"docs_pages",
"docs_page_extra_links",
"deleted_api_keys",
"servers",
] as const
export type DSQL_DATASQUIREL_USERS = {
id?: number;
uuid?: string;
first_name?: string;
last_name?: string;
uid?: string;
email?: string;
user_type?: string;
user_priviledge?: number;
username?: string;
password?: string;
image?: string;
image_thumbnail?: string;
social_login?: number;
social_platform?: string;
social_id?: string;
mariadb_user?: string;
mariadb_host?: string;
mariadb_pass?: string;
disk_usage_in_mb?: number;
verification_status?: number;
date_created?: string;
date_created_code?: number;
date_created_timestamp?: string;
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
export type DSQL_DATASQUIREL_MARIADB_USERS = {
id?: number;
uuid?: string;
user_id?: number;
username?: string;
host?: string;
password?: string;
primary?: number;
grants?: string;
date_created?: string;
date_created_code?: number;
date_created_timestamp?: string;
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
export type DSQL_DATASQUIREL_API_KEYS = {
id?: number;
uuid?: string;
user_id?: number;
name?: string;
slug?: string;
key?: string;
scope?: string;
csrf?: string;
date_created?: string;
date_created_code?: number;
date_created_timestamp?: string;
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
export type DSQL_DATASQUIREL_INVITATIONS = {
id?: number;
uuid?: string;
inviting_user_id?: number;
invited_user_email?: string;
invitation_status?: string;
database_access?: string;
priviledge?: string;
db_tables_data?: string;
date_created?: string;
date_created_code?: number;
date_created_timestamp?: string;
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
export type DSQL_DATASQUIREL_USER_USERS = {
id?: number;
uuid?: string;
user_id?: number;
invited_user_id?: number;
database?: string;
database_access?: string;
first_name?: string;
last_name?: string;
email?: string;
username?: string;
password?: string;
phone?: string;
user_type?: string;
user_priviledge?: string;
image?: string;
image_thumbnail?: string;
city?: string;
state?: string;
country?: string;
zip_code?: string;
address?: string;
social_login?: number;
social_platform?: string;
social_id?: string;
verification_status?: number;
more_user_data?: string;
date_created?: string;
date_created_code?: number;
date_created_timestamp?: string;
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
export type DSQL_DATASQUIREL_DELEGATED_USER_TABLES = {
id?: number;
uuid?: string;
delegated_user_id?: number;
root_user_id?: number;
database?: string;
table?: string;
priviledge?: string;
date_created?: string;
date_created_code?: number;
date_created_timestamp?: string;
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
export type DSQL_DATASQUIREL_USER_DATABASES = {
id?: number;
uuid?: string;
user_id?: number;
db_name?: string;
db_slug?: string;
db_full_name?: string;
db_image?: string;
db_description?: string;
remote_connected?: number;
remote_connection_type?: string;
remote_db_full_name?: string;
remote_connection_host?: string;
remote_connection_key?: string;
active_clone?: number;
active_clone_parent_db?: string;
active_data?: number;
date_created?: string;
date_created_code?: number;
date_created_timestamp?: string;
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
export type DSQL_DATASQUIREL_USER_DATABASE_TABLES = {
id?: number;
uuid?: string;
user_id?: number;
db_id?: number;
db_slug?: string;
table_name?: string;
table_slug?: string;
table_description?: string;
child_table?: number;
child_table_parent_database?: string;
child_table_parent_table?: string;
active_data?: number;
date_created?: string;
date_created_code?: number;
date_created_timestamp?: string;
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
export type DSQL_DATASQUIREL_USER_MEDIA = {
id?: number;
uuid?: string;
user_id?: number;
media_name?: string;
folder?: string;
media_url?: string;
media_thumbnail_url?: string;
media_path?: string;
media_thumbnail_path?: string;
media_type?: string;
width?: number;
height?: number;
size?: number;
private?: number;
date_created?: string;
date_created_code?: number;
date_created_timestamp?: string;
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
export type DSQL_DATASQUIREL_DELEGATED_USERS = {
id?: number;
uuid?: string;
user_id?: number;
delegated_user_id?: number;
permissions?: string;
permission_level_code?: number;
date_created?: string;
date_created_code?: number;
date_created_timestamp?: string;
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
export type DSQL_DATASQUIREL_UNSUBSCRIBES = {
id?: number;
uuid?: string;
user_id?: number;
email?: string;
type?: string;
date_created?: string;
date_created_code?: number;
date_created_timestamp?: string;
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
export type DSQL_DATASQUIREL_NOTIFICATIONS = {
id?: number;
uuid?: string;
user_id?: number;
title?: string;
message?: string;
date_created?: string;
date_created_code?: number;
date_created_timestamp?: string;
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
export type DSQL_DATASQUIREL_DOCS_PAGES = {
id?: number;
uuid?: string;
title?: string;
slug?: string;
description?: string;
content?: string;
text_content?: string;
level?: number;
page_order?: number;
parent_id?: number;
date_created?: string;
date_created_code?: number;
date_created_timestamp?: string;
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
export type DSQL_DATASQUIREL_DOCS_PAGE_EXTRA_LINKS = {
id?: number;
uuid?: string;
docs_page_id?: number;
title?: string;
description?: string;
url?: string;
date_created?: string;
date_created_code?: number;
date_created_timestamp?: string;
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
export type DSQL_DATASQUIREL_DELETED_API_KEYS = {
id?: number;
uuid?: string;
user_id?: number;
key?: string;
date_created?: string;
date_created_code?: number;
date_created_timestamp?: string;
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
export type DSQL_DATASQUIREL_SERVERS = {
id?: number;
uuid?: string;
server_id?: number;
ip?: string;
ssh_user?: string;
ssh_port?: number;
date_created?: string;
date_created_code?: number;
date_created_timestamp?: string;
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
@@ -0,0 +1,44 @@
import { execSync, ExecSyncOptions } from "child_process";
import os from "os";
import connDbHandler from "../db/conn-db-handler";
export type ExportMariaDBDatabaseParam = {
dbFullName: string;
targetFilePath: string;
mariadbUser?: string;
mariadbHost?: string;
mariadbPass?: string;
};
export default async function importMariadbDatabase({
dbFullName,
targetFilePath,
mariadbHost,
mariadbPass,
mariadbUser,
}: ExportMariaDBDatabaseParam) {
const mysqlPath = os.platform().match(/win/i)
? "'" +
"C:\\Program Files\\MySQL\\MySQL Server 8.0\\bin\\mysql.exe" +
"'"
: "mysql";
const finalMariadbUser = mariadbUser || process.env.DSQL_DB_USERNAME;
const finalMariadbHost = mariadbHost || process.env.DSQL_DB_HOST;
const finalMariadbPass = mariadbPass || process.env.DSQL_DB_PASSWORD;
await connDbHandler(
global.DSQL_DB_CONN,
`CREATE DATABASE IF NOT EXISTS ${dbFullName}`
);
const cmd = `${mysqlPath} -u ${finalMariadbUser} -h ${finalMariadbHost} -p${finalMariadbPass} ${dbFullName} < ${targetFilePath}`;
let execSyncOptions: ExecSyncOptions = {
encoding: "utf-8",
};
const importDb = execSync(cmd, execSyncOptions);
return importDb;
}
@@ -20,7 +20,7 @@ export default function grabDirNames(param?: Param) {
const pakageSharedDir = path.join(appDir, `package-shared`);
const mainDbTypeDefFile = path.join(appDir, `types/dsql.ts`);
const mainDbTypeDefFile = path.join(pakageSharedDir, `types/dsql.ts`);
const mainShemaJSONFilePath = path.join(schemasDir, `main.json`);
const defaultTableFieldsJSONFilePath = path.join(
pakageSharedDir,
@@ -57,6 +57,11 @@ export default function grabDirNames(param?: Param) {
? path.join(userPrivateSQLExportsDir, userPrivateDbExportZipFileName)
: undefined;
const userPrivateDbImportZipFileName = `db-export.zip`;
const userPrivateDbImportZipFilePath = userPrivateSQLExportsDir
? path.join(userPrivateSQLExportsDir, userPrivateDbImportZipFileName)
: undefined;
return {
schemasDir,
userDirPath,
@@ -73,5 +78,7 @@ export default function grabDirNames(param?: Param) {
userPrivateTempJSONSchemaFilePath,
userPrivateDbExportZipFileName,
userPrivateDbExportZipFilePath,
userPrivateDbImportZipFileName,
userPrivateDbImportZipFilePath,
};
}
@@ -0,0 +1,14 @@
type Param = {
str: string;
userId: string | number;
};
export default function replaceDatasquirelDbName({
str,
userId,
}: Param): string {
const dbNamePrefix = process.env.DSQL_USER_DB_PREFIX;
const userNameRegex = new RegExp(`${dbNamePrefix}\\d+_`, "g");
const newPrefix = `${dbNamePrefix}${userId}_`;
return str.replace(userNameRegex, newPrefix);
}
+32
View File
@@ -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;
+60
View File
@@ -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);
}