This commit is contained in:
Benjamin Toby
2025-07-05 14:59:30 +01:00
parent 6e334c2525
commit 7e8bb37c09
526 changed files with 17560 additions and 11386 deletions
@@ -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}\``,
});
}
+219 -178
View File
@@ -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;
}