38 lines
1.9 KiB
JavaScript
38 lines
1.9 KiB
JavaScript
import varDatabaseDbHandler from "../utils/varDatabaseDbHandler";
|
|
import grabDSQLSchemaIndexComment from "../utils/grab-dsql-schema-index-comment";
|
|
/**
|
|
* 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, }) {
|
|
const allExistingIndexes = await varDatabaseDbHandler({
|
|
queryString: `SHOW INDEXES FROM \`${dbFullName}\`.\`${tableName}\``,
|
|
});
|
|
for (let g = 0; g < indexes.length; g++) {
|
|
const { indexType, indexName, indexTableFields, alias } = indexes[g];
|
|
if (!(alias === null || alias === void 0 ? void 0 : alias.match(/./)))
|
|
continue;
|
|
/**
|
|
* @description Check for existing Index in MYSQL db
|
|
*/
|
|
try {
|
|
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
|
|
*/
|
|
const queryString = `CREATE${indexType == "full_text" ? " FULLTEXT" : ""} INDEX \`${alias}\` ON \`${dbFullName}\`.\`${tableName}\`(${indexTableFields === null || indexTableFields === void 0 ? void 0 : indexTableFields.map((nm) => nm.value).map((nm) => `\`${nm}\``).join(",")}) COMMENT '${grabDSQLSchemaIndexComment()} ${indexName}'`;
|
|
const addIndex = await varDatabaseDbHandler({ queryString });
|
|
}
|
|
}
|
|
const allExistingIndexesAfterUpdate = await varDatabaseDbHandler({
|
|
queryString: `SHOW INDEXES FROM \`${dbFullName}\`.\`${tableName}\``,
|
|
});
|
|
}
|