Refactor Code to typescript
This commit is contained in:
+2
-8
@@ -1,16 +1,10 @@
|
||||
// @ts-check
|
||||
|
||||
/**
|
||||
* Convert Camel Joined Text to Camel Spaced Text
|
||||
* ==============================================================================
|
||||
* @description this function takes a camel cased text without spaces, and returns
|
||||
* a camel-case-spaced text
|
||||
*
|
||||
* @param {string} text - text string without spaces
|
||||
*
|
||||
* @returns {string | null}
|
||||
*/
|
||||
module.exports = function camelJoinedtoCamelSpace(text) {
|
||||
export default function camelJoinedtoCamelSpace(text: string): string | null {
|
||||
if (!text?.match(/./)) {
|
||||
return "";
|
||||
}
|
||||
@@ -56,4 +50,4 @@ module.exports = function camelJoinedtoCamelSpace(text) {
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
-10
@@ -1,10 +0,0 @@
|
||||
declare function _exports({ dbFullName, tableName, tableInfoArray, dbSchema, clone, tableSchema, recordedDbEntry, }: {
|
||||
dbFullName: string;
|
||||
tableName: string;
|
||||
tableInfoArray: any[];
|
||||
dbSchema?: import("../../types").DSQL_DatabaseSchemaType[];
|
||||
tableSchema?: import("../../types").DSQL_TableSchemaType;
|
||||
recordedDbEntry?: any;
|
||||
clone?: boolean;
|
||||
}): Promise<any>;
|
||||
export = _exports;
|
||||
+21
-27
@@ -1,30 +1,23 @@
|
||||
// @ts-check
|
||||
import varDatabaseDbHandler from "./varDatabaseDbHandler";
|
||||
import generateColumnDescription from "./generateColumnDescription";
|
||||
import supplementTable from "./supplementTable";
|
||||
import dbHandler from "./dbHandler";
|
||||
import { DSQL_DatabaseSchemaType, DSQL_TableSchemaType } from "../../types";
|
||||
|
||||
const varDatabaseDbHandler = require("./varDatabaseDbHandler");
|
||||
const generateColumnDescription = require("./generateColumnDescription");
|
||||
const supplementTable = require("./supplementTable");
|
||||
const dbHandler = require("./dbHandler");
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
type Param = {
|
||||
dbFullName: string;
|
||||
tableName: string;
|
||||
tableInfoArray: any[];
|
||||
dbSchema?: DSQL_DatabaseSchemaType[];
|
||||
tableSchema?: DSQL_TableSchemaType;
|
||||
recordedDbEntry?: any;
|
||||
clone?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {object} params
|
||||
* @param {string} params.dbFullName
|
||||
* @param {string} params.tableName
|
||||
* @param {any[]} params.tableInfoArray
|
||||
* @param {import("../../types").DSQL_DatabaseSchemaType[]} [params.dbSchema]
|
||||
* @param {import("../../types").DSQL_TableSchemaType} [params.tableSchema]
|
||||
* @param {any} [params.recordedDbEntry]
|
||||
* @param {boolean} [params.clone] - Is this a newly cloned table?
|
||||
* @returns
|
||||
* # Create Table Functions
|
||||
*/
|
||||
module.exports = async function createTable({
|
||||
export default async function createTable({
|
||||
dbFullName,
|
||||
tableName,
|
||||
tableInfoArray,
|
||||
@@ -32,7 +25,7 @@ module.exports = async function createTable({
|
||||
clone,
|
||||
tableSchema,
|
||||
recordedDbEntry,
|
||||
}) {
|
||||
}: Param) {
|
||||
/**
|
||||
* Format tableInfoArray
|
||||
*
|
||||
@@ -65,7 +58,8 @@ module.exports = async function createTable({
|
||||
});
|
||||
|
||||
/** @type {import("../../types").MYSQL_user_database_tables_table_def} */
|
||||
const table = existingTable?.[0];
|
||||
const table: import("../../types").MYSQL_user_database_tables_table_def =
|
||||
existingTable?.[0];
|
||||
|
||||
if (!table?.id) {
|
||||
const newTableEntry = await dbHandler({
|
||||
@@ -98,7 +92,7 @@ module.exports = async function createTable({
|
||||
let primaryKeySet = false;
|
||||
|
||||
/** @type {import("../../types").DSQL_FieldSchemaType[]} */
|
||||
let foreignKeys = [];
|
||||
let foreignKeys: import("../../types").DSQL_FieldSchemaType[] = [];
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
@@ -204,7 +198,7 @@ module.exports = async function createTable({
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
};
|
||||
}
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
-6
@@ -1,6 +0,0 @@
|
||||
declare function _exports({ query, values, database }: {
|
||||
query: string;
|
||||
values?: string[] | object;
|
||||
database?: string;
|
||||
}): Promise<any[] | object | null>;
|
||||
export = _exports;
|
||||
@@ -1,14 +1,8 @@
|
||||
// @ts-check
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
const mysql = require("serverless-mysql");
|
||||
const grabDbSSL = require("../../utils/backend/grabDbSSL");
|
||||
import mysql from "serverless-mysql";
|
||||
import grabDbSSL from "../../utils/backend/grabDbSSL";
|
||||
|
||||
let connection = mysql({
|
||||
config: {
|
||||
@@ -21,25 +15,20 @@ let connection = mysql({
|
||||
},
|
||||
});
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
type Param = {
|
||||
query: string;
|
||||
values?: string[] | object;
|
||||
database?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* # Main DB Handler Function
|
||||
* @async
|
||||
*
|
||||
* @param {object} params
|
||||
* @param {string} params.query
|
||||
* @param {string[] | object} [params.values]
|
||||
* @param {string} [params.database]
|
||||
*
|
||||
* @returns {Promise<any[] | object | null>}
|
||||
*/
|
||||
module.exports = async function dbHandler({ query, values, database }) {
|
||||
export default async function dbHandler({
|
||||
query,
|
||||
values,
|
||||
database,
|
||||
}: Param): Promise<any[] | object | null> {
|
||||
/**
|
||||
* Switch Database
|
||||
*
|
||||
@@ -88,7 +77,7 @@ module.exports = async function dbHandler({ query, values, database }) {
|
||||
|
||||
/** ********************* Clean up */
|
||||
await connection.end();
|
||||
} catch (/** @type {any} */ error) {
|
||||
} catch (/** @type {any} */ error: any) {
|
||||
if (process.env.FIRST_RUN) {
|
||||
return null;
|
||||
}
|
||||
@@ -115,4 +104,4 @@ module.exports = async function dbHandler({ query, values, database }) {
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
declare function _exports({ columnData, primaryKeySet, }: {
|
||||
columnData: import("../../types").DSQL_FieldSchemaType;
|
||||
primaryKeySet?: boolean;
|
||||
}): {
|
||||
fieldEntryText: string;
|
||||
newPrimaryKeySet: boolean;
|
||||
};
|
||||
export = _exports;
|
||||
@@ -1,108 +0,0 @@
|
||||
// @ts-check
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
/**
|
||||
* Generate SQL text for Field
|
||||
* ==============================================================================
|
||||
* @param {object} params - Single object params
|
||||
* @param {import("../../types").DSQL_FieldSchemaType} params.columnData - Field object
|
||||
* @param {boolean} [params.primaryKeySet] - Table Name(slug)
|
||||
*
|
||||
* @returns {{ fieldEntryText: string, newPrimaryKeySet: boolean }}
|
||||
*/
|
||||
module.exports = function generateColumnDescription({
|
||||
columnData,
|
||||
primaryKeySet,
|
||||
}) {
|
||||
/**
|
||||
* Format tableInfoArray
|
||||
*
|
||||
* @description Format tableInfoArray
|
||||
*/
|
||||
const {
|
||||
fieldName,
|
||||
dataType,
|
||||
nullValue,
|
||||
primaryKey,
|
||||
autoIncrement,
|
||||
defaultValue,
|
||||
defaultValueLiteral,
|
||||
foreignKey,
|
||||
updatedField,
|
||||
onUpdate,
|
||||
onUpdateLiteral,
|
||||
onDelete,
|
||||
onDeleteLiteral,
|
||||
defaultField,
|
||||
encrypted,
|
||||
json,
|
||||
newTempField,
|
||||
notNullValue,
|
||||
originName,
|
||||
plainText,
|
||||
pattern,
|
||||
patternFlags,
|
||||
richText,
|
||||
} = columnData;
|
||||
|
||||
let fieldEntryText = "";
|
||||
|
||||
fieldEntryText += `\`${fieldName}\` ${dataType}`;
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
// if (String(fieldEntryText).match(/ UUID$/)) {
|
||||
// fieldEntryText += ` DEFAULT UUID()`;
|
||||
// } else
|
||||
if (nullValue) {
|
||||
fieldEntryText += " DEFAULT NULL";
|
||||
} else if (defaultValueLiteral) {
|
||||
fieldEntryText += ` DEFAULT ${defaultValueLiteral}`;
|
||||
} else if (defaultValue) {
|
||||
if (String(defaultValue).match(/uuid\(\)/i)) {
|
||||
fieldEntryText += ` DEFAULT UUID()`;
|
||||
} else {
|
||||
fieldEntryText += ` DEFAULT '${defaultValue}'`;
|
||||
}
|
||||
} 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;
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
return { fieldEntryText, newPrimaryKeySet: primaryKeySet || false };
|
||||
};
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
type Param = {
|
||||
columnData: import("../../types").DSQL_FieldSchemaType;
|
||||
primaryKeySet?: boolean;
|
||||
};
|
||||
|
||||
type Return = {
|
||||
fieldEntryText: string;
|
||||
newPrimaryKeySet: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* # Generate Table Column Description
|
||||
*/
|
||||
export default function generateColumnDescription({
|
||||
columnData,
|
||||
primaryKeySet,
|
||||
}: Param): Return {
|
||||
/**
|
||||
* Format tableInfoArray
|
||||
*
|
||||
* @description Format tableInfoArray
|
||||
*/
|
||||
const {
|
||||
fieldName,
|
||||
dataType,
|
||||
nullValue,
|
||||
primaryKey,
|
||||
autoIncrement,
|
||||
defaultValue,
|
||||
defaultValueLiteral,
|
||||
onUpdateLiteral,
|
||||
notNullValue,
|
||||
} = columnData;
|
||||
|
||||
let fieldEntryText = "";
|
||||
|
||||
fieldEntryText += `\`${fieldName}\` ${dataType}`;
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
if (nullValue) {
|
||||
fieldEntryText += " DEFAULT NULL";
|
||||
} else if (defaultValueLiteral) {
|
||||
fieldEntryText += ` DEFAULT ${defaultValueLiteral}`;
|
||||
} else if (defaultValue) {
|
||||
if (String(defaultValue).match(/uuid\(\)/i)) {
|
||||
fieldEntryText += ` DEFAULT UUID()`;
|
||||
} else {
|
||||
fieldEntryText += ` DEFAULT '${defaultValue}'`;
|
||||
}
|
||||
} 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;
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
return {
|
||||
fieldEntryText,
|
||||
newPrimaryKeySet: primaryKeySet || false,
|
||||
};
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
declare function _exports(queryString: string): Promise<any>;
|
||||
export = _exports;
|
||||
+7
-10
@@ -1,14 +1,11 @@
|
||||
// @ts-check
|
||||
|
||||
const dbHandler = require("./dbHandler");
|
||||
import dbHandler from "./dbHandler";
|
||||
|
||||
/**
|
||||
* Create database from Schema Function
|
||||
* ==============================================================================
|
||||
* @param {string} queryString - Query String
|
||||
* @returns {Promise<any>}
|
||||
* # Create database from Schema Function
|
||||
*/
|
||||
module.exports = async function noDatabaseDbHandler(queryString) {
|
||||
export default async function noDatabaseDbHandler(
|
||||
queryString: string
|
||||
): Promise<any> {
|
||||
/**
|
||||
* Declare variables
|
||||
*
|
||||
@@ -28,7 +25,7 @@ module.exports = async function noDatabaseDbHandler(queryString) {
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
} catch (/** @type {any} */ error) {
|
||||
} catch (error: any) {
|
||||
console.log("ERROR in noDatabaseDbHandler =>", error.message);
|
||||
}
|
||||
|
||||
@@ -42,4 +39,4 @@ module.exports = async function noDatabaseDbHandler(queryString) {
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
+5
-4
@@ -1,6 +1,7 @@
|
||||
// @ts-check
|
||||
|
||||
module.exports = function slugToCamelTitle(/** @type {String} */ text) {
|
||||
/**
|
||||
* # Sulg To Camel Case
|
||||
*/
|
||||
export default function slugToCamelTitle(text: string) {
|
||||
if (text) {
|
||||
let addArray = text.split("-").filter((item) => item !== "");
|
||||
let camelArray = addArray.map((item) => {
|
||||
@@ -15,4 +16,4 @@ module.exports = function slugToCamelTitle(/** @type {String} */ text) {
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
declare function _exports({ tableInfoArray }: {
|
||||
tableInfoArray: import("../../types").DSQL_FieldSchemaType[];
|
||||
}): import("../../types").DSQL_FieldSchemaType[];
|
||||
export = _exports;
|
||||
+8
-14
@@ -1,19 +1,13 @@
|
||||
// @ts-check
|
||||
import { DSQL_FieldSchemaType } from "../../types";
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
type Param = {
|
||||
tableInfoArray: DSQL_FieldSchemaType[];
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {object} param0
|
||||
* @param {import("../../types").DSQL_FieldSchemaType[]} param0.tableInfoArray
|
||||
* @returns
|
||||
* # Supplement Table
|
||||
*/
|
||||
module.exports = function supplementTable({ tableInfoArray }) {
|
||||
export default function supplementTable({ tableInfoArray }: Param) {
|
||||
/**
|
||||
* Format tableInfoArray
|
||||
*
|
||||
@@ -30,7 +24,7 @@ module.exports = function supplementTable({ tableInfoArray }) {
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
defaultFields.forEach((field) => {
|
||||
defaultFields.forEach((field: any) => {
|
||||
let fieldExists = finalTableArray.filter(
|
||||
(_field) => _field.fieldName === field.fieldName
|
||||
);
|
||||
@@ -49,7 +43,7 @@ module.exports = function supplementTable({ tableInfoArray }) {
|
||||
////////////////////////////////////////
|
||||
|
||||
return finalTableArray;
|
||||
};
|
||||
}
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
-15
@@ -1,15 +0,0 @@
|
||||
declare function _exports({ dbFullName, tableName, tableInfoArray, userId, dbSchema, tableIndexes, tableSchema, clone, childDb, tableIndex, tableNameFull, recordedDbEntry, }: {
|
||||
dbFullName: string;
|
||||
tableName: string;
|
||||
tableSchema: import("../../types").DSQL_TableSchemaType;
|
||||
tableNameFull?: string;
|
||||
tableInfoArray: import("../../types").DSQL_FieldSchemaType[];
|
||||
userId?: number | string | null;
|
||||
dbSchema: import("../../types").DSQL_DatabaseSchemaType[];
|
||||
tableIndexes?: import("../../types").DSQL_IndexSchemaType[];
|
||||
clone?: boolean;
|
||||
tableIndex?: number;
|
||||
childDb?: boolean;
|
||||
recordedDbEntry?: any;
|
||||
}): Promise<any>;
|
||||
export = _exports;
|
||||
+44
-92
@@ -1,46 +1,31 @@
|
||||
// @ts-check
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
///////////////////////// - Update Table Function - ////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
const fs = require("fs");
|
||||
const varDatabaseDbHandler = require("./varDatabaseDbHandler");
|
||||
import fs from "fs";
|
||||
import varDatabaseDbHandler from "./varDatabaseDbHandler";
|
||||
|
||||
const defaultFieldsRegexp =
|
||||
/^id$|^uuid$|^date_created$|^date_created_code$|^date_created_timestamp$|^date_updated$|^date_updated_code$|^date_updated_timestamp$/;
|
||||
|
||||
const generateColumnDescription = require("./generateColumnDescription");
|
||||
const dbHandler = require("./dbHandler");
|
||||
import generateColumnDescription from "./generateColumnDescription";
|
||||
import dbHandler from "./dbHandler";
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
type Param = {
|
||||
dbFullName: string;
|
||||
tableName: string;
|
||||
tableSchema: import("../../types").DSQL_TableSchemaType;
|
||||
tableNameFull?: string;
|
||||
tableInfoArray: import("../../types").DSQL_FieldSchemaType[];
|
||||
userId?: number | string | null;
|
||||
dbSchema: import("../../types").DSQL_DatabaseSchemaType[];
|
||||
tableIndexes?: import("../../types").DSQL_IndexSchemaType[];
|
||||
clone?: boolean;
|
||||
tableIndex?: number;
|
||||
childDb?: boolean;
|
||||
recordedDbEntry?: any;
|
||||
};
|
||||
|
||||
/**
|
||||
* Update table function
|
||||
* ==============================================================================
|
||||
* @param {object} params - Single object params
|
||||
* @param {string} params.dbFullName - Database full name => "datasquirel_user_4394_db_name"
|
||||
* @param {string} params.tableName - Table Name(slug)
|
||||
* @param {import("../../types").DSQL_TableSchemaType} params.tableSchema - Table Name(slug)
|
||||
* @param {string} [params.tableNameFull] - Table Name(slug)
|
||||
* @param {import("../../types").DSQL_FieldSchemaType[]} params.tableInfoArray - Table Info Array
|
||||
* @param {number | string | null} [params.userId] - User ID
|
||||
* @param {import("../../types").DSQL_DatabaseSchemaType[]} params.dbSchema - Single post
|
||||
* @param {import("../../types").DSQL_IndexSchemaType[]} [params.tableIndexes] - Table Indexes
|
||||
* @param {boolean} [params.clone] - Is this a newly cloned table?
|
||||
* @param {number} [params.tableIndex] - The number index of the table in the dbSchema array
|
||||
* @param {boolean} [params.childDb] - The number index of the table in the dbSchema array
|
||||
* @param {any} [params.recordedDbEntry] - The database object as recorded in `user_databases` table
|
||||
* # Update table function
|
||||
*/
|
||||
module.exports = async function updateTable({
|
||||
export default async function updateTable({
|
||||
dbFullName,
|
||||
tableName,
|
||||
tableInfoArray,
|
||||
@@ -53,7 +38,7 @@ module.exports = async function updateTable({
|
||||
tableIndex,
|
||||
tableNameFull,
|
||||
recordedDbEntry,
|
||||
}) {
|
||||
}: Param) {
|
||||
/**
|
||||
* Initialize
|
||||
* ==========================================
|
||||
@@ -61,7 +46,7 @@ module.exports = async function updateTable({
|
||||
*/
|
||||
|
||||
/** @type {any[]} */
|
||||
let errorLogs = [];
|
||||
let errorLogs: any[] = [];
|
||||
|
||||
/**
|
||||
* @description Initialize table info array. This value will be
|
||||
@@ -79,23 +64,19 @@ module.exports = async function updateTable({
|
||||
* @type {string[]}
|
||||
* @description Table update query string array
|
||||
*/
|
||||
const updateTableQueryArray = [];
|
||||
const updateTableQueryArray: string[] = [];
|
||||
|
||||
/**
|
||||
* @type {string[]}
|
||||
* @description Constriants query string array
|
||||
*/
|
||||
const constraintsQueryArray = [];
|
||||
const constraintsQueryArray: string[] = [];
|
||||
|
||||
/**
|
||||
* @description Push the query initial value
|
||||
*/
|
||||
updateTableQueryArray.push(`ALTER TABLE \`${tableName}\``);
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
if (childDb) {
|
||||
try {
|
||||
if (!recordedDbEntry) {
|
||||
@@ -109,7 +90,8 @@ module.exports = async function updateTable({
|
||||
});
|
||||
|
||||
/** @type {import("../../types").MYSQL_user_database_tables_table_def} */
|
||||
const table = existingTable?.[0];
|
||||
const table: import("../../types").MYSQL_user_database_tables_table_def =
|
||||
existingTable?.[0];
|
||||
|
||||
if (!table?.id) {
|
||||
const newTableEntry = await dbHandler({
|
||||
@@ -136,27 +118,25 @@ module.exports = async function updateTable({
|
||||
} catch (error) {}
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* @type {import("../../types").DSQL_MYSQL_SHOW_INDEXES_Type[]}
|
||||
* @description All indexes from MYSQL db
|
||||
*/ // @ts-ignore
|
||||
const allExistingIndexes = await varDatabaseDbHandler({
|
||||
queryString: `SHOW INDEXES FROM \`${tableName}\``,
|
||||
database: dbFullName,
|
||||
});
|
||||
const allExistingIndexes: import("../../types").DSQL_MYSQL_SHOW_INDEXES_Type[] =
|
||||
await varDatabaseDbHandler({
|
||||
queryString: `SHOW INDEXES FROM \`${tableName}\``,
|
||||
database: dbFullName,
|
||||
});
|
||||
|
||||
/**
|
||||
* @type {import("../../types").DSQL_MYSQL_SHOW_COLUMNS_Type[]}
|
||||
* @description All columns from MYSQL db
|
||||
*/ // @ts-ignore
|
||||
const allExistingColumns = await varDatabaseDbHandler({
|
||||
queryString: `SHOW COLUMNS FROM \`${tableName}\``,
|
||||
database: dbFullName,
|
||||
});
|
||||
const allExistingColumns: import("../../types").DSQL_MYSQL_SHOW_COLUMNS_Type[] =
|
||||
await varDatabaseDbHandler({
|
||||
queryString: `SHOW COLUMNS FROM \`${tableName}\``,
|
||||
database: dbFullName,
|
||||
});
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
@@ -164,11 +144,7 @@ module.exports = async function updateTable({
|
||||
* @type {string[]}
|
||||
* @description Updated column names Array
|
||||
*/
|
||||
const updatedColumnsArray = [];
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
const updatedColumnsArray: string[] = [];
|
||||
|
||||
/**
|
||||
* @description Iterate through every existing column
|
||||
@@ -251,7 +227,7 @@ module.exports = async function updateTable({
|
||||
JSON.stringify(userSchemaData),
|
||||
"utf8"
|
||||
);
|
||||
} catch (/** @type {any} */ error) {
|
||||
} catch (/** @type {any} */ error: any) {
|
||||
console.log("Update table error =>", error.message);
|
||||
}
|
||||
|
||||
@@ -271,10 +247,6 @@ module.exports = async function updateTable({
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Handle MYSQL Table Indexes
|
||||
* ===================================================
|
||||
@@ -350,10 +322,6 @@ module.exports = async function updateTable({
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Handle MYSQL Foreign Keys
|
||||
* ===================================================
|
||||
@@ -365,7 +333,9 @@ module.exports = async function updateTable({
|
||||
* @description All MSQL Foreign Keys
|
||||
* @type {import("../../types").DSQL_MYSQL_FOREIGN_KEYS_Type[] | null}
|
||||
*/ // @ts-ignore
|
||||
const allForeignKeys = await varDatabaseDbHandler({
|
||||
const allForeignKeys:
|
||||
| import("../../types").DSQL_MYSQL_FOREIGN_KEYS_Type[]
|
||||
| null = await varDatabaseDbHandler({
|
||||
queryString: `SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = '${dbFullName}' AND TABLE_NAME='${tableName}' AND CONSTRAINT_TYPE='FOREIGN KEY'`,
|
||||
database: dbFullName,
|
||||
});
|
||||
@@ -390,10 +360,6 @@ module.exports = async function updateTable({
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Handle DATASQUIREL schema fields for current table
|
||||
* ===================================================
|
||||
@@ -435,7 +401,7 @@ module.exports = async function updateTable({
|
||||
////////////////////////////////////////
|
||||
|
||||
/** @type {any} */
|
||||
let existingColumnIndex;
|
||||
let existingColumnIndex: any;
|
||||
|
||||
/**
|
||||
* @description Existing MYSQL field object
|
||||
@@ -516,10 +482,6 @@ module.exports = async function updateTable({
|
||||
*/
|
||||
updateTableQueryArray.push(updateText + ",");
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* @description Handle foreing keys if available, and if there is no
|
||||
* "clone" boolean = true
|
||||
@@ -582,19 +544,9 @@ module.exports = async function updateTable({
|
||||
*/
|
||||
return "No Changes Made to Table";
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
} catch (/** @type {any} */ error) {
|
||||
} catch (/** @type {any} */ error: any) {
|
||||
console.log('Error in "updateTable" shell function =>', error.message);
|
||||
|
||||
return "Error in Updating Table";
|
||||
}
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
declare function _exports({ queryString, queryValuesArray, database, tableSchema, }: {
|
||||
queryString: string;
|
||||
queryValuesArray?: string[];
|
||||
database?: string;
|
||||
tableSchema?: import("../../types").DSQL_TableSchemaType;
|
||||
}): Promise<any>;
|
||||
export = _exports;
|
||||
+13
-20
@@ -1,25 +1,22 @@
|
||||
// @ts-check
|
||||
import dbHandler from "./dbHandler";
|
||||
import { DSQL_TableSchemaType } from "../../types";
|
||||
|
||||
const fs = require("fs");
|
||||
const dbHandler = require("./dbHandler");
|
||||
type Param = {
|
||||
queryString: string;
|
||||
queryValuesArray?: string[];
|
||||
database?: string;
|
||||
tableSchema?: DSQL_TableSchemaType;
|
||||
};
|
||||
|
||||
/**
|
||||
* DB handler for specific database
|
||||
* ==============================================================================
|
||||
* @async
|
||||
* @param {object} params - Single object params
|
||||
* @param {string} params.queryString - SQL string
|
||||
* @param {string[]} [params.queryValuesArray] - Values Array
|
||||
* @param {string} [params.database] - Database name
|
||||
* @param {import("../../types").DSQL_TableSchemaType} [params.tableSchema] - Table schema
|
||||
* @returns {Promise<any>}
|
||||
* # DB handler for specific database
|
||||
*/
|
||||
module.exports = async function varDatabaseDbHandler({
|
||||
export default async function varDatabaseDbHandler({
|
||||
queryString,
|
||||
queryValuesArray,
|
||||
database,
|
||||
tableSchema,
|
||||
}) {
|
||||
}: Param): Promise<any> {
|
||||
/**
|
||||
* Declare variables
|
||||
*
|
||||
@@ -54,7 +51,7 @@ module.exports = async function varDatabaseDbHandler({
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
} catch (/** @type {any} */ error) {
|
||||
} catch (/** @type {any} */ error: any) {
|
||||
console.log("Shell Vardb Error =>", error.message);
|
||||
}
|
||||
|
||||
@@ -64,8 +61,4 @@ module.exports = async function varDatabaseDbHandler({
|
||||
* @description Return results add to cache if "req" param is passed
|
||||
*/
|
||||
return results;
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user