This commit is contained in:
Benjamin Toby
2025-01-12 18:19:20 +01:00
parent a3561da53d
commit 186cc76ffb
46 changed files with 344 additions and 211 deletions
+27 -5
View File
@@ -1,8 +1,6 @@
import path from "path";
import fs from "fs";
require("dotenv").config({ path: "./../.env" });
import noDatabaseDbHandler from "./utils/noDatabaseDbHandler";
import varDatabaseDbHandler from "./utils/varDatabaseDbHandler";
import createTable from "./utils/createTable";
@@ -10,8 +8,7 @@ import updateTable from "./utils/updateTable";
import dbHandler from "./utils/dbHandler";
import EJSON from "../utils/ejson";
import { DSQL_DatabaseSchemaType } from "../types";
const execFlag = process.argv.find((arg) => arg === "--exec");
import { parseArgs } from "util";
type Param = {
userId?: number | string | null;
@@ -27,6 +24,14 @@ export default async function createDbFromSchema({
targetDatabase,
dbSchemaData,
}: Param) {
console.log("///////////////////////////////");
console.log("///////////////////////////////");
console.log("Rebuilding Database ...");
console.log("process.env.DSQL_DB_HOST", process.env.DSQL_DB_HOST);
console.log("process.env.DSQL_DB_USERNAME", process.env.DSQL_DB_USERNAME);
console.log("process.env.DSQL_DB_PASSWORD", process.env.DSQL_DB_PASSWORD);
console.log("process.env.DSQL_DB_NAME", process.env.DSQL_DB_NAME);
const schemaPath = userId
? path.join(
String(process.env.DSQL_USER_DB_SCHEMA_PATH),
@@ -58,11 +63,15 @@ export default async function createDbFromSchema({
continue;
}
console.log("Checking Database ...");
/** @type {any} */
const dbCheck: any = await noDatabaseDbHandler(
`SELECT SCHEMA_NAME AS dbFullName FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '${dbFullName}'`
);
console.log("DB Checked Success!");
if (dbCheck && dbCheck[0]?.dbFullName) {
// Database Exists
} else {
@@ -297,8 +306,21 @@ export default async function createDbFromSchema({
}
}
}
console.log("Database Successfully Rebuilt!");
console.log("///////////////////////////////");
console.log("///////////////////////////////");
}
if (execFlag) {
const { values } = parseArgs({
args: process.argv,
options: {
exec: { type: "boolean" },
},
strict: true,
allowPositionals: true,
});
if (values.exec) {
createDbFromSchema({});
}
+1 -1
View File
@@ -26,7 +26,7 @@ for (let i = 0; i < sourceFiles.length; i++) {
const dstFile = dstFiles[i];
fs.watch(srcFolder, { recursive: true }, (evtType, prev) => {
if (prev?.match(/\(/) || prev?.match(/\.js$/i)) {
if (prev?.match(/\(/) || prev?.match(/\.(j|t)s$/i)) {
return;
}
@@ -1,39 +1,16 @@
import dbHandler from "./dbHandler";
/**
* # Create database from Schema Function
*/
export default async function noDatabaseDbHandler(
queryString: string
): Promise<any> {
/**
* Declare variables
*
* @description Declare "results" variable
*/
let results;
/**
* Fetch from db
*
* @description Fetch data from db if no cache
*/
try {
/** ********************* Run Query */
results = await dbHandler({ query: queryString });
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
} catch (error: any) {
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 {