This commit is contained in:
Tben
2023-08-12 14:36:18 +01:00
parent f34d90d147
commit 16be9117e9
44 changed files with 3456 additions and 7 deletions
+254
View File
@@ -0,0 +1,254 @@
// @ts-check
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
const path = require("path");
////////////////////////////////////////
const noDatabaseDbHandler = require("./utils/noDatabaseDbHandler");
const varDatabaseDbHandler = require("./utils/varDatabaseDbHandler");
const createTable = require("./utils/createTable");
const updateTable = require("./utils/updateTable");
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/**
* Create database from Schema
* ==============================================================================
* @description Create database from Schema. This function is called when the user
* runs the "dsql create" command. `NOTE`: there must be a "dsql.schema.json" file
* in the root of the project for this function to work
*
* @param {import("../../types/database-schema.td").DSQL_DatabaseSchemaType[]} dbSchema - An array of database schema objects
*/
async function createDbFromSchema(dbSchema) {
/**
* Grab Schema
*
* @description Grab Schema
*/
if (!dbSchema || !Array.isArray(dbSchema) || !dbSchema[0]) {
console.log("Invalid DB schema data");
return;
}
for (let i = 0; i < dbSchema.length; i++) {
/** @type {import("../../types/database-schema.td").DSQL_DatabaseSchemaType} */
const database = dbSchema[i];
const { dbFullName, tables } = database;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/** @type {{ dbFullName: string }[] | null} */
const dbCheck = await noDatabaseDbHandler({ query: `SELECT SCHEMA_NAME AS dbFullName FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '${dbFullName}'` });
if (dbCheck && dbCheck[0]?.dbFullName) {
// Database Exists
} else {
const newDatabase = await noDatabaseDbHandler({ query: `CREATE DATABASE IF NOT EXISTS \`${dbFullName}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_bin` });
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* Select all tables
* @type {{ TABLE_NAME: string }[] | null}
* @description Select All tables in target database
*/
const allTables = await noDatabaseDbHandler({ query: `SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='${dbFullName}'` });
let tableDropped;
if (!allTables) {
console.log("No Tables to Update");
continue;
}
for (let tb = 0; tb < allTables.length; tb++) {
const { TABLE_NAME } = allTables[tb];
/**
* @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 (!tables.filter((_table) => _table.tableName === TABLE_NAME)[0]) {
const oldTableFilteredArray = tables.filter((_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 (oldTableFilteredArray && oldTableFilteredArray[0]) {
console.log("Renaming Table");
await varDatabaseDbHandler({
queryString: `RENAME TABLE \`${oldTableFilteredArray[0].tableNameOld}\` TO \`${oldTableFilteredArray[0].tableName}\``,
database: dbFullName,
});
} else {
console.log(`Dropping Table from ${dbFullName}`);
// deepcode ignore reDOS: <NO user input>
await varDatabaseDbHandler({
queryString: `DROP TABLE \`${TABLE_NAME}\``,
database: dbFullName,
});
tableDropped = true;
}
}
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* @description Iterate through each table and perform table actions
*/
for (let t = 0; t < tables.length; t++) {
const table = tables[t];
if (tableDropped) continue;
const { tableName, fields, indexes } = table;
/**
* @description Check if table exists
*/
const tableCheck = await varDatabaseDbHandler({
queryString: `
SELECT EXISTS (
SELECT
TABLE_NAME
FROM
information_schema.TABLES
WHERE
TABLE_SCHEMA = ? AND
TABLE_NAME = ?
) AS tableExists`,
queryValuesArray: [dbFullName, table.tableName],
database: dbFullName,
});
////////////////////////////////////////
if (tableCheck && tableCheck[0]?.tableExists > 0) {
/**
* @description Update table if table exists
*/
const updateExistingTable = await updateTable({
dbFullName: dbFullName,
tableName: tableName,
tableInfoArray: fields,
dbSchema,
tableIndexes: indexes,
tableIndex: t,
});
if (table.childrenTables && table.childrenTables[0]) {
for (let ch = 0; ch < table.childrenTables.length; ch++) {
const childTable = table.childrenTables[ch];
const updateExistingChildTable = await updateTable({
dbFullName: childTable.dbNameFull,
tableName: childTable.tableName,
tableInfoArray: fields,
dbSchema,
tableIndexes: indexes,
clone: true,
});
// console.log(updateExistingChildTable);
}
}
////////////////////////////////////////
} else {
////////////////////////////////////////
/**
* @description Create new Table if table doesnt exist
*/
const createNewTable = await createTable({
tableName: tableName,
tableInfoArray: fields,
varDatabaseDbHandler,
dbFullName: dbFullName,
dbSchema,
});
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;
/**
* @type {DSQL_MYSQL_SHOW_INDEXES_Type[] | null}
* @description All indexes from MYSQL db
*/
const allExistingIndexes = await varDatabaseDbHandler({
queryString: `SHOW INDEXES FROM \`${tableName}\``,
database: dbFullName,
});
/**
* @description Check for existing Index in MYSQL db
*/
try {
const existingKeyInDb = allExistingIndexes ? allExistingIndexes.filter((indexObject) => indexObject.Key_name === alias) : null;
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 ${tableName}(${indexTableFields
.map((nm) => nm.value)
.map((nm) => `\`${nm}\``)
.join(",")}) COMMENT 'schema_index'`,
database: dbFullName,
});
}
}
}
}
}
////////////////////////////////////////
}
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
}
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
module.exports = createDbFromSchema;
+73
View File
@@ -0,0 +1,73 @@
[
{
"title": "VARCHAR",
"name": "VARCHAR",
"value": "0-255",
"argument": true,
"description": "Varchar is simply letters and numbers within the range 0 - 255",
"maxValue": 255
},
{
"title": "TINYINT",
"name": "TINYINT",
"value": "0-100",
"description": "TINYINT means Integers: 0 to 100",
"maxValue": 127
},
{
"title": "SMALLINT",
"name": "SMALLINT",
"value": "0-255",
"description": "SMALLINT means Integers: 0 to 240933",
"maxValue": 32767
},
{
"title": "MEDIUMINT",
"name": "MEDIUMINT",
"value": "0-255",
"description": "MEDIUMINT means Integers: 0 to 1245568545560",
"maxValue": 8388607
},
{
"title": "INT",
"name": "INT",
"value": "0-255",
"description": "INT means Integers: 0 to 12560",
"maxValue": 2147483647
},
{
"title": "BIGINT",
"name": "BIGINT",
"value": "0-255",
"description": "BIGINT means Integers: 0 to 1245569056767568545560",
"maxValue": 2e63
},
{
"title": "TINYTEXT",
"name": "TINYTEXT",
"value": "0-255",
"description": "Text with 255 max characters",
"maxValue": 127
},
{
"title": "TEXT",
"name": "TEXT",
"value": "0-100",
"description": "MEDIUMTEXT is just text with max length 16,777,215",
"maxValue": 127
},
{
"title": "MEDIUMTEXT",
"name": "MEDIUMTEXT",
"value": "0-255",
"description": "MEDIUMTEXT is just text with max length 16,777,215",
"maxValue": 127
},
{
"title": "LONGTEXT",
"name": "LONGTEXT",
"value": "0-255",
"description": "LONGTEXT is just text with max length 4,294,967,295",
"maxValue": 127
}
]
+39
View File
@@ -0,0 +1,39 @@
[
{
"fieldName": "id",
"dataType": "BIGINT",
"notNullValue": true,
"primaryKey": true,
"autoIncrement": true
},
{
"fieldName": "date_created",
"dataType": "VARCHAR(250)",
"notNullValue": true
},
{
"fieldName": "date_created_code",
"dataType": "BIGINT",
"notNullValue": true
},
{
"fieldName": "date_created_timestamp",
"dataType": "TIMESTAMP",
"defaultValueLiteral": "CURRENT_TIMESTAMP"
},
{
"fieldName": "date_updated",
"dataType": "VARCHAR(250)",
"notNullValue": true
},
{
"fieldName": "date_updated_code",
"dataType": "BIGINT",
"notNullValue": true
},
{
"fieldName": "date_updated_timestamp",
"dataType": "TIMESTAMP",
"defaultValueLiteral": "CURRENT_TIMESTAMP"
}
]
+17
View File
@@ -0,0 +1,17 @@
{
"fieldName": "string",
"dataType": "BIGINT",
"nullValue": true,
"primaryKey": true,
"autoIncrement": true,
"defaultValue": "CURRENT_TIMESTAMP",
"defaultValueLiteral": "CURRENT_TIMESTAMP",
"notNullValue": true,
"foreignKey": {
"foreignKeyName": "Name",
"destinationTableName": "Table Name",
"destinationTableColumnName": "Column Name",
"cascadeDelete": true,
"cascadeUpdate": true
}
}
+5
View File
@@ -0,0 +1,5 @@
const fs = require("fs");
async function deploy() {}
deploy();
+53
View File
@@ -0,0 +1,53 @@
require("dotenv").config({ path: "./../.env" });
////////////////////////////////////////
const noDatabaseDbHandler = require("../functions/backend/noDatabaseDbHandler");
const varDatabaseDbHandler = require("../functions/backend/varDatabaseDbHandler");
const createTable = require("./utils/createTable");
const updateTable = require("./utils/updateTable");
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/**
* Grab Schema
*
* @description Grab Schema
*/
varDatabaseDbHandler({
queryString: `SELECT user_database_tables.*,user_databases.db_full_name FROM user_database_tables JOIN user_databases ON user_database_tables.db_id=user_databases.id`,
database: "datasquirel",
}).then(async (tables) => {
for (let i = 0; i < tables.length; i++) {
const table = tables[i];
const { id, user_id, db_id, db_full_name, table_name, table_slug, table_description } = table;
const tableInfo = await varDatabaseDbHandler({
queryString: `SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='${db_full_name}' AND TABLE_NAME='${table_slug}'`,
database: db_full_name,
});
const updateDbCharset = await varDatabaseDbHandler({
queryString: `ALTER DATABASE ${db_full_name} CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin;`,
database: db_full_name,
});
const updateEncoding = await varDatabaseDbHandler({
queryString: `ALTER TABLE \`${table_slug}\` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin`,
database: db_full_name,
});
console.log(updateEncoding);
}
process.exit();
});
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
+64
View File
@@ -0,0 +1,64 @@
require("dotenv").config({ path: "./../.env" });
////////////////////////////////////////
const noDatabaseDbHandler = require("../functions/backend/noDatabaseDbHandler");
const serverError = require("../functions/backend/serverError");
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
async function createDbFromSchema({ userId }) {
/**
* Grab Schema
*
* @description Grab Schema
*/
try {
const allDatabases = await noDatabaseDbHandler(`SHOW DATABASES`);
const datasquirelUserDatabases = allDatabases.filter((database) => database.Database.match(/datasquirel_user_/));
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 'datasquirel_full_access'@'127.0.0.1' WITH GRANT OPTION`);
console.log(grantDbPriviledges);
}
const flushPriviledged = await noDatabaseDbHandler(`FLUSH PRIVILEGES`);
} catch (error) {
serverError({
component: "shell/grantDbPriviledges/main-catch-error",
message: error.message,
user: { id: userId },
});
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
process.exit();
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
}
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
const userArg = process.argv[process.argv.indexOf("--user")];
const externalUser = process.argv[process.argv.indexOf("--user") + 1];
createDbFromSchema({ userId: userArg ? externalUser : null });
+61
View File
@@ -0,0 +1,61 @@
const fs = require("fs");
const { exec } = require("child_process");
require("dotenv").config({ path: "./../.env" });
const sourceFile = process.argv.indexOf("--src") >= 0 ? process.argv[process.argv.indexOf("--src") + 1] : null;
const destinationFile = process.argv.indexOf("--dst") >= 0 ? process.argv[process.argv.indexOf("--dst") + 1] : null;
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
console.log("Running Less compiler ...");
const sourceFiles = sourceFile.split(",");
const dstFiles = destinationFile.split(",");
for (let i = 0; i < sourceFiles.length; i++) {
const srcFolder = sourceFiles[i];
const dstFile = dstFiles[i];
fs.watch(srcFolder, { recursive: true }, (evtType, prev) => {
if (prev?.match(/\(/) || prev?.match(/\.js$/i)) {
return;
}
let finalSrcPath = `${srcFolder}/main.less`;
let finalDstPath = dstFile;
if (prev?.match(/\[/)) {
const paths = prev.split("/");
const targetPathFull = paths[paths.length - 1];
const targetPath = targetPathFull.replace(/\[|\]/g, "").replace(/\.less/, "");
const destinationFileParentFolder = dstFile.replace(/\/[^\/]+\.css$/, "");
const targetDstFilePath = `${destinationFileParentFolder}/${targetPath}.css`;
finalSrcPath = `${srcFolder}/${targetPathFull}`;
finalDstPath = targetDstFilePath;
}
exec(`lessc ${finalSrcPath} ${finalDstPath?.match(/\.css$/) ? finalDstPath : finalDstPath.replace(/\/$/, "") + "/_main.css"}`, (error, stdout, stderr) => {
/** @type {Error} */
if (error) {
console.log("ERROR =>", error.message);
if (!evtType?.match(/change/i) && prev.match(/\[/)) {
fs.unlinkSync(finalDstPath);
}
return;
}
console.log("Less Compilation \x1b[32msuccessful\x1b[0m!");
});
});
}
+21
View File
@@ -0,0 +1,21 @@
const fs = require("fs");
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/**
* Grab Schema
*
* @description Grab Schema
*/
const imageBase64 = fs.readFileSync("./../public/images/unique-tokens-icon.png", "base64");
console.log(imageBase64.toString());
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
+102
View File
@@ -0,0 +1,102 @@
const fs = require("fs");
require("dotenv").config({ path: "./../.env" });
////////////////////////////////////////
const noDatabaseDbHandler = require("../functions/backend/noDatabaseDbHandler");
const varDatabaseDbHandler = require("../functions/backend/varDatabaseDbHandler");
const createTable = require("./utils/createTable");
const slugToCamelTitle = require("./utils/slugToCamelTitle");
const updateTable = require("./utils/updateTable");
/** ****************************************************************************** */
const userId = process.argv.indexOf("--userId") >= 0 ? process.argv[process.argv.indexOf("--userId") + 1] : null;
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/**
* Grab Schema
*
* @description Grab Schema
*/
async function recoverMainJsonFromDb() {
if (!userId) {
console.log("No user Id provided");
return;
}
const databases = await global.DB_HANDLER(`SELECT * FROM user_databases WHERE user_id='${userId}'`);
const dbWrite = [];
for (let i = 0; i < databases.length; i++) {
const { id, db_name, db_slug, db_full_name, db_image, db_description } = databases[i];
const dbObject = {
dbName: db_name,
dbSlug: db_slug,
dbFullName: db_full_name,
dbDescription: db_description,
dbImage: db_image,
tables: [],
};
const tables = await global.DB_HANDLER(`SELECT * FROM user_database_tables WHERE user_id='${userId}' AND db_id='${id}'`);
for (let j = 0; j < tables.length; j++) {
const { table_name, table_slug, table_description } = tables[j];
const tableObject = {
tableName: table_slug,
tableFullName: table_name,
fields: [],
indexes: [],
};
const tableFields = await varDatabaseDbHandler({
database: db_full_name,
queryString: `SHOW COLUMNS FROM ${table_slug}`,
});
for (let k = 0; k < tableFields.length; k++) {
const { Field, Type, Null, Default, Key } = tableFields[k];
const fieldObject = {
fieldName: Field,
dataType: Type.toUpperCase(),
};
if (Default?.match(/./) && !Default?.match(/timestamp/i)) fieldObject["defaultValue"] = Default;
if (Key?.match(/pri/i)) {
fieldObject["primaryKey"] = true;
fieldObject["autoIncrement"] = true;
}
if (Default?.match(/timestamp/i)) fieldObject["defaultValueLiteral"] = Default;
if (Null?.match(/yes/i)) fieldObject["nullValue"] = true;
if (Null?.match(/no/i)) fieldObject["notNullValue"] = true;
tableObject.fields.push(fieldObject);
}
dbObject.tables.push(tableObject);
}
dbWrite.push(dbObject);
}
fs.writeFileSync(`./../jsonData/dbSchemas/users/user-${userId}/main.json`, JSON.stringify(dbWrite, null, 4), "utf-8");
process.exit();
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
recoverMainJsonFromDb();
+27
View File
@@ -0,0 +1,27 @@
const fs = require("fs");
const { exec } = require("child_process");
require("dotenv").config({ path: "./../.env" });
const sourceFile = process.argv.indexOf("--src") >= 0 ? process.argv[process.argv.indexOf("--src") + 1] : null;
const destinationFile = process.argv.indexOf("--dst") >= 0 ? process.argv[process.argv.indexOf("--dst") + 1] : null;
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
console.log("Running Tailwind CSS compiler ...");
fs.watch("./../", (curr, prev) => {
exec(`npx tailwindcss -i ./tailwind/main.css -o ./styles/tailwind.css`, (error, stdout, stderr) => {
if (error) {
console.log("ERROR =>", error.message);
return;
}
console.log("Tailwind CSS Compilation \x1b[32msuccessful\x1b[0m!");
});
});
+221
View File
@@ -0,0 +1,221 @@
require("dotenv").config({ path: "./../.env" });
const dbEngine = require("datasquirel/engine");
const http = require("http");
const datasquirel = require("datasquirel");
`curl http://www.dataden.tech`;
datasquirel
.get({
db: "test",
key: process.env.DATASQUIREL_READ_ONLY_KEY,
query: "SELECT title, slug, body FROM blog_posts",
})
.then((response) => {
console.log(response);
});
// dbEngine.db
// .query({
// dbFullName: "datasquirel",
// dbHost: process.env.DB_HOST,
// dbPassword: process.env.DB_PASSWORD,
// dbUsername: process.env.DB_USERNAME,
// query: "SHOW TABLES",
// })
// .then((res) => {
// console.log("res =>", res);
// });
// run({
// key: "bc057a2cd57922e085739c89b4985e5e676b655d7cc0ba7604659cad0a08c252040120c06597a5d22959a502a44bd816",
// db: "showmerebates",
// query: "SELECT * FROM test_table",
// }).then((res) => {
// console.log("res =>", res);
// });
post({
key: "3115fce7ea7772eda75f8f0e55a1414c5c018b4920f4bc99a2d4d7000bac203c15a7036fd3d7ef55ae67a002d4c757895b5c58ff82079a04ba6d42d23d4353256985090959a58a9af8e03cb277fc7895413e6f28ae11b1cc15329c7f94cdcf9a795f54d6e1d319adc287dc147143e62d",
database: "showmerebates",
query: {
action: "delete",
table: "test_table",
identifierColumnName: "id",
identifierValue: 6,
},
}).then((res) => {
console.log("res =>", res);
});
async function run({ key, db, query }) {
const httpResponse = await new Promise((resolve, reject) => {
http.request(
{
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: key,
},
hostname: "localhost",
port: 7070,
path: `/api/query/get?db=${db}&query=${query
.replace(/\n|\r|\n\r/g, "")
.replace(/ {2,}/g, " ")
.replace(/ /g, "+")}`,
},
/**
* Callback Function
*
* @description https request callback
*/
(response) => {
var str = "";
response.on("data", function (chunk) {
str += chunk;
});
response.on("end", function () {
resolve(JSON.parse(str));
});
response.on("error", (err) => {
reject(err);
});
}
).end();
});
return httpResponse;
}
/**
* @typedef {Object} PostReturn
* @property {boolean} success - Did the function run successfully?
* @property {(Object[]|string)} [payload=[]] - The Y Coordinate
*/
/**
* @typedef {object} PostDataPayload
* @property {string} action - "insert" | "update" | "delete"
* @property {string} table - Table name(slug) eg "blog_posts"
* @property {string} identifierColumnName - Table identifier field name => eg. "id" OR "email"
* @property {string} identifierValue - Corresponding value of the selected field name => This
* checks identifies a the target row for "update" or "delete". Not needed for "insert"
* @property {object} data - Table insert payload object => This must have keys that match
* table fields
* @property {string?} duplicateColumnName - Duplicate column name to check for
* @property {string?} duplicateColumnValue - Duplicate column value to match. If no "update" param
* provided, function will return null
* @property {boolean?} update - Should the "insert" action update the existing entry if indeed
* the entry with "duplicateColumnValue" exists?
*/
/**
* Post request
* ==============================================================================
* @async
*
* @param {Object} params - Single object passed
* @param {string} params.key - FULL ACCESS API Key
* @param {string} params.database - Database Name
* @param {PostDataPayload} params.query - SQL query String or Request Object
*
* @returns { Promise<PostReturn> } - Return Object
*/
async function post({ key, query, database }) {
/**
* Make https request
*
* @description make a request to datasquirel.com
*/
const httpResponse = await new Promise((resolve, reject) => {
const reqPayloadString = JSON.stringify({
query,
database,
}).replace(/\n|\r|\n\r/gm, "");
try {
JSON.parse(reqPayloadString);
} catch (error) {
console.log(error);
console.log(reqPayloadString);
return {
success: false,
payload: null,
error: "Query object is invalid. Please Check query data values",
};
}
const reqPayload = reqPayloadString;
const httpsRequest = http.request(
{
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.from(reqPayload).length,
Authorization: key,
},
hostname: "localhost",
port: 7070,
path: `/api/query/post`,
},
/**
* Callback Function
*
* @description https request callback
*/
(response) => {
var str = "";
response.on("data", function (chunk) {
str += chunk;
});
response.on("end", function () {
try {
resolve(JSON.parse(str));
} catch (error) {
console.log(error.message);
console.log("Fetched Payload =>", str);
resolve({
success: false,
payload: null,
error: error.message,
});
}
});
response.on("error", (err) => {
resolve({
success: false,
payload: null,
error: err.message,
});
});
}
);
httpsRequest.write(reqPayload);
httpsRequest.on("error", (error) => {
console.log("HTTPS request ERROR =>", error);
});
httpsRequest.end();
});
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
return httpResponse;
}
+81
View File
@@ -0,0 +1,81 @@
const fs = require("fs");
require("dotenv").config({ path: "./../.env" });
////////////////////////////////////////
const noDatabaseDbHandler = require("../functions/backend/noDatabaseDbHandler");
const serverError = require("../functions/backend/serverError");
const varDatabaseDbHandler = require("../functions/backend/varDatabaseDbHandler");
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
async function updateChildrenTablesOnDb() {
/**
* Grab Schema
*
* @description Grab Schema
*/
try {
const rootDir = "./../jsonData/dbSchemas/users";
const userFolders = fs.readdirSync(rootDir);
for (let i = 0; i < userFolders.length; i++) {
const folder = userFolders[i];
const userId = folder.replace(/user-/, "");
const databases = JSON.parse(fs.readFileSync(`${rootDir}/${folder}/main.json`));
for (let j = 0; j < databases.length; j++) {
const db = databases[j];
const dbTables = db.tables;
for (let k = 0; k < dbTables.length; k++) {
const table = dbTables[k];
if (table?.childTable) {
const originTableName = table.childTableName;
const originDbName = table.childTableDbFullName;
const WHERE_CLAUSE = `WHERE user_id='${userId}' AND db_slug='${db.dbSlug}' AND table_slug='${table.tableName}'`;
const existingTableInDb = await global.DB_HANDLER(`SELECT * FROM user_database_tables ${WHERE_CLAUSE}`);
console.log(existingTableInDb);
if (existingTableInDb && existingTableInDb[0]) {
const updateChildrenTablesInfo = await global.DB_HANDLER(`UPDATE user_database_tables SET child_table='1',child_table_parent_database='${originDbName}',child_table_parent_table='${originTableName}' WHERE id='${existingTableInDb[0].id}'`);
console.log(updateChildrenTablesInfo);
}
}
}
}
}
} catch (error) {
console.log(error);
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
process.exit();
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
}
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
// const userArg = process.argv[process.argv.indexOf("--user")];
// const externalUser = process.argv[process.argv.indexOf("--user") + 1];
updateChildrenTablesOnDb();
+53
View File
@@ -0,0 +1,53 @@
require("dotenv").config({ path: "./../.env" });
////////////////////////////////////////
const noDatabaseDbHandler = require("../functions/backend/noDatabaseDbHandler");
const varDatabaseDbHandler = require("../functions/backend/varDatabaseDbHandler");
const createTable = require("./utils/createTable");
const updateTable = require("./utils/updateTable");
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/**
* Grab Schema
*
* @description Grab Schema
*/
varDatabaseDbHandler({
queryString: `SELECT user_database_tables.*,user_databases.db_full_name FROM user_database_tables JOIN user_databases ON user_database_tables.db_id=user_databases.id`,
database: "datasquirel",
}).then(async (tables) => {
for (let i = 0; i < tables.length; i++) {
const table = tables[i];
const { id, user_id, db_id, db_full_name, table_name, table_slug, table_description } = table;
const tableInfo = await varDatabaseDbHandler({
queryString: `SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='${db_full_name}' AND TABLE_NAME='${table_slug}'`,
database: db_full_name,
});
const updateCreationDateTimestamp = await varDatabaseDbHandler({
queryString: `ALTER TABLE \`${table_slug}\` MODIFY COLUMN date_created_timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP`,
database: db_full_name,
});
const updateDateTimestamp = await varDatabaseDbHandler({
queryString: `ALTER TABLE \`${table_slug}\` MODIFY COLUMN date_updated_timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP`,
database: db_full_name,
});
console.log("Date Updated Column updated");
}
process.exit();
});
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
@@ -0,0 +1,51 @@
require("dotenv").config({ path: "./../.env" });
////////////////////////////////////////
const noDatabaseDbHandler = require("../functions/backend/noDatabaseDbHandler");
const serverError = require("../functions/backend/serverError");
const varDatabaseDbHandler = require("../functions/backend/varDatabaseDbHandler");
const createTable = require("./utils/createTable");
const updateTable = require("./utils/updateTable");
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/**
* Grab Schema
*
* @description Grab Schema
*/
varDatabaseDbHandler({
queryString: `SELECT DISTINCT db_id FROM user_database_tables`,
database: "datasquirel",
}).then(async (tables) => {
// console.log(tables);
// process.exit();
for (let i = 0; i < tables.length; i++) {
const table = tables[i];
try {
const { db_id } = table;
const dbSlug = await global.DB_HANDLER(`SELECT db_slug FROM user_databases WHERE id='${db_id}'`);
const updateTableSlug = await global.DB_HANDLER(`UPDATE user_database_tables SET db_slug='${dbSlug[0].db_slug}' WHERE db_id='${db_id}'`);
} catch (error) {
serverError({
component: "shell/updateDbSlugsForTableRecords/main-catch-error",
message: error.message,
user: {},
});
}
}
process.exit();
});
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
@@ -0,0 +1,52 @@
// @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) {
if (!text?.match(/./)) {
return "";
}
if (text?.match(/ /)) {
return text;
}
if (text) {
let textArray = text.split("");
let capIndexes = [];
for (let i = 0; i < textArray.length; i++) {
const char = textArray[i];
if (i === 0) continue;
if (char.match(/[A-Z]/)) {
capIndexes.push(i);
}
}
let textChunks = [`${textArray[0].toUpperCase()}${text.substring(1, capIndexes[0])}`];
for (let j = 0; j < capIndexes.length; j++) {
const capIndex = capIndexes[j];
if (capIndex === 0) continue;
const startIndex = capIndex + 1;
const endIndex = capIndexes[j + 1];
textChunks.push(`${textArray[capIndex].toUpperCase()}${text.substring(startIndex, endIndex)}`);
}
return textChunks.join(" ");
} else {
return null;
}
};
+112
View File
@@ -0,0 +1,112 @@
// @ts-check
const generateColumnDescription = require("./generateColumnDescription");
const supplementTable = require("./supplementTable");
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
module.exports = async function createTable({ dbFullName, tableName, tableInfoArray, varDatabaseDbHandler, dbSchema }) {
/**
* Format tableInfoArray
*
* @description Format tableInfoArray
*/
const finalTable = supplementTable({ tableInfoArray: tableInfoArray });
/**
* Grab Schema
*
* @description Grab Schema
*/
const createTableQueryArray = [];
createTableQueryArray.push(`CREATE TABLE IF NOT EXISTS \`${tableName}\` (`);
////////////////////////////////////////
let primaryKeySet = false;
let foreignKeys = [];
////////////////////////////////////////
for (let i = 0; i < finalTable.length; i++) {
const column = finalTable[i];
const { fieldName, dataType, nullValue, primaryKey, autoIncrement, defaultValue, defaultValueLiteral, foreignKey, updatedField } = column;
if (foreignKey) {
foreignKeys.push({
fieldName: fieldName,
...foreignKey,
});
}
let { fieldEntryText, newPrimaryKeySet } = generateColumnDescription({ columnData: column, primaryKeySet: primaryKeySet });
primaryKeySet = newPrimaryKeySet;
////////////////////////////////////////
if (fieldName?.match(/updated_timestamp/i)) {
fieldEntryText += " ON UPDATE CURRENT_TIMESTAMP";
}
////////////////////////////////////////
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, destinationTableName, destinationTableColumnName, cascadeDelete, cascadeUpdate, foreignKeyName } = foreighKey;
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,
database: dbFullName,
});
return newTable;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
};
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
+126
View File
@@ -0,0 +1,126 @@
// @ts-check
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
const fs = require("fs");
const mysql = require("mysql");
const endConnection = require("./endConnection");
const connection = mysql.createConnection({
host: process.env.DSQL_HOST,
user: process.env.DSQL_USER,
database: process.env.DSQL_DB_NAME,
password: process.env.DSQL_PASS,
charset: "utf8mb4",
port: process.env.DSQL_PORT?.match(/.../) ? parseInt(process.env.DSQL_PORT) : undefined,
});
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
/**
* Main DB Handler Function
* ==============================================================================
* @async
* @param {object} params - Single Param object containing params
* @param {string} params.query - Query String
* @param {(string | number)[]} [params.values] - Values
* @param {object} [params.dbSchema] - Database Schema
* @param {string} [params.database] - Target Database
*
* @returns {Promise<object | null>}
*/
module.exports = async function dbHandler({ query, values, database }) {
/**
* Declare variables
*
* @description Declare "results" variable
*/
let changeDbError;
if (database) {
connection.changeUser({ database: database }, (error) => {
if (error) {
console.log("DB handler error in switching database:", error.message);
changeDbError = error.message;
}
});
}
if (changeDbError) {
return { error: changeDbError };
}
/**
* Declare variables
*
* @description Declare "results" variable
*/
let results;
/**
* Fetch from db
*
* @description Fetch data from db if no cache
*/
try {
results = await new Promise((resolve, reject) => {
if (connection.state !== "disconnected") {
if (values) {
connection.query(query, values, (error, results, fields) => {
if (error) {
console.log("DB handler error:", error.message);
resolve({
error: error.message,
});
} else {
resolve(JSON.parse(JSON.stringify(results)));
}
setTimeout(() => {
endConnection(connection);
}, 500);
});
} else {
connection.query(query, (error, results, fields) => {
if (error) {
console.log("DB handler error:", error.message);
resolve({
error: error.message,
});
} else {
resolve(JSON.parse(JSON.stringify(results)));
}
setTimeout(() => {
endConnection(connection);
}, 500);
});
}
}
});
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
} catch (error) {
console.log("DB handler error:", error.message);
results = null;
}
/**
* Return results
*
* @description Return results add to cache if "req" param is passed
*/
if (results) {
return results;
} else {
return null;
}
};
+12
View File
@@ -0,0 +1,12 @@
/**
* Regular expression to match default fields
*
* @description Regular expression to match default fields
*/
const defaultFieldsRegexp = /^id$|^date_created$|^date_created_code$|^date_created_timestamp$|^date_updated$|^date_updated_code$|^date_updated_timestamp$/;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
module.exports = defaultFieldsRegexp;
+14
View File
@@ -0,0 +1,14 @@
// @ts-check
const mysql = require("mysql");
/**
* @param {mysql.Connection} connection - the active MYSQL connection
*/
module.exports = function endConnection(connection) {
if (connection.state !== "disconnected") {
connection.end((err) => {
console.log(err?.message);
});
}
};
@@ -0,0 +1,68 @@
// @ts-check
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/**
* Generate SQL text for Field
* ==============================================================================
* @param {object} params - Single object params
* @param {import("../../../types/database-schema.td").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, notNullValue } = columnData;
let fieldEntryText = "";
fieldEntryText += `\`${fieldName}\` ${dataType}`;
////////////////////////////////////////
if (nullValue) {
fieldEntryText += " DEFAULT NULL";
} else if (defaultValueLiteral) {
fieldEntryText += ` DEFAULT ${defaultValueLiteral}`;
} else if (defaultValue) {
fieldEntryText += ` DEFAULT '${defaultValue}'`;
} else if (notNullValue) {
fieldEntryText += ` NOT NULL`;
}
////////////////////////////////////////
if (primaryKey && !primaryKeySet) {
fieldEntryText += " PRIMARY KEY";
primaryKeySet = true;
}
////////////////////////////////////////
if (autoIncrement) {
fieldEntryText += " AUTO_INCREMENT";
primaryKeySet = true;
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
return { fieldEntryText, newPrimaryKeySet: primaryKeySet || false };
};
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
+90
View File
@@ -0,0 +1,90 @@
// @ts-check
const fs = require("fs");
const dbHandler = require("./dbHandler");
const mysql = require("mysql");
const endConnection = require("./endConnection");
const connection = mysql.createConnection({
host: process.env.DSQL_HOST,
user: process.env.DSQL_USER,
password: process.env.DSQL_PASS,
charset: "utf8mb4",
port: process.env.DSQL_PORT?.match(/.../) ? parseInt(process.env.DSQL_PORT) : undefined,
});
/**
* Create database from Schema Function
* ==============================================================================
* @param {object} params - Single Param object containing params
* @param {string} params.query - Query String
* @param {string[]} [params.values] - Values
*
* @returns {Promise<object[] | null>}
*/
module.exports = async function noDatabaseDbHandler({ query, values }) {
/**
* Declare variables
*
* @description Declare "results" variable
*/
let results;
/**
* Fetch from db
*
* @description Fetch data from db if no cache
*/
try {
/** ********************* Run Query */
results = await new Promise((resolve, reject) => {
if (connection.state !== "disconnected") {
if (values) {
connection.query(query, values, (error, results, fields) => {
if (error) {
console.log("NO-DB handler error:", error.message);
resolve({
error: error.message,
});
} else {
resolve(JSON.parse(JSON.stringify(results)));
}
setTimeout(() => {
endConnection(connection);
}, 500);
});
} else {
connection.query(query, (error, results, fields) => {
if (error) {
console.log("NO-DB handler error:", error.message);
resolve({
error: error.message,
});
} else {
resolve(JSON.parse(JSON.stringify(results)));
}
setTimeout(() => {
endConnection(connection);
}, 500);
});
}
}
});
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
} catch (error) {
console.log("ERROR in noDatabaseDbHandler =>", error.message);
}
/**
* Return results
*
* @description Return results add to cache if "req" param is passed
*/
if (results) {
return results;
} else {
return null;
}
};
+73
View File
@@ -0,0 +1,73 @@
// @ts-check
const decrypt = require("../../../functions/decrypt");
const defaultFieldsRegexp = require("./defaultFieldsRegexp");
/**
* Parse Database results
* ==============================================================================
* @description this function takes a database results array gotten from a DB handler
* function, decrypts encrypted fields, and returns an updated array with no encrypted
* fields
*
* @param {object} params - Single object params
* @param {{}[]} params.unparsedResults - Array of data objects containing Fields(keys)
* and corresponding values of the fields(values)
* @param {import("../../../types/database-schema.td").DSQL_TableSchemaType} [params.tableSchema] - Table schema
* @returns {Promise<object[]|null>}
*/
module.exports = async function parseDbResults({ unparsedResults, tableSchema }) {
/**
* Declare variables
*
* @description Declare "results" variable
*/
let parsedResults = [];
try {
/**
* Declare variables
*
* @description Declare "results" variable
*/
for (let pr = 0; pr < unparsedResults.length; pr++) {
let result = unparsedResults[pr];
let resultFieldNames = Object.keys(result);
for (let i = 0; i < resultFieldNames.length; i++) {
const resultFieldName = resultFieldNames[i];
let resultFieldSchema = tableSchema?.fields[i];
if (resultFieldName?.match(defaultFieldsRegexp)) {
continue;
}
let value = result[resultFieldName];
if (typeof value !== "number" && !value) {
// parsedResults.push(result);
continue;
}
if (resultFieldSchema?.encrypted) {
if (value?.match(/./)) {
result[resultFieldName] = decrypt(value);
}
}
}
parsedResults.push(result);
}
/**
* Declare variables
*
* @description Declare "results" variable
*/
return parsedResults;
} catch (error) {
console.log("ERROR in parseDbResults Function =>", error.message);
return unparsedResults;
}
};
+16
View File
@@ -0,0 +1,16 @@
// @ts-check
module.exports = function slugToCamelTitle(text) {
if (text) {
let addArray = text.split("-").filter((item) => item !== "");
let camelArray = addArray.map((item) => {
return item.substr(0, 1).toUpperCase() + item.substr(1).toLowerCase();
});
let parsedAddress = camelArray.join(" ");
return parsedAddress;
} else {
return null;
}
};
+48
View File
@@ -0,0 +1,48 @@
// @ts-check
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
module.exports = function supplementTable({ tableInfoArray }) {
/**
* Format tableInfoArray
*
* @description Format tableInfoArray
*/
let finalTableArray = tableInfoArray;
const defaultFields = require("../data/defaultFields.json");
////////////////////////////////////////
let primaryKeyExists = finalTableArray.filter((_field) => _field.primaryKey);
////////////////////////////////////////
defaultFields.forEach((field) => {
let fieldExists = finalTableArray.filter((_field) => _field.fieldName === field.fieldName);
if (fieldExists && fieldExists[0]) {
return;
} else if (field.fieldName === "id" && !primaryKeyExists[0]) {
finalTableArray.unshift(field);
} else {
finalTableArray.push(field);
}
});
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
return finalTableArray;
};
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
+457
View File
@@ -0,0 +1,457 @@
// @ts-check
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
///////////////////////// - Update Table Function - ////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
const fs = require("fs");
const path = require("path");
const defaultFieldsRegexp = /^id$|^date_created$|^date_created_code$|^date_created_timestamp$|^date_updated$|^date_updated_code$|^date_updated_timestamp$/;
const generateColumnDescription = require("./generateColumnDescription");
const varDatabaseDbHandler = require("./varDatabaseDbHandler");
const schemaPath = path.resolve(process.cwd(), "dsql.schema.json");
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/**
* 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/database-schema.td").DSQL_FieldSchemaType[]} params.tableInfoArray - Table Info Array
* @param {import("../../../types/database-schema.td").DSQL_DatabaseSchemaType[]} params.dbSchema - Single post
* @param {import("../../../types/database-schema.td").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
*
* @returns {Promise<string|object[]|null>}
*/
module.exports = async function updateTable({ dbFullName, tableName, tableInfoArray, dbSchema, tableIndexes, clone, tableIndex }) {
/**
* Initialize
* ==========================================
* @description Initial setup
*/
/**
* @description Initialize table info array. This value will be
* changing depending on if a field is renamed or not.
*/
let upToDateTableFieldsArray = tableInfoArray;
/**
* Handle Table updates
*
* @description Try to undate table, catch error if anything goes wrong
*/
try {
/**
* @type {string[]}
* @description Table update query string array
*/
const updateTableQueryArray = [];
/**
* @type {string[]}
* @description Constriants query string array
*/
const constraintsQueryArray = [];
/**
* @description Push the query initial value
*/
updateTableQueryArray.push(`ALTER TABLE \`${tableName}\``);
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* @type {DSQL_MYSQL_SHOW_INDEXES_Type[] | null}
* @description All indexes from MYSQL db
*/
const allExistingIndexes = await varDatabaseDbHandler({
queryString: `SHOW INDEXES FROM \`${tableName}\``,
database: dbFullName,
});
/**
* @type {DSQL_MYSQL_SHOW_COLUMNS_Type[] | null}
* @description All columns from MYSQL db
*/
const allExistingColumns = await varDatabaseDbHandler({
queryString: `SHOW COLUMNS FROM \`${tableName}\``,
database: dbFullName,
});
////////////////////////////////////////
/**
* @type {string[]}
* @description Updated column names Array
*/
const updatedColumnsArray = [];
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* @description Iterate through every existing column
*/
if (allExistingColumns)
for (let e = 0; e < allExistingColumns.length; e++) {
const { Field } = allExistingColumns[e];
if (Field.match(defaultFieldsRegexp)) continue;
/**
* @description This finds out whether the fieldName corresponds with the MSQL Field name
* if the fildName doesn't match any MYSQL Field name, the field is deleted.
*/
let existingEntry = upToDateTableFieldsArray.filter((column) => column.fieldName === Field || column.originName === Field);
if (existingEntry && existingEntry[0]) {
/**
* @description Check if Field name has been updated
*/
if (existingEntry[0].updatedField) {
updatedColumnsArray.push(existingEntry[0].fieldName);
const renameColumn = await varDatabaseDbHandler({
queryString: `ALTER TABLE ${tableName} RENAME COLUMN \`${existingEntry[0].originName}\` TO \`${existingEntry[0].fieldName}\``,
database: dbFullName,
});
console.log(`Column Renamed from "${existingEntry[0].originName}" to "${existingEntry[0].fieldName}"`);
/**
* Update Db Schema
* ===================================================
* @description Update Db Schema after renaming column
*/
try {
const userSchemaData = dbSchema;
const targetDbIndex = userSchemaData.findIndex((db) => db.dbFullName === dbFullName);
const targetTableIndex = userSchemaData[targetDbIndex].tables.findIndex((table) => table.tableName === tableName);
const targetFieldIndex = userSchemaData[targetDbIndex].tables[targetTableIndex].fields.findIndex((field) => field.fieldName === existingEntry[0].fieldName);
delete userSchemaData[targetDbIndex].tables[targetTableIndex].fields[targetFieldIndex]["originName"];
delete userSchemaData[targetDbIndex].tables[targetTableIndex].fields[targetFieldIndex]["updatedField"];
/**
* @description Set New Table Fields Array
*/
upToDateTableFieldsArray = userSchemaData[targetDbIndex].tables[targetTableIndex].fields;
fs.writeFileSync(schemaPath, JSON.stringify(userSchemaData), "utf8");
} catch (error) {
console.log("Error in updating Table =>", error.message);
}
////////////////////////////////////////
}
////////////////////////////////////////
continue;
////////////////////////////////////////
} else {
// console.log("Column Deleted =>", Field);
await varDatabaseDbHandler({
queryString: `ALTER TABLE ${tableName} DROP COLUMN \`${Field}\``,
database: dbFullName,
});
}
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* Handle MYSQL Table Indexes
* ===================================================
* @description Iterate through each table index(if available)
* and perform operations
*/
if (allExistingIndexes)
for (let f = 0; f < allExistingIndexes.length; f++) {
const { Key_name, Index_comment } = allExistingIndexes[f];
/**
* @description Check if this index was specifically created
* by datasquirel
*/
if (Index_comment?.match(/schema_index/)) {
try {
const existingKeyInSchema = tableIndexes ? tableIndexes.filter((indexObject) => indexObject.alias === Key_name) : null;
if (!existingKeyInSchema?.[0]) throw new Error(`This Index(${Key_name}) Has been Deleted!`);
} catch (error) {
/**
* @description Drop Index: This happens when the MYSQL index is not
* present in the datasquirel DB schema
*/
await varDatabaseDbHandler({
queryString: `ALTER TABLE ${tableName} DROP INDEX \`${Key_name}\``,
database: dbFullName,
});
}
}
}
/**
* Handle DATASQUIREL Table Indexes
* ===================================================
* @description Iterate through each datasquirel schema
* table index(if available), and perform operations
*/
if (tableIndexes && tableIndexes[0]) {
for (let g = 0; g < tableIndexes.length; g++) {
const { indexType, indexName, indexTableFields, alias } = tableIndexes[g];
if (!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
*/
await varDatabaseDbHandler({
queryString: `CREATE${indexType.match(/fullText/i) ? " FULLTEXT" : ""} INDEX \`${alias}\` ON ${tableName}(${indexTableFields
.map((nm) => nm.value)
.map((nm) => `\`${nm}\``)
.join(",")}) COMMENT 'schema_index'`,
database: dbFullName,
});
}
}
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* Handle MYSQL Foreign Keys
* ===================================================
* @description Iterate through each datasquirel schema
* table index(if available), and perform operations
*/
/**
* @description All MSQL Foreign Keys
* @type {DSQL_MYSQL_FOREIGN_KEYS_Type[] | null}
*/
const allForeignKeys = 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,
});
if (allForeignKeys)
for (let c = 0; c < allForeignKeys.length; c++) {
const { CONSTRAINT_NAME } = allForeignKeys[c];
/**
* @description Skip if Key is the PRIMARY Key
*/
if (CONSTRAINT_NAME.match(/PRIMARY/)) continue;
/**
* @description Drop all foreign Keys to avoid MYSQL errors when adding/updating
* Foreign keys
*/
const dropForeignKey = await varDatabaseDbHandler({
queryString: `ALTER TABLE ${tableName} DROP FOREIGN KEY \`${CONSTRAINT_NAME}\``,
database: dbFullName,
});
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* Handle DATASQUIREL schema fields for current table
* ===================================================
* @description Iterate through each field object and
* perform operations
*/
for (let i = 0; i < upToDateTableFieldsArray.length; i++) {
const column = upToDateTableFieldsArray[i];
const prevColumn = upToDateTableFieldsArray[i - 1];
const nextColumn = upToDateTableFieldsArray[i + 1];
const { fieldName, dataType, nullValue, primaryKey, autoIncrement, defaultValue, defaultValueLiteral, foreignKey, updatedField } = column;
////////////////////////////////////////
/**
* @description Skip default fields
*/
if (fieldName.match(/^id$|^date_/)) continue;
/**
* @description Skip columns that have been updated recently
*/
// if (updatedColumnsArray.includes(fieldName)) continue;
////////////////////////////////////////
let updateText = "";
////////////////////////////////////////
let existingColumnIndex;
/**
* @description Existing MYSQL field object
*/
let existingColumn =
allExistingColumns && allExistingColumns[0]
? allExistingColumns.filter((_column, _index) => {
if (_column.Field === fieldName) {
existingColumnIndex = _index;
return true;
}
})
: null;
/**
* @description Construct SQL text snippet for this field
*/
let { fieldEntryText } = generateColumnDescription({ columnData: column });
/**
* @description Modify Column(Field) if it already exists
* in MYSQL database
*/
if (existingColumn && existingColumn[0]?.Field) {
const { Field, Type, Null, Key, Default, Extra } = existingColumn[0];
let isColumnReordered = existingColumnIndex ? i < existingColumnIndex : false;
if (Field === fieldName && !isColumnReordered && dataType.toUpperCase() === Type.toUpperCase()) {
updateText += `MODIFY COLUMN ${fieldEntryText}`;
// continue;
} else {
updateText += `MODIFY COLUMN ${fieldEntryText}${isColumnReordered ? (prevColumn?.fieldName ? " AFTER `" + prevColumn.fieldName + "`" : nextColumn?.fieldName ? " BEFORE `" + nextColumn.fieldName + "`" : "") : ""}`;
// if (userId) {
// } else {
// updateText += `MODIFY COLUMN ${fieldEntryText}`;
// }
}
} else if (prevColumn && prevColumn.fieldName) {
/**
* @description Add new Column AFTER previous column, if
* previous column exists
*/
updateText += `ADD COLUMN ${fieldEntryText} AFTER \`${prevColumn.fieldName}\``;
} else if (nextColumn && nextColumn.fieldName) {
/**
* @description Add new Column BEFORE next column, if
* next column exists
*/
updateText += `ADD COLUMN ${fieldEntryText} BEFORE \`${nextColumn.fieldName}\``;
} else {
/**
* @description Append new column to the end of existing columns
*/
updateText += `ADD COLUMN ${fieldEntryText}`;
}
////////////////////////////////////////
/**
* @description Pust SQL code snippet to updateTableQueryArray Array
* Add a comma(,) to separate from the next snippet
*/
updateTableQueryArray.push(updateText + ",");
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* @description Handle foreing keys if available, and if there is no
* "clone" boolean = true
*/
if (!clone && foreignKey) {
const { destinationTableName, destinationTableColumnName, cascadeDelete, cascadeUpdate, foreignKeyName } = foreignKey;
const foreinKeyText = `ADD CONSTRAINT \`${foreignKeyName}\` FOREIGN KEY (${fieldName}) REFERENCES ${destinationTableName}(${destinationTableColumnName})${cascadeDelete ? " ON DELETE CASCADE" : ""}${cascadeUpdate ? " ON UPDATE CASCADE" : ""}`;
// const foreinKeyText = `ADD CONSTRAINT \`${foreignKeyName}\` FOREIGN KEY (${fieldName}) REFERENCES ${destinationTableName}(${destinationTableColumnName})${cascadeDelete ? " ON DELETE CASCADE" : ""}${cascadeUpdate ? " ON UPDATE CASCADE" : ""}` + ",";
const finalQueryString = `ALTER TABLE \`${tableName}\` ${foreinKeyText}`;
const addForeignKey = await varDatabaseDbHandler({
database: dbFullName,
queryString: finalQueryString,
});
}
////////////////////////////////////////
}
/**
* @description Construct final SQL query by combning all SQL snippets in
* updateTableQueryArray Arry, and trimming the final comma(,)
*/
const updateTableQuery = updateTableQueryArray.join(" ").replace(/,$/, "");
////////////////////////////////////////
/**
* @description Check if SQL snippets array has more than 1 entries
* This is because 1 entry means "ALTER TABLE table_name" only, without any
* Alter directives like "ADD COLUMN" or "MODIFY COLUMN"
*/
if (updateTableQueryArray.length > 1) {
const updateTable = await varDatabaseDbHandler({
queryString: updateTableQuery,
database: dbFullName,
});
return updateTable;
} else {
/**
* @description If only 1 SQL snippet is left in updateTableQueryArray, this
* means that no updates have been made to the table
*/
return "No Changes Made to Table";
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
} catch (error) {
console.log('Error in "updateTable" function =>', error.message);
return "Error in Updating Table";
}
};
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
+88
View File
@@ -0,0 +1,88 @@
// @ts-check
const fs = require("fs");
const mysql = require("mysql");
const parseDbResults = require("./parseDbResults");
const dbHandler = require("./dbHandler");
/**
* 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/database-schema.td").DSQL_TableSchemaType} [params.tableSchema] - Table schema
* @returns {Promise<any[]|null>}
*/
module.exports = async function varDatabaseDbHandler({ queryString, queryValuesArray, database, tableSchema }) {
/**
* Create Connection
*
* @description Create Connection
*/
const connection = mysql.createConnection({
host: process.env.DSQL_SOCKET_HOST,
user: process.env.DSQL_SOCKET_USER,
password: process.env.DSQL_SOCKET_PASS || "",
database: process.env.DSQL_SOCKET_DB_NAME,
charset: "utf8mb4",
port: parseInt(process.env.DSQL_SOCKET_DB_NAME || "") || undefined,
});
/**
* Declare variables
*
* @description Declare "results" variable
*/
let results;
/**
* Fetch from db
*
* @description Fetch data from db if no cache
*/
try {
if (queryString && queryValuesArray && Array.isArray(queryValuesArray) && queryValuesArray[0]) {
results = await dbHandler({ query: queryString, values: queryValuesArray, database: database });
} else {
results = await dbHandler({ query: queryString, database: database });
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
} catch (error) {
console.log("\x1b[31mvarDatabaseDbHandler ERROR\x1b[0m =>", database, error);
}
/**
* Return results
*
* @description Return results add to cache if "req" param is passed
*/
if (results && tableSchema) {
try {
const unparsedResults = results;
// deepcode ignore reDOS: <please specify a reason of ignoring this>
const parsedResults = await parseDbResults({ unparsedResults: unparsedResults, tableSchema: tableSchema });
return parsedResults;
} catch (error) {
console.log("\x1b[31mvarDatabaseDbHandler ERROR\x1b[0m =>", database, error);
return null;
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
} else if (results) {
return results;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
} else {
return null;
}
};
@@ -0,0 +1,57 @@
// @ts-check
const fs = require("fs");
const parseDbResults = require("./parseDbResults");
const dbHandler = require("./dbHandler");
/**
*
* @param {object} param0
* @param {string} param0.queryString
* @param {object} param0.database
* @param {object[]} [param0.queryValuesArray]
* @param {object | null} [param0.tableSchema]
* @returns
*/
module.exports = async function varReadOnlyDatabaseDbHandler({ queryString, database, queryValuesArray, tableSchema }) {
/**
* Declare variables
*
* @description Declare "results" variable
*/
let results;
/**
* Fetch from db
*
* @description Fetch data from db if no cache
*/
try {
results = await dbHandler({ query: queryString, values: queryValuesArray, database: database });
////////////////////////////////////////
} catch (error) {
////////////////////////////////////////
console.log("\x1b[31mvarReadOnlyDatabaseDbHandler ERROR\x1b[0m =>", database, error.message);
/**
* Return error
*/
return error.message;
}
/**
* Return results
*
* @description Return results add to cache if "req" param is passed
*/
if (results) {
const unparsedResults = results;
// deepcode ignore reDOS: <please specify a reason of ignoring this>
const parsedResults = await parseDbResults({ unparsedResults: unparsedResults, tableSchema: tableSchema });
return parsedResults;
} else {
return null;
}
};