This commit is contained in:
Tben 2023-08-12 15:14:00 +01:00
parent 722f85cd4a
commit 320a271e10
4 changed files with 243 additions and 235 deletions

View File

@ -30,11 +30,13 @@ const updateTable = require("./utils/updateTable");
* @param {import("../../types/database-schema.td").DSQL_DatabaseSchemaType[]} dbSchema - An array of database schema objects * @param {import("../../types/database-schema.td").DSQL_DatabaseSchemaType[]} dbSchema - An array of database schema objects
*/ */
async function createDbFromSchema(dbSchema) { async function createDbFromSchema(dbSchema) {
try {
/** /**
* Grab Schema * Grab Schema
* *
* @description Grab Schema * @description Grab Schema
*/ */
console.log("Starting createDbFromSchema ...");
if (!dbSchema || !Array.isArray(dbSchema) || !dbSchema[0]) { if (!dbSchema || !Array.isArray(dbSchema) || !dbSchema[0]) {
console.log("Invalid DB schema data"); console.log("Invalid DB schema data");
@ -46,6 +48,8 @@ async function createDbFromSchema(dbSchema) {
const database = dbSchema[i]; const database = dbSchema[i];
const { dbFullName, tables } = database; const { dbFullName, tables } = database;
console.log("Now constructing database =>", database?.dbFullName);
//////////////////////////////////////// ////////////////////////////////////////
//////////////////////////////////////// ////////////////////////////////////////
//////////////////////////////////////// ////////////////////////////////////////
@ -53,6 +57,8 @@ async function createDbFromSchema(dbSchema) {
/** @type {{ dbFullName: string }[] | null} */ /** @type {{ dbFullName: string }[] | null} */
const dbCheck = await noDatabaseDbHandler({ query: `SELECT SCHEMA_NAME AS dbFullName FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '${dbFullName}'` }); const dbCheck = await noDatabaseDbHandler({ query: `SELECT SCHEMA_NAME AS dbFullName FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '${dbFullName}'` });
console.log("DB CHeck =>", dbCheck);
if (dbCheck && dbCheck[0]?.dbFullName) { if (dbCheck && dbCheck[0]?.dbFullName) {
// Database Exists // Database Exists
} else { } else {
@ -204,7 +210,7 @@ async function createDbFromSchema(dbSchema) {
if (!alias?.match(/./)) continue; if (!alias?.match(/./)) continue;
/** /**
* @type {DSQL_MYSQL_SHOW_INDEXES_Type[] | null} * @type {any[] | null}
* @description All indexes from MYSQL db * @description All indexes from MYSQL db
*/ */
const allExistingIndexes = await varDatabaseDbHandler({ const allExistingIndexes = await varDatabaseDbHandler({
@ -243,6 +249,9 @@ async function createDbFromSchema(dbSchema) {
//////////////////////////////////////// ////////////////////////////////////////
//////////////////////////////////////// ////////////////////////////////////////
//////////////////////////////////////// ////////////////////////////////////////
} catch (error) {
console.log("Error in createDbFromSchema => ", error.message);
}
} }
/** ****************************************************************************** */ /** ****************************************************************************** */

View File

@ -15,6 +15,7 @@ const connection = mysql.createConnection({
password: process.env.DSQL_PASS, password: process.env.DSQL_PASS,
charset: "utf8mb4", charset: "utf8mb4",
port: process.env.DSQL_PORT?.match(/.../) ? parseInt(process.env.DSQL_PORT) : undefined, port: process.env.DSQL_PORT?.match(/.../) ? parseInt(process.env.DSQL_PORT) : undefined,
timeout: 5000,
}); });
////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////
@ -71,7 +72,6 @@ module.exports = async function dbHandler({ query, values, database }) {
*/ */
try { try {
results = await new Promise((resolve, reject) => { results = await new Promise((resolve, reject) => {
if (connection.state !== "disconnected") {
if (values) { if (values) {
connection.query(query, values, (error, results, fields) => { connection.query(query, values, (error, results, fields) => {
if (error) { if (error) {
@ -82,9 +82,10 @@ module.exports = async function dbHandler({ query, values, database }) {
} else { } else {
resolve(JSON.parse(JSON.stringify(results))); resolve(JSON.parse(JSON.stringify(results)));
} }
setTimeout(() => {
endConnection(connection); // setTimeout(() => {
}, 500); // endConnection(connection);
// }, 500);
}); });
} else { } else {
connection.query(query, (error, results, fields) => { connection.query(query, (error, results, fields) => {
@ -96,12 +97,11 @@ module.exports = async function dbHandler({ query, values, database }) {
} else { } else {
resolve(JSON.parse(JSON.stringify(results))); resolve(JSON.parse(JSON.stringify(results)));
} }
setTimeout(() => { // setTimeout(() => {
endConnection(connection); // endConnection(connection);
}, 500); // }, 500);
}); });
} }
}
}); });
//////////////////////////////////////// ////////////////////////////////////////

View File

@ -11,6 +11,7 @@ const connection = mysql.createConnection({
password: process.env.DSQL_PASS, password: process.env.DSQL_PASS,
charset: "utf8mb4", charset: "utf8mb4",
port: process.env.DSQL_PORT?.match(/.../) ? parseInt(process.env.DSQL_PORT) : undefined, port: process.env.DSQL_PORT?.match(/.../) ? parseInt(process.env.DSQL_PORT) : undefined,
timeout: 5000,
}); });
/** /**
@ -38,7 +39,6 @@ module.exports = async function noDatabaseDbHandler({ query, values }) {
try { try {
/** ********************* Run Query */ /** ********************* Run Query */
results = await new Promise((resolve, reject) => { results = await new Promise((resolve, reject) => {
if (connection.state !== "disconnected") {
if (values) { if (values) {
connection.query(query, values, (error, results, fields) => { connection.query(query, values, (error, results, fields) => {
if (error) { if (error) {
@ -49,9 +49,9 @@ module.exports = async function noDatabaseDbHandler({ query, values }) {
} else { } else {
resolve(JSON.parse(JSON.stringify(results))); resolve(JSON.parse(JSON.stringify(results)));
} }
setTimeout(() => { // setTimeout(() => {
endConnection(connection); // endConnection(connection);
}, 500); // }, 500);
}); });
} else { } else {
connection.query(query, (error, results, fields) => { connection.query(query, (error, results, fields) => {
@ -63,12 +63,11 @@ module.exports = async function noDatabaseDbHandler({ query, values }) {
} else { } else {
resolve(JSON.parse(JSON.stringify(results))); resolve(JSON.parse(JSON.stringify(results)));
} }
setTimeout(() => { // setTimeout(() => {
endConnection(connection); // endConnection(connection);
}, 500); // }, 500);
}); });
} }
}
}); });
//////////////////////////////////////// ////////////////////////////////////////
//////////////////////////////////////// ////////////////////////////////////////

View File

@ -1,6 +1,6 @@
{ {
"name": "datasquirel", "name": "datasquirel",
"version": "1.5.1", "version": "1.5.2",
"description": "Cloud-based SQL data management tool", "description": "Cloud-based SQL data management tool",
"main": "index.js", "main": "index.js",
"bin": { "bin": {