Updates
This commit is contained in:
@@ -0,0 +1,163 @@
|
||||
import varDatabaseDbHandler from "../../functions/backend/varDatabaseDbHandler";
|
||||
import { default as grabUserSchemaData } from "../../functions/backend/grabUserSchemaData";
|
||||
import { default as setUserSchemaData } from "../../functions/backend/setUserSchemaData";
|
||||
import addDbEntry from "../../functions/backend/db/addDbEntry";
|
||||
import slugToCamelTitle from "../../shell/utils/slugToCamelTitle";
|
||||
import { DSQL_DATASQUIREL_USER_DATABASES } from "@/package-shared/types/dsql";
|
||||
import {
|
||||
DSQL_FieldSchemaType,
|
||||
DSQL_MYSQL_SHOW_COLUMNS_Type,
|
||||
DSQL_TableSchemaType,
|
||||
} from "@/package-shared/types";
|
||||
|
||||
type Params = {
|
||||
userId: number | string;
|
||||
database: DSQL_DATASQUIREL_USER_DATABASES;
|
||||
};
|
||||
|
||||
export default async function createDbSchemaFromDb({
|
||||
userId,
|
||||
database,
|
||||
}: Params) {
|
||||
try {
|
||||
if (!userId) {
|
||||
console.log("No user Id provided");
|
||||
return;
|
||||
}
|
||||
|
||||
const userSchemaData = grabUserSchemaData({ userId });
|
||||
if (!userSchemaData) throw new Error("User schema data not found!");
|
||||
|
||||
const targetDb: { tables: object[] } = userSchemaData.filter(
|
||||
(dbObject) => dbObject.dbFullName === database.db_full_name
|
||||
)[0];
|
||||
|
||||
const existingTables = await varDatabaseDbHandler({
|
||||
database: database.db_full_name,
|
||||
queryString: `SHOW TABLES FROM ${database.db_full_name}`,
|
||||
});
|
||||
|
||||
if (!existingTables) throw new Error("No Existing Tables");
|
||||
|
||||
for (let i = 0; i < existingTables.length; i++) {
|
||||
const table = existingTables[i];
|
||||
const tableName = Object.values(table)[0] as string;
|
||||
|
||||
const tableInsert = await addDbEntry({
|
||||
dbFullName: "datasquirel",
|
||||
tableName: "user_database_tables",
|
||||
data: {
|
||||
user_id: userId,
|
||||
db_id: database.id,
|
||||
db_slug: database.db_slug,
|
||||
table_name: slugToCamelTitle(tableName),
|
||||
table_slug: tableName,
|
||||
},
|
||||
});
|
||||
|
||||
const tableObject: DSQL_TableSchemaType = {
|
||||
tableName: tableName,
|
||||
tableFullName: slugToCamelTitle(tableName) || "",
|
||||
fields: [],
|
||||
indexes: [],
|
||||
};
|
||||
|
||||
const tableColumns: DSQL_MYSQL_SHOW_COLUMNS_Type[] =
|
||||
await varDatabaseDbHandler({
|
||||
database: database.db_full_name,
|
||||
queryString: `SHOW COLUMNS FROM ${database.db_full_name}.${tableName}`,
|
||||
});
|
||||
|
||||
if (tableColumns) {
|
||||
for (let k = 0; k < tableColumns.length; k++) {
|
||||
const tableColumn = tableColumns[k];
|
||||
const { Field, Type, Null, Key, Default, Extra } =
|
||||
tableColumn;
|
||||
|
||||
const fieldObject: DSQL_FieldSchemaType = {
|
||||
fieldName: Field,
|
||||
dataType: Type.toUpperCase(),
|
||||
};
|
||||
|
||||
if (Null?.match(/^no$/i)) fieldObject.notNullValue = true;
|
||||
if (Key?.match(/^pri$/i)) fieldObject.primaryKey = true;
|
||||
if (Default?.toString()?.match(/./))
|
||||
fieldObject.defaultValue = Default;
|
||||
if (Default?.toString()?.match(/timestamp/i)) {
|
||||
delete fieldObject.defaultValue;
|
||||
fieldObject.defaultValueLiteral = Default;
|
||||
}
|
||||
if (Extra?.toString()?.match(/auto_increment/i))
|
||||
fieldObject.autoIncrement = true;
|
||||
|
||||
tableObject.fields.push(fieldObject);
|
||||
}
|
||||
}
|
||||
|
||||
const tableIndexes = await varDatabaseDbHandler({
|
||||
database: database.db_full_name,
|
||||
queryString: `SHOW INDEXES FROM ${database.db_full_name}.${tableName}`,
|
||||
});
|
||||
|
||||
if (tableIndexes) {
|
||||
for (let m = 0; m < tableIndexes.length; m++) {
|
||||
const indexObject = tableIndexes[m];
|
||||
const {
|
||||
Table,
|
||||
Key_name,
|
||||
Column_name,
|
||||
Null,
|
||||
Index_type,
|
||||
Index_comment,
|
||||
} = indexObject;
|
||||
|
||||
if (!Index_comment?.match(/^schema_index$/)) continue;
|
||||
|
||||
const indexNewObject: import("@/package-shared/types").DSQL_IndexSchemaType =
|
||||
{
|
||||
indexType: Index_type?.match(/fulltext/i)
|
||||
? "fullText"
|
||||
: "regular",
|
||||
indexName: Key_name,
|
||||
indexTableFields: [],
|
||||
};
|
||||
|
||||
const targetTableFieldObject = tableColumns?.filter(
|
||||
(col) => col.Field === Column_name
|
||||
)[0];
|
||||
|
||||
const existingIndexField = tableObject.indexes?.filter(
|
||||
(indx) => indx.indexName == Key_name
|
||||
);
|
||||
|
||||
if (existingIndexField && existingIndexField[0]) {
|
||||
existingIndexField[0].indexTableFields?.push({
|
||||
value: Column_name,
|
||||
dataType: targetTableFieldObject.Type.toUpperCase(),
|
||||
});
|
||||
} else {
|
||||
indexNewObject.indexTableFields = [
|
||||
{
|
||||
value: Column_name,
|
||||
dataType:
|
||||
targetTableFieldObject.Type.toUpperCase(),
|
||||
},
|
||||
];
|
||||
|
||||
tableObject.indexes?.push(indexNewObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
targetDb.tables.push(tableObject);
|
||||
}
|
||||
|
||||
setUserSchemaData({ schemaData: userSchemaData, userId });
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -7,13 +7,14 @@ import connDbHandler from "../../../utils/db/conn-db-handler";
|
||||
import checkIfIsMaster from "../../../utils/check-if-is-master";
|
||||
import { DbContextsArray } from "./runQuery";
|
||||
import debugLog from "../../../utils/logging/debug-log";
|
||||
import { PostInsertReturn } from "../../../types";
|
||||
|
||||
type Param = {
|
||||
type Param<T extends { [k: string]: any } = any> = {
|
||||
dbContext?: (typeof DbContextsArray)[number];
|
||||
paradigm?: "Read Only" | "Full Access";
|
||||
dbFullName?: string;
|
||||
tableName: string;
|
||||
data: any;
|
||||
data: T;
|
||||
tableSchema?: import("../../../types").DSQL_TableSchemaType;
|
||||
duplicateColumnName?: string;
|
||||
duplicateColumnValue?: string;
|
||||
@@ -27,7 +28,7 @@ type Param = {
|
||||
/**
|
||||
* Add a db Entry Function
|
||||
*/
|
||||
export default async function addDbEntry({
|
||||
export default async function addDbEntry<T extends { [k: string]: any } = any>({
|
||||
dbContext,
|
||||
paradigm,
|
||||
dbFullName,
|
||||
@@ -41,10 +42,7 @@ export default async function addDbEntry({
|
||||
encryptionSalt,
|
||||
forceLocal,
|
||||
debug,
|
||||
}: Param): Promise<any> {
|
||||
/**
|
||||
* Initialize variables
|
||||
*/
|
||||
}: Param<T>): Promise<PostInsertReturn | null> {
|
||||
const isMaster = forceLocal
|
||||
? true
|
||||
: checkIfIsMaster({ dbContext, dbFullName });
|
||||
@@ -64,10 +62,6 @@ export default async function addDbEntry({
|
||||
? global.DSQL_DB_CONN
|
||||
: global.DSQL_READ_ONLY_DB_CONN || global.DSQL_DB_CONN;
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
if (data?.["date_created_timestamp"]) delete data["date_created_timestamp"];
|
||||
if (data?.["date_updated_timestamp"]) delete data["date_updated_timestamp"];
|
||||
if (data?.["date_updated"]) delete data["date_updated"];
|
||||
@@ -75,10 +69,6 @@ export default async function addDbEntry({
|
||||
if (data?.["date_created"]) delete data["date_created"];
|
||||
if (data?.["date_created_code"]) delete data["date_created_code"];
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
if (duplicateColumnName && typeof duplicateColumnName === "string") {
|
||||
const checkDuplicateQuery = `SELECT * FROM ${
|
||||
isMaster ? "" : `\`${dbFullName}\`.`
|
||||
@@ -107,11 +97,6 @@ export default async function addDbEntry({
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Declare variables
|
||||
*
|
||||
* @description Declare "results" variable
|
||||
*/
|
||||
const dataKeys = Object.keys(data);
|
||||
|
||||
let insertKeysArray = [];
|
||||
@@ -120,7 +105,6 @@ export default async function addDbEntry({
|
||||
for (let i = 0; i < dataKeys.length; i++) {
|
||||
try {
|
||||
const dataKey = dataKeys[i];
|
||||
// @ts-ignore
|
||||
let value = data?.[dataKey];
|
||||
|
||||
const targetFieldSchemaArray = tableSchema
|
||||
@@ -186,8 +170,6 @@ export default async function addDbEntry({
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
if (!data?.["date_created"]) {
|
||||
insertKeysArray.push("`date_created`");
|
||||
insertValuesArray.push(Date());
|
||||
@@ -198,8 +180,6 @@ export default async function addDbEntry({
|
||||
insertValuesArray.push(Date.now());
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
if (!data?.["date_updated"]) {
|
||||
insertKeysArray.push("`date_updated`");
|
||||
insertValuesArray.push(Date());
|
||||
@@ -210,8 +190,6 @@ export default async function addDbEntry({
|
||||
insertValuesArray.push(Date.now());
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
const query = `INSERT INTO ${
|
||||
isMaster ? "" : `\`${dbFullName}\`.`
|
||||
}\`${tableName}\` (${insertKeysArray.join(",")}) VALUES (${insertValuesArray
|
||||
@@ -254,8 +232,5 @@ export default async function addDbEntry({
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Return statement
|
||||
*/
|
||||
return newInsert;
|
||||
}
|
||||
|
||||
@@ -4,8 +4,9 @@ import encrypt from "../../dsql/encrypt";
|
||||
import checkIfIsMaster from "../../../utils/check-if-is-master";
|
||||
import connDbHandler from "../../../utils/db/conn-db-handler";
|
||||
import { DbContextsArray } from "./runQuery";
|
||||
import { PostInsertReturn } from "../../../types";
|
||||
|
||||
type Param = {
|
||||
type Param<T extends { [k: string]: any } = any> = {
|
||||
dbContext?: (typeof DbContextsArray)[number];
|
||||
dbFullName?: string;
|
||||
tableName: string;
|
||||
@@ -13,7 +14,7 @@ type Param = {
|
||||
encryptionSalt?: string;
|
||||
data: any;
|
||||
tableSchema?: import("../../../types").DSQL_TableSchemaType;
|
||||
identifierColumnName: string;
|
||||
identifierColumnName: keyof T;
|
||||
identifierValue: string | number;
|
||||
forceLocal?: boolean;
|
||||
};
|
||||
@@ -22,7 +23,9 @@ type Param = {
|
||||
* # Update DB Function
|
||||
* @description
|
||||
*/
|
||||
export default async function updateDbEntry({
|
||||
export default async function updateDbEntry<
|
||||
T extends { [k: string]: any } = any
|
||||
>({
|
||||
dbContext,
|
||||
dbFullName,
|
||||
tableName,
|
||||
@@ -33,7 +36,7 @@ export default async function updateDbEntry({
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
forceLocal,
|
||||
}: Param): Promise<object | null> {
|
||||
}: Param<T>): Promise<PostInsertReturn | null> {
|
||||
/**
|
||||
* Check if data is valid
|
||||
*/
|
||||
@@ -157,9 +160,9 @@ export default async function updateDbEntry({
|
||||
|
||||
const query = `UPDATE ${
|
||||
isMaster ? "" : `\`${dbFullName}\`.`
|
||||
}\`${tableName}\` SET ${updateKeyValueArray.join(
|
||||
","
|
||||
)} WHERE \`${identifierColumnName}\`=?`;
|
||||
}\`${tableName}\` SET ${updateKeyValueArray.join(",")} WHERE \`${
|
||||
identifierColumnName as string
|
||||
}\`=?`;
|
||||
|
||||
updateValues.push(identifierValue);
|
||||
|
||||
|
||||
@@ -1,25 +1,27 @@
|
||||
import { DSQL_DatabaseSchemaType, UserType } from "@/package-shared/types";
|
||||
import serverError from "./serverError";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import grabDirNames from "@/package-shared/utils/backend/names/grab-dir-names";
|
||||
import { EJSON } from "@/client-exports";
|
||||
|
||||
type Params = {
|
||||
userId?: string | number;
|
||||
};
|
||||
|
||||
/**
|
||||
* # Grab User Schema Data
|
||||
*/
|
||||
export default function grabUserSchemaData({
|
||||
userId,
|
||||
}: {
|
||||
userId: string | number;
|
||||
}): import("../../types").DSQL_DatabaseSchemaType[] | null {
|
||||
}: Params): DSQL_DatabaseSchemaType[] | null {
|
||||
try {
|
||||
const userSchemaFilePath = path.resolve(
|
||||
process.cwd(),
|
||||
`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${userId}/main.json`
|
||||
const { userSchemaMainJSONFilePath } = grabDirNames({ userId });
|
||||
const schemaJSON = fs.readFileSync(
|
||||
userSchemaMainJSONFilePath || "",
|
||||
"utf-8"
|
||||
);
|
||||
const userSchemaData = JSON.parse(
|
||||
fs.readFileSync(userSchemaFilePath, "utf-8")
|
||||
);
|
||||
|
||||
return userSchemaData;
|
||||
const schemaObj = EJSON.parse(schemaJSON) as DSQL_DatabaseSchemaType[];
|
||||
return schemaObj;
|
||||
} catch (error: any) {
|
||||
serverError({
|
||||
component: "grabUserSchemaData",
|
||||
|
||||
@@ -2,6 +2,7 @@ import serverError from "./serverError";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import { DSQL_DatabaseSchemaType } from "../../types";
|
||||
import grabDirNames from "@/package-shared/utils/backend/names/grab-dir-names";
|
||||
|
||||
type Param = {
|
||||
userId: string | number;
|
||||
@@ -16,12 +17,14 @@ export default function setUserSchemaData({
|
||||
schemaData,
|
||||
}: Param): boolean {
|
||||
try {
|
||||
const userSchemaFilePath = path.resolve(
|
||||
process.cwd(),
|
||||
`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${userId}/main.json`
|
||||
);
|
||||
const { userSchemaMainJSONFilePath } = grabDirNames({ userId });
|
||||
|
||||
if (!userSchemaMainJSONFilePath) {
|
||||
throw new Error(`No User Schema JSON found!`);
|
||||
}
|
||||
|
||||
fs.writeFileSync(
|
||||
userSchemaFilePath,
|
||||
userSchemaMainJSONFilePath,
|
||||
JSON.stringify(schemaData),
|
||||
"utf8"
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user