Updates
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
import varDatabaseDbHandler from "../utils/varDatabaseDbHandler";
|
||||
import { DSQL_DatabaseSchemaType, PostInsertReturn } from "../../types";
|
||||
import { DSQL_DATASQUIREL_USER_DATABASES } from "../../types/dsql";
|
||||
import numberfy from "../../utils/numberfy";
|
||||
import addDbEntry from "../../functions/backend/db/addDbEntry";
|
||||
|
||||
type Param = {
|
||||
userId?: number | string | null;
|
||||
dbSchema: DSQL_DatabaseSchemaType;
|
||||
};
|
||||
|
||||
/**
|
||||
* # Create database from Schema Function
|
||||
* @requires DSQL_DB_CONN - Gobal Variable for Datasquirel Database
|
||||
*/
|
||||
export default async function checkDbRecordCreateDbSchema({
|
||||
userId,
|
||||
dbSchema,
|
||||
}: Param): Promise<DSQL_DATASQUIREL_USER_DATABASES | undefined> {
|
||||
try {
|
||||
const {
|
||||
dbFullName,
|
||||
dbName,
|
||||
dbSlug,
|
||||
dbDescription,
|
||||
dbImage,
|
||||
childDatabase,
|
||||
childDatabaseDbFullName,
|
||||
} = dbSchema;
|
||||
|
||||
let recordedDbEntryArray = userId
|
||||
? await varDatabaseDbHandler({
|
||||
queryString: `SELECT * FROM datasquirel.user_databases WHERE db_full_name = ?`,
|
||||
queryValuesArray: [dbFullName],
|
||||
})
|
||||
: undefined;
|
||||
|
||||
let recordedDbEntry: DSQL_DATASQUIREL_USER_DATABASES | undefined =
|
||||
recordedDbEntryArray?.[0];
|
||||
|
||||
if (!recordedDbEntry?.id && userId) {
|
||||
const newDbEntryObj: DSQL_DATASQUIREL_USER_DATABASES = {
|
||||
user_id: numberfy(userId),
|
||||
db_name: dbName,
|
||||
db_slug: dbSlug,
|
||||
db_full_name: dbFullName,
|
||||
db_description: dbDescription,
|
||||
db_image: dbImage,
|
||||
active_clone: childDatabase ? 1 : undefined,
|
||||
active_clone_parent_db: childDatabaseDbFullName,
|
||||
};
|
||||
|
||||
const newDbEntry = (await addDbEntry({
|
||||
data: newDbEntryObj,
|
||||
tableName: "user_databases",
|
||||
forceLocal: true,
|
||||
})) as PostInsertReturn;
|
||||
|
||||
if (newDbEntry.insertId) {
|
||||
recordedDbEntryArray = await varDatabaseDbHandler({
|
||||
queryString: `SELECT * FROM datasquirel.user_databases WHERE db_full_name = ?`,
|
||||
queryValuesArray: [dbFullName],
|
||||
});
|
||||
recordedDbEntry = recordedDbEntryArray?.[0];
|
||||
}
|
||||
}
|
||||
|
||||
return recordedDbEntry;
|
||||
} catch (error) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
import varDatabaseDbHandler from "../utils/varDatabaseDbHandler";
|
||||
import dbHandler from "../utils/dbHandler";
|
||||
import {
|
||||
DSQL_DatabaseSchemaType,
|
||||
DSQL_TableSchemaType,
|
||||
PostInsertReturn,
|
||||
} from "../../types";
|
||||
import {
|
||||
DSQL_DATASQUIREL_USER_DATABASE_TABLES,
|
||||
DSQL_DATASQUIREL_USER_DATABASES,
|
||||
DsqlTables,
|
||||
} from "../../types/dsql";
|
||||
import sqlGenerator from "../../functions/dsql/sql/sql-generator";
|
||||
import numberfy from "../../utils/numberfy";
|
||||
import addDbEntry from "../../functions/backend/db/addDbEntry";
|
||||
|
||||
type Param = {
|
||||
userId?: number | string | null;
|
||||
tableSchema?: DSQL_TableSchemaType;
|
||||
dbSchema: DSQL_DatabaseSchemaType[];
|
||||
dbRecord?: DSQL_DATASQUIREL_USER_DATABASES;
|
||||
dbFullName: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* # Create database from Schema Function
|
||||
* @requires DSQL_DB_CONN - Gobal Variable for Datasquirel Database
|
||||
*/
|
||||
export default async function checkTableRecordCreateDbSchema({
|
||||
userId,
|
||||
tableSchema,
|
||||
dbSchema,
|
||||
dbRecord,
|
||||
dbFullName,
|
||||
}: Param): Promise<DSQL_DATASQUIREL_USER_DATABASE_TABLES | undefined> {
|
||||
if (!tableSchema) return undefined;
|
||||
|
||||
try {
|
||||
const queryObj = sqlGenerator<DSQL_DATASQUIREL_USER_DATABASE_TABLES>({
|
||||
tableName: "user_database_tables" as (typeof DsqlTables)[number],
|
||||
genObject: {
|
||||
query: {
|
||||
db_id: {
|
||||
value: String(dbRecord?.id),
|
||||
},
|
||||
table_slug: {
|
||||
value: tableSchema.tableName,
|
||||
},
|
||||
user_id: {
|
||||
value: String(userId),
|
||||
},
|
||||
},
|
||||
},
|
||||
dbFullName: "datasquirel",
|
||||
});
|
||||
|
||||
let recordedTableEntryArray = userId
|
||||
? await varDatabaseDbHandler({
|
||||
queryString: queryObj?.string || "",
|
||||
queryValuesArray: queryObj?.values,
|
||||
})
|
||||
: undefined;
|
||||
|
||||
let recordedTableEntry:
|
||||
| DSQL_DATASQUIREL_USER_DATABASE_TABLES
|
||||
| undefined = recordedTableEntryArray?.[0];
|
||||
|
||||
if (!recordedTableEntry?.id && userId) {
|
||||
const newTableInsertObject: DSQL_DATASQUIREL_USER_DATABASE_TABLES =
|
||||
{
|
||||
user_id: numberfy(userId),
|
||||
db_id: dbRecord?.id,
|
||||
db_slug: dbRecord?.db_slug,
|
||||
table_name: tableSchema.tableFullName,
|
||||
table_slug: tableSchema.tableName,
|
||||
};
|
||||
|
||||
if (tableSchema?.childTable && tableSchema.childTableName) {
|
||||
const parentDb = dbSchema.find(
|
||||
(db) => db.dbFullName == tableSchema.childTableDbFullName
|
||||
);
|
||||
const parentDbTable = parentDb?.tables.find(
|
||||
(tbl) => tbl.tableName == tableSchema.childTableName
|
||||
);
|
||||
if (parentDb && parentDbTable) {
|
||||
newTableInsertObject["child_table"] = 1;
|
||||
newTableInsertObject["child_table_parent_database"] =
|
||||
parentDb.dbFullName;
|
||||
newTableInsertObject["child_table_parent_table"] =
|
||||
parentDbTable.tableName;
|
||||
}
|
||||
}
|
||||
|
||||
const newTableRecordEntry = (await addDbEntry({
|
||||
data: newTableInsertObject,
|
||||
tableName: "user_database_tables",
|
||||
dbContext: "Master",
|
||||
forceLocal: true,
|
||||
})) as PostInsertReturn;
|
||||
|
||||
if (newTableRecordEntry.insertId) {
|
||||
recordedTableEntryArray = await varDatabaseDbHandler({
|
||||
queryString: queryObj?.string || "",
|
||||
queryValuesArray: queryObj?.values,
|
||||
});
|
||||
|
||||
recordedTableEntry = recordedTableEntryArray?.[0];
|
||||
}
|
||||
}
|
||||
|
||||
return recordedTableEntry;
|
||||
} catch (error) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
import varDatabaseDbHandler from "../utils/varDatabaseDbHandler";
|
||||
import { DSQL_IndexSchemaType } from "../../types";
|
||||
|
||||
type Param = {
|
||||
tableName: string;
|
||||
dbFullName: string;
|
||||
indexes: DSQL_IndexSchemaType[];
|
||||
};
|
||||
|
||||
/**
|
||||
* Handle DATASQUIREL Table Indexes
|
||||
* ===================================================
|
||||
* @description Iterate through each datasquirel schema
|
||||
* table index(if available), and perform operations
|
||||
*/
|
||||
export default async function handleIndexescreateDbFromSchema({
|
||||
dbFullName,
|
||||
tableName,
|
||||
indexes,
|
||||
}: Param) {
|
||||
for (let g = 0; g < indexes.length; g++) {
|
||||
const { indexType, indexName, indexTableFields, alias } = indexes[g];
|
||||
|
||||
if (!alias?.match(/./)) continue;
|
||||
|
||||
/**
|
||||
* @description Check for existing Index in MYSQL db
|
||||
*/
|
||||
try {
|
||||
/**
|
||||
* @type {import("../../types").DSQL_MYSQL_SHOW_INDEXES_Type[]}
|
||||
* @description All indexes from MYSQL db
|
||||
*/ // @ts-ignore
|
||||
const allExistingIndexes: import("../../types").DSQL_MYSQL_SHOW_INDEXES_Type[] =
|
||||
await varDatabaseDbHandler({
|
||||
queryString: `SHOW INDEXES FROM \`${dbFullName}\`.\`${tableName}\``,
|
||||
});
|
||||
|
||||
const existingKeyInDb = allExistingIndexes.filter(
|
||||
(indexObject) => indexObject.Key_name === alias
|
||||
);
|
||||
if (!existingKeyInDb[0])
|
||||
throw new Error("This Index Does not Exist");
|
||||
} catch (error) {
|
||||
/**
|
||||
* @description Create new index if determined that it
|
||||
* doesn't exist in MYSQL db
|
||||
*/
|
||||
await varDatabaseDbHandler({
|
||||
queryString: `CREATE${
|
||||
indexType?.match(/fullText/i) ? " FULLTEXT" : ""
|
||||
} INDEX \`${alias}\` ON \`${dbFullName}\`.\`${tableName}\`(${indexTableFields
|
||||
?.map((nm) => nm.value)
|
||||
.map((nm) => `\`${nm}\``)
|
||||
.join(",")}) COMMENT 'schema_index'`,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
+37
-91
@@ -1,18 +1,21 @@
|
||||
import fs from "fs";
|
||||
|
||||
import noDatabaseDbHandler from "./utils/noDatabaseDbHandler";
|
||||
import varDatabaseDbHandler from "./utils/varDatabaseDbHandler";
|
||||
import createTable from "./utils/createTable";
|
||||
import updateTable from "./utils/updateTable";
|
||||
import dbHandler from "./utils/dbHandler";
|
||||
import EJSON from "../utils/ejson";
|
||||
import { DSQL_DatabaseSchemaType } from "../types";
|
||||
import grabDirNames from "../utils/backend/names/grab-dir-names";
|
||||
import noDatabaseDbHandler from "../utils/noDatabaseDbHandler";
|
||||
import varDatabaseDbHandler from "../utils/varDatabaseDbHandler";
|
||||
import createTable from "../utils/createTable";
|
||||
import updateTable from "../utils/updateTable";
|
||||
import dbHandler from "../utils/dbHandler";
|
||||
import EJSON from "../../utils/ejson";
|
||||
import { DSQL_DatabaseSchemaType } from "../../types";
|
||||
import grabDirNames from "../../utils/backend/names/grab-dir-names";
|
||||
import checkDbRecordCreateDbSchema from "./check-db-record";
|
||||
import checkTableRecordCreateDbSchema from "./check-table-record";
|
||||
import handleIndexescreateDbFromSchema from "./handle-indexes";
|
||||
|
||||
type Param = {
|
||||
userId?: number | string | null;
|
||||
targetDatabase?: string;
|
||||
dbSchemaData?: import("../types").DSQL_DatabaseSchemaType[];
|
||||
dbSchemaData?: import("../../types").DSQL_DatabaseSchemaType[];
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -41,19 +44,15 @@ export default async function createDbFromSchema({
|
||||
return;
|
||||
}
|
||||
|
||||
// await createDatabasesFromSchema(dbSchema);
|
||||
|
||||
for (let i = 0; i < dbSchema.length; i++) {
|
||||
const database: DSQL_DatabaseSchemaType = dbSchema[i];
|
||||
|
||||
const { dbFullName, tables, dbName, dbSlug, childrenDatabases } =
|
||||
database;
|
||||
const { dbFullName, tables, dbSlug, childrenDatabases } = database;
|
||||
|
||||
if (targetDatabase && dbFullName != targetDatabase) {
|
||||
continue;
|
||||
}
|
||||
|
||||
/** @type {any} */
|
||||
const dbCheck: any = await noDatabaseDbHandler(
|
||||
`SELECT SCHEMA_NAME AS dbFullName FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '${dbFullName}'`
|
||||
);
|
||||
@@ -64,16 +63,14 @@ export default async function createDbFromSchema({
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Select all tables
|
||||
* @type {any}
|
||||
* @description Select All tables in target database
|
||||
*/
|
||||
const allTables: any = await noDatabaseDbHandler(
|
||||
`SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='${dbFullName}'`
|
||||
);
|
||||
|
||||
// let tableDropped;
|
||||
let recordedDbEntry = await checkDbRecordCreateDbSchema({
|
||||
dbSchema: database,
|
||||
userId,
|
||||
});
|
||||
|
||||
for (let tb = 0; tb < allTables.length; tb++) {
|
||||
const { TABLE_NAME } = allTables[tb];
|
||||
@@ -115,15 +112,6 @@ export default async function createDbFromSchema({
|
||||
}
|
||||
}
|
||||
|
||||
const recordedDbEntryArray = userId
|
||||
? await varDatabaseDbHandler({
|
||||
queryString: `SELECT * FROM datasquirel.user_databases WHERE db_full_name = ?`,
|
||||
queryValuesArray: [dbFullName],
|
||||
})
|
||||
: undefined;
|
||||
|
||||
const recordedDbEntry = recordedDbEntryArray?.[0];
|
||||
|
||||
/**
|
||||
* @description Iterate through each table and perform table actions
|
||||
*/
|
||||
@@ -189,11 +177,7 @@ export default async function createDbFromSchema({
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
} else {
|
||||
////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* @description Create new Table if table doesnt exist
|
||||
*/
|
||||
@@ -205,66 +189,28 @@ export default async function createDbFromSchema({
|
||||
recordedDbEntry,
|
||||
});
|
||||
|
||||
if (indexes && indexes[0]) {
|
||||
/**
|
||||
* Handle DATASQUIREL Table Indexes
|
||||
* ===================================================
|
||||
* @description Iterate through each datasquirel schema
|
||||
* table index(if available), and perform operations
|
||||
*/
|
||||
if (indexes && indexes[0]) {
|
||||
for (let g = 0; g < indexes.length; g++) {
|
||||
const {
|
||||
indexType,
|
||||
indexName,
|
||||
indexTableFields,
|
||||
alias,
|
||||
} = indexes[g];
|
||||
|
||||
if (!alias?.match(/./)) continue;
|
||||
|
||||
/**
|
||||
* @description Check for existing Index in MYSQL db
|
||||
*/
|
||||
try {
|
||||
/**
|
||||
* @type {import("../types").DSQL_MYSQL_SHOW_INDEXES_Type[]}
|
||||
* @description All indexes from MYSQL db
|
||||
*/ // @ts-ignore
|
||||
const allExistingIndexes: import("../types").DSQL_MYSQL_SHOW_INDEXES_Type[] =
|
||||
await varDatabaseDbHandler({
|
||||
queryString: `SHOW INDEXES FROM \`${dbFullName}\`.\`${tableName}\``,
|
||||
});
|
||||
|
||||
const existingKeyInDb =
|
||||
allExistingIndexes.filter(
|
||||
(indexObject) =>
|
||||
indexObject.Key_name === alias
|
||||
);
|
||||
if (!existingKeyInDb[0])
|
||||
throw new Error(
|
||||
"This Index Does not Exist"
|
||||
);
|
||||
} catch (error) {
|
||||
/**
|
||||
* @description Create new index if determined that it
|
||||
* doesn't exist in MYSQL db
|
||||
*/
|
||||
await varDatabaseDbHandler({
|
||||
queryString: `CREATE${
|
||||
indexType?.match(/fullText/i)
|
||||
? " FULLTEXT"
|
||||
: ""
|
||||
} INDEX \`${alias}\` ON \`${dbFullName}\`.\`${tableName}\`(${indexTableFields
|
||||
?.map((nm) => nm.value)
|
||||
.map((nm) => `\`${nm}\``)
|
||||
.join(",")}) COMMENT 'schema_index'`,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Handle DATASQUIREL Table Indexes
|
||||
* ===================================================
|
||||
* @description Iterate through each datasquirel schema
|
||||
* table index(if available), and perform operations
|
||||
*/
|
||||
if (indexes?.[0]) {
|
||||
handleIndexescreateDbFromSchema({
|
||||
dbFullName,
|
||||
indexes,
|
||||
tableName,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const tableRecord = await checkTableRecordCreateDbSchema({
|
||||
dbFullName,
|
||||
dbSchema,
|
||||
tableSchema: table,
|
||||
dbRecord: recordedDbEntry,
|
||||
userId,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -25,7 +25,7 @@ export default async function dbHandler({
|
||||
} else {
|
||||
results = await CONNECTION.query(query);
|
||||
}
|
||||
} catch (/** @type {any} */ error: any) {
|
||||
} catch (error: any) {
|
||||
if (process.env.FIRST_RUN) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user