Updates
This commit is contained in:
Executable
+57
@@ -0,0 +1,57 @@
|
||||
// @ts-check
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
const mysql = require("serverless-mysql");
|
||||
const grabDbSSL = require("../utils/backend/grabDbSSL");
|
||||
|
||||
const connection = mysql({
|
||||
config: {
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
database: process.env.DSQL_DB_NAME,
|
||||
charset: "utf8mb4",
|
||||
ssl: grabDbSSL(),
|
||||
},
|
||||
});
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* # Main DB Handler Function
|
||||
* @async
|
||||
*
|
||||
* @param {object} params
|
||||
* @param {string} params.query
|
||||
* @param {string[] | object} [params.values]
|
||||
* @param {string} [params.database]
|
||||
*
|
||||
* @returns {Promise<object|null>}
|
||||
*/
|
||||
(async () => {
|
||||
/**
|
||||
* Switch Database
|
||||
*
|
||||
* @description If a database is provided, switch to it
|
||||
*/
|
||||
try {
|
||||
const result = await connection.query(
|
||||
"SELECT id,first_name,last_name FROM users LIMIT 3"
|
||||
);
|
||||
console.log("Connection Query Success =>", result);
|
||||
} catch (/** @type {any} */ error) {
|
||||
console.log("Connection query ERROR =>", error.message);
|
||||
} finally {
|
||||
connection.end();
|
||||
process.exit();
|
||||
}
|
||||
})();
|
||||
Executable
+302
@@ -0,0 +1,302 @@
|
||||
// @ts-check
|
||||
|
||||
const path = require("path");
|
||||
const fs = require("fs");
|
||||
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
|
||||
const noDatabaseDbHandler = require("./utils/noDatabaseDbHandler");
|
||||
const varDatabaseDbHandler = require("./utils/varDatabaseDbHandler");
|
||||
const createTable = require("./utils/createTable");
|
||||
const updateTable = require("./utils/updateTable");
|
||||
const dbHandler = require("./utils/dbHandler");
|
||||
const EJSON = require("../utils/ejson");
|
||||
|
||||
const execFlag = process.argv.find((arg) => arg === "--exec");
|
||||
|
||||
/**
|
||||
* Create database from Schema Function
|
||||
* ==============================================================================
|
||||
* @param {object} params - Single object params
|
||||
* @param {number|string|null} [params.userId] - User ID or null
|
||||
* @param {string} [params.targetDatabase] - User Database full name
|
||||
* @param {import("../types").DSQL_DatabaseSchemaType[]} [params.dbSchemaData]
|
||||
*/
|
||||
async function createDbFromSchema({ userId, targetDatabase, dbSchemaData }) {
|
||||
const schemaPath = userId
|
||||
? path.join(
|
||||
String(process.env.DSQL_USER_DB_SCHEMA_PATH),
|
||||
`/user-${userId}/main.json`
|
||||
)
|
||||
: path.resolve(__dirname, "../../jsonData/dbSchemas/main.json");
|
||||
|
||||
/** @type {import("../types").DSQL_DatabaseSchemaType[] | undefined} */
|
||||
const dbSchema =
|
||||
dbSchemaData ||
|
||||
/** @type {import("../types").DSQL_DatabaseSchemaType[] | undefined} */ (
|
||||
EJSON.parse(fs.readFileSync(schemaPath, "utf8"))
|
||||
);
|
||||
|
||||
if (!dbSchema) {
|
||||
console.log("Schema Not Found!");
|
||||
return;
|
||||
}
|
||||
|
||||
// await createDatabasesFromSchema(dbSchema);
|
||||
|
||||
for (let i = 0; i < dbSchema.length; i++) {
|
||||
/** @type {import("../types").DSQL_DatabaseSchemaType} */
|
||||
const database = dbSchema[i];
|
||||
const { dbFullName, tables, dbName, dbSlug, childrenDatabases } =
|
||||
database;
|
||||
|
||||
if (targetDatabase && dbFullName != targetDatabase) {
|
||||
continue;
|
||||
}
|
||||
|
||||
/** @type {any} */
|
||||
const dbCheck = await noDatabaseDbHandler(
|
||||
`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(
|
||||
`CREATE DATABASE IF NOT EXISTS \`${dbFullName}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_bin`
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Select all tables
|
||||
* @type {any}
|
||||
* @description Select All tables in target database
|
||||
*/
|
||||
const allTables = await noDatabaseDbHandler(
|
||||
`SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='${dbFullName}'`
|
||||
);
|
||||
|
||||
// let tableDropped;
|
||||
|
||||
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}`);
|
||||
await varDatabaseDbHandler({
|
||||
queryString: `DROP TABLE \`${TABLE_NAME}\``,
|
||||
database: dbFullName,
|
||||
});
|
||||
|
||||
const deleteTableEntry = await dbHandler({
|
||||
query: `DELETE FROM user_database_tables WHERE user_id = ? AND db_slug = ? AND table_slug = ?`,
|
||||
values: [userId, dbSlug, TABLE_NAME],
|
||||
database: "datasquirel",
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const recordedDbEntryArray = userId
|
||||
? await varDatabaseDbHandler({
|
||||
database: "datasquirel",
|
||||
queryString: `SELECT * FROM user_databases WHERE db_full_name = ?`,
|
||||
queryValuesArray: [dbFullName],
|
||||
})
|
||||
: undefined;
|
||||
|
||||
const recordedDbEntry = recordedDbEntryArray?.[0];
|
||||
|
||||
/**
|
||||
* @description Iterate through each table and perform table actions
|
||||
*/
|
||||
for (let t = 0; t < tables.length; t++) {
|
||||
const table = tables[t];
|
||||
|
||||
const { tableName, fields, indexes } = table;
|
||||
|
||||
/**
|
||||
* @description Check if table exists
|
||||
* @type {any}
|
||||
*/
|
||||
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,
|
||||
tableNameFull: table.tableFullName,
|
||||
tableInfoArray: fields,
|
||||
userId,
|
||||
dbSchema,
|
||||
tableIndexes: indexes,
|
||||
tableIndex: t,
|
||||
childDb: database.childDatabase || undefined,
|
||||
recordedDbEntry,
|
||||
tableSchema: table,
|
||||
});
|
||||
|
||||
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,
|
||||
tableNameFull: childTable.tableNameFull,
|
||||
tableInfoArray: fields,
|
||||
userId,
|
||||
dbSchema,
|
||||
tableIndexes: indexes,
|
||||
clone: true,
|
||||
childDb: database.childDatabase || undefined,
|
||||
recordedDbEntry,
|
||||
tableSchema: table,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
} else {
|
||||
////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* @description Create new Table if table doesnt exist
|
||||
*/
|
||||
const createNewTable = await createTable({
|
||||
tableName: tableName,
|
||||
tableInfoArray: fields,
|
||||
dbFullName: dbFullName,
|
||||
dbSchema,
|
||||
tableSchema: table,
|
||||
recordedDbEntry,
|
||||
});
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* @description Check for existing Index in MYSQL db
|
||||
*/
|
||||
try {
|
||||
/**
|
||||
* @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 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,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Check all children databases
|
||||
*/
|
||||
if (childrenDatabases?.[0]) {
|
||||
for (let ch = 0; ch < childrenDatabases.length; ch++) {
|
||||
const childDb = childrenDatabases[ch];
|
||||
const { dbFullName } = childDb;
|
||||
|
||||
await createDbFromSchema({
|
||||
userId,
|
||||
targetDatabase: dbFullName,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = createDbFromSchema;
|
||||
|
||||
if (execFlag) {
|
||||
createDbFromSchema({});
|
||||
}
|
||||
Executable
+7
@@ -0,0 +1,7 @@
|
||||
// @ts-check
|
||||
|
||||
const fs = require("fs");
|
||||
|
||||
async function deploy() {}
|
||||
|
||||
deploy();
|
||||
Executable
+58
@@ -0,0 +1,58 @@
|
||||
// @ts-check
|
||||
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
const varDatabaseDbHandler = require("../functions/backend/varDatabaseDbHandler");
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
/**
|
||||
* 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,
|
||||
});
|
||||
}
|
||||
|
||||
process.exit();
|
||||
});
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
@@ -0,0 +1,10 @@
|
||||
// @ts-check
|
||||
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
const jsonFile = path.resolve(__dirname, "../../jsonData/userPriviledges.json");
|
||||
const base64File = Buffer.from(fs.readFileSync(jsonFile, "utf8")).toString(
|
||||
"base64"
|
||||
);
|
||||
console.log(base64File);
|
||||
Executable
+79
@@ -0,0 +1,79 @@
|
||||
// @ts-check
|
||||
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
|
||||
const serverError = require("../functions/backend/serverError");
|
||||
const noDatabaseDbHandler = require("./utils/noDatabaseDbHandler");
|
||||
////////////////////////////////////////
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
/**
|
||||
* # Create Database From Schema
|
||||
* @param {object} param0
|
||||
* @param {string | null} param0.userId
|
||||
*/
|
||||
async function createDbFromSchema({ userId }) {
|
||||
/**
|
||||
* Grab Schema
|
||||
*
|
||||
* @description Grab Schema
|
||||
*/
|
||||
try {
|
||||
const defaultMariadbUserHost = process.env.DSQL_DB_HOST || "127.0.0.1";
|
||||
|
||||
const allDatabases = await noDatabaseDbHandler(`SHOW DATABASES`);
|
||||
|
||||
const datasquirelUserDatabases = allDatabases.filter(
|
||||
(/** @type {any} */ 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 '${process.env.DSQL_DB_FULL_ACCESS_USERNAME}'@'%' WITH GRANT OPTION`
|
||||
);
|
||||
|
||||
const grantRead = await noDatabaseDbHandler(
|
||||
`GRANT SELECT ON ${Database}.* TO '${process.env.DSQL_DB_READ_ONLY_USERNAME}'@'%'`
|
||||
);
|
||||
}
|
||||
|
||||
const flushPriviledged = await noDatabaseDbHandler(`FLUSH PRIVILEGES`);
|
||||
} catch (/** @type {any} */ 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 });
|
||||
Executable
+79
@@ -0,0 +1,79 @@
|
||||
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!");
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
# Handle Datasquirel MariaDB Users and Grants
|
||||
|
||||
## Files
|
||||
|
||||
### refreshUsersAndGrants.js
|
||||
|
||||
This script checks MariaDB users and updates their privileges using the `mariadb_users` table in `datasquirel` database.
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
// @ts-check
|
||||
|
||||
const noDatabaseDbHandler = require("../utils/noDatabaseDbHandler");
|
||||
|
||||
/**
|
||||
* @typedef {object} GrantType
|
||||
* @property {string} database - Database Name
|
||||
* @property {string} table - Table Name
|
||||
* @property {string[]} privileges - Privileges
|
||||
*/
|
||||
|
||||
/**
|
||||
* Handle Grants for Users
|
||||
* ================================================
|
||||
* @param {object} params - Single object params
|
||||
* @param {string} params.username - Username
|
||||
* @param {string} params.host - Host
|
||||
* @param {GrantType[]} params.grants - Grants
|
||||
* @param {string} params.userId
|
||||
*
|
||||
* @returns {Promise<boolean>} success
|
||||
*/
|
||||
async function handleGrants({ username, host, grants, userId }) {
|
||||
let success = false;
|
||||
|
||||
console.log(`Handling Grants for User =>`, username, host);
|
||||
|
||||
if (!username) {
|
||||
console.log(`No username provided.`);
|
||||
return success;
|
||||
}
|
||||
|
||||
if (!host) {
|
||||
console.log(
|
||||
`No Host provided. \x1b[35m\`--host\`\x1b[0m flag is required`
|
||||
);
|
||||
return success;
|
||||
}
|
||||
|
||||
if (!grants) {
|
||||
console.log(`No grants Array provided.`);
|
||||
return success;
|
||||
}
|
||||
|
||||
try {
|
||||
const existingUser = await noDatabaseDbHandler(
|
||||
`SELECT * FROM mysql.user WHERE User = '${username}' AND Host = '${host}'`
|
||||
);
|
||||
|
||||
const isUserExisting = Boolean(existingUser?.[0]?.User);
|
||||
|
||||
if (isUserExisting) {
|
||||
const userGrants = await noDatabaseDbHandler(
|
||||
`SHOW GRANTS FOR '${username}'@'${host}'`
|
||||
);
|
||||
|
||||
for (let i = 0; i < userGrants.length; i++) {
|
||||
const grantObject = userGrants[i];
|
||||
const grant = grantObject?.[Object.keys(grantObject)[0]];
|
||||
|
||||
if (grant?.match(/GRANT .* PRIVILEGES ON .* TO/)) {
|
||||
const revokeGrantText = grant
|
||||
.replace(/GRANT/, "REVOKE")
|
||||
.replace(/ TO /, " FROM ");
|
||||
|
||||
const revokePrivilege = await noDatabaseDbHandler(
|
||||
revokeGrantText
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @type {GrantType[]}
|
||||
*/
|
||||
const grantsArray = grants;
|
||||
|
||||
for (let i = 0; i < grantsArray.length; i++) {
|
||||
const grantObject = grantsArray[i];
|
||||
const { database, table, privileges } = grantObject;
|
||||
|
||||
const tableText = table == "*" ? "*" : `\`${table}\``;
|
||||
const databaseText =
|
||||
database == "*"
|
||||
? `\`${process.env.DSQL_USER_DB_PREFIX}${userId}_%\``
|
||||
: `\`${database}\``;
|
||||
|
||||
const privilegesText = privileges.includes("ALL")
|
||||
? "ALL PRIVILEGES"
|
||||
: privileges.join(", ");
|
||||
|
||||
const grantText = `GRANT ${privilegesText} ON ${databaseText}.${tableText} TO '${username}'@'${host}'`;
|
||||
|
||||
const grantPriviledge = await noDatabaseDbHandler(grantText);
|
||||
}
|
||||
}
|
||||
|
||||
success = true;
|
||||
} catch (/** @type {any} */ error) {
|
||||
console.log(`Error in adding SQL user =>`, error.message);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
module.exports = handleGrants;
|
||||
+269
@@ -0,0 +1,269 @@
|
||||
// @ts-check
|
||||
|
||||
const path = require("path");
|
||||
require("dotenv").config({ path: path.resolve(__dirname, "../../../.env") });
|
||||
|
||||
const generator = require("generate-password");
|
||||
const noDatabaseDbHandler = require("../utils/noDatabaseDbHandler");
|
||||
const dbHandler = require("../utils/dbHandler");
|
||||
const handleGrants = require("./handleGrants");
|
||||
const encrypt = require("../../functions/dsql/encrypt");
|
||||
const decrypt = require("../../functions/dsql/decrypt");
|
||||
|
||||
const defaultMariadbUserHost = process.env.DSQL_DB_HOST || "127.0.0.1";
|
||||
|
||||
/**
|
||||
* Refresh Mariadb User Grants
|
||||
* ===================================================
|
||||
* @param {object} params
|
||||
* @param {number | string} [params.userId]
|
||||
* @param {string} [params.mariadbUserHost]
|
||||
* @param {string} [params.mariadbUser]
|
||||
* @param {string | number} [params.sqlUserID]
|
||||
*/
|
||||
async function refreshUsersAndGrants({
|
||||
userId,
|
||||
mariadbUserHost,
|
||||
mariadbUser,
|
||||
sqlUserID,
|
||||
}) {
|
||||
/**
|
||||
* @description Users
|
||||
* @type {*[] | null}
|
||||
*/ // @ts-ignore
|
||||
const users = await dbHandler({
|
||||
query: `SELECT * FROM users`,
|
||||
});
|
||||
|
||||
if (!users?.[0]) {
|
||||
process.exit();
|
||||
}
|
||||
|
||||
for (let i = 0; i < users.length; i++) {
|
||||
const user = users[i];
|
||||
|
||||
if (!user) continue;
|
||||
if (userId && user.id != userId) continue;
|
||||
|
||||
try {
|
||||
const { mariadb_user, mariadb_host, mariadb_pass, id } = user;
|
||||
const existingUser = await noDatabaseDbHandler(
|
||||
`SELECT * FROM mysql.user WHERE User = '${mariadb_user}' AND Host = '${mariadb_host}'`
|
||||
);
|
||||
|
||||
const existingMariaDBUserArray =
|
||||
userId && sqlUserID
|
||||
? await dbHandler({
|
||||
query: `SELECT * FROM mariadb_users WHERE id = ? AND user_id = ?`,
|
||||
values: [sqlUserID, userId],
|
||||
})
|
||||
: null;
|
||||
|
||||
/**
|
||||
* @type {import("../../types").MYSQL_mariadb_users_table_def | undefined}
|
||||
*/
|
||||
const activeMariadbUserObject = Array.isArray(
|
||||
existingMariaDBUserArray
|
||||
)
|
||||
? existingMariaDBUserArray?.[0]
|
||||
: undefined;
|
||||
|
||||
const isPrimary = activeMariadbUserObject
|
||||
? String(activeMariadbUserObject.primary)?.match(/1/)
|
||||
? true
|
||||
: false
|
||||
: false;
|
||||
|
||||
const isUserExisting = Boolean(existingUser?.[0]?.User);
|
||||
|
||||
const isThisPrimaryHost = Boolean(
|
||||
mariadbUserHost == defaultMariadbUserHost
|
||||
);
|
||||
|
||||
const dslUsername = `dsql_user_${id}`;
|
||||
const dsqlPassword = activeMariadbUserObject?.password
|
||||
? activeMariadbUserObject.password
|
||||
: isUserExisting
|
||||
? mariadb_pass
|
||||
: generator.generate({
|
||||
length: 16,
|
||||
numbers: true,
|
||||
symbols: true,
|
||||
uppercase: true,
|
||||
exclude: "*#.'`\"",
|
||||
});
|
||||
|
||||
const encryptedPassword = activeMariadbUserObject?.password
|
||||
? activeMariadbUserObject.password
|
||||
: isUserExisting
|
||||
? mariadb_pass
|
||||
: encrypt({
|
||||
data: dsqlPassword,
|
||||
encryptionKey: process.env.DSQL_ENCRYPTION_PASSWORD,
|
||||
encryptionSalt: process.env.DSQL_ENCRYPTION_SALT,
|
||||
});
|
||||
if (
|
||||
!isUserExisting &&
|
||||
!sqlUserID &&
|
||||
!isPrimary &&
|
||||
!mariadbUserHost &&
|
||||
!mariadbUser
|
||||
) {
|
||||
const createNewUser = await noDatabaseDbHandler(
|
||||
`CREATE USER IF NOT EXISTS '${dslUsername}'@'${defaultMariadbUserHost}' IDENTIFIED BY '${dsqlPassword}' REQUIRE SSL`
|
||||
);
|
||||
|
||||
console.log("createNewUser", createNewUser);
|
||||
|
||||
console.log(
|
||||
`User ${user.id}: ${user.first_name} ${user.last_name} SQL credentials successfully updated.`
|
||||
);
|
||||
|
||||
const updateUser = await dbHandler({
|
||||
query: `UPDATE users SET mariadb_user = ?, mariadb_host = ?, mariadb_pass = ? WHERE id = ?`,
|
||||
values: [
|
||||
dslUsername,
|
||||
defaultMariadbUserHost,
|
||||
encryptedPassword,
|
||||
user.id,
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
if (isPrimary) {
|
||||
const finalHost = mariadbUserHost
|
||||
? mariadbUserHost
|
||||
: mariadb_host;
|
||||
|
||||
const updateUser = await dbHandler({
|
||||
query: `UPDATE users SET mariadb_user = ?, mariadb_host = ?, mariadb_pass = ? WHERE id = ?`,
|
||||
values: [
|
||||
dslUsername,
|
||||
finalHost,
|
||||
encryptedPassword,
|
||||
user.id,
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////
|
||||
//////////////////////////////////////////////
|
||||
//////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* @description Handle mariadb_users table
|
||||
*/
|
||||
const existingMariadbPrimaryUser = await dbHandler({
|
||||
query: `SELECT * FROM mariadb_users WHERE user_id = ? AND \`primary\` = 1`,
|
||||
values: [id],
|
||||
});
|
||||
|
||||
const isPrimaryUserExisting = Boolean(
|
||||
Array.isArray(existingMariadbPrimaryUser) &&
|
||||
existingMariadbPrimaryUser?.[0]?.user_id
|
||||
);
|
||||
|
||||
/** @type {import("./handleGrants").GrantType[]} */
|
||||
const primaryUserGrants = [
|
||||
{
|
||||
database: "*",
|
||||
table: "*",
|
||||
privileges: ["ALL"],
|
||||
},
|
||||
];
|
||||
|
||||
if (!isPrimaryUserExisting) {
|
||||
const insertPrimaryMariadbUser = await dbHandler({
|
||||
query: `INSERT INTO mariadb_users (user_id, username, password, \`primary\`, grants) VALUES (?, ?, ?, ?, ?)`,
|
||||
values: [
|
||||
id,
|
||||
dslUsername,
|
||||
encryptedPassword,
|
||||
"1",
|
||||
JSON.stringify(primaryUserGrants),
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////
|
||||
|
||||
const existingExtraMariadbUsers = await dbHandler({
|
||||
query: `SELECT * FROM mariadb_users WHERE user_id = ? AND \`primary\` != '1'`,
|
||||
values: [id],
|
||||
});
|
||||
|
||||
if (Array.isArray(existingExtraMariadbUsers)) {
|
||||
for (let i = 0; i < existingExtraMariadbUsers.length; i++) {
|
||||
const mariadbUser = existingExtraMariadbUsers[i];
|
||||
const {
|
||||
user_id,
|
||||
username,
|
||||
host,
|
||||
password,
|
||||
primary,
|
||||
grants,
|
||||
} = mariadbUser;
|
||||
|
||||
if (mariadbUser && username != mariadbUser) continue;
|
||||
if (mariadbUserHost && host != mariadbUserHost) continue;
|
||||
|
||||
const decrptedPassword = decrypt({
|
||||
encryptedString: password,
|
||||
encryptionKey: process.env.DSQL_ENCRYPTION_PASSWORD,
|
||||
encryptionSalt: process.env.DSQL_ENCRYPTION_SALT,
|
||||
});
|
||||
|
||||
const existingExtraMariadbUser = await noDatabaseDbHandler(
|
||||
`SELECT * FROM mysql.user WHERE User = '${username}' AND Host = '${host}'`
|
||||
);
|
||||
|
||||
const isExtraMariadbUserExisting = Boolean(
|
||||
existingExtraMariadbUser?.[0]?.User
|
||||
);
|
||||
|
||||
if (!isExtraMariadbUserExisting) {
|
||||
await noDatabaseDbHandler(
|
||||
`CREATE USER IF NOT EXISTS '${username}'@'${host}' IDENTIFIED BY '${decrptedPassword}' REQUIRE SSL`
|
||||
);
|
||||
}
|
||||
|
||||
const isGrantHandled = await handleGrants({
|
||||
username,
|
||||
host,
|
||||
grants:
|
||||
grants && typeof grants == "string"
|
||||
? JSON.parse(grants)
|
||||
: [],
|
||||
userId: String(userId),
|
||||
});
|
||||
|
||||
if (!isGrantHandled) {
|
||||
console.log(
|
||||
`Error in handling grants for user ${username}@${host}`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////
|
||||
//////////////////////////////////////////////
|
||||
//////////////////////////////////////////////
|
||||
} catch (/** @type {any} */ error) {
|
||||
console.log(`Error in adding SQL user =>`, error.message);
|
||||
}
|
||||
}
|
||||
|
||||
process.exit();
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
}
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
module.exports = refreshUsersAndGrants;
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
// @ts-check
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
require("dotenv").config({ path: "../../.env" });
|
||||
const generator = require("generate-password");
|
||||
const noDatabaseDbHandler = require("../utils/noDatabaseDbHandler");
|
||||
const dbHandler = require("../utils/dbHandler");
|
||||
const encrypt = require("../../functions/dsql/encrypt");
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
/**
|
||||
* Create database from Schema Function
|
||||
* ==============================================================================
|
||||
* @param {object} params - Single object params
|
||||
* @param {number|string|null} params.userId - User ID or null
|
||||
*/
|
||||
async function resetSQLCredentialsPasswords() {
|
||||
/**
|
||||
* @description Users
|
||||
* @type {*[] | null}
|
||||
*/ // @ts-ignore
|
||||
const users = await dbHandler({
|
||||
query: `SELECT * FROM users`,
|
||||
});
|
||||
|
||||
if (!users) {
|
||||
process.exit();
|
||||
}
|
||||
|
||||
for (let i = 0; i < users.length; i++) {
|
||||
const user = users[i];
|
||||
|
||||
if (!user) continue;
|
||||
|
||||
try {
|
||||
/**
|
||||
* @type {any[]}
|
||||
*/ // @ts-ignore
|
||||
const maridbUsers = await dbHandler({
|
||||
query: `SELECT * FROM mysql.user WHERE User = 'dsql_user_${user.id}'`,
|
||||
});
|
||||
|
||||
for (let j = 0; j < maridbUsers.length; j++) {
|
||||
const { User, Host } = maridbUsers[j];
|
||||
|
||||
const password = generator.generate({
|
||||
length: 16,
|
||||
numbers: true,
|
||||
symbols: true,
|
||||
uppercase: true,
|
||||
exclude: "*#.'`\"",
|
||||
});
|
||||
|
||||
const encryptedPassword = encrypt({
|
||||
data: password,
|
||||
encryptionKey: process.env.DSQL_ENCRYPTION_PASSWORD,
|
||||
encryptionSalt: process.env.DSQL_ENCRYPTION_SALT,
|
||||
});
|
||||
|
||||
await noDatabaseDbHandler(
|
||||
`SET PASSWORD FOR '${User}'@'${Host}' = PASSWORD('${password}')`
|
||||
);
|
||||
|
||||
if (user.mariadb_user == User && user.mariadb_host == Host) {
|
||||
const updateUser = await dbHandler({
|
||||
query: `UPDATE users SET mariadb_pass = ? WHERE id = ?`,
|
||||
values: [encryptedPassword, user.id],
|
||||
});
|
||||
}
|
||||
|
||||
console.log(
|
||||
`User ${user.id}: ${user.first_name} ${user.last_name} Password Updated successfully added.`
|
||||
);
|
||||
}
|
||||
} catch (/** @type {any} */ error) {
|
||||
console.log(
|
||||
`Error Updating User ${user.id} Password =>`,
|
||||
error.message
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
process.exit();
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
}
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
resetSQLCredentialsPasswords();
|
||||
+185
@@ -0,0 +1,185 @@
|
||||
// @ts-check
|
||||
|
||||
const path = require("path");
|
||||
require("dotenv").config({ path: "../../../.env" });
|
||||
const fs = require("fs");
|
||||
const { execSync } = require("child_process");
|
||||
const EJSON = require("../../../utils/ejson");
|
||||
const DB_HANDLER = require("../../../utils/backend/global-db/DB_HANDLER");
|
||||
const addDbEntry = require("../../../functions/backend/db/addDbEntry");
|
||||
const addMariadbUser = require("../../../functions/backend/addMariadbUser");
|
||||
const updateDbEntry = require("../../../functions/backend/db/updateDbEntry");
|
||||
const hashPassword = require("../../../functions/dsql/hashPassword");
|
||||
|
||||
const tmpDir = process.argv[process.argv.length - 1];
|
||||
|
||||
/**
|
||||
* # Create New User
|
||||
*/
|
||||
async function createUser() {
|
||||
/**
|
||||
* Validate Form
|
||||
*
|
||||
* @description Check if request body is valid
|
||||
*/
|
||||
try {
|
||||
const isTmpDir = Boolean(tmpDir?.match(/\.json$/));
|
||||
const targetPath = isTmpDir
|
||||
? path.resolve(process.cwd(), tmpDir)
|
||||
: path.resolve(__dirname, "./new-user.json");
|
||||
|
||||
const userObj = EJSON.parse(fs.readFileSync(targetPath, "utf-8"));
|
||||
|
||||
if (typeof userObj !== "object" || Array.isArray(userObj))
|
||||
throw new Error("User Object Invalid!");
|
||||
|
||||
const ROOT_DIR = path.resolve(__dirname, "../../../");
|
||||
|
||||
/**
|
||||
* Validate Form
|
||||
*
|
||||
* @description Check if request body is valid
|
||||
*/
|
||||
const first_name = userObj.first_name;
|
||||
const last_name = userObj.last_name;
|
||||
const email = userObj.email;
|
||||
const password = userObj.password;
|
||||
const username = userObj.username;
|
||||
|
||||
if (!email?.match(/.*@.*\..*/)) return false;
|
||||
|
||||
if (
|
||||
!first_name?.match(/^[a-zA-Z]+$/) ||
|
||||
!last_name?.match(/^[a-zA-Z]+$/)
|
||||
)
|
||||
return false;
|
||||
|
||||
if (password?.match(/ /)) return false;
|
||||
|
||||
if (username?.match(/ /)) return false;
|
||||
|
||||
let hashedPassword = hashPassword({
|
||||
encryptionKey: process.env.DSQL_ENCRYPTION_PASSWORD || "",
|
||||
password: password,
|
||||
});
|
||||
|
||||
let existingUser = await DB_HANDLER(
|
||||
`SELECT * FROM users WHERE email='${email}'`
|
||||
);
|
||||
|
||||
if (existingUser?.[0]) {
|
||||
console.log("User Exists");
|
||||
return false;
|
||||
}
|
||||
|
||||
const newUser = await addDbEntry({
|
||||
dbFullName: "datasquirel",
|
||||
tableName: "users",
|
||||
data: { ...userObj, password: hashedPassword },
|
||||
});
|
||||
|
||||
if (!newUser?.insertId) return false;
|
||||
|
||||
/**
|
||||
* Add a Mariadb User for this User
|
||||
*/
|
||||
await addMariadbUser({ userId: newUser.insertId });
|
||||
|
||||
const STATIC_ROOT = process.env.DSQL_STATIC_SERVER_DIR;
|
||||
|
||||
if (!STATIC_ROOT) {
|
||||
console.log("Static File ENV not Found!");
|
||||
throw new Error("No Static Path");
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new user folder and file
|
||||
*
|
||||
* @description Create new user folder and file
|
||||
*/
|
||||
let newUserSchemaFolderPath = `${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${newUser.insertId}`;
|
||||
let newUserMediaFolderPath = path.join(
|
||||
STATIC_ROOT,
|
||||
`images/user-images/user-${newUser.insertId}`
|
||||
);
|
||||
|
||||
fs.mkdirSync(newUserSchemaFolderPath, { recursive: true });
|
||||
fs.mkdirSync(newUserMediaFolderPath, { recursive: true });
|
||||
|
||||
fs.writeFileSync(
|
||||
`${newUserSchemaFolderPath}/main.json`,
|
||||
JSON.stringify([]),
|
||||
"utf8"
|
||||
);
|
||||
|
||||
const imageBasePath = path.join(
|
||||
STATIC_ROOT,
|
||||
`images/user-images/user-${newUser.insertId}`
|
||||
);
|
||||
|
||||
if (!fs.existsSync(imageBasePath)) {
|
||||
fs.mkdirSync(imageBasePath, { recursive: true });
|
||||
}
|
||||
|
||||
let imagePath = path.join(
|
||||
STATIC_ROOT,
|
||||
`images/user-images/user-${newUser.insertId}/user-${newUser.insertId}-profile.jpg`
|
||||
);
|
||||
|
||||
let imageThumbnailPath = path.join(
|
||||
STATIC_ROOT,
|
||||
`images/user-images/user-${newUser.insertId}/user-${newUser.insertId}-profile-thumbnail.jpg`
|
||||
);
|
||||
|
||||
let prodImageUrl = imagePath.replace(
|
||||
STATIC_ROOT,
|
||||
process.env.DSQL_STATIC_HOST || ""
|
||||
);
|
||||
let prodImageThumbnailUrl = imageThumbnailPath.replace(
|
||||
STATIC_ROOT,
|
||||
process.env.DSQL_STATIC_HOST || ""
|
||||
);
|
||||
|
||||
fs.copyFileSync(
|
||||
path.join(ROOT_DIR, "/public/images/user-preset.png"),
|
||||
imagePath
|
||||
);
|
||||
fs.copyFileSync(
|
||||
path.join(ROOT_DIR, "/public/images/user-preset-thumbnail.png"),
|
||||
imageThumbnailPath
|
||||
);
|
||||
|
||||
execSync(`chmod 644 ${imagePath} ${imageThumbnailPath}`);
|
||||
|
||||
const updateImages = await updateDbEntry({
|
||||
dbFullName: "datasquirel",
|
||||
tableName: "users",
|
||||
identifierColumnName: "id",
|
||||
identifierValue: newUser.insertId,
|
||||
data: {
|
||||
image: prodImageUrl,
|
||||
image_thumbnail: prodImageThumbnailUrl,
|
||||
},
|
||||
});
|
||||
|
||||
if (isTmpDir) {
|
||||
try {
|
||||
fs.unlinkSync(path.resolve(process.cwd(), tmpDir));
|
||||
} catch (error) {}
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (/** @type {any} */ error) {
|
||||
console.log(`Error in creating user => ${error.message}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
createUser().then((res) => {
|
||||
if (res) {
|
||||
console.log("User Creation Success!!!");
|
||||
} else {
|
||||
console.log("User Creation Failed!");
|
||||
}
|
||||
process.exit();
|
||||
});
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
// @ts-check
|
||||
|
||||
const path = require("path");
|
||||
require("dotenv").config({ path: "../../../.env" });
|
||||
const fs = require("fs");
|
||||
const EJSON = require("../../../utils/ejson");
|
||||
const hashPassword = require("../../../functions/dsql/hashPassword");
|
||||
const updateDbEntry = require("../../../functions/backend/db/updateDbEntry");
|
||||
|
||||
const tmpDir = process.argv[process.argv.length - 1];
|
||||
|
||||
/**
|
||||
* # Create New User
|
||||
*/
|
||||
async function createUser() {
|
||||
/**
|
||||
* Validate Form
|
||||
*
|
||||
* @description Check if request body is valid
|
||||
*/
|
||||
try {
|
||||
const isTmpDir = Boolean(tmpDir?.match(/\.json$/));
|
||||
const targetPath = isTmpDir
|
||||
? path.resolve(process.cwd(), tmpDir)
|
||||
: path.resolve(__dirname, "./update-user.json");
|
||||
const updateUserObj = EJSON.parse(fs.readFileSync(targetPath, "utf-8"));
|
||||
|
||||
if (typeof updateUserObj !== "object" || Array.isArray(updateUserObj))
|
||||
throw new Error("Update User Object Invalid!");
|
||||
|
||||
let hashedPassword = updateUserObj.password
|
||||
? hashPassword({
|
||||
encryptionKey: process.env.DSQL_ENCRYPTION_PASSWORD || "",
|
||||
password: updateUserObj.password,
|
||||
})
|
||||
: undefined;
|
||||
|
||||
let updatePayload = { ...updateUserObj };
|
||||
if (hashedPassword) {
|
||||
updatePayload["password"] = hashedPassword;
|
||||
}
|
||||
|
||||
/** @type {any} */
|
||||
const newUser = await updateDbEntry({
|
||||
dbFullName: "datasquirel",
|
||||
tableName: "users",
|
||||
data: { ...updatePayload, id: undefined },
|
||||
identifierColumnName: "id",
|
||||
identifierValue: updatePayload.id,
|
||||
});
|
||||
|
||||
if (!newUser?.affectedRows) return false;
|
||||
|
||||
if (isTmpDir) {
|
||||
try {
|
||||
fs.unlinkSync(path.resolve(process.cwd(), tmpDir));
|
||||
} catch (error) {}
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (/** @type {any} */ error) {
|
||||
console.log(`Error in creating user => ${error.message}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
createUser().then((res) => {
|
||||
if (res) {
|
||||
console.log("User Update Success!!!");
|
||||
} else {
|
||||
console.log("User Update Failed!");
|
||||
}
|
||||
process.exit();
|
||||
});
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"id": "1",
|
||||
"verification_status": "1"
|
||||
}
|
||||
Executable
+24
@@ -0,0 +1,24 @@
|
||||
// @ts-check
|
||||
|
||||
const fs = require("fs");
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
/**
|
||||
* Grab Schema
|
||||
*
|
||||
* @description Grab Schema
|
||||
*/
|
||||
const imageBase64 = fs.readFileSync(
|
||||
"./../public/images/unique-tokens-icon.png",
|
||||
"base64"
|
||||
);
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
// @ts-check
|
||||
|
||||
const fs = require("fs");
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
const varDatabaseDbHandler = require("../functions/backend/varDatabaseDbHandler");
|
||||
const DB_HANDLER = require("../utils/backend/global-db/DB_HANDLER");
|
||||
|
||||
/** ****************************************************************************** */
|
||||
|
||||
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 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];
|
||||
|
||||
/** @type {any} */
|
||||
const dbObject = {
|
||||
dbName: db_name,
|
||||
dbSlug: db_slug,
|
||||
dbFullName: db_full_name,
|
||||
dbDescription: db_description,
|
||||
dbImage: db_image,
|
||||
tables: [],
|
||||
};
|
||||
|
||||
const tables = await 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];
|
||||
|
||||
/** @type {any} */
|
||||
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];
|
||||
|
||||
/** @type {any} */
|
||||
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(
|
||||
`${String(
|
||||
process.env.DSQL_USER_DB_SCHEMA_PATH
|
||||
)}/user-${userId}/main.json`,
|
||||
JSON.stringify(dbWrite, null, 4),
|
||||
"utf-8"
|
||||
);
|
||||
|
||||
process.exit();
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
recoverMainJsonFromDb();
|
||||
Executable
+103
@@ -0,0 +1,103 @@
|
||||
// @ts-check
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
const generator = require("generate-password");
|
||||
const noDatabaseDbHandler = require("./utils/noDatabaseDbHandler");
|
||||
const dbHandler = require("./utils/dbHandler");
|
||||
const encrypt = require("../functions/dsql/encrypt");
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
/**
|
||||
* Create database from Schema Function
|
||||
* ==============================================================================
|
||||
* @param {object} params - Single object params
|
||||
* @param {number|string|null} params.userId - User ID or null
|
||||
*/
|
||||
async function resetSQLCredentials() {
|
||||
/**
|
||||
* @description Users
|
||||
* @type {*[] | null}
|
||||
*/ // @ts-ignore
|
||||
const users = await dbHandler({
|
||||
query: `SELECT * FROM users`,
|
||||
});
|
||||
|
||||
if (!users) {
|
||||
process.exit();
|
||||
}
|
||||
|
||||
for (let i = 0; i < users.length; i++) {
|
||||
const user = users[i];
|
||||
|
||||
if (!user) continue;
|
||||
|
||||
const defaultMariadbUserHost = process.env.DSQL_DB_HOST || "127.0.0.1";
|
||||
|
||||
try {
|
||||
const username = `dsql_user_${user.id}`;
|
||||
const password = generator.generate({
|
||||
length: 16,
|
||||
numbers: true,
|
||||
symbols: true,
|
||||
uppercase: true,
|
||||
exclude: "*#.'`\"",
|
||||
});
|
||||
const encryptedPassword = encrypt({ data: password });
|
||||
|
||||
await noDatabaseDbHandler(`DROP USER IF EXISTS '${username}'@'%'`);
|
||||
await noDatabaseDbHandler(
|
||||
`DROP USER IF EXISTS '${username}'@'${defaultMariadbUserHost}'`
|
||||
);
|
||||
|
||||
await noDatabaseDbHandler(
|
||||
`CREATE USER IF NOT EXISTS '${username}'@'${defaultMariadbUserHost}' IDENTIFIED BY '${password}' REQUIRE SSL`
|
||||
);
|
||||
|
||||
await noDatabaseDbHandler(
|
||||
`GRANT ALL PRIVILEGES ON \`datasquirel_user_${user.id}_%\`.* TO '${username}'@'${defaultMariadbUserHost}'`
|
||||
);
|
||||
|
||||
await noDatabaseDbHandler(`FLUSH PRIVILEGES`);
|
||||
|
||||
const updateUser = await dbHandler({
|
||||
query: `UPDATE users SET mariadb_user = ?, mariadb_host = ?, mariadb_pass = ? WHERE id = ?`,
|
||||
values: [
|
||||
username,
|
||||
defaultMariadbUserHost,
|
||||
encryptedPassword,
|
||||
user.id,
|
||||
],
|
||||
});
|
||||
|
||||
console.log(
|
||||
`User ${user.id}: ${user.first_name} ${user.last_name} SQL credentials successfully added.`
|
||||
);
|
||||
} catch (/** @type {any} */ error) {
|
||||
console.log(`Error in adding SQL user =>`, error.message);
|
||||
}
|
||||
}
|
||||
|
||||
process.exit();
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
}
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
resetSQLCredentials();
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
// @ts-check
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
const generator = require("generate-password");
|
||||
const noDatabaseDbHandler = require("./utils/noDatabaseDbHandler");
|
||||
const dbHandler = require("./utils/dbHandler");
|
||||
const encrypt = require("../functions/dsql/encrypt");
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
/**
|
||||
* Create database from Schema Function
|
||||
* ==============================================================================
|
||||
* @param {object} params - Single object params
|
||||
* @param {number|string|null} params.userId - User ID or null
|
||||
*/
|
||||
async function resetSQLCredentialsPasswords() {
|
||||
/**
|
||||
* @description Users
|
||||
* @type {*[] | null}
|
||||
*/ // @ts-ignore
|
||||
const users = await dbHandler({
|
||||
query: `SELECT * FROM users`,
|
||||
});
|
||||
|
||||
if (!users) {
|
||||
process.exit();
|
||||
}
|
||||
|
||||
for (let i = 0; i < users.length; i++) {
|
||||
const user = users[i];
|
||||
|
||||
if (!user) continue;
|
||||
|
||||
const defaultMariadbUserHost = process.env.DSQL_DB_HOST || "127.0.0.1";
|
||||
|
||||
try {
|
||||
const username = `dsql_user_${user.id}`;
|
||||
const password = generator.generate({
|
||||
length: 16,
|
||||
numbers: true,
|
||||
symbols: true,
|
||||
uppercase: true,
|
||||
exclude: "*#.'`\"",
|
||||
});
|
||||
const encryptedPassword = encrypt({ data: password });
|
||||
|
||||
await noDatabaseDbHandler(
|
||||
`SET PASSWORD FOR '${username}'@'${defaultMariadbUserHost}' = PASSWORD('${password}')`
|
||||
);
|
||||
|
||||
const updateUser = await dbHandler({
|
||||
query: `UPDATE users SET mariadb_pass = ? WHERE id = ?`,
|
||||
values: [encryptedPassword, user.id],
|
||||
});
|
||||
|
||||
console.log(
|
||||
`User ${user.id}: ${user.first_name} ${user.last_name} Password Updated successfully added.`
|
||||
);
|
||||
} catch (/** @type {any} */ error) {
|
||||
console.log(
|
||||
`Error Updating User ${user.id} Password =>`,
|
||||
error.message
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
process.exit();
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
}
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
resetSQLCredentialsPasswords();
|
||||
Executable
+93
@@ -0,0 +1,93 @@
|
||||
// @ts-check
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
const generator = require("generate-password");
|
||||
const noDatabaseDbHandler = require("./utils/noDatabaseDbHandler");
|
||||
const dbHandler = require("./utils/dbHandler");
|
||||
const encrypt = require("../functions/dsql/encrypt");
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
/**
|
||||
* Create database from Schema Function
|
||||
* ==============================================================================
|
||||
* @param {object} params - Single object params
|
||||
* @param {number|string|null} params.userId - User ID or null
|
||||
*/
|
||||
async function setSQLCredentials() {
|
||||
/**
|
||||
* @description Users
|
||||
* @type {*[] | null}
|
||||
*/ // @ts-ignore
|
||||
const users = await dbHandler({
|
||||
query: `SELECT * FROM users`,
|
||||
});
|
||||
|
||||
if (!users) {
|
||||
process.exit();
|
||||
}
|
||||
|
||||
for (let i = 0; i < users.length; i++) {
|
||||
const user = users[i];
|
||||
|
||||
if (!user) continue;
|
||||
if (user.mariadb_user && user.mariadb_pass) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
const username = `dsql_user_${user.id}`;
|
||||
const password = generator.generate({
|
||||
length: 16,
|
||||
numbers: true,
|
||||
symbols: true,
|
||||
uppercase: true,
|
||||
exclude: "*#.'`\"",
|
||||
});
|
||||
const encryptedPassword = encrypt({ data: password });
|
||||
|
||||
await noDatabaseDbHandler(
|
||||
`CREATE USER IF NOT EXISTS '${username}'@'127.0.0.1' IDENTIFIED BY '${password}' REQUIRE SSL`
|
||||
);
|
||||
|
||||
await noDatabaseDbHandler(
|
||||
`GRANT ALL PRIVILEGES ON \`datasquirel\\_user\\_${user.id}\\_%\`.* TO '${username}'@'127.0.0.1'`
|
||||
);
|
||||
await noDatabaseDbHandler(`FLUSH PRIVILEGES`);
|
||||
|
||||
const updateUser = await dbHandler({
|
||||
query: `UPDATE users SET mariadb_user = ?, mariadb_host = '127.0.0.1' mariadb_pass = ? WHERE id = ?`,
|
||||
values: [username, encryptedPassword, user.id],
|
||||
});
|
||||
|
||||
console.log(
|
||||
`User ${user.id}: ${user.first_name} ${user.last_name} SQL credentials successfully added.`
|
||||
);
|
||||
} catch (/** @type {any} */ error) {
|
||||
console.log(`Error in adding SQL user =>`, error.message);
|
||||
}
|
||||
}
|
||||
|
||||
process.exit();
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
}
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
setSQLCredentials();
|
||||
Executable
+29
@@ -0,0 +1,29 @@
|
||||
// @ts-check
|
||||
|
||||
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!");
|
||||
});
|
||||
});
|
||||
Executable
+58
@@ -0,0 +1,58 @@
|
||||
// @ts-check
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
require("dotenv").config({ path: "./.env" });
|
||||
const grabDbSSL = require("../utils/backend/grabDbSSL");
|
||||
const mysql = require("serverless-mysql");
|
||||
|
||||
const connection = mysql({
|
||||
config: {
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASS,
|
||||
// database: process.env.DSQL_DB_NAME,
|
||||
charset: "utf8mb4",
|
||||
ssl: grabDbSSL(),
|
||||
},
|
||||
});
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* # Main DB Handler Function
|
||||
* @async
|
||||
*
|
||||
* @param {object} params
|
||||
* @param {string} params.query
|
||||
* @param {string[] | object} [params.values]
|
||||
* @param {string} [params.database]
|
||||
*
|
||||
* @returns {Promise<object|null>}
|
||||
*/
|
||||
(async () => {
|
||||
/**
|
||||
* Switch Database
|
||||
*
|
||||
* @description If a database is provided, switch to it
|
||||
*/
|
||||
try {
|
||||
const result = await connection.query("SHOW DATABASES");
|
||||
|
||||
const parsedResults = JSON.parse(JSON.stringify(result));
|
||||
|
||||
console.log("parsedResults =>", parsedResults);
|
||||
} catch (/** @type {any} */ error) {
|
||||
console.log("Connection query ERROR =>", error.message);
|
||||
} finally {
|
||||
connection.end();
|
||||
process.exit();
|
||||
}
|
||||
})();
|
||||
Executable
+221
@@ -0,0 +1,221 @@
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
|
||||
const dbEngine = require("@moduletrace/datasquirel/engine");
|
||||
const http = require("http");
|
||||
|
||||
const datasquirel = require("@moduletrace/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.DSQL_DB_HOST,
|
||||
// dbPassword: process.env.DSQL_DB_PASSWORD,
|
||||
// dbUsername: process.env.DSQL_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.message);
|
||||
});
|
||||
|
||||
httpsRequest.end();
|
||||
});
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
return httpResponse;
|
||||
}
|
||||
Executable
+102
@@ -0,0 +1,102 @@
|
||||
// @ts-check
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
const generator = require("generate-password");
|
||||
const noDatabaseDbHandler = require("./utils/noDatabaseDbHandler");
|
||||
const dbHandler = require("./utils/dbHandler");
|
||||
const encrypt = require("../functions/dsql/encrypt");
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
/**
|
||||
* Create database from Schema Function
|
||||
* ==============================================================================
|
||||
* @param {object} params - Single object params
|
||||
* @param {number|string|null} params.userId - User ID or null
|
||||
*/
|
||||
async function testSQLEscape() {
|
||||
/**
|
||||
* @description Users
|
||||
* @type {*[] | null}
|
||||
*/ // @ts-ignore
|
||||
const users = await dbHandler({
|
||||
query: `SELECT * FROM users`,
|
||||
});
|
||||
|
||||
if (!users) {
|
||||
process.exit();
|
||||
}
|
||||
|
||||
for (let i = 0; i < users.length; i++) {
|
||||
const user = users[i];
|
||||
|
||||
if (!user) continue;
|
||||
|
||||
const defaultMariadbUserHost = process.env.DSQL_DB_HOST || "127.0.0.1";
|
||||
|
||||
try {
|
||||
const username = `dsql_user_${user.id}`;
|
||||
const password = generator.generate({
|
||||
length: 16,
|
||||
numbers: true,
|
||||
symbols: true,
|
||||
uppercase: true,
|
||||
exclude: "*#.'`\"",
|
||||
});
|
||||
const encryptedPassword = encrypt({ data: password });
|
||||
|
||||
await noDatabaseDbHandler(
|
||||
`DROP USER '${username}'@'${defaultMariadbUserHost}'`
|
||||
);
|
||||
|
||||
await noDatabaseDbHandler(
|
||||
`CREATE USER IF NOT EXISTS '${username}'@'${defaultMariadbUserHost}' IDENTIFIED BY '${password}' REQUIRE SSL`
|
||||
);
|
||||
|
||||
await noDatabaseDbHandler(
|
||||
`GRANT ALL PRIVILEGES ON \`datasquirel\\_user\\_${user.id}\\_%\`.* TO '${username}'@'${defaultMariadbUserHost}'`
|
||||
);
|
||||
|
||||
await noDatabaseDbHandler(`FLUSH PRIVILEGES`);
|
||||
|
||||
const updateUser = await dbHandler({
|
||||
query: `UPDATE users SET mariadb_user = ?, mariadb_host = ? mariadb_pass = ? WHERE id = ?`,
|
||||
values: [
|
||||
username,
|
||||
defaultMariadbUserHost,
|
||||
encryptedPassword,
|
||||
user.id,
|
||||
],
|
||||
});
|
||||
|
||||
console.log(
|
||||
`User ${user.id}: ${user.first_name} ${user.last_name} SQL credentials successfully added.`
|
||||
);
|
||||
} catch (/** @type {any} */ error) {
|
||||
console.log(`Error in adding SQL user =>`, error.message);
|
||||
}
|
||||
}
|
||||
|
||||
process.exit();
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
}
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
testSQLEscape();
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
// @ts-check
|
||||
|
||||
const DB_HANDLER = require("../utils/backend/global-db/DB_HANDLER");
|
||||
const fs = require("fs");
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
async function updateChildrenTablesOnDb() {
|
||||
/**
|
||||
* Grab Schema
|
||||
*
|
||||
* @description Grab Schema
|
||||
*/
|
||||
try {
|
||||
const rootDir = String(process.env.DSQL_USER_DB_SCHEMA_PATH);
|
||||
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`, "utf-8")
|
||||
);
|
||||
|
||||
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 DB_HANDLER(
|
||||
`SELECT * FROM user_database_tables ${WHERE_CLAUSE}`
|
||||
);
|
||||
|
||||
if (existingTableInDb && existingTableInDb[0]) {
|
||||
const updateChildrenTablesInfo = await 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}'`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} 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();
|
||||
Executable
+60
@@ -0,0 +1,60 @@
|
||||
// @ts-check
|
||||
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
const varDatabaseDbHandler = require("../functions/backend/varDatabaseDbHandler");
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
/**
|
||||
* 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();
|
||||
});
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
// @ts-check
|
||||
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
|
||||
const serverError = require("../functions/backend/serverError");
|
||||
const varDatabaseDbHandler = require("./utils/varDatabaseDbHandler");
|
||||
const DB_HANDLER = require("../utils/backend/global-db/DB_HANDLER");
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
/**
|
||||
* 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 DB_HANDLER(
|
||||
`SELECT db_slug FROM user_databases WHERE id='${db_id}'`
|
||||
);
|
||||
|
||||
const updateTableSlug = await DB_HANDLER(
|
||||
`UPDATE user_database_tables SET db_slug='${dbSlug[0].db_slug}' WHERE db_id='${db_id}'`
|
||||
);
|
||||
} catch (/** @type {any} */ error) {
|
||||
serverError({
|
||||
component:
|
||||
"shell/updateDbSlugsForTableRecords/main-catch-error",
|
||||
message: error.message,
|
||||
user: {},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
process.exit();
|
||||
});
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
Executable
+70
@@ -0,0 +1,70 @@
|
||||
// @ts-check
|
||||
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
const grabDbSSL = require("../utils/backend/grabDbSSL");
|
||||
const mysql = require("serverless-mysql");
|
||||
|
||||
const connection = mysql({
|
||||
config: {
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
database: process.env.DSQL_DB_NAME,
|
||||
charset: "utf8mb4",
|
||||
ssl: grabDbSSL(),
|
||||
},
|
||||
});
|
||||
|
||||
/**
|
||||
* # Main DB Handler Function
|
||||
* @async
|
||||
*
|
||||
* @param {object} params
|
||||
* @param {string} params.query
|
||||
* @param {string[] | object} [params.values]
|
||||
* @param {string} [params.database]
|
||||
*
|
||||
* @returns {Promise<object|null>}
|
||||
*/
|
||||
(async () => {
|
||||
/**
|
||||
* Switch Database
|
||||
*
|
||||
* @description If a database is provided, switch to it
|
||||
*/
|
||||
try {
|
||||
const result = await connection.query(
|
||||
"SELECT user,host,ssl_type FROM mysql.user"
|
||||
);
|
||||
const parsedResults = JSON.parse(JSON.stringify(result));
|
||||
|
||||
for (let i = 0; i < parsedResults.length; i++) {
|
||||
const user = parsedResults[i];
|
||||
|
||||
if (
|
||||
user.User !== process.env.DSQL_DB_READ_ONLY_USERNAME ||
|
||||
user.User !== process.env.DSQL_DB_FULL_ACCESS_USERNAME ||
|
||||
!user.User?.match(/dsql_user_.*/i)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const { User, Host, ssl_type } = user;
|
||||
|
||||
if (ssl_type === "ANY") {
|
||||
continue;
|
||||
}
|
||||
|
||||
const addUserSSL = await connection.query(
|
||||
`ALTER USER '${User}'@'${Host}' REQUIRE SSL`
|
||||
);
|
||||
|
||||
console.log(`addUserSSL => ${User}@${Host}`, addUserSSL);
|
||||
}
|
||||
} catch (/** @type {any} */ error) {
|
||||
console.log("Connection query ERROR =>", error.message);
|
||||
} finally {
|
||||
connection.end();
|
||||
process.exit();
|
||||
}
|
||||
})();
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
// @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;
|
||||
}
|
||||
};
|
||||
Executable
+212
@@ -0,0 +1,212 @@
|
||||
// @ts-check
|
||||
|
||||
const varDatabaseDbHandler = require("./varDatabaseDbHandler");
|
||||
const generateColumnDescription = require("./generateColumnDescription");
|
||||
const supplementTable = require("./supplementTable");
|
||||
const dbHandler = require("./dbHandler");
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
/**
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
module.exports = async function createTable({
|
||||
dbFullName,
|
||||
tableName,
|
||||
tableInfoArray,
|
||||
dbSchema,
|
||||
clone,
|
||||
tableSchema,
|
||||
recordedDbEntry,
|
||||
}) {
|
||||
/**
|
||||
* 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}\` (`);
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
try {
|
||||
if (!recordedDbEntry) {
|
||||
throw new Error("Recorded Db entry not found!");
|
||||
}
|
||||
|
||||
const existingTable = await varDatabaseDbHandler({
|
||||
database: "datasquirel",
|
||||
queryString: `SELECT * FROM user_database_tables WHERE db_id = ? AND table_slug = ?`,
|
||||
queryValuesArray: [recordedDbEntry.id, tableSchema?.tableName],
|
||||
});
|
||||
|
||||
/** @type {import("../../types").MYSQL_user_database_tables_table_def} */
|
||||
const table = existingTable?.[0];
|
||||
|
||||
if (!table?.id) {
|
||||
const newTableEntry = await dbHandler({
|
||||
query: `INSERT INTO user_database_tables SET ?`,
|
||||
values: {
|
||||
user_id: recordedDbEntry.user_id,
|
||||
db_id: recordedDbEntry.id,
|
||||
db_slug: recordedDbEntry.db_slug,
|
||||
table_name: tableSchema?.tableFullName,
|
||||
table_slug: tableSchema?.tableName,
|
||||
child_table: tableSchema?.childTable ? "1" : null,
|
||||
child_table_parent_database:
|
||||
tableSchema?.childTableDbFullName || null,
|
||||
child_table_parent_table:
|
||||
tableSchema?.childTableName || null,
|
||||
date_created: Date(),
|
||||
date_created_code: Date.now(),
|
||||
date_updated: Date(),
|
||||
date_updated_code: Date.now(),
|
||||
},
|
||||
database: "datasquirel",
|
||||
});
|
||||
}
|
||||
} catch (error) {}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
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,
|
||||
onUpdate,
|
||||
onUpdateLiteral,
|
||||
onDelete,
|
||||
onDeleteLiteral,
|
||||
defaultField,
|
||||
encrypted,
|
||||
json,
|
||||
newTempField,
|
||||
notNullValue,
|
||||
originName,
|
||||
plainText,
|
||||
pattern,
|
||||
patternFlags,
|
||||
richText,
|
||||
} = column;
|
||||
|
||||
if (foreignKey) {
|
||||
foreignKeys.push({
|
||||
fieldName: fieldName,
|
||||
...foreignKey,
|
||||
});
|
||||
}
|
||||
|
||||
let { fieldEntryText, newPrimaryKeySet } = generateColumnDescription({
|
||||
columnData: column,
|
||||
primaryKeySet: primaryKeySet,
|
||||
});
|
||||
|
||||
primaryKeySet = newPrimaryKeySet;
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
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;
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
};
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
Executable
+118
@@ -0,0 +1,118 @@
|
||||
// @ts-check
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
const mysql = require("serverless-mysql");
|
||||
const grabDbSSL = require("../../utils/backend/grabDbSSL");
|
||||
|
||||
let connection = mysql({
|
||||
config: {
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
database: process.env.DSQL_DB_NAME,
|
||||
charset: "utf8mb4",
|
||||
ssl: grabDbSSL(),
|
||||
},
|
||||
});
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* # 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 }) {
|
||||
/**
|
||||
* Switch Database
|
||||
*
|
||||
* @description If a database is provided, switch to it
|
||||
*/
|
||||
let isDbCorrect = true;
|
||||
|
||||
if (database) {
|
||||
connection = mysql({
|
||||
config: {
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
database: database,
|
||||
charset: "utf8mb4",
|
||||
ssl: grabDbSSL(),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (!isDbCorrect) {
|
||||
console.log(
|
||||
"Shell Db Handler ERROR in switching Database! Operation Failed!"
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Declare variables
|
||||
*
|
||||
* @description Declare "results" variable
|
||||
*/
|
||||
let results;
|
||||
|
||||
/**
|
||||
* Fetch from db
|
||||
*
|
||||
* @description Fetch data from db if no cache
|
||||
*/
|
||||
try {
|
||||
if (query && values) {
|
||||
results = await connection.query(query, values);
|
||||
} else {
|
||||
results = await connection.query(query);
|
||||
}
|
||||
|
||||
/** ********************* Clean up */
|
||||
await connection.end();
|
||||
} catch (/** @type {any} */ error) {
|
||||
if (process.env.FIRST_RUN) {
|
||||
return null;
|
||||
}
|
||||
|
||||
console.log("ERROR in dbHandler =>", error.message);
|
||||
console.log(error);
|
||||
console.log(connection.config());
|
||||
|
||||
fs.appendFileSync(
|
||||
path.resolve(__dirname, "../.tmp/dbErrorLogs.txt"),
|
||||
JSON.stringify(error, null, 4) + "\n" + Date() + "\n\n\n",
|
||||
"utf8"
|
||||
);
|
||||
results = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return results
|
||||
*
|
||||
* @description Return results add to cache if "req" param is passed
|
||||
*/
|
||||
if (results) {
|
||||
return JSON.parse(JSON.stringify(results));
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
// @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 };
|
||||
};
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
// @ts-check
|
||||
|
||||
const dbHandler = require("./dbHandler");
|
||||
|
||||
/**
|
||||
* Create database from Schema Function
|
||||
* ==============================================================================
|
||||
* @param {string} queryString - Query String
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
module.exports = async function noDatabaseDbHandler(queryString) {
|
||||
/**
|
||||
* 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 (/** @type {any} */ 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;
|
||||
}
|
||||
};
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
// @ts-check
|
||||
|
||||
module.exports = function slugToCamelTitle(/** @type {String} */ 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;
|
||||
}
|
||||
};
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
// @ts-check
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {object} param0
|
||||
* @param {import("../../types").DSQL_FieldSchemaType[]} param0.tableInfoArray
|
||||
* @returns
|
||||
*/
|
||||
module.exports = function supplementTable({ tableInfoArray }) {
|
||||
/**
|
||||
* Format tableInfoArray
|
||||
*
|
||||
* @description Format tableInfoArray
|
||||
*/
|
||||
let finalTableArray = tableInfoArray;
|
||||
const defaultFields = require("../../../package-shared/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;
|
||||
};
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
Executable
+593
File diff suppressed because it is too large
Load Diff
+71
@@ -0,0 +1,71 @@
|
||||
// @ts-check
|
||||
|
||||
const fs = require("fs");
|
||||
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").DSQL_TableSchemaType} [params.tableSchema] - Table schema
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
module.exports = async function varDatabaseDbHandler({
|
||||
queryString,
|
||||
queryValuesArray,
|
||||
database,
|
||||
tableSchema,
|
||||
}) {
|
||||
/**
|
||||
* 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,
|
||||
});
|
||||
} else {
|
||||
results = await dbHandler({
|
||||
query: queryString,
|
||||
database,
|
||||
});
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
} catch (/** @type {any} */ error) {
|
||||
console.log("Shell Vardb Error =>", error.message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return results
|
||||
*
|
||||
* @description Return results add to cache if "req" param is passed
|
||||
*/
|
||||
return results;
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
};
|
||||
Reference in New Issue
Block a user