This commit is contained in:
Tben 2023-08-13 14:16:32 +01:00
parent 43287b0329
commit 80e34fa6ec
5 changed files with 89 additions and 147 deletions

View File

@ -1,13 +1,12 @@
// @ts-check
const fs = require("fs"); const fs = require("fs");
require("dotenv").config({ path: "./../.env" }); require("dotenv").config({ path: "./../.env" });
//////////////////////////////////////// ////////////////////////////////////////
const noDatabaseDbHandler = require("../functions/backend/noDatabaseDbHandler"); const dbHandler = require("./utils/dbHandler");
const varDatabaseDbHandler = require("../functions/backend/varDatabaseDbHandler"); const varDatabaseDbHandler = require("./utils/varDatabaseDbHandler");
const createTable = require("./utils/createTable");
const slugToCamelTitle = require("./utils/slugToCamelTitle");
const updateTable = require("./utils/updateTable");
/** ****************************************************************************** */ /** ****************************************************************************** */
@ -31,8 +30,12 @@ async function recoverMainJsonFromDb() {
return; return;
} }
const databases = await global.DB_HANDLER(`SELECT * FROM user_databases WHERE user_id='${userId}'`); const databases = await dbHandler({
query: `SELECT * FROM user_databases WHERE user_id = ?`,
values: [userId],
});
/** @type {*[]} */
const dbWrite = []; const dbWrite = [];
for (let i = 0; i < databases.length; i++) { for (let i = 0; i < databases.length; i++) {
@ -44,10 +47,14 @@ async function recoverMainJsonFromDb() {
dbFullName: db_full_name, dbFullName: db_full_name,
dbDescription: db_description, dbDescription: db_description,
dbImage: db_image, dbImage: db_image,
/** @type {*[]} */
tables: [], tables: [],
}; };
const tables = await global.DB_HANDLER(`SELECT * FROM user_database_tables WHERE user_id='${userId}' AND db_id='${id}'`); const tables = await dbHandler({
query: `SELECT * FROM user_database_tables WHERE user_id = ? AND db_id = ?`,
values: [userId, id],
});
for (let j = 0; j < tables.length; j++) { for (let j = 0; j < tables.length; j++) {
const { table_name, table_slug, table_description } = tables[j]; const { table_name, table_slug, table_description } = tables[j];
@ -55,7 +62,9 @@ async function recoverMainJsonFromDb() {
const tableObject = { const tableObject = {
tableName: table_slug, tableName: table_slug,
tableFullName: table_name, tableFullName: table_name,
/** @type {*[]} */
fields: [], fields: [],
/** @type {*[]} */
indexes: [], indexes: [],
}; };
@ -64,24 +73,26 @@ async function recoverMainJsonFromDb() {
queryString: `SHOW COLUMNS FROM ${table_slug}`, queryString: `SHOW COLUMNS FROM ${table_slug}`,
}); });
for (let k = 0; k < tableFields.length; k++) { if (tableFields) {
const { Field, Type, Null, Default, Key } = tableFields[k]; for (let k = 0; k < tableFields.length; k++) {
const { Field, Type, Null, Default, Key } = tableFields[k];
const fieldObject = { const fieldObject = {
fieldName: Field, fieldName: Field,
dataType: Type.toUpperCase(), dataType: Type.toUpperCase(),
}; };
if (Default?.match(/./) && !Default?.match(/timestamp/i)) fieldObject["defaultValue"] = Default; if (Default?.match(/./) && !Default?.match(/timestamp/i)) fieldObject["defaultValue"] = Default;
if (Key?.match(/pri/i)) { if (Key?.match(/pri/i)) {
fieldObject["primaryKey"] = true; fieldObject["primaryKey"] = true;
fieldObject["autoIncrement"] = 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);
} }
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); dbObject.tables.push(tableObject);

View File

@ -1,81 +0,0 @@
const fs = require("fs");
require("dotenv").config({ path: "./../.env" });
////////////////////////////////////////
const noDatabaseDbHandler = require("../functions/backend/noDatabaseDbHandler");
const serverError = require("../functions/backend/serverError");
const varDatabaseDbHandler = require("../functions/backend/varDatabaseDbHandler");
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
async function updateChildrenTablesOnDb() {
/**
* Grab Schema
*
* @description Grab Schema
*/
try {
const rootDir = "./../jsonData/dbSchemas/users";
const userFolders = fs.readdirSync(rootDir);
for (let i = 0; i < userFolders.length; i++) {
const folder = userFolders[i];
const userId = folder.replace(/user-/, "");
const databases = JSON.parse(fs.readFileSync(`${rootDir}/${folder}/main.json`));
for (let j = 0; j < databases.length; j++) {
const db = databases[j];
const dbTables = db.tables;
for (let k = 0; k < dbTables.length; k++) {
const table = dbTables[k];
if (table?.childTable) {
const originTableName = table.childTableName;
const originDbName = table.childTableDbFullName;
const WHERE_CLAUSE = `WHERE user_id='${userId}' AND db_slug='${db.dbSlug}' AND table_slug='${table.tableName}'`;
const existingTableInDb = await global.DB_HANDLER(`SELECT * FROM user_database_tables ${WHERE_CLAUSE}`);
console.log(existingTableInDb);
if (existingTableInDb && existingTableInDb[0]) {
const updateChildrenTablesInfo = await global.DB_HANDLER(`UPDATE user_database_tables SET child_table='1',child_table_parent_database='${originDbName}',child_table_parent_table='${originTableName}' WHERE id='${existingTableInDb[0].id}'`);
console.log(updateChildrenTablesInfo);
}
}
}
}
}
} catch (error) {
console.log(error);
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
process.exit();
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
}
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
// const userArg = process.argv[process.argv.indexOf("--user")];
// const externalUser = process.argv[process.argv.indexOf("--user") + 1];
updateChildrenTablesOnDb();

View File

@ -7,6 +7,7 @@
*/ */
const fs = require("fs"); const fs = require("fs");
const httpsRequest = require("./httpsRequest"); const httpsRequest = require("./httpsRequest");
const dbHandler = require("../../../engine/utils/dbHandler");
////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////
@ -122,7 +123,10 @@ async function githubLogin({ code, clientId, clientSecret }) {
//////////////////////////////////////////////// ////////////////////////////////////////////////
if (!gitHubUser?.email) { if (!gitHubUser?.email) {
const existingGithubUser = await global.DB_HANDLER(`SELECT email FROM users WHERE social_login='1' AND social_platform='github' AND social_id='${gitHubUser?.id || ""}'`); const existingGithubUser = await dbHandler({
query: `SELECT email FROM users WHERE social_login='1' AND social_platform='github' AND social_id= ?`,
values: [gitHubUser?.id || ""],
});
if (existingGithubUser && existingGithubUser[0] && gitHubUser) { if (existingGithubUser && existingGithubUser[0] && gitHubUser) {
gitHubUser.email = existingGithubUser[0].email; gitHubUser.email = existingGithubUser[0].email;

View File

@ -1,3 +1,5 @@
// @ts-check
/** /**
* ============================================================================== * ==============================================================================
* Imports * Imports
@ -11,8 +13,8 @@ const fs = require("fs");
const { OAuth2Client } = require("google-auth-library"); const { OAuth2Client } = require("google-auth-library");
const { hashPassword } = require("../passwordHash"); const dbHandler = require("../../../engine/utils/dbHandler");
const serverError = require("../serverError"); const hashPassword = require("../../../../functions/hashPassword");
////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////
@ -44,6 +46,7 @@ module.exports = async function googleLogin({ usertype, foundUser, isSocialValid
//[CLIENT_ID_1, CLIENT_ID_2, CLIENT_ID_3] //[CLIENT_ID_1, CLIENT_ID_2, CLIENT_ID_3]
}); });
// @ts-ignore
const payload = ticket.payload; const payload = ticket.payload;
const userid = payload["sub"]; const userid = payload["sub"];
@ -63,7 +66,10 @@ module.exports = async function googleLogin({ usertype, foundUser, isSocialValid
//////////////////////////////////////////////// ////////////////////////////////////////////////
//////////////////////////////////////////////// ////////////////////////////////////////////////
let existinEmail = await global.DB_HANDLER(`SELECT * FROM ${usertype} WHERE email='${payload.email}' AND social_login!='1' AND social_platform!='google'`); let existinEmail = await dbHandler({
query: `SELECT * FROM ${usertype} WHERE email = ? AND social_login!='1' AND social_platform!='google'`,
values: [payload.email],
});
if (existinEmail && existinEmail[0]) { if (existinEmail && existinEmail[0]) {
loginFailureReason = "Email Exists Already"; loginFailureReason = "Email Exists Already";
@ -73,7 +79,10 @@ module.exports = async function googleLogin({ usertype, foundUser, isSocialValid
//////////////////////////////////////// ////////////////////////////////////////
foundUser = await global.DB_HANDLER(`SELECT * FROM ${usertype} WHERE email='${payload.email}' AND social_login='1' AND social_platform='google'`); foundUser = await dbHandler({
query: `SELECT * FROM ${usertype} WHERE email = ? AND social_login='1' AND social_platform='google'`,
values: [payload.email],
});
if (foundUser && foundUser[0]) { if (foundUser && foundUser[0]) {
newFoundUser = foundUser; newFoundUser = foundUser;
@ -84,50 +93,49 @@ module.exports = async function googleLogin({ usertype, foundUser, isSocialValid
//////////////////////////////////////////////// ////////////////////////////////////////////////
//////////////////////////////////////////////// ////////////////////////////////////////////////
let newUser = await global.DB_HANDLER(`INSERT INTO ${usertype} ( let newUser = await dbHandler({
first_name, query: `INSERT INTO ${usertype} (
last_name, first_name,
social_platform, last_name,
social_name, social_platform,
social_id, social_name,
email, social_id,
image, email,
image_thumbnail, image,
password, image_thumbnail,
verification_status, password,
social_login, verification_status,
terms_agreement, social_login,
date_created, terms_agreement,
date_code date_created,
) VALUES ( date_code
'${payload.given_name}', ) VALUES (
'${payload.family_name}', '${payload.given_name}',
'google', '${payload.family_name}',
'google_${payload.email.replace(/@.*/, "")}', 'google',
'${payload.sub}', 'google_${payload.email.replace(/@.*/, "")}',
'${payload.email}', '${payload.sub}',
'${payload.picture}', '${payload.email}',
'${payload.picture}', '${payload.picture}',
'${socialHashedPassword}', '${payload.picture}',
'1', '${socialHashedPassword}',
'1', '1',
'1', '1',
'${Date()}', '1',
'${Date.now()}' '${Date()}',
)`); '${Date.now()}'
)`,
});
newFoundUser = await global.DB_HANDLER(`SELECT * FROM ${usertype} WHERE id='${newUser.insertId}'`); newFoundUser = await dbHandler({
query: `SELECT * FROM ${usertype} WHERE id = ?`,
values: [newUser.insertId],
});
//////////////////////////////////////////////// ////////////////////////////////////////////////
//////////////////////////////////////////////// ////////////////////////////////////////////////
//////////////////////////////////////////////// ////////////////////////////////////////////////
} catch (error) { } catch (error) {
serverError({
component: "googleLogin",
message: error.message,
user: {},
});
loginFailureReason = error; loginFailureReason = error;
isUserValid = false; isUserValid = false;

View File

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