Updates
This commit is contained in:
@@ -3,10 +3,12 @@ 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";
|
||||
import updateDbEntry from "../../functions/backend/db/updateDbEntry";
|
||||
|
||||
type Param = {
|
||||
userId?: number | string | null;
|
||||
dbSchema: DSQL_DatabaseSchemaType;
|
||||
isMain?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -16,7 +18,10 @@ type Param = {
|
||||
export default async function checkDbRecordCreateDbSchema({
|
||||
userId,
|
||||
dbSchema,
|
||||
isMain,
|
||||
}: Param): Promise<DSQL_DATASQUIREL_USER_DATABASES | undefined> {
|
||||
if (isMain) return undefined;
|
||||
|
||||
try {
|
||||
const {
|
||||
dbFullName,
|
||||
@@ -25,7 +30,8 @@ export default async function checkDbRecordCreateDbSchema({
|
||||
dbDescription,
|
||||
dbImage,
|
||||
childDatabase,
|
||||
childDatabaseDbFullName,
|
||||
childDatabaseDbId,
|
||||
id,
|
||||
} = dbSchema;
|
||||
|
||||
let recordedDbEntryArray = userId
|
||||
@@ -38,31 +44,41 @@ export default async function checkDbRecordCreateDbSchema({
|
||||
let recordedDbEntry: DSQL_DATASQUIREL_USER_DATABASES | undefined =
|
||||
recordedDbEntryArray?.[0];
|
||||
|
||||
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,
|
||||
db_schema_id: numberfy(id),
|
||||
active_clone_parent_db_id: numberfy(childDatabaseDbId),
|
||||
};
|
||||
|
||||
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<DSQL_DATASQUIREL_USER_DATABASES>({
|
||||
data: newDbEntryObj,
|
||||
tableName: "user_databases",
|
||||
forceLocal: true,
|
||||
});
|
||||
|
||||
const newDbEntry = (await addDbEntry({
|
||||
data: newDbEntryObj,
|
||||
tableName: "user_databases",
|
||||
forceLocal: true,
|
||||
})) as PostInsertReturn;
|
||||
|
||||
if (newDbEntry.insertId) {
|
||||
if (newDbEntry.payload?.insertId) {
|
||||
recordedDbEntryArray = await varDatabaseDbHandler({
|
||||
queryString: `SELECT * FROM datasquirel.user_databases WHERE db_full_name = ?`,
|
||||
queryValuesArray: [dbFullName || "NULL"],
|
||||
});
|
||||
recordedDbEntry = recordedDbEntryArray?.[0];
|
||||
}
|
||||
} else if (recordedDbEntry?.id) {
|
||||
await updateDbEntry<DSQL_DATASQUIREL_USER_DATABASES>({
|
||||
data: newDbEntryObj,
|
||||
tableName: "user_databases",
|
||||
forceLocal: true,
|
||||
identifierColumnName: "id",
|
||||
identifierValue: String(recordedDbEntry.id),
|
||||
});
|
||||
}
|
||||
|
||||
return recordedDbEntry;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import varDatabaseDbHandler from "../utils/varDatabaseDbHandler";
|
||||
import dbHandler from "../utils/dbHandler";
|
||||
import {
|
||||
DSQL_DatabaseSchemaType,
|
||||
DSQL_TableSchemaType,
|
||||
@@ -71,34 +70,34 @@ export default async function checkTableRecordCreateDbSchema({
|
||||
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) {
|
||||
if (tableSchema?.childTable && tableSchema.childTableId) {
|
||||
const parentDb = dbSchema.find(
|
||||
(db) => db.dbFullName == tableSchema.childTableDbFullName
|
||||
(db) => db.id == tableSchema.childTableDbId
|
||||
);
|
||||
const parentDbTable = parentDb?.tables.find(
|
||||
(tbl) => tbl.tableName == tableSchema.childTableName
|
||||
(tbl) => tbl.id == tableSchema.childTableId
|
||||
);
|
||||
if (parentDb && parentDbTable) {
|
||||
newTableInsertObject["child_table"] = 1;
|
||||
newTableInsertObject["child_table_parent_database"] =
|
||||
parentDb.dbFullName;
|
||||
newTableInsertObject["child_table_parent_table"] =
|
||||
parentDbTable.tableName;
|
||||
newTableInsertObject[
|
||||
"child_table_parent_database_schema_id"
|
||||
] = numberfy(parentDb.id);
|
||||
newTableInsertObject["child_table_parent_table_schema_id"] =
|
||||
numberfy(parentDbTable.id);
|
||||
}
|
||||
}
|
||||
|
||||
const newTableRecordEntry = (await addDbEntry({
|
||||
const newTableRecordEntry = await addDbEntry({
|
||||
data: newTableInsertObject,
|
||||
tableName: "user_database_tables",
|
||||
dbContext: "Master",
|
||||
forceLocal: true,
|
||||
})) as PostInsertReturn;
|
||||
});
|
||||
|
||||
if (newTableRecordEntry.insertId) {
|
||||
if (newTableRecordEntry.payload?.insertId) {
|
||||
recordedTableEntryArray = await varDatabaseDbHandler({
|
||||
queryString: queryObj?.string || "",
|
||||
queryValuesArray: queryObj?.values,
|
||||
|
||||
@@ -0,0 +1,317 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import grabDirNames from "../../utils/backend/names/grab-dir-names";
|
||||
import EJSON from "../../utils/ejson";
|
||||
import { DSQL_DatabaseSchemaType } from "../../types";
|
||||
import numberfy from "../../utils/numberfy";
|
||||
import _ from "lodash";
|
||||
import uniqueByKey from "../../utils/unique-by-key";
|
||||
|
||||
type Params = {
|
||||
userId?: string | number | null;
|
||||
dbId?: string | number;
|
||||
dbSlug?: string;
|
||||
};
|
||||
|
||||
export default function grabRequiredDatabaseSchemas(
|
||||
params: Params
|
||||
): DSQL_DatabaseSchemaType[] | undefined {
|
||||
const primaryDbSchema = grabPrimaryRequiredDbSchema(params);
|
||||
if (!primaryDbSchema) return undefined;
|
||||
|
||||
let relatedDatabases: DSQL_DatabaseSchemaType[] = [];
|
||||
|
||||
const childrenDatabases = primaryDbSchema.childrenDatabases || [];
|
||||
const childrenTables =
|
||||
primaryDbSchema.tables
|
||||
.map((tbl) => {
|
||||
return tbl.childrenTables || [];
|
||||
})
|
||||
.flat() || [];
|
||||
|
||||
for (let i = 0; i < childrenDatabases.length; i++) {
|
||||
const childDb = childrenDatabases[i];
|
||||
const childDbSchema = grabPrimaryRequiredDbSchema({
|
||||
userId: params.userId,
|
||||
dbId: childDb.dbId,
|
||||
});
|
||||
if (!childDbSchema?.dbSlug) continue;
|
||||
relatedDatabases.push(childDbSchema);
|
||||
}
|
||||
|
||||
for (let i = 0; i < childrenTables.length; i++) {
|
||||
const childTbl = childrenTables[i];
|
||||
const childTableDbSchema = grabPrimaryRequiredDbSchema({
|
||||
userId: params.userId,
|
||||
dbId: childTbl.dbId,
|
||||
});
|
||||
if (!childTableDbSchema?.dbSlug) continue;
|
||||
relatedDatabases.push(childTableDbSchema);
|
||||
}
|
||||
|
||||
return uniqueByKey([primaryDbSchema, ...relatedDatabases], "dbFullName");
|
||||
}
|
||||
|
||||
export function grabPrimaryRequiredDbSchema({ userId, dbId, dbSlug }: Params) {
|
||||
let finalDbId = dbId;
|
||||
|
||||
if (!finalDbId && userId && dbSlug) {
|
||||
const searchedDb = findDbNameInSchemaDir({ dbName: dbSlug, userId });
|
||||
|
||||
if (searchedDb?.id) {
|
||||
finalDbId = searchedDb.id;
|
||||
}
|
||||
}
|
||||
|
||||
if (!finalDbId) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const { targetUserPrivateDir, oldSchemasDir } = grabDirNames({
|
||||
userId,
|
||||
});
|
||||
|
||||
const finalSchemaDir = targetUserPrivateDir || oldSchemasDir;
|
||||
|
||||
if (!finalSchemaDir) {
|
||||
console.log(`finalSchemaDir not found!`);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (finalDbId) {
|
||||
const dbIdSchema = path.resolve(finalSchemaDir, `${finalDbId}.json`);
|
||||
if (fs.existsSync(dbIdSchema)) {
|
||||
const dbIdSchemaObject = EJSON.parse(
|
||||
fs.readFileSync(dbIdSchema, "utf-8")
|
||||
) as DSQL_DatabaseSchemaType | undefined;
|
||||
return dbIdSchemaObject;
|
||||
}
|
||||
}
|
||||
|
||||
const dbSchemasFiles = fs.readdirSync(finalSchemaDir);
|
||||
|
||||
let targetDbSchema: DSQL_DatabaseSchemaType | undefined;
|
||||
|
||||
try {
|
||||
for (let i = 0; i < dbSchemasFiles.length; i++) {
|
||||
const fileOrPath = dbSchemasFiles[i];
|
||||
if (!fileOrPath.endsWith(`.json`)) continue;
|
||||
if (!fileOrPath.match(/^\d+.json/)) continue;
|
||||
|
||||
const targetFileJSONPath = path.join(finalSchemaDir, fileOrPath);
|
||||
const targetSchema = EJSON.parse(
|
||||
fs.readFileSync(targetFileJSONPath, "utf-8")
|
||||
) as DSQL_DatabaseSchemaType | undefined;
|
||||
|
||||
if (targetSchema && finalDbId && targetSchema?.id == finalDbId) {
|
||||
targetDbSchema = targetSchema;
|
||||
}
|
||||
}
|
||||
} catch (error) {}
|
||||
|
||||
if (targetDbSchema) {
|
||||
return targetDbSchema;
|
||||
}
|
||||
// else if ( dbFullName) {
|
||||
// let existingSchemaInMainJSON = findTargetDbSchemaFromMainSchema(
|
||||
// dbFullName
|
||||
// );
|
||||
|
||||
// const nextID = grabLatestDbSchemaID(finalSchemaDir);
|
||||
|
||||
// if (existingSchemaInMainJSON) {
|
||||
// existingSchemaInMainJSON.id = nextID;
|
||||
// fs.writeFileSync(
|
||||
// path.join(finalSchemaDir, `${nextID}.json`),
|
||||
// EJSON.stringify(existingSchemaInMainJSON) || "[]"
|
||||
// );
|
||||
// return existingSchemaInMainJSON;
|
||||
// }
|
||||
// }
|
||||
|
||||
console.log(`userSchemaDir not found!`);
|
||||
console.log(`userId`, userId);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function findDbNameInSchemaDir({
|
||||
userId,
|
||||
dbName,
|
||||
}: {
|
||||
userId?: string | number;
|
||||
dbName?: string;
|
||||
}) {
|
||||
if (!userId) {
|
||||
console.log(`userId not provided!`);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (!dbName) {
|
||||
console.log(`dbName not provided!`);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const { targetUserPrivateDir } = grabDirNames({
|
||||
userId,
|
||||
});
|
||||
|
||||
if (!targetUserPrivateDir) {
|
||||
console.log(`targetUserPrivateDir not found!`);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const dbSchemasFiles = fs.readdirSync(targetUserPrivateDir);
|
||||
|
||||
let targetDbSchema: DSQL_DatabaseSchemaType | undefined;
|
||||
|
||||
try {
|
||||
for (let i = 0; i < dbSchemasFiles.length; i++) {
|
||||
const fileOrPath = dbSchemasFiles[i];
|
||||
if (!fileOrPath.endsWith(`.json`)) continue;
|
||||
if (!fileOrPath.match(/^\d+.json/)) continue;
|
||||
|
||||
const targetFileJSONPath = path.join(
|
||||
targetUserPrivateDir,
|
||||
fileOrPath
|
||||
);
|
||||
const targetSchema = EJSON.parse(
|
||||
fs.readFileSync(targetFileJSONPath, "utf-8")
|
||||
) as DSQL_DatabaseSchemaType | undefined;
|
||||
|
||||
if (!targetSchema) continue;
|
||||
|
||||
if (
|
||||
targetSchema.dbFullName == dbName ||
|
||||
targetSchema.dbSlug == dbName
|
||||
) {
|
||||
targetDbSchema = targetSchema;
|
||||
return targetSchema;
|
||||
}
|
||||
}
|
||||
} catch (error) {}
|
||||
|
||||
return targetDbSchema;
|
||||
}
|
||||
|
||||
type UpdateDbSchemaParam = {
|
||||
dbSchema: DSQL_DatabaseSchemaType;
|
||||
userId?: string | number | null;
|
||||
};
|
||||
|
||||
export function writeUpdatedDbSchema({
|
||||
dbSchema,
|
||||
userId,
|
||||
}: UpdateDbSchemaParam): { success?: boolean; dbSchemaId?: string | number } {
|
||||
const { targetUserPrivateDir } = grabDirNames({
|
||||
userId,
|
||||
});
|
||||
|
||||
if (!targetUserPrivateDir) {
|
||||
console.log(`user ${userId} has no targetUserPrivateDir`);
|
||||
return {};
|
||||
}
|
||||
|
||||
if (dbSchema.id) {
|
||||
const dbIdSchemaPath = path.join(
|
||||
targetUserPrivateDir,
|
||||
`${dbSchema.id}.json`
|
||||
);
|
||||
|
||||
fs.writeFileSync(dbIdSchemaPath, EJSON.stringify(dbSchema) || "[]");
|
||||
|
||||
return { success: true };
|
||||
} else {
|
||||
const nextID = grabLatestDbSchemaID(targetUserPrivateDir);
|
||||
|
||||
dbSchema.id = nextID;
|
||||
|
||||
fs.writeFileSync(
|
||||
path.join(targetUserPrivateDir, `${nextID}.json`),
|
||||
EJSON.stringify(dbSchema) || "[]"
|
||||
);
|
||||
|
||||
return { success: true, dbSchemaId: nextID };
|
||||
}
|
||||
}
|
||||
|
||||
export function deleteDbSchema({ dbSchema, userId }: UpdateDbSchemaParam) {
|
||||
const { targetUserPrivateDir, userSchemaMainJSONFilePath } = grabDirNames({
|
||||
userId,
|
||||
});
|
||||
|
||||
if (!targetUserPrivateDir) return;
|
||||
|
||||
const targetDbSchema = grabPrimaryRequiredDbSchema({
|
||||
dbId: dbSchema.id,
|
||||
userId,
|
||||
});
|
||||
|
||||
const schemaFile = path.join(
|
||||
targetUserPrivateDir,
|
||||
`${targetDbSchema?.id}.json`
|
||||
);
|
||||
|
||||
try {
|
||||
fs.unlinkSync(schemaFile);
|
||||
} catch (error) {}
|
||||
|
||||
if (
|
||||
userSchemaMainJSONFilePath &&
|
||||
fs.existsSync(userSchemaMainJSONFilePath)
|
||||
) {
|
||||
try {
|
||||
let allDbSchemas = EJSON.parse(
|
||||
fs.readFileSync(userSchemaMainJSONFilePath, "utf-8")
|
||||
) as DSQL_DatabaseSchemaType[] | undefined;
|
||||
|
||||
if (allDbSchemas?.[0]) {
|
||||
for (let i = 0; i < allDbSchemas.length; i++) {
|
||||
const dbSch = allDbSchemas[i];
|
||||
if (
|
||||
dbSch.dbFullName == dbSchema.dbFullName ||
|
||||
dbSch.id == dbSchema.id
|
||||
) {
|
||||
allDbSchemas.splice(i, 1);
|
||||
}
|
||||
}
|
||||
|
||||
fs.writeFileSync(
|
||||
userSchemaMainJSONFilePath,
|
||||
EJSON.stringify(allDbSchemas) || "[]"
|
||||
);
|
||||
}
|
||||
} catch (error) {}
|
||||
}
|
||||
}
|
||||
|
||||
export function findTargetDbSchemaFromMainSchema(
|
||||
schemas: DSQL_DatabaseSchemaType[],
|
||||
dbFullName?: string,
|
||||
dbId?: string | number
|
||||
): DSQL_DatabaseSchemaType | undefined {
|
||||
const targetDbSchema = schemas.find(
|
||||
(sch) => sch.dbFullName == dbFullName || (dbId && sch.id == dbId)
|
||||
);
|
||||
return targetDbSchema;
|
||||
}
|
||||
|
||||
export function grabLatestDbSchemaID(userSchemaDir: string) {
|
||||
const dbSchemasFiles = fs.readdirSync(userSchemaDir);
|
||||
const dbNumbers = dbSchemasFiles
|
||||
.filter((dbSch) => {
|
||||
if (!dbSch.endsWith(`.json`)) return false;
|
||||
if (dbSch.match(/^\d+\.json/)) return true;
|
||||
return false;
|
||||
})
|
||||
.map((dbSch) => numberfy(dbSch.replace(/[^0-9]/g, "")));
|
||||
|
||||
if (dbNumbers[0])
|
||||
return (
|
||||
(dbNumbers
|
||||
.sort((a, b) => {
|
||||
return a - b;
|
||||
})
|
||||
.pop() || 0) + 1
|
||||
);
|
||||
return 1;
|
||||
}
|
||||
@@ -1,5 +1,9 @@
|
||||
import varDatabaseDbHandler from "../utils/varDatabaseDbHandler";
|
||||
import { DSQL_IndexSchemaType } from "../../types";
|
||||
import {
|
||||
DSQL_IndexSchemaType,
|
||||
DSQL_MYSQL_SHOW_INDEXES_Type,
|
||||
} from "../../types";
|
||||
import grabDSQLSchemaIndexComment from "../utils/grab-dsql-schema-index-comment";
|
||||
|
||||
type Param = {
|
||||
tableName: string;
|
||||
@@ -18,6 +22,11 @@ export default async function handleIndexescreateDbFromSchema({
|
||||
tableName,
|
||||
indexes,
|
||||
}: Param) {
|
||||
const allExistingIndexes: DSQL_MYSQL_SHOW_INDEXES_Type[] =
|
||||
await varDatabaseDbHandler({
|
||||
queryString: `SHOW INDEXES FROM \`${dbFullName}\`.\`${tableName}\``,
|
||||
});
|
||||
|
||||
for (let g = 0; g < indexes.length; g++) {
|
||||
const { indexType, indexName, indexTableFields, alias } = indexes[g];
|
||||
|
||||
@@ -27,38 +36,32 @@ export default async function handleIndexescreateDbFromSchema({
|
||||
* @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) {
|
||||
global.ERROR_CALLBACK?.(
|
||||
`Error Handling Indexes on Creating Schema`,
|
||||
error as 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'`,
|
||||
});
|
||||
const queryString = `CREATE${
|
||||
indexType == "full_text" ? " FULLTEXT" : ""
|
||||
} INDEX \`${alias}\` ON \`${dbFullName}\`.\`${tableName}\`(${indexTableFields
|
||||
?.map((nm) => nm.value)
|
||||
.map((nm) => `\`${nm}\``)
|
||||
.join(
|
||||
","
|
||||
)}) COMMENT '${grabDSQLSchemaIndexComment()} ${indexName}'`;
|
||||
|
||||
const addIndex = await varDatabaseDbHandler({ queryString });
|
||||
}
|
||||
}
|
||||
|
||||
const allExistingIndexesAfterUpdate: DSQL_MYSQL_SHOW_INDEXES_Type[] =
|
||||
await varDatabaseDbHandler({
|
||||
queryString: `SHOW INDEXES FROM \`${dbFullName}\`.\`${tableName}\``,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,21 +1,22 @@
|
||||
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 checkDbRecordCreateDbSchema from "./check-db-record";
|
||||
import checkTableRecordCreateDbSchema from "./check-table-record";
|
||||
import handleIndexescreateDbFromSchema from "./handle-indexes";
|
||||
import grabRequiredDatabaseSchemas, {
|
||||
grabPrimaryRequiredDbSchema,
|
||||
} from "./grab-required-database-schemas";
|
||||
import dbHandler from "../../functions/backend/dbHandler";
|
||||
|
||||
type Param = {
|
||||
userId?: number | string | null;
|
||||
targetDatabase?: string;
|
||||
dbSchemaData?: import("../../types").DSQL_DatabaseSchemaType[];
|
||||
dbSchemaData?: DSQL_DatabaseSchemaType[];
|
||||
targetTable?: string;
|
||||
dbId?: string | number;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -26,84 +27,79 @@ export default async function createDbFromSchema({
|
||||
userId,
|
||||
targetDatabase,
|
||||
dbSchemaData,
|
||||
targetTable,
|
||||
dbId,
|
||||
}: Param): Promise<boolean> {
|
||||
const { userSchemaMainJSONFilePath, mainShemaJSONFilePath } = grabDirNames({
|
||||
userId,
|
||||
});
|
||||
|
||||
const schemaPath = userSchemaMainJSONFilePath || mainShemaJSONFilePath;
|
||||
|
||||
const dbSchema: DSQL_DatabaseSchemaType[] | undefined =
|
||||
dbSchemaData ||
|
||||
(EJSON.parse(fs.readFileSync(schemaPath, "utf8")) as
|
||||
| DSQL_DatabaseSchemaType[]
|
||||
| undefined);
|
||||
|
||||
if (!dbSchema) {
|
||||
console.log("Schema Not Found!");
|
||||
return false;
|
||||
}
|
||||
|
||||
for (let i = 0; i < dbSchema.length; i++) {
|
||||
const database: DSQL_DatabaseSchemaType = dbSchema[i];
|
||||
|
||||
const { dbFullName, tables, dbSlug, childrenDatabases } = database;
|
||||
|
||||
if (!dbFullName) continue;
|
||||
|
||||
if (targetDatabase && dbFullName != targetDatabase) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const dbCheck: any = await noDatabaseDbHandler(
|
||||
`SELECT SCHEMA_NAME AS dbFullName FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '${dbFullName}'`
|
||||
);
|
||||
|
||||
if (!dbCheck?.[0]?.dbFullName) {
|
||||
const newDatabase = await noDatabaseDbHandler(
|
||||
`CREATE DATABASE IF NOT EXISTS \`${dbFullName}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_bin`
|
||||
);
|
||||
}
|
||||
|
||||
const allTables: any = await noDatabaseDbHandler(
|
||||
`SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='${dbFullName}'`
|
||||
);
|
||||
|
||||
let recordedDbEntry = await checkDbRecordCreateDbSchema({
|
||||
dbSchema: database,
|
||||
try {
|
||||
const { userSchemaMainJSONFilePath } = grabDirNames({
|
||||
userId,
|
||||
});
|
||||
|
||||
for (let tb = 0; tb < allTables.length; tb++) {
|
||||
const { TABLE_NAME } = allTables[tb];
|
||||
let dbSchema = dbSchemaData
|
||||
? dbSchemaData
|
||||
: dbId
|
||||
? grabRequiredDatabaseSchemas({
|
||||
dbId,
|
||||
userId,
|
||||
})
|
||||
: undefined;
|
||||
|
||||
const targetTableSchema = tables.find(
|
||||
(_table) => _table.tableName === TABLE_NAME
|
||||
if (!dbSchema) {
|
||||
console.log("Schema Not Found!");
|
||||
return false;
|
||||
}
|
||||
|
||||
const isMain = !userSchemaMainJSONFilePath;
|
||||
|
||||
for (let i = 0; i < dbSchema.length; i++) {
|
||||
const database: DSQL_DatabaseSchemaType = dbSchema[i];
|
||||
|
||||
const { dbFullName, tables, dbSlug, childrenDatabases } = database;
|
||||
|
||||
if (!dbFullName) continue;
|
||||
|
||||
if (targetDatabase && dbFullName != targetDatabase) {
|
||||
continue;
|
||||
}
|
||||
|
||||
console.log(`Handling database => ${dbFullName}`);
|
||||
|
||||
const dbCheck: any = await noDatabaseDbHandler(
|
||||
`SELECT SCHEMA_NAME AS dbFullName FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '${dbFullName}'`
|
||||
);
|
||||
|
||||
/**
|
||||
* @description Check if TABLE_NAME is part of the tables contained
|
||||
* in the user schema JSON. If it's not, the table is either deleted
|
||||
* or the table name has been recently changed
|
||||
*/
|
||||
if (!targetTableSchema) {
|
||||
const oldTable = tables.find(
|
||||
(_table) =>
|
||||
_table.tableNameOld &&
|
||||
_table.tableNameOld === TABLE_NAME
|
||||
if (!dbCheck?.[0]?.dbFullName) {
|
||||
const newDatabase = await noDatabaseDbHandler(
|
||||
`CREATE DATABASE IF NOT EXISTS \`${dbFullName}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_bin`
|
||||
);
|
||||
}
|
||||
|
||||
const allTables: any = await noDatabaseDbHandler(
|
||||
`SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='${dbFullName}'`
|
||||
);
|
||||
|
||||
let recordedDbEntry = await checkDbRecordCreateDbSchema({
|
||||
dbSchema: database,
|
||||
userId,
|
||||
isMain,
|
||||
});
|
||||
|
||||
for (let tb = 0; tb < allTables.length; tb++) {
|
||||
const { TABLE_NAME } = allTables[tb];
|
||||
|
||||
const targetTableSchema = tables.find(
|
||||
(_table) => _table.tableName === TABLE_NAME
|
||||
);
|
||||
|
||||
/**
|
||||
* @description Check if this table has been recently renamed. Rename
|
||||
* table id true. Drop table if false
|
||||
* @description Check if TABLE_NAME is part of the tables contained
|
||||
* in the user schema JSON. If it's not, the table is either deleted
|
||||
* or the table name has been recently changed
|
||||
*/
|
||||
if (oldTable) {
|
||||
console.log("Renaming Table");
|
||||
await varDatabaseDbHandler({
|
||||
queryString: `RENAME TABLE \`${dbFullName}\`.\`${oldTable.tableNameOld}\` TO \`${oldTable.tableName}\``,
|
||||
});
|
||||
} else {
|
||||
console.log(`Dropping Table from ${dbFullName}`);
|
||||
if (!targetTableSchema) {
|
||||
console.log(
|
||||
`Dropping Table ${TABLE_NAME} from ${dbFullName}`
|
||||
);
|
||||
await varDatabaseDbHandler({
|
||||
queryString: `DROP TABLE \`${dbFullName}\`.\`${TABLE_NAME}\``,
|
||||
});
|
||||
@@ -112,130 +108,175 @@ export default async function createDbFromSchema({
|
||||
query: `DELETE FROM datasquirel.user_database_tables WHERE user_id = ? AND db_slug = ? AND table_slug = ?`,
|
||||
values: [userId, dbSlug, TABLE_NAME],
|
||||
});
|
||||
|
||||
// const oldTable = tables.find(
|
||||
// (_table) =>
|
||||
// _table.tableNameOld &&
|
||||
// _table.tableNameOld === TABLE_NAME
|
||||
// );
|
||||
|
||||
// /**
|
||||
// * @description Check if this table has been recently renamed. Rename
|
||||
// * table id true. Drop table if false
|
||||
// */
|
||||
// if (oldTable) {
|
||||
// console.log("Renaming Table");
|
||||
// await varDatabaseDbHandler({
|
||||
// queryString: `RENAME TABLE \`${dbFullName}\`.\`${oldTable.tableNameOld}\` TO \`${oldTable.tableName}\``,
|
||||
// });
|
||||
// } else {
|
||||
// console.log(
|
||||
// `Dropping Table ${TABLE_NAME} from ${dbFullName}`
|
||||
// );
|
||||
// await varDatabaseDbHandler({
|
||||
// queryString: `DROP TABLE \`${dbFullName}\`.\`${TABLE_NAME}\``,
|
||||
// });
|
||||
|
||||
// const deleteTableEntry = await dbHandler({
|
||||
// query: `DELETE FROM datasquirel.user_database_tables WHERE user_id = ? AND db_slug = ? AND table_slug = ?`,
|
||||
// values: [userId, dbSlug, TABLE_NAME],
|
||||
// });
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Iterate through each table and perform table actions
|
||||
*/
|
||||
for (let t = 0; t < tables.length; t++) {
|
||||
const table = tables[t];
|
||||
|
||||
const { tableName, fields, indexes } = table;
|
||||
|
||||
/**
|
||||
* @description Check if table exists
|
||||
* @type {any}
|
||||
* @description Iterate through each table and perform table actions
|
||||
*/
|
||||
const tableCheck: any = await varDatabaseDbHandler({
|
||||
queryString: `
|
||||
SELECT EXISTS (
|
||||
SELECT
|
||||
TABLE_NAME
|
||||
FROM
|
||||
information_schema.TABLES
|
||||
WHERE
|
||||
TABLE_SCHEMA = ? AND
|
||||
TABLE_NAME = ?
|
||||
) AS tableExists`,
|
||||
queryValuesArray: [dbFullName, table.tableName],
|
||||
});
|
||||
for (let t = 0; t < tables.length; t++) {
|
||||
const table = tables[t];
|
||||
|
||||
////////////////////////////////////////
|
||||
const { tableName, fields, indexes } = table;
|
||||
|
||||
if (targetTable && tableName !== targetTable) continue;
|
||||
|
||||
console.log(`Handling table => ${tableName}`);
|
||||
|
||||
if (tableCheck && tableCheck[0]?.tableExists > 0) {
|
||||
/**
|
||||
* @description Update table if table exists
|
||||
* @description Check if table exists
|
||||
* @type {any}
|
||||
*/
|
||||
const updateExistingTable = await updateTable({
|
||||
dbFullName: dbFullName,
|
||||
tableName: tableName,
|
||||
tableNameFull: table.tableFullName,
|
||||
tableInfoArray: fields,
|
||||
userId,
|
||||
dbSchema,
|
||||
tableIndexes: indexes,
|
||||
tableIndex: t,
|
||||
childDb: database.childDatabase || undefined,
|
||||
recordedDbEntry,
|
||||
tableSchema: table,
|
||||
const tableCheck: any = await varDatabaseDbHandler({
|
||||
queryString: `
|
||||
SELECT EXISTS (
|
||||
SELECT
|
||||
TABLE_NAME
|
||||
FROM
|
||||
information_schema.TABLES
|
||||
WHERE
|
||||
TABLE_SCHEMA = ? AND
|
||||
TABLE_NAME = ?
|
||||
) AS tableExists`,
|
||||
queryValuesArray: [dbFullName, table.tableName],
|
||||
});
|
||||
|
||||
if (table.childrenTables && table.childrenTables[0]) {
|
||||
for (let ch = 0; ch < table.childrenTables.length; ch++) {
|
||||
const childTable = table.childrenTables[ch];
|
||||
if (tableCheck && tableCheck[0]?.tableExists > 0) {
|
||||
/**
|
||||
* @description Update table if table exists
|
||||
*/
|
||||
const updateExistingTable = await updateTable({
|
||||
dbFullName: dbFullName,
|
||||
tableName: tableName,
|
||||
tableFields: fields,
|
||||
userId,
|
||||
dbSchema: database,
|
||||
tableIndexes: indexes,
|
||||
recordedDbEntry,
|
||||
tableSchema: table,
|
||||
isMain,
|
||||
});
|
||||
|
||||
const updateExistingChildTable = await updateTable({
|
||||
dbFullName: childTable.dbNameFull,
|
||||
tableName: childTable.tableName,
|
||||
tableNameFull: childTable.tableNameFull,
|
||||
tableInfoArray: fields,
|
||||
userId,
|
||||
dbSchema,
|
||||
tableIndexes: indexes,
|
||||
clone: true,
|
||||
childDb: database.childDatabase || undefined,
|
||||
recordedDbEntry,
|
||||
tableSchema: table,
|
||||
if (table.childrenTables && table.childrenTables[0]) {
|
||||
for (
|
||||
let ch = 0;
|
||||
ch < table.childrenTables.length;
|
||||
ch++
|
||||
) {
|
||||
const childTable = table.childrenTables[ch];
|
||||
|
||||
const childTableParentDbSchema =
|
||||
grabPrimaryRequiredDbSchema({
|
||||
dbId: childTable.dbId,
|
||||
userId,
|
||||
});
|
||||
|
||||
if (!childTableParentDbSchema?.dbFullName) continue;
|
||||
|
||||
const childTableSchema =
|
||||
childTableParentDbSchema.tables.find(
|
||||
(tbl) => tbl.id == childTable.tableId
|
||||
);
|
||||
|
||||
if (!childTableSchema) continue;
|
||||
|
||||
const updateExistingChildTable = await updateTable({
|
||||
dbFullName: childTableParentDbSchema.dbFullName,
|
||||
tableName: childTableSchema.tableName,
|
||||
tableFields: childTableSchema.fields,
|
||||
userId,
|
||||
dbSchema: childTableParentDbSchema,
|
||||
tableIndexes: childTableSchema.indexes,
|
||||
clone: true,
|
||||
recordedDbEntry,
|
||||
tableSchema: table,
|
||||
isMain,
|
||||
});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/**
|
||||
* @description Create new Table if table doesnt exist
|
||||
*/
|
||||
const createNewTable = await createTable({
|
||||
tableName: tableName,
|
||||
tableInfoArray: fields,
|
||||
dbFullName: dbFullName,
|
||||
tableSchema: table,
|
||||
recordedDbEntry,
|
||||
isMain,
|
||||
});
|
||||
|
||||
/**
|
||||
* Handle DATASQUIREL Table Indexes
|
||||
* ===================================================
|
||||
* @description Iterate through each datasquirel schema
|
||||
* table index(if available), and perform operations
|
||||
*/
|
||||
if (indexes?.[0]) {
|
||||
handleIndexescreateDbFromSchema({
|
||||
dbFullName,
|
||||
indexes,
|
||||
tableName,
|
||||
});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/**
|
||||
* @description Create new Table if table doesnt exist
|
||||
*/
|
||||
const createNewTable = await createTable({
|
||||
tableName: tableName,
|
||||
tableInfoArray: fields,
|
||||
dbFullName: dbFullName,
|
||||
tableSchema: table,
|
||||
recordedDbEntry,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle DATASQUIREL Table Indexes
|
||||
* ===================================================
|
||||
* @description Iterate through each datasquirel schema
|
||||
* table index(if available), and perform operations
|
||||
*/
|
||||
if (indexes?.[0]) {
|
||||
handleIndexescreateDbFromSchema({
|
||||
dbFullName,
|
||||
indexes,
|
||||
tableName,
|
||||
});
|
||||
/**
|
||||
* @description Check all children databases
|
||||
*/
|
||||
if (childrenDatabases?.[0]) {
|
||||
for (let ch = 0; ch < childrenDatabases.length; ch++) {
|
||||
const childDb = childrenDatabases[ch];
|
||||
const { dbId } = childDb;
|
||||
|
||||
const targetDatabase = dbSchema.find(
|
||||
(dbSch) => dbSch.childDatabaseDbId == dbId
|
||||
);
|
||||
|
||||
if (targetDatabase?.id) {
|
||||
await createDbFromSchema({
|
||||
userId,
|
||||
dbId: targetDatabase?.id,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const tableRecord = await checkTableRecordCreateDbSchema({
|
||||
dbFullName,
|
||||
dbSchema,
|
||||
tableSchema: table,
|
||||
dbRecord: recordedDbEntry,
|
||||
userId,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Check all children databases
|
||||
*/
|
||||
if (childrenDatabases?.[0]) {
|
||||
for (let ch = 0; ch < childrenDatabases.length; ch++) {
|
||||
const childDb = childrenDatabases[ch];
|
||||
const { dbId } = childDb;
|
||||
|
||||
const targetDatabase = dbSchema.find(
|
||||
(dbSch) => dbSch.id == dbId
|
||||
);
|
||||
|
||||
await createDbFromSchema({
|
||||
userId,
|
||||
targetDatabase: targetDatabase?.dbFullName,
|
||||
});
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} catch (error: any) {
|
||||
console.log(`createDbFromSchema ERROR => ${error.message}`);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
import { AppNames } from "../dict/app-names";
|
||||
import serverError from "../functions/backend/serverError";
|
||||
import noDatabaseDbHandler from "./utils/noDatabaseDbHandler";
|
||||
|
||||
@@ -16,26 +17,17 @@ async function grantFullPrivileges({ userId }: { userId: string | null }) {
|
||||
|
||||
const allDatabases = await noDatabaseDbHandler(`SHOW DATABASES`);
|
||||
|
||||
const datasquirelUserDatabases = allDatabases.filter(
|
||||
(/** @type {any} */ database: any) =>
|
||||
database.Database.match(/datasquirel_user_/)
|
||||
const datasquirelUserDatabases = allDatabases.filter((database: any) =>
|
||||
database.Database.match(new RegExp(`^${AppNames["DsqlDbPrefix"]}`))
|
||||
);
|
||||
|
||||
for (let i = 0; i < datasquirelUserDatabases.length; i++) {
|
||||
const datasquirelUserDatabase = datasquirelUserDatabases[i];
|
||||
const { Database } = datasquirelUserDatabase;
|
||||
|
||||
// const grantDbPriviledges = await noDatabaseDbHandler(
|
||||
// `GRANT ALL PRIVILEGES ON ${Database}.* TO '${process.env.DSQL_DB_FULL_ACCESS_USERNAME}'@'%' WITH GRANT OPTION`
|
||||
// );
|
||||
|
||||
// const grantRead = await noDatabaseDbHandler(
|
||||
// `GRANT SELECT ON ${Database}.* TO '${process.env.DSQL_DB_READ_ONLY_USERNAME}'@'%'`
|
||||
// );
|
||||
}
|
||||
|
||||
const flushPriviledged = await noDatabaseDbHandler(`FLUSH PRIVILEGES`);
|
||||
} catch (/** @type {any} */ error: any) {
|
||||
} catch (error: any) {
|
||||
serverError({
|
||||
component: "shell/grantDbPriviledges/main-catch-error",
|
||||
message: error.message,
|
||||
|
||||
@@ -1,137 +0,0 @@
|
||||
import generator from "generate-password";
|
||||
import noDatabaseDbHandler from "../utils/noDatabaseDbHandler";
|
||||
import dbHandler from "../utils/dbHandler";
|
||||
import handleGrants from "./handleGrants";
|
||||
import encrypt from "../../functions/dsql/encrypt";
|
||||
import decrypt from "../../functions/dsql/decrypt";
|
||||
import { DSQL_DATASQUIREL_MARIADB_USERS } from "../../types/dsql";
|
||||
import { MariaDBUser } from "../../types";
|
||||
|
||||
type Param = {
|
||||
userId?: number | string;
|
||||
mariadbUserHost?: string;
|
||||
mariadbUsername?: string;
|
||||
sqlUserID?: string | number;
|
||||
};
|
||||
|
||||
/**
|
||||
* # Refresh Mariadb User Grants
|
||||
*/
|
||||
export default async function refreshUsersAndGrants({
|
||||
userId,
|
||||
mariadbUserHost,
|
||||
mariadbUsername,
|
||||
sqlUserID,
|
||||
}: Param) {
|
||||
const mariadbUsers = (await dbHandler({
|
||||
query: `SELECT * FROM mariadb_users`,
|
||||
})) as any[] | null;
|
||||
|
||||
if (!mariadbUsers?.[0]) {
|
||||
return;
|
||||
}
|
||||
|
||||
const isRootUser = userId
|
||||
? userId == Number(process.env.DSQL_SU_USER_ID)
|
||||
: false;
|
||||
|
||||
const isWildcardHost = mariadbUserHost == "%";
|
||||
|
||||
if (isWildcardHost && !isRootUser) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (let i = 0; i < mariadbUsers.length; i++) {
|
||||
const mariadbUser = mariadbUsers[i] as
|
||||
| DSQL_DATASQUIREL_MARIADB_USERS
|
||||
| undefined;
|
||||
|
||||
if (!mariadbUser) continue;
|
||||
if (userId && mariadbUser.user_id != userId) continue;
|
||||
if (sqlUserID && mariadbUser.id != sqlUserID) continue;
|
||||
|
||||
try {
|
||||
const { username, password, host, user_id } = mariadbUser;
|
||||
|
||||
const existingUser = await noDatabaseDbHandler(
|
||||
`SELECT * FROM mysql.user WHERE User = '${username}' AND Host = '${host}'`
|
||||
);
|
||||
|
||||
const isUserExisting = Boolean(existingUser?.[0]?.User);
|
||||
|
||||
const isPrimary = String(mariadbUser.primary)?.match(/1/)
|
||||
? true
|
||||
: false;
|
||||
|
||||
const dsqlPassword = mariadbUser?.password
|
||||
? decrypt({ encryptedString: mariadbUser.password })
|
||||
: isUserExisting && password
|
||||
? decrypt({ encryptedString: password })
|
||||
: generator.generate({
|
||||
length: 16,
|
||||
numbers: true,
|
||||
symbols: true,
|
||||
uppercase: true,
|
||||
exclude: "*#.'`\"",
|
||||
});
|
||||
|
||||
const encryptedPassword = mariadbUser?.password
|
||||
? mariadbUser.password
|
||||
: isUserExisting
|
||||
? password
|
||||
: encrypt({ data: dsqlPassword });
|
||||
|
||||
if (!isUserExisting) {
|
||||
if (isWildcardHost) {
|
||||
const _existingUsers = (await noDatabaseDbHandler(
|
||||
`SELECT * FROM mysql.user WHERE user='${mariadbUsername}'`
|
||||
)) as MariaDBUser[];
|
||||
|
||||
for (let i = 0; i < _existingUsers.length; i++) {
|
||||
const exUsr = _existingUsers[i];
|
||||
await noDatabaseDbHandler(
|
||||
`DROP USER '${exUsr.User}'@'${exUsr.Host}'`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const createNewUser = await noDatabaseDbHandler(
|
||||
`CREATE USER IF NOT EXISTS '${mariadbUsername}'@'${mariadbUserHost}' IDENTIFIED BY '${dsqlPassword}'`
|
||||
);
|
||||
}
|
||||
|
||||
if (isPrimary) {
|
||||
const updateUser = await dbHandler({
|
||||
query: `UPDATE users SET mariadb_user = ?, mariadb_host = ?, mariadb_pass = ? WHERE id = ?`,
|
||||
values: [
|
||||
mariadbUsername,
|
||||
mariadbUserHost,
|
||||
encryptedPassword,
|
||||
user_id,
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
const isGrantHandled = await handleGrants({
|
||||
username: mariadbUser.username,
|
||||
host: mariadbUser.host,
|
||||
grants:
|
||||
mariadbUser.grants && typeof mariadbUser.grants == "string"
|
||||
? JSON.parse(mariadbUser.grants)
|
||||
: [],
|
||||
userId: String(user_id),
|
||||
});
|
||||
|
||||
if (!isGrantHandled) {
|
||||
console.log(
|
||||
`Error in handling grants for user ${mariadbUser.username}@${mariadbUser.host}`
|
||||
);
|
||||
}
|
||||
} catch (error: any) {
|
||||
global.ERROR_CALLBACK?.(
|
||||
`Error Refreshing MariaDB Users and Grants`,
|
||||
error as Error
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
require("dotenv").config({ path: "../../.env" });
|
||||
import generator from "generate-password";
|
||||
import noDatabaseDbHandler from "../utils/noDatabaseDbHandler";
|
||||
import dbHandler from "../utils/dbHandler";
|
||||
import dbHandler from "../../functions/backend/dbHandler";
|
||||
import encrypt from "../../functions/dsql/encrypt";
|
||||
import grabSQLKeyName from "../../utils/grab-sql-key-name";
|
||||
|
||||
/**
|
||||
* # Reset SQL Passwords
|
||||
@@ -23,7 +24,9 @@ async function resetSQLCredentialsPasswords() {
|
||||
|
||||
try {
|
||||
const maridbUsers = (await dbHandler({
|
||||
query: `SELECT * FROM mysql.user WHERE User = 'dsql_user_${user.id}'`,
|
||||
query: `SELECT * FROM mysql.user WHERE User = '${grabSQLKeyName(
|
||||
{ type: "user", userId: user.id }
|
||||
)}'`,
|
||||
})) as any[];
|
||||
|
||||
for (let j = 0; j < maridbUsers.length; j++) {
|
||||
|
||||
@@ -8,6 +8,8 @@ import addDbEntry from "../../../functions/backend/db/addDbEntry";
|
||||
import addMariadbUser from "../../../functions/backend/addMariadbUser";
|
||||
import updateDbEntry from "../../../functions/backend/db/updateDbEntry";
|
||||
import hashPassword from "../../../functions/dsql/hashPassword";
|
||||
import { DSQL_DATASQUIREL_USERS } from "../../../types/dsql";
|
||||
import grabDirNames from "../../../utils/backend/names/grab-dir-names";
|
||||
|
||||
const tmpDir = process.argv[process.argv.length - 1];
|
||||
|
||||
@@ -76,14 +78,14 @@ async function createUser() {
|
||||
data: { ...userObj, password: hashedPassword },
|
||||
});
|
||||
|
||||
if (!newUser?.insertId) return false;
|
||||
if (!newUser?.payload?.insertId) return false;
|
||||
|
||||
/**
|
||||
* Add a Mariadb User for this User
|
||||
*/
|
||||
await addMariadbUser({ userId: newUser.insertId });
|
||||
await addMariadbUser({ userId: newUser.payload.insertId });
|
||||
|
||||
const STATIC_ROOT = process.env.DSQL_STATIC_SERVER_DIR;
|
||||
const { STATIC_ROOT } = grabDirNames();
|
||||
|
||||
if (!STATIC_ROOT) {
|
||||
console.log("Static File ENV not Found!");
|
||||
@@ -95,10 +97,10 @@ async function createUser() {
|
||||
*
|
||||
* @description Create new user folder and file
|
||||
*/
|
||||
let newUserSchemaFolderPath = `${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${newUser.insertId}`;
|
||||
let newUserSchemaFolderPath = `${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${newUser.payload.insertId}`;
|
||||
let newUserMediaFolderPath = path.join(
|
||||
STATIC_ROOT,
|
||||
`images/user-images/user-${newUser.insertId}`
|
||||
`images/user-images/user-${newUser.payload.insertId}`
|
||||
);
|
||||
|
||||
fs.mkdirSync(newUserSchemaFolderPath, { recursive: true });
|
||||
@@ -112,7 +114,7 @@ async function createUser() {
|
||||
|
||||
const imageBasePath = path.join(
|
||||
STATIC_ROOT,
|
||||
`images/user-images/user-${newUser.insertId}`
|
||||
`images/user-images/user-${newUser.payload.insertId}`
|
||||
);
|
||||
|
||||
if (!fs.existsSync(imageBasePath)) {
|
||||
@@ -121,12 +123,12 @@ async function createUser() {
|
||||
|
||||
let imagePath = path.join(
|
||||
STATIC_ROOT,
|
||||
`images/user-images/user-${newUser.insertId}/user-${newUser.insertId}-profile.jpg`
|
||||
`images/user-images/user-${newUser.payload.insertId}/user-${newUser.payload.insertId}-profile.jpg`
|
||||
);
|
||||
|
||||
let imageThumbnailPath = path.join(
|
||||
STATIC_ROOT,
|
||||
`images/user-images/user-${newUser.insertId}/user-${newUser.insertId}-profile-thumbnail.jpg`
|
||||
`images/user-images/user-${newUser.payload.insertId}/user-${newUser.payload.insertId}-profile-thumbnail.jpg`
|
||||
);
|
||||
|
||||
let prodImageUrl = imagePath.replace(
|
||||
@@ -149,11 +151,11 @@ async function createUser() {
|
||||
|
||||
execSync(`chmod 644 ${imagePath} ${imageThumbnailPath}`);
|
||||
|
||||
const updateImages = await updateDbEntry({
|
||||
const updateImages = await updateDbEntry<DSQL_DATASQUIREL_USERS>({
|
||||
dbFullName: "datasquirel",
|
||||
tableName: "users",
|
||||
identifierColumnName: "id",
|
||||
identifierValue: newUser.insertId,
|
||||
identifierValue: newUser.payload.insertId,
|
||||
data: {
|
||||
image: prodImageUrl,
|
||||
image_thumbnail: prodImageThumbnailUrl,
|
||||
|
||||
@@ -1,80 +0,0 @@
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
import generator from "generate-password";
|
||||
import noDatabaseDbHandler from "./utils/noDatabaseDbHandler";
|
||||
import dbHandler from "./utils/dbHandler";
|
||||
import encrypt from "../functions/dsql/encrypt";
|
||||
|
||||
/**
|
||||
* Create database from Schema Function
|
||||
* ==============================================================================
|
||||
* @param {object} params - Single object params
|
||||
* @param {number|string|null} params.userId - User ID or null
|
||||
*/
|
||||
async function resetSQLCredentials() {
|
||||
const users = (await dbHandler({
|
||||
query: `SELECT * FROM users`,
|
||||
})) as any[];
|
||||
|
||||
if (!users) {
|
||||
process.exit();
|
||||
}
|
||||
|
||||
for (let i = 0; i < users.length; i++) {
|
||||
const user = users[i];
|
||||
|
||||
if (!user) continue;
|
||||
|
||||
const defaultMariadbUserHost = process.env.DSQL_DB_HOST || "127.0.0.1";
|
||||
|
||||
try {
|
||||
const username = `dsql_user_${user.id}`;
|
||||
const password = generator.generate({
|
||||
length: 16,
|
||||
numbers: true,
|
||||
symbols: true,
|
||||
uppercase: true,
|
||||
exclude: "*#.'`\"",
|
||||
});
|
||||
const encryptedPassword = encrypt({ data: password });
|
||||
|
||||
await noDatabaseDbHandler(`DROP USER IF EXISTS '${username}'@'%'`);
|
||||
await noDatabaseDbHandler(
|
||||
`DROP USER IF EXISTS '${username}'@'${defaultMariadbUserHost}'`
|
||||
);
|
||||
|
||||
await noDatabaseDbHandler(
|
||||
`CREATE USER IF NOT EXISTS '${username}'@'${defaultMariadbUserHost}' IDENTIFIED BY '${password}'`
|
||||
);
|
||||
|
||||
await noDatabaseDbHandler(
|
||||
`GRANT ALL PRIVILEGES ON \`datasquirel_user_${user.id}_%\`.* TO '${username}'@'${defaultMariadbUserHost}'`
|
||||
);
|
||||
|
||||
await noDatabaseDbHandler(`FLUSH PRIVILEGES`);
|
||||
|
||||
const updateUser = await dbHandler({
|
||||
query: `UPDATE users SET mariadb_user = ?, mariadb_host = ?, mariadb_pass = ? WHERE id = ?`,
|
||||
values: [
|
||||
username,
|
||||
defaultMariadbUserHost,
|
||||
encryptedPassword,
|
||||
user.id,
|
||||
],
|
||||
});
|
||||
|
||||
console.log(
|
||||
`User ${user.id}: ${user.first_name} ${user.last_name} SQL credentials successfully added.`
|
||||
);
|
||||
} catch (error: any) {
|
||||
global.ERROR_CALLBACK?.(
|
||||
`Error Resetting SQL credentials`,
|
||||
error as Error
|
||||
);
|
||||
console.log(`Error in adding SQL user =>`, error.message);
|
||||
}
|
||||
}
|
||||
|
||||
process.exit();
|
||||
}
|
||||
|
||||
resetSQLCredentials();
|
||||
@@ -1,8 +1,9 @@
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
import generator from "generate-password";
|
||||
import noDatabaseDbHandler from "./utils/noDatabaseDbHandler";
|
||||
import dbHandler from "./utils/dbHandler";
|
||||
import dbHandler from "../functions/backend/dbHandler";
|
||||
import encrypt from "../functions/dsql/encrypt";
|
||||
import grabSQLKeyName from "../utils/grab-sql-key-name";
|
||||
|
||||
/**
|
||||
* # Create database from Schema Function
|
||||
@@ -24,7 +25,7 @@ async function resetSQLCredentialsPasswords() {
|
||||
const defaultMariadbUserHost = process.env.DSQL_DB_HOST || "127.0.0.1";
|
||||
|
||||
try {
|
||||
const username = `dsql_user_${user.id}`;
|
||||
const username = grabSQLKeyName({ type: "user", userId: user.id });
|
||||
const password = generator.generate({
|
||||
length: 16,
|
||||
numbers: true,
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
import generator from "generate-password";
|
||||
import noDatabaseDbHandler from "./utils/noDatabaseDbHandler";
|
||||
import dbHandler from "./utils/dbHandler";
|
||||
import dbHandler from "../functions/backend/dbHandler";
|
||||
import encrypt from "../functions/dsql/encrypt";
|
||||
import grabSQLKeyName from "../utils/grab-sql-key-name";
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
@@ -32,7 +33,7 @@ async function setSQLCredentials() {
|
||||
}
|
||||
|
||||
try {
|
||||
const username = `dsql_user_${user.id}`;
|
||||
const username = grabSQLKeyName({ type: "user", userId: user.id });
|
||||
const password = generator.generate({
|
||||
length: 16,
|
||||
numbers: true,
|
||||
|
||||
@@ -1,21 +1,9 @@
|
||||
// @ts-check
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
import generator from "generate-password";
|
||||
import noDatabaseDbHandler from "./utils/noDatabaseDbHandler";
|
||||
import dbHandler from "./utils/dbHandler";
|
||||
import dbHandler from "../functions/backend/dbHandler";
|
||||
import encrypt from "../functions/dsql/encrypt";
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
import grabSQLKeyName from "../utils/grab-sql-key-name";
|
||||
|
||||
/**
|
||||
* # Test SQL Escape
|
||||
@@ -37,7 +25,7 @@ export default async function testSQLEscape() {
|
||||
const defaultMariadbUserHost = process.env.DSQL_DB_HOST || "127.0.0.1";
|
||||
|
||||
try {
|
||||
const username = `dsql_user_${user.id}`;
|
||||
const username = grabSQLKeyName({ type: "user", userId: user.id });
|
||||
const password = generator.generate({
|
||||
length: 16,
|
||||
numbers: true,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
import mysql from "serverless-mysql";
|
||||
import grabDSQLConnection from "../utils/grab-dsql-connection";
|
||||
import grabSQLKeyName from "../utils/grab-sql-key-name";
|
||||
|
||||
/**
|
||||
* # Main DB Handler Function
|
||||
@@ -28,7 +29,7 @@ import grabDSQLConnection from "../utils/grab-dsql-connection";
|
||||
if (
|
||||
user.User !== process.env.DSQL_DB_READ_ONLY_USERNAME ||
|
||||
user.User !== process.env.DSQL_DB_FULL_ACCESS_USERNAME ||
|
||||
!user.User?.match(/dsql_user_.*/i)
|
||||
!user.User?.match(new RegExp(grabSQLKeyName({ type: "user" })))
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
import varDatabaseDbHandler from "./varDatabaseDbHandler";
|
||||
import { DSQL_TableSchemaType } from "../../types";
|
||||
import {
|
||||
DSQL_DATASQUIREL_USER_DATABASE_TABLES,
|
||||
DSQL_DATASQUIREL_USER_DATABASES,
|
||||
} from "../../types/dsql";
|
||||
import numberfy from "../../utils/numberfy";
|
||||
import updateDbEntry from "../../functions/backend/db/updateDbEntry";
|
||||
import addDbEntry from "../../functions/backend/db/addDbEntry";
|
||||
import slugToNormalText from "../../utils/slug-to-normal-text";
|
||||
import debugLog from "../../utils/logging/debug-log";
|
||||
import _ from "lodash";
|
||||
|
||||
type Param = {
|
||||
tableSchema?: DSQL_TableSchemaType;
|
||||
recordedDbEntry?: DSQL_DATASQUIREL_USER_DATABASES;
|
||||
update?: boolean;
|
||||
isMain?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* # Handle Table Record Update and Insert
|
||||
*/
|
||||
export default async function ({
|
||||
tableSchema,
|
||||
recordedDbEntry,
|
||||
update,
|
||||
isMain,
|
||||
}: Param): Promise<number | undefined> {
|
||||
if (isMain) return undefined;
|
||||
|
||||
let tableId: number | undefined;
|
||||
|
||||
const targetDatabase = "datasquirel";
|
||||
const targetTableName = "user_database_tables";
|
||||
|
||||
if (!tableSchema?.tableName) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const newTableSchema = _.cloneDeep(tableSchema);
|
||||
|
||||
try {
|
||||
if (!recordedDbEntry) {
|
||||
throw new Error("Recorded Db entry not found!");
|
||||
}
|
||||
|
||||
// const existingTableName = newTableSchema.tableNameOld
|
||||
// ? newTableSchema.tableNameOld
|
||||
// : newTableSchema.tableName;
|
||||
|
||||
const newTableEntry: DSQL_DATASQUIREL_USER_DATABASE_TABLES = {
|
||||
user_id: recordedDbEntry.user_id,
|
||||
db_id: recordedDbEntry.id,
|
||||
db_slug: recordedDbEntry.db_slug,
|
||||
table_name: slugToNormalText(newTableSchema.tableName),
|
||||
table_slug: newTableSchema.tableName,
|
||||
child_table: newTableSchema.childTable ? 1 : 0,
|
||||
child_table_parent_database_schema_id: newTableSchema.childTableDbId
|
||||
? numberfy(newTableSchema.childTableDbId)
|
||||
: 0,
|
||||
child_table_parent_table_schema_id: newTableSchema.childTableId
|
||||
? numberfy(newTableSchema.childTableId)
|
||||
: 0,
|
||||
table_schema_id: newTableSchema.id
|
||||
? numberfy(newTableSchema.id)
|
||||
: 0,
|
||||
active_data: newTableSchema.updateData ? 1 : 0,
|
||||
};
|
||||
|
||||
const existingTable = await varDatabaseDbHandler({
|
||||
queryString: `SELECT * FROM ${targetDatabase}.${targetTableName} WHERE db_id = ? AND table_slug = ?`,
|
||||
queryValuesArray: [
|
||||
String(recordedDbEntry.id),
|
||||
String(newTableSchema.tableName),
|
||||
],
|
||||
});
|
||||
|
||||
const table: DSQL_DATASQUIREL_USER_DATABASE_TABLES = existingTable?.[0];
|
||||
|
||||
if (table?.id) {
|
||||
tableId = table.id;
|
||||
if (update) {
|
||||
await updateDbEntry<DSQL_DATASQUIREL_USER_DATABASE_TABLES>({
|
||||
data: newTableEntry,
|
||||
identifierColumnName: "id",
|
||||
identifierValue: table.id,
|
||||
tableName: targetTableName,
|
||||
dbFullName: targetDatabase,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
const newTableEntryRes =
|
||||
await addDbEntry<DSQL_DATASQUIREL_USER_DATABASE_TABLES>({
|
||||
data: newTableEntry,
|
||||
tableName: targetTableName,
|
||||
dbFullName: targetDatabase,
|
||||
});
|
||||
|
||||
if (newTableEntryRes?.payload?.insertId) {
|
||||
tableId = newTableEntryRes.payload.insertId;
|
||||
}
|
||||
}
|
||||
|
||||
if (newTableSchema.tableNameOld) {
|
||||
}
|
||||
|
||||
return tableId;
|
||||
} catch (error) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,18 @@
|
||||
import varDatabaseDbHandler from "./varDatabaseDbHandler";
|
||||
import generateColumnDescription from "./generateColumnDescription";
|
||||
import supplementTable from "./supplementTable";
|
||||
import dbHandler from "./dbHandler";
|
||||
import { DSQL_DatabaseSchemaType, DSQL_TableSchemaType } from "../../types";
|
||||
import { DSQL_FieldSchemaType, DSQL_TableSchemaType } from "../../types";
|
||||
import { DSQL_DATASQUIREL_USER_DATABASES } from "../../types/dsql";
|
||||
import handleTableForeignKey from "./handle-table-foreign-key";
|
||||
import createTableHandleTableRecord from "./create-table-handle-table-record";
|
||||
|
||||
type Param = {
|
||||
dbFullName: string;
|
||||
tableName: string;
|
||||
tableInfoArray: any[];
|
||||
tableInfoArray: DSQL_FieldSchemaType[];
|
||||
tableSchema?: DSQL_TableSchemaType;
|
||||
recordedDbEntry?: any;
|
||||
recordedDbEntry?: DSQL_DATASQUIREL_USER_DATABASES;
|
||||
isMain?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -21,110 +24,28 @@ export default async function createTable({
|
||||
tableInfoArray,
|
||||
tableSchema,
|
||||
recordedDbEntry,
|
||||
isMain,
|
||||
}: Param) {
|
||||
/**
|
||||
* Format tableInfoArray
|
||||
*
|
||||
* @description Format tableInfoArray
|
||||
*/
|
||||
const finalTable = supplementTable({ tableInfoArray: tableInfoArray });
|
||||
|
||||
/**
|
||||
* Grab Schema
|
||||
*
|
||||
* @description Grab Schema
|
||||
*/
|
||||
let tableId = await createTableHandleTableRecord({
|
||||
recordedDbEntry,
|
||||
tableSchema,
|
||||
isMain,
|
||||
});
|
||||
|
||||
if (!tableId && !isMain) throw new Error(`Couldn't grab table ID`);
|
||||
|
||||
const createTableQueryArray = [];
|
||||
|
||||
createTableQueryArray.push(
|
||||
`CREATE TABLE IF NOT EXISTS \`${dbFullName}\`.\`${tableName}\` (`
|
||||
);
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
try {
|
||||
if (!recordedDbEntry) {
|
||||
throw new Error("Recorded Db entry not found!");
|
||||
}
|
||||
|
||||
const existingTable = await varDatabaseDbHandler({
|
||||
queryString: `SELECT * FROM datasquirel.user_database_tables WHERE db_id = ? AND table_slug = ?`,
|
||||
queryValuesArray: [recordedDbEntry.id, tableSchema?.tableName],
|
||||
});
|
||||
|
||||
/** @type {import("../../types").MYSQL_user_database_tables_table_def} */
|
||||
const table: import("../../types").MYSQL_user_database_tables_table_def =
|
||||
existingTable?.[0];
|
||||
|
||||
if (!table?.id) {
|
||||
const newTableEntry = await dbHandler({
|
||||
query: `INSERT INTO datasquirel.user_database_tables SET ?`,
|
||||
values: {
|
||||
user_id: recordedDbEntry.user_id,
|
||||
db_id: recordedDbEntry.id,
|
||||
db_slug: recordedDbEntry.db_slug,
|
||||
table_name: tableSchema?.tableFullName,
|
||||
table_slug: tableSchema?.tableName,
|
||||
child_table: tableSchema?.childTable ? "1" : null,
|
||||
child_table_parent_database:
|
||||
tableSchema?.childTableDbFullName || null,
|
||||
child_table_parent_table:
|
||||
tableSchema?.childTableName || null,
|
||||
date_created: Date(),
|
||||
date_created_code: Date.now(),
|
||||
date_updated: Date(),
|
||||
date_updated_code: Date.now(),
|
||||
},
|
||||
});
|
||||
}
|
||||
} catch (error) {}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
let primaryKeySet = false;
|
||||
|
||||
/** @type {import("../../types").DSQL_FieldSchemaType[]} */
|
||||
let foreignKeys: import("../../types").DSQL_FieldSchemaType[] = [];
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
for (let i = 0; i < finalTable.length; i++) {
|
||||
const column = finalTable[i];
|
||||
const {
|
||||
fieldName,
|
||||
dataType,
|
||||
nullValue,
|
||||
primaryKey,
|
||||
autoIncrement,
|
||||
defaultValue,
|
||||
defaultValueLiteral,
|
||||
foreignKey,
|
||||
updatedField,
|
||||
onUpdate,
|
||||
onUpdateLiteral,
|
||||
onDelete,
|
||||
onDeleteLiteral,
|
||||
defaultField,
|
||||
encrypted,
|
||||
json,
|
||||
newTempField,
|
||||
notNullValue,
|
||||
originName,
|
||||
plainText,
|
||||
pattern,
|
||||
patternFlags,
|
||||
richText,
|
||||
} = column;
|
||||
|
||||
if (foreignKey) {
|
||||
foreignKeys.push({
|
||||
...column,
|
||||
});
|
||||
}
|
||||
|
||||
let { fieldEntryText, newPrimaryKeySet } = generateColumnDescription({
|
||||
columnData: column,
|
||||
@@ -133,56 +54,39 @@ export default async function createTable({
|
||||
|
||||
primaryKeySet = newPrimaryKeySet;
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
const comma = (() => {
|
||||
if (foreignKeys[0]) return ",";
|
||||
if (i === finalTable.length - 1) return "";
|
||||
return ",";
|
||||
})();
|
||||
|
||||
createTableQueryArray.push(" " + fieldEntryText + comma);
|
||||
|
||||
////////////////////////////////////////
|
||||
}
|
||||
|
||||
if (foreignKeys[0]) {
|
||||
foreignKeys.forEach((foreighKey, index, array) => {
|
||||
const fieldName = foreighKey.fieldName;
|
||||
const destinationTableName =
|
||||
foreighKey.foreignKey?.destinationTableName;
|
||||
const destinationTableColumnName =
|
||||
foreighKey.foreignKey?.destinationTableColumnName;
|
||||
const cascadeDelete = foreighKey.foreignKey?.cascadeDelete;
|
||||
const cascadeUpdate = foreighKey.foreignKey?.cascadeUpdate;
|
||||
const foreignKeyName = foreighKey.foreignKey?.foreignKeyName;
|
||||
|
||||
const comma = (() => {
|
||||
if (index === foreignKeys.length - 1) return "";
|
||||
return ",";
|
||||
})();
|
||||
|
||||
createTableQueryArray.push(
|
||||
` CONSTRAINT \`${foreignKeyName}\` FOREIGN KEY (\`${fieldName}\`) REFERENCES \`${destinationTableName}\`(${destinationTableColumnName})${
|
||||
cascadeDelete ? " ON DELETE CASCADE" : ""
|
||||
}${cascadeUpdate ? " ON UPDATE CASCADE" : ""}${comma}`
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
createTableQueryArray.push(
|
||||
`) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;`
|
||||
);
|
||||
|
||||
const createTableQuery = createTableQueryArray.join("\n");
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
const newTable = await varDatabaseDbHandler({
|
||||
queryString: createTableQuery,
|
||||
});
|
||||
|
||||
return newTable;
|
||||
for (let i = 0; i < finalTable.length; i++) {
|
||||
const column = finalTable[i];
|
||||
const { foreignKey, fieldName } = column;
|
||||
|
||||
if (!fieldName) continue;
|
||||
|
||||
if (foreignKey) {
|
||||
await handleTableForeignKey({
|
||||
dbFullName,
|
||||
foreignKey,
|
||||
tableName,
|
||||
fieldName,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return tableId;
|
||||
}
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import grabDSQLConnection from "../../utils/grab-dsql-connection";
|
||||
|
||||
type Param = {
|
||||
query: string;
|
||||
values?: string[] | object;
|
||||
};
|
||||
|
||||
/**
|
||||
* # Main DB Handler Function
|
||||
* @requires DSQL_DB_CONN - Gobal Variable for Datasquirel Database
|
||||
*/
|
||||
export default async function dbHandler({
|
||||
query,
|
||||
values,
|
||||
}: Param): Promise<any[] | object | null> {
|
||||
const CONNECTION = grabDSQLConnection();
|
||||
|
||||
let results;
|
||||
|
||||
try {
|
||||
if (query && values) {
|
||||
results = await CONNECTION.query(query, values);
|
||||
} else {
|
||||
results = await CONNECTION.query(query);
|
||||
}
|
||||
} catch (error: any) {
|
||||
global.ERROR_CALLBACK?.(`DB Handler Error...`, error as Error);
|
||||
|
||||
if (process.env.FIRST_RUN) {
|
||||
return null;
|
||||
}
|
||||
|
||||
console.log("ERROR in dbHandler =>", error.message);
|
||||
console.log(error);
|
||||
console.log(CONNECTION.config());
|
||||
|
||||
const tmpFolder = path.resolve(process.cwd(), "./.tmp");
|
||||
if (!fs.existsSync(tmpFolder))
|
||||
fs.mkdirSync(tmpFolder, { recursive: true });
|
||||
|
||||
fs.appendFileSync(
|
||||
path.resolve(tmpFolder, "./dbErrorLogs.txt"),
|
||||
JSON.stringify(error, null, 4) + "\n" + Date() + "\n\n\n",
|
||||
"utf8"
|
||||
);
|
||||
results = null;
|
||||
} finally {
|
||||
await CONNECTION?.end();
|
||||
}
|
||||
|
||||
if (results) {
|
||||
return JSON.parse(JSON.stringify(results));
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
import grabSQLKeyName from "../../utils/grab-sql-key-name";
|
||||
import varDatabaseDbHandler from "./varDatabaseDbHandler";
|
||||
|
||||
type Param = {
|
||||
dbFullName: string;
|
||||
tableName: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* # Drop All Foreign Keys
|
||||
*/
|
||||
export default async function dropAllForeignKeys({
|
||||
dbFullName,
|
||||
tableName,
|
||||
}: Param) {
|
||||
try {
|
||||
// const rows = await varDatabaseDbHandler({
|
||||
// queryString: `SELECT CONSTRAINT_NAME FROM information_schema.REFERENTIAL_CONSTRAINTS WHERE TABLE_NAME = '${tableName}' AND CONSTRAINT_SCHEMA = '${dbFullName}'`,
|
||||
// });
|
||||
|
||||
// console.log("rows", rows);
|
||||
// console.log("dbFullName", dbFullName);
|
||||
// console.log("tableName", tableName);
|
||||
|
||||
// for (const row of rows) {
|
||||
// await varDatabaseDbHandler({
|
||||
// queryString: `ALTER TABLE \`${dbFullName}\`.\`${tableName}\` DROP FOREIGN KEY \`${row.CONSTRAINT_NAME}\`
|
||||
// `,
|
||||
// });
|
||||
// }
|
||||
|
||||
const foreignKeys = await varDatabaseDbHandler({
|
||||
queryString: `SHOW INDEXES FROM \`${dbFullName}\`.\`${tableName}\` WHERE Key_name LIKE '${grabSQLKeyName(
|
||||
{ type: "foreign_key" }
|
||||
)}%'`,
|
||||
});
|
||||
|
||||
for (const fk of foreignKeys) {
|
||||
if (
|
||||
fk.Key_name.match(
|
||||
new RegExp(grabSQLKeyName({ type: "foreign_key" }))
|
||||
)
|
||||
) {
|
||||
await varDatabaseDbHandler({
|
||||
queryString: `ALTER TABLE \`${dbFullName}\`.\`${tableName}\` DROP INDEX \`${fk.Key_name}\`
|
||||
`,
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.log(`dropAllForeignKeys ERROR => ${error.message}`);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,9 @@
|
||||
import { DSQL_FieldSchemaType } from "../../types";
|
||||
import dataTypeConstructor from "../../utils/db/schema/data-type-constructor";
|
||||
import dataTypeParser from "../../utils/db/schema/data-type-parser";
|
||||
|
||||
type Param = {
|
||||
columnData: import("../../types").DSQL_FieldSchemaType;
|
||||
columnData: DSQL_FieldSchemaType;
|
||||
primaryKeySet?: boolean;
|
||||
};
|
||||
|
||||
@@ -15,11 +19,6 @@ export default function generateColumnDescription({
|
||||
columnData,
|
||||
primaryKeySet,
|
||||
}: Param): Return {
|
||||
/**
|
||||
* Format tableInfoArray
|
||||
*
|
||||
* @description Format tableInfoArray
|
||||
*/
|
||||
const {
|
||||
fieldName,
|
||||
dataType,
|
||||
@@ -30,13 +29,19 @@ export default function generateColumnDescription({
|
||||
defaultValueLiteral,
|
||||
onUpdateLiteral,
|
||||
notNullValue,
|
||||
unique,
|
||||
} = columnData;
|
||||
|
||||
let fieldEntryText = "";
|
||||
|
||||
fieldEntryText += `\`${fieldName}\` ${dataType}`;
|
||||
const finalDataTypeObject = dataTypeParser(dataType);
|
||||
const finalDataType = dataTypeConstructor(
|
||||
finalDataTypeObject.type,
|
||||
finalDataTypeObject.limit,
|
||||
finalDataTypeObject.decimal
|
||||
);
|
||||
|
||||
////////////////////////////////////////
|
||||
fieldEntryText += `\`${fieldName}\` ${finalDataType}`;
|
||||
|
||||
if (nullValue) {
|
||||
fieldEntryText += " DEFAULT NULL";
|
||||
@@ -46,35 +51,32 @@ export default function generateColumnDescription({
|
||||
if (String(defaultValue).match(/uuid\(\)/i)) {
|
||||
fieldEntryText += ` DEFAULT UUID()`;
|
||||
} else {
|
||||
fieldEntryText += ` DEFAULT '${defaultValue}'`;
|
||||
fieldEntryText += ` DEFAULT '${String(defaultValue)
|
||||
.replace(/^\'|\'$/g, "")
|
||||
.replace(/\'/g, "\\'")}'`;
|
||||
}
|
||||
} else if (notNullValue) {
|
||||
fieldEntryText += ` NOT NULL`;
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
if (onUpdateLiteral) {
|
||||
fieldEntryText += ` ON UPDATE ${onUpdateLiteral}`;
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
if (primaryKey && !primaryKeySet) {
|
||||
fieldEntryText += " PRIMARY KEY";
|
||||
primaryKeySet = true;
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
if (autoIncrement) {
|
||||
fieldEntryText += " AUTO_INCREMENT";
|
||||
primaryKeySet = true;
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
if (unique) {
|
||||
fieldEntryText += " UNIQUE";
|
||||
primaryKeySet = true;
|
||||
}
|
||||
|
||||
return {
|
||||
fieldEntryText,
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
export default function grabDSQLSchemaIndexComment() {
|
||||
return `dsql_schema_index`;
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
import varDatabaseDbHandler from "./varDatabaseDbHandler";
|
||||
import { DSQL_ForeignKeyType } from "../../types";
|
||||
|
||||
type Param = {
|
||||
dbFullName: string;
|
||||
tableName: string;
|
||||
foreignKey: DSQL_ForeignKeyType;
|
||||
fieldName: string;
|
||||
errorLogs?: any[];
|
||||
};
|
||||
|
||||
/**
|
||||
* # Update table function
|
||||
*/
|
||||
export default async function handleTableForeignKey({
|
||||
dbFullName,
|
||||
tableName,
|
||||
foreignKey,
|
||||
errorLogs,
|
||||
fieldName,
|
||||
}: Param) {
|
||||
const {
|
||||
destinationTableName,
|
||||
destinationTableColumnName,
|
||||
cascadeDelete,
|
||||
cascadeUpdate,
|
||||
foreignKeyName,
|
||||
} = foreignKey;
|
||||
|
||||
let finalQueryString = `ALTER TABLE \`${dbFullName}\`.\`${tableName}\``;
|
||||
|
||||
finalQueryString += ` ADD CONSTRAINT \`${foreignKeyName}\` FOREIGN KEY (\`${fieldName}\`)`;
|
||||
finalQueryString += ` REFERENCES \`${destinationTableName}\`(\`${destinationTableColumnName}\`)`;
|
||||
|
||||
if (cascadeDelete) finalQueryString += ` ON DELETE CASCADE`;
|
||||
if (cascadeUpdate) finalQueryString += ` ON UPDATE CASCADE`;
|
||||
|
||||
// let foreinKeyText = `ADD CONSTRAINT \`${foreignKeyName}\` FOREIGN KEY (\`${destinationTableColumnType}\`) REFERENCES \`${destinationTableName}\`(\`${destinationTableColumnName}\`)${
|
||||
// cascadeDelete ? " ON DELETE CASCADE" : ""
|
||||
// }${cascadeUpdate ? " ON UPDATE CASCADE" : ""}`;
|
||||
|
||||
// let finalQueryString = `ALTER TABLE \`${dbFullName}\`.\`${tableName}\` ${foreinKeyText}`;
|
||||
|
||||
const addForeignKey = await varDatabaseDbHandler({
|
||||
queryString: finalQueryString,
|
||||
});
|
||||
|
||||
if (!addForeignKey?.serverStatus) {
|
||||
errorLogs?.push(addForeignKey);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import dbHandler from "./dbHandler";
|
||||
import dbHandler from "../../functions/backend/dbHandler";
|
||||
|
||||
export default async function noDatabaseDbHandler(
|
||||
queryString: string
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,4 @@
|
||||
import dbHandler from "./dbHandler";
|
||||
import { DSQL_TableSchemaType } from "../../types";
|
||||
import dbHandler from "../../functions/backend/dbHandler";
|
||||
|
||||
type Param = {
|
||||
queryString: string;
|
||||
|
||||
Reference in New Issue
Block a user