Updates
This commit is contained in:
@@ -4,6 +4,7 @@ import NO_DB_HANDLER from "../../utils/backend/global-db/NO_DB_HANDLER";
|
||||
import addDbEntry from "./db/addDbEntry";
|
||||
import encrypt from "../dsql/encrypt";
|
||||
import LOCAL_DB_HANDLER from "../../utils/backend/global-db/LOCAL_DB_HANDLER";
|
||||
import grabSQLKeyName from "../../utils/grab-sql-key-name";
|
||||
|
||||
type Param = {
|
||||
userId: number | string;
|
||||
@@ -16,7 +17,7 @@ export default async function addMariadbUser({ userId }: Param): Promise<any> {
|
||||
try {
|
||||
const defaultMariadbUserHost = process.env.DSQL_DB_HOST || "127.0.0.1";
|
||||
|
||||
const username = `dsql_user_${userId}`;
|
||||
const username = grabSQLKeyName({ type: "user", userId });
|
||||
const password = generator.generate({
|
||||
length: 16,
|
||||
numbers: true,
|
||||
|
||||
@@ -1,16 +1,19 @@
|
||||
import serverError from "./serverError";
|
||||
import DB_HANDLER from "../../utils/backend/global-db/DB_HANDLER";
|
||||
import { default as grabUserSchemaData } from "./grabUserSchemaData";
|
||||
import { default as setUserSchemaData } from "./setUserSchemaData";
|
||||
import addDbEntry from "./db/addDbEntry";
|
||||
import createDbFromSchema from "../../shell/createDbFromSchema";
|
||||
import LOCAL_DB_HANDLER from "../../utils/backend/global-db/LOCAL_DB_HANDLER";
|
||||
import grabNewUsersTableSchema from "./grabNewUsersTableSchema";
|
||||
import {
|
||||
grabPrimaryRequiredDbSchema,
|
||||
writeUpdatedDbSchema,
|
||||
} from "../../shell/createDbFromSchema/grab-required-database-schemas";
|
||||
|
||||
type Param = {
|
||||
userId: number;
|
||||
database: string;
|
||||
payload?: { [s: string]: any };
|
||||
dbId: string | number;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -20,6 +23,7 @@ export default async function addUsersTableToDb({
|
||||
userId,
|
||||
database,
|
||||
payload,
|
||||
dbId,
|
||||
}: Param): Promise<any> {
|
||||
try {
|
||||
const dbFullName = database;
|
||||
@@ -27,12 +31,10 @@ export default async function addUsersTableToDb({
|
||||
const userPreset = grabNewUsersTableSchema({ payload });
|
||||
if (!userPreset) throw new Error("Couldn't Get User Preset!");
|
||||
|
||||
const userSchemaData = grabUserSchemaData({ userId });
|
||||
if (!userSchemaData) throw new Error("User schema data not found!");
|
||||
|
||||
let targetDatabase = userSchemaData.find(
|
||||
(db: any) => db.dbFullName === database
|
||||
);
|
||||
let targetDatabase = grabPrimaryRequiredDbSchema({
|
||||
dbId,
|
||||
userId,
|
||||
});
|
||||
|
||||
if (!targetDatabase) {
|
||||
throw new Error("Couldn't Find Target Database!");
|
||||
@@ -48,7 +50,7 @@ export default async function addUsersTableToDb({
|
||||
targetDatabase.tables.push(userPreset);
|
||||
}
|
||||
|
||||
setUserSchemaData({ schemaData: userSchemaData, userId });
|
||||
writeUpdatedDbSchema({ dbSchema: targetDatabase, userId });
|
||||
|
||||
const targetDb: any[] | null = global.DSQL_USE_LOCAL
|
||||
? await LOCAL_DB_HANDLER(
|
||||
|
||||
@@ -5,56 +5,56 @@ import { CheckApiCredentialsFn } from "../../types";
|
||||
/**
|
||||
* # Grap API Credentials
|
||||
*/
|
||||
const grabApiCred: CheckApiCredentialsFn = ({
|
||||
key,
|
||||
database,
|
||||
table,
|
||||
user_id,
|
||||
media,
|
||||
}) => {
|
||||
if (!key) return null;
|
||||
if (!user_id) return null;
|
||||
// const grabApiCred: CheckApiCredentialsFn = ({
|
||||
// key,
|
||||
// database,
|
||||
// table,
|
||||
// user_id,
|
||||
// media,
|
||||
// }) => {
|
||||
// if (!key) return null;
|
||||
// if (!user_id) return null;
|
||||
|
||||
try {
|
||||
const allowedKeysPath = process.env.DSQL_API_KEYS_PATH;
|
||||
// try {
|
||||
// const allowedKeysPath = process.env.DSQL_API_KEYS_PATH;
|
||||
|
||||
if (!allowedKeysPath)
|
||||
throw new Error(
|
||||
"process.env.DSQL_API_KEYS_PATH variable not found"
|
||||
);
|
||||
// if (!allowedKeysPath)
|
||||
// throw new Error(
|
||||
// "process.env.DSQL_API_KEYS_PATH variable not found"
|
||||
// );
|
||||
|
||||
const ApiJSON = decrypt({ encryptedString: key });
|
||||
// const ApiJSON = decrypt({ encryptedString: key });
|
||||
|
||||
const ApiObject: import("../../types").ApiKeyObject = JSON.parse(
|
||||
ApiJSON || ""
|
||||
);
|
||||
// const ApiObject: import("../../types").ApiKeyObject = JSON.parse(
|
||||
// ApiJSON || ""
|
||||
// );
|
||||
|
||||
const isApiKeyValid = fs.existsSync(
|
||||
`${allowedKeysPath}/${ApiObject.sign}`
|
||||
);
|
||||
// const isApiKeyValid = fs.existsSync(
|
||||
// `${allowedKeysPath}/${ApiObject.sign}`
|
||||
// );
|
||||
|
||||
if (String(ApiObject.user_id) !== String(user_id)) return null;
|
||||
// if (String(ApiObject.user_id) !== String(user_id)) return null;
|
||||
|
||||
if (!isApiKeyValid) return null;
|
||||
if (!ApiObject.target_database) return ApiObject;
|
||||
if (media) return ApiObject;
|
||||
// if (!isApiKeyValid) return null;
|
||||
// if (!ApiObject.target_database) return ApiObject;
|
||||
// if (media) return ApiObject;
|
||||
|
||||
if (!database && ApiObject.target_database) return null;
|
||||
const isDatabaseAllowed = ApiObject.target_database
|
||||
?.split(",")
|
||||
.includes(String(database));
|
||||
// if (!database && ApiObject.target_database) return null;
|
||||
// const isDatabaseAllowed = ApiObject.target_database
|
||||
// ?.split(",")
|
||||
// .includes(String(database));
|
||||
|
||||
if (isDatabaseAllowed && !ApiObject.target_table) return ApiObject;
|
||||
if (isDatabaseAllowed && !table && ApiObject.target_table) return null;
|
||||
const isTableAllowed = ApiObject.target_table
|
||||
?.split(",")
|
||||
.includes(String(table));
|
||||
if (isTableAllowed) return ApiObject;
|
||||
return null;
|
||||
} catch (error: any) {
|
||||
console.log(`api-cred ERROR: ${error.message}`);
|
||||
return { error: `api-cred ERROR: ${error.message}` };
|
||||
}
|
||||
};
|
||||
// if (isDatabaseAllowed && !ApiObject.target_table) return ApiObject;
|
||||
// if (isDatabaseAllowed && !table && ApiObject.target_table) return null;
|
||||
// const isTableAllowed = ApiObject.target_table
|
||||
// ?.split(",")
|
||||
// .includes(String(table));
|
||||
// if (isTableAllowed) return ApiObject;
|
||||
// return null;
|
||||
// } catch (error: any) {
|
||||
// console.log(`api-cred ERROR: ${error.message}`);
|
||||
// return { error: `api-cred ERROR: ${error.message}` };
|
||||
// }
|
||||
// };
|
||||
|
||||
export default grabApiCred;
|
||||
// export default grabApiCred;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import getCsrfHeaderName from "../../../actions/get-csrf-header-name";
|
||||
import { AppNames } from "../../../dict/app-names";
|
||||
|
||||
type Param = {
|
||||
database?: string;
|
||||
@@ -22,8 +23,14 @@ export default function getAuthCookieNames(params?: Param): Return {
|
||||
process.env.DSQL_COOKIES_ONE_TIME_CODE_NAME || "one-time-code";
|
||||
|
||||
const targetDatabase =
|
||||
params?.database?.replace(/^datasquirel_user_\d+_/, "") ||
|
||||
process.env.DSQL_DB_NAME?.replace(/^datasquirel_user_\d+_/, "");
|
||||
params?.database?.replace(
|
||||
new RegExp(`^${AppNames["DsqlDbPrefix"]}\\d+_`),
|
||||
""
|
||||
) ||
|
||||
process.env.DSQL_DB_NAME?.replace(
|
||||
new RegExp(`^${AppNames["DsqlDbPrefix"]}\\d+_`),
|
||||
""
|
||||
);
|
||||
|
||||
let keyCookieName = cookiesPrefix;
|
||||
if (params?.userId) keyCookieName += `user_${params.userId}_`;
|
||||
|
||||
@@ -1,24 +1,35 @@
|
||||
import varDatabaseDbHandler from "../../functions/backend/varDatabaseDbHandler";
|
||||
import { default as grabUserSchemaData } from "../../functions/backend/grabUserSchemaData";
|
||||
import { default as setUserSchemaData } from "../../functions/backend/setUserSchemaData";
|
||||
import addDbEntry from "../../functions/backend/db/addDbEntry";
|
||||
import slugToCamelTitle from "../../shell/utils/slugToCamelTitle";
|
||||
import { DSQL_DATASQUIREL_USER_DATABASES } from "../../types/dsql";
|
||||
import {
|
||||
DSQL_DATASQUIREL_USER_DATABASE_TABLES,
|
||||
DSQL_DATASQUIREL_USER_DATABASES,
|
||||
} from "../../types/dsql";
|
||||
import {
|
||||
DSQL_FieldSchemaType,
|
||||
DSQL_IndexSchemaType,
|
||||
DSQL_MYSQL_SHOW_COLUMNS_Type,
|
||||
DSQL_TableSchemaType,
|
||||
} from "../../types";
|
||||
import grabDSQLSchemaIndexComment from "../../shell/utils/grab-dsql-schema-index-comment";
|
||||
import {
|
||||
grabPrimaryRequiredDbSchema,
|
||||
writeUpdatedDbSchema,
|
||||
} from "../../shell/createDbFromSchema/grab-required-database-schemas";
|
||||
import _n from "../../utils/numberfy";
|
||||
import dataTypeParser from "../../utils/db/schema/data-type-parser";
|
||||
import dataTypeConstructor from "../../utils/db/schema/data-type-constructor";
|
||||
|
||||
type Params = {
|
||||
userId: number | string;
|
||||
database: DSQL_DATASQUIREL_USER_DATABASES;
|
||||
dbId?: string | number;
|
||||
};
|
||||
|
||||
export default async function createDbSchemaFromDb({
|
||||
userId,
|
||||
database,
|
||||
dbId,
|
||||
}: Params) {
|
||||
try {
|
||||
if (!userId) {
|
||||
@@ -26,12 +37,12 @@ export default async function createDbSchemaFromDb({
|
||||
return;
|
||||
}
|
||||
|
||||
const userSchemaData = grabUserSchemaData({ userId });
|
||||
if (!userSchemaData) throw new Error("User schema data not found!");
|
||||
const targetDb = grabPrimaryRequiredDbSchema({
|
||||
userId,
|
||||
dbId: database.db_schema_id || dbId,
|
||||
});
|
||||
|
||||
const targetDb: { tables: object[] } = userSchemaData.filter(
|
||||
(dbObject) => dbObject.dbFullName === database.db_full_name
|
||||
)[0];
|
||||
if (!targetDb) throw new Error(`Target Db not found!`);
|
||||
|
||||
const existingTables = await varDatabaseDbHandler({
|
||||
database: database.db_full_name,
|
||||
@@ -44,21 +55,21 @@ export default async function createDbSchemaFromDb({
|
||||
const table = existingTables[i];
|
||||
const tableName = Object.values(table)[0] as string;
|
||||
|
||||
const tableInsert = await addDbEntry({
|
||||
dbFullName: "datasquirel",
|
||||
tableName: "user_database_tables",
|
||||
data: {
|
||||
user_id: userId,
|
||||
db_id: database.id,
|
||||
db_slug: database.db_slug,
|
||||
table_name: slugToCamelTitle(tableName),
|
||||
table_slug: tableName,
|
||||
},
|
||||
});
|
||||
const tableInsert =
|
||||
await addDbEntry<DSQL_DATASQUIREL_USER_DATABASE_TABLES>({
|
||||
dbFullName: "datasquirel",
|
||||
tableName: "user_database_tables",
|
||||
data: {
|
||||
user_id: _n(userId),
|
||||
db_id: database.id,
|
||||
db_slug: database.db_slug,
|
||||
table_name: slugToCamelTitle(tableName) || undefined,
|
||||
table_slug: tableName,
|
||||
},
|
||||
});
|
||||
|
||||
const tableObject: DSQL_TableSchemaType = {
|
||||
tableName: tableName,
|
||||
tableFullName: slugToCamelTitle(tableName) || "",
|
||||
fields: [],
|
||||
indexes: [],
|
||||
};
|
||||
@@ -75,9 +86,15 @@ export default async function createDbSchemaFromDb({
|
||||
const { Field, Type, Null, Key, Default, Extra } =
|
||||
tableColumn;
|
||||
|
||||
const parsedDataType = dataTypeParser(Type.toUpperCase());
|
||||
|
||||
const fieldObject: DSQL_FieldSchemaType = {
|
||||
fieldName: Field,
|
||||
dataType: Type.toUpperCase(),
|
||||
dataType: dataTypeConstructor(
|
||||
parsedDataType.type,
|
||||
parsedDataType.limit,
|
||||
parsedDataType.decimal
|
||||
),
|
||||
};
|
||||
|
||||
if (Null?.match(/^no$/i)) fieldObject.notNullValue = true;
|
||||
@@ -112,11 +129,16 @@ export default async function createDbSchemaFromDb({
|
||||
Index_comment,
|
||||
} = indexObject;
|
||||
|
||||
if (!Index_comment?.match(/^schema_index$/)) continue;
|
||||
if (
|
||||
!Index_comment?.match(
|
||||
new RegExp(grabDSQLSchemaIndexComment())
|
||||
)
|
||||
)
|
||||
continue;
|
||||
|
||||
const indexNewObject: DSQL_IndexSchemaType = {
|
||||
indexType: Index_type?.match(/fulltext/i)
|
||||
? "fullText"
|
||||
? "full_text"
|
||||
: "regular",
|
||||
indexName: Key_name,
|
||||
indexTableFields: [],
|
||||
@@ -152,8 +174,7 @@ export default async function createDbSchemaFromDb({
|
||||
targetDb.tables.push(tableObject);
|
||||
}
|
||||
|
||||
setUserSchemaData({ schemaData: userSchemaData, userId });
|
||||
|
||||
writeUpdatedDbSchema({ dbSchema: targetDb, userId });
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
|
||||
@@ -7,17 +7,30 @@ import connDbHandler from "../../../utils/db/conn-db-handler";
|
||||
import checkIfIsMaster from "../../../utils/check-if-is-master";
|
||||
import { DbContextsArray } from "./runQuery";
|
||||
import debugLog from "../../../utils/logging/debug-log";
|
||||
import { PostInsertReturn } from "../../../types";
|
||||
import {
|
||||
APIResponseObject,
|
||||
DSQL_TableSchemaType,
|
||||
PostInsertReturn,
|
||||
} from "../../../types";
|
||||
import purgeDefaultFields from "../../../utils/purge-default-fields";
|
||||
|
||||
type Param<T extends { [k: string]: any } = any> = {
|
||||
export type AddDbEntryParam<
|
||||
T extends { [k: string]: any } = any,
|
||||
K extends string = string
|
||||
> = {
|
||||
dbContext?: (typeof DbContextsArray)[number];
|
||||
paradigm?: "Read Only" | "Full Access";
|
||||
dbFullName?: string;
|
||||
tableName: string;
|
||||
data: T;
|
||||
tableSchema?: import("../../../types").DSQL_TableSchemaType;
|
||||
duplicateColumnName?: string;
|
||||
duplicateColumnValue?: string;
|
||||
tableName: K;
|
||||
data?: T;
|
||||
batchData?: T[];
|
||||
tableSchema?: DSQL_TableSchemaType;
|
||||
duplicateColumnName?: keyof T;
|
||||
duplicateColumnValue?: string | number;
|
||||
/**
|
||||
* Update Entry if a duplicate is found.
|
||||
* Requires `duplicateColumnName` and `duplicateColumnValue` parameters
|
||||
*/
|
||||
update?: boolean;
|
||||
encryptionKey?: string;
|
||||
encryptionSalt?: string;
|
||||
@@ -28,12 +41,16 @@ type Param<T extends { [k: string]: any } = any> = {
|
||||
/**
|
||||
* Add a db Entry Function
|
||||
*/
|
||||
export default async function addDbEntry<T extends { [k: string]: any } = any>({
|
||||
export default async function addDbEntry<
|
||||
T extends { [k: string]: any } = any,
|
||||
K extends string = string
|
||||
>({
|
||||
dbContext,
|
||||
paradigm,
|
||||
dbFullName,
|
||||
tableName,
|
||||
data,
|
||||
batchData,
|
||||
tableSchema,
|
||||
duplicateColumnName,
|
||||
duplicateColumnValue,
|
||||
@@ -42,7 +59,7 @@ export default async function addDbEntry<T extends { [k: string]: any } = any>({
|
||||
encryptionSalt,
|
||||
forceLocal,
|
||||
debug,
|
||||
}: Param<T>): Promise<PostInsertReturn | null> {
|
||||
}: AddDbEntryParam<T, K>): Promise<APIResponseObject<PostInsertReturn>> {
|
||||
const isMaster = forceLocal
|
||||
? true
|
||||
: checkIfIsMaster({ dbContext, dbFullName });
|
||||
@@ -62,14 +79,21 @@ export default async function addDbEntry<T extends { [k: string]: any } = any>({
|
||||
? global.DSQL_DB_CONN
|
||||
: global.DSQL_READ_ONLY_DB_CONN || global.DSQL_DB_CONN;
|
||||
|
||||
if (data?.["date_created_timestamp"]) delete data["date_created_timestamp"];
|
||||
if (data?.["date_updated_timestamp"]) delete data["date_updated_timestamp"];
|
||||
if (data?.["date_updated"]) delete data["date_updated"];
|
||||
if (data?.["date_updated_code"]) delete data["date_updated_code"];
|
||||
if (data?.["date_created"]) delete data["date_created"];
|
||||
if (data?.["date_created_code"]) delete data["date_created_code"];
|
||||
let newData = _.cloneDeep(data);
|
||||
if (newData) {
|
||||
newData = purgeDefaultFields(newData);
|
||||
}
|
||||
|
||||
if (duplicateColumnName && typeof duplicateColumnName === "string") {
|
||||
let newBatchData = _.cloneDeep(batchData) as any[];
|
||||
if (newBatchData) {
|
||||
newBatchData = purgeDefaultFields(newBatchData);
|
||||
}
|
||||
|
||||
if (
|
||||
duplicateColumnName &&
|
||||
typeof duplicateColumnName === "string" &&
|
||||
newData
|
||||
) {
|
||||
const checkDuplicateQuery = `SELECT * FROM ${
|
||||
isMaster ? "" : `\`${dbFullName}\`.`
|
||||
}\`${tableName}\` WHERE \`${duplicateColumnName}\`=?`;
|
||||
@@ -81,13 +105,17 @@ export default async function addDbEntry<T extends { [k: string]: any } = any>({
|
||||
);
|
||||
|
||||
if (duplicateValue?.[0] && !update) {
|
||||
return null;
|
||||
} else if (duplicateValue && duplicateValue[0] && update) {
|
||||
return {
|
||||
success: false,
|
||||
payload: undefined,
|
||||
msg: "Duplicate entry found",
|
||||
};
|
||||
} else if (duplicateValue?.[0] && update) {
|
||||
return await updateDbEntry({
|
||||
dbContext,
|
||||
dbFullName,
|
||||
tableName,
|
||||
data,
|
||||
data: newData,
|
||||
tableSchema,
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
@@ -97,140 +125,188 @@ export default async function addDbEntry<T extends { [k: string]: any } = any>({
|
||||
}
|
||||
}
|
||||
|
||||
const dataKeys = Object.keys(data);
|
||||
function generateQuery(data: T) {
|
||||
const dataKeys = Object.keys(data);
|
||||
|
||||
let insertKeysArray = [];
|
||||
let insertValuesArray = [];
|
||||
let insertKeysArray = [];
|
||||
let insertValuesArray = [];
|
||||
|
||||
for (let i = 0; i < dataKeys.length; i++) {
|
||||
try {
|
||||
const dataKey = dataKeys[i];
|
||||
let value = data?.[dataKey];
|
||||
for (let i = 0; i < dataKeys.length; i++) {
|
||||
try {
|
||||
const dataKey = dataKeys[i];
|
||||
let value = data[dataKey];
|
||||
|
||||
const targetFieldSchemaArray = tableSchema
|
||||
? tableSchema?.fields?.filter(
|
||||
(field) => field.fieldName == dataKey
|
||||
)
|
||||
: null;
|
||||
const targetFieldSchema =
|
||||
targetFieldSchemaArray && targetFieldSchemaArray[0]
|
||||
? targetFieldSchemaArray[0]
|
||||
const targetFieldSchemaArray = tableSchema
|
||||
? tableSchema?.fields?.filter(
|
||||
(field) => field.fieldName == dataKey
|
||||
)
|
||||
: null;
|
||||
const targetFieldSchema =
|
||||
targetFieldSchemaArray && targetFieldSchemaArray[0]
|
||||
? targetFieldSchemaArray[0]
|
||||
: null;
|
||||
|
||||
if (value == null || value == undefined) continue;
|
||||
if (value == null || value == undefined) continue;
|
||||
|
||||
if (
|
||||
targetFieldSchema?.dataType?.match(/int$/i) &&
|
||||
typeof value == "string" &&
|
||||
!value?.match(/./)
|
||||
)
|
||||
continue;
|
||||
if (
|
||||
targetFieldSchema?.dataType?.match(/int$/i) &&
|
||||
typeof value == "string" &&
|
||||
!value?.match(/./)
|
||||
)
|
||||
continue;
|
||||
|
||||
if (targetFieldSchema?.encrypted) {
|
||||
value = encrypt({
|
||||
data: value,
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
});
|
||||
console.log("DSQL: Encrypted value =>", value);
|
||||
}
|
||||
|
||||
const htmlRegex = /<[^>]+>/g;
|
||||
|
||||
if (targetFieldSchema?.richText || String(value).match(htmlRegex)) {
|
||||
value = sanitizeHtml(value, sanitizeHtmlOptions);
|
||||
}
|
||||
|
||||
if (targetFieldSchema?.pattern) {
|
||||
const pattern = new RegExp(
|
||||
targetFieldSchema.pattern,
|
||||
targetFieldSchema.patternFlags || ""
|
||||
);
|
||||
if (!pattern.test(value)) {
|
||||
console.log("DSQL: Pattern not matched =>", value);
|
||||
value = "";
|
||||
if (targetFieldSchema?.encrypted) {
|
||||
value = encrypt({
|
||||
data: value,
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
});
|
||||
console.log("DSQL: Encrypted value =>", value);
|
||||
}
|
||||
}
|
||||
|
||||
insertKeysArray.push("`" + dataKey + "`");
|
||||
const htmlRegex = /<[^>]+>/g;
|
||||
|
||||
if (typeof value === "object") {
|
||||
value = JSON.stringify(value);
|
||||
}
|
||||
if (
|
||||
targetFieldSchema?.richText ||
|
||||
String(value).match(htmlRegex)
|
||||
) {
|
||||
value = sanitizeHtml(value, sanitizeHtmlOptions);
|
||||
}
|
||||
|
||||
if (typeof value == "number") {
|
||||
insertValuesArray.push(String(value));
|
||||
} else {
|
||||
insertValuesArray.push(value);
|
||||
if (targetFieldSchema?.pattern) {
|
||||
const pattern = new RegExp(
|
||||
targetFieldSchema.pattern,
|
||||
targetFieldSchema.patternFlags || ""
|
||||
);
|
||||
if (!pattern.test(value)) {
|
||||
console.log("DSQL: Pattern not matched =>", value);
|
||||
value = "";
|
||||
}
|
||||
}
|
||||
|
||||
insertKeysArray.push("`" + dataKey + "`");
|
||||
|
||||
if (typeof value === "object") {
|
||||
value = JSON.stringify(value);
|
||||
}
|
||||
|
||||
if (typeof value == "number") {
|
||||
insertValuesArray.push(String(value));
|
||||
} else {
|
||||
insertValuesArray.push(value);
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.log(
|
||||
"DSQL: Error in parsing data keys =>",
|
||||
error.message
|
||||
);
|
||||
global.ERROR_CALLBACK?.(
|
||||
`Error parsing Data Keys`,
|
||||
error as Error
|
||||
);
|
||||
continue;
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.log("DSQL: Error in parsing data keys =>", error.message);
|
||||
global.ERROR_CALLBACK?.(`Error parsing Data Keys`, error as Error);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (!data?.["date_created"]) {
|
||||
insertKeysArray.push("`date_created`");
|
||||
insertValuesArray.push(Date());
|
||||
}
|
||||
|
||||
if (!data?.["date_created_code"]) {
|
||||
insertKeysArray.push("`date_created_code`");
|
||||
insertValuesArray.push(Date.now());
|
||||
}
|
||||
|
||||
if (!data?.["date_updated"]) {
|
||||
insertKeysArray.push("`date_updated`");
|
||||
insertValuesArray.push(Date());
|
||||
}
|
||||
|
||||
if (!data?.["date_updated_code"]) {
|
||||
insertKeysArray.push("`date_updated_code`");
|
||||
insertValuesArray.push(Date.now());
|
||||
|
||||
const queryValuesArray = insertValuesArray;
|
||||
|
||||
return { queryValuesArray, insertValuesArray, insertKeysArray };
|
||||
}
|
||||
|
||||
const query = `INSERT INTO ${
|
||||
isMaster ? "" : `\`${dbFullName}\`.`
|
||||
}\`${tableName}\` (${insertKeysArray.join(",")}) VALUES (${insertValuesArray
|
||||
.map(() => "?")
|
||||
.join(",")})`;
|
||||
const queryValuesArray = insertValuesArray;
|
||||
if (newData) {
|
||||
const { insertKeysArray, insertValuesArray, queryValuesArray } =
|
||||
generateQuery(newData);
|
||||
|
||||
if (debug) {
|
||||
debugLog({
|
||||
log: DB_CONN?.getConfig(),
|
||||
addTime: true,
|
||||
label: "DB_CONN Config",
|
||||
});
|
||||
const query = `INSERT INTO ${
|
||||
isMaster && !dbFullName ? "" : `\`${dbFullName}\`.`
|
||||
}\`${tableName}\` (${insertKeysArray.join(
|
||||
","
|
||||
)}) VALUES (${insertValuesArray.map(() => "?").join(",")})`;
|
||||
|
||||
debugLog({
|
||||
log: query,
|
||||
addTime: true,
|
||||
label: "query",
|
||||
});
|
||||
const newInsert = await connDbHandler(
|
||||
DB_CONN,
|
||||
query,
|
||||
queryValuesArray,
|
||||
debug
|
||||
);
|
||||
|
||||
debugLog({
|
||||
log: queryValuesArray,
|
||||
addTime: true,
|
||||
label: "queryValuesArray",
|
||||
});
|
||||
return {
|
||||
success: Boolean(newInsert?.insertId),
|
||||
payload: newInsert,
|
||||
queryObject: {
|
||||
sql: query,
|
||||
params: queryValuesArray,
|
||||
},
|
||||
};
|
||||
} else if (newBatchData) {
|
||||
let batchInsertKeysArray: string[] | undefined;
|
||||
let batchInsertValuesArray: any[][] = [];
|
||||
let batchQueryValuesArray: any[][] = [];
|
||||
|
||||
for (let i = 0; i < newBatchData.length; i++) {
|
||||
const singleBatchData = newBatchData[i];
|
||||
const { insertKeysArray, insertValuesArray, queryValuesArray } =
|
||||
generateQuery(singleBatchData);
|
||||
|
||||
if (!batchInsertKeysArray) {
|
||||
batchInsertKeysArray = insertKeysArray;
|
||||
}
|
||||
|
||||
batchInsertValuesArray.push(insertValuesArray);
|
||||
batchQueryValuesArray.push(queryValuesArray);
|
||||
}
|
||||
|
||||
const query = `INSERT INTO ${
|
||||
isMaster && !dbFullName ? "" : `\`${dbFullName}\`.`
|
||||
}\`${tableName}\` (${batchInsertKeysArray?.join(
|
||||
","
|
||||
)}) VALUES ${batchInsertValuesArray
|
||||
.map((vl) => `(${vl.map(() => "?").join(",")})`)
|
||||
.join(",")}`;
|
||||
|
||||
console.log("query", query);
|
||||
console.log("batchQueryValuesArray", batchQueryValuesArray);
|
||||
|
||||
const newInsert = await connDbHandler(
|
||||
DB_CONN,
|
||||
query,
|
||||
batchQueryValuesArray.flat(),
|
||||
debug
|
||||
);
|
||||
|
||||
if (debug) {
|
||||
debugLog({
|
||||
log: newInsert,
|
||||
addTime: true,
|
||||
label: "newInsert",
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
success: Boolean(newInsert?.insertId),
|
||||
payload: newInsert,
|
||||
queryObject: {
|
||||
sql: query,
|
||||
params: batchQueryValuesArray.flat(),
|
||||
},
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
success: false,
|
||||
payload: undefined,
|
||||
msg: "No data provided",
|
||||
};
|
||||
}
|
||||
|
||||
const newInsert = await connDbHandler(
|
||||
DB_CONN,
|
||||
query,
|
||||
queryValuesArray,
|
||||
debug
|
||||
);
|
||||
|
||||
if (debug) {
|
||||
debugLog({
|
||||
log: newInsert,
|
||||
addTime: true,
|
||||
label: "newInsert",
|
||||
});
|
||||
}
|
||||
|
||||
return newInsert;
|
||||
}
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
import { DSQL_TableSchemaType, PostInsertReturn } from "../../../types";
|
||||
import checkIfIsMaster from "../../../utils/check-if-is-master";
|
||||
import connDbHandler from "../../../utils/db/conn-db-handler";
|
||||
import { DbContextsArray } from "./runQuery";
|
||||
|
||||
type Param = {
|
||||
type Param<T extends { [k: string]: any } = any, K extends string = string> = {
|
||||
dbContext?: (typeof DbContextsArray)[number];
|
||||
dbFullName: string;
|
||||
tableName: string;
|
||||
tableSchema?: import("../../../types").DSQL_TableSchemaType;
|
||||
identifierColumnName: string;
|
||||
dbFullName?: string;
|
||||
tableName: K;
|
||||
tableSchema?: DSQL_TableSchemaType;
|
||||
identifierColumnName: keyof T;
|
||||
identifierValue: string | number;
|
||||
forceLocal?: boolean;
|
||||
};
|
||||
@@ -16,14 +17,17 @@ type Param = {
|
||||
* # Delete DB Entry Function
|
||||
* @description
|
||||
*/
|
||||
export default async function deleteDbEntry({
|
||||
export default async function deleteDbEntry<
|
||||
T extends { [k: string]: any } = any,
|
||||
K extends string = string
|
||||
>({
|
||||
dbContext,
|
||||
dbFullName,
|
||||
tableName,
|
||||
identifierColumnName,
|
||||
identifierValue,
|
||||
forceLocal,
|
||||
}: Param): Promise<object | null> {
|
||||
}: Param<T, K>): Promise<PostInsertReturn | null> {
|
||||
try {
|
||||
const isMaster = forceLocal
|
||||
? true
|
||||
@@ -32,9 +36,6 @@ export default async function deleteDbEntry({
|
||||
const DB_CONN = isMaster
|
||||
? global.DSQL_DB_CONN
|
||||
: global.DSQL_FULL_ACCESS_DB_CONN || global.DSQL_DB_CONN;
|
||||
const DB_RO_CONN = isMaster
|
||||
? global.DSQL_DB_CONN
|
||||
: global.DSQL_READ_ONLY_DB_CONN || global.DSQL_DB_CONN;
|
||||
|
||||
/**
|
||||
* Execution
|
||||
@@ -42,8 +43,8 @@ export default async function deleteDbEntry({
|
||||
* @description
|
||||
*/
|
||||
const query = `DELETE FROM ${
|
||||
isMaster ? "" : `\`${dbFullName}\`.`
|
||||
}\`${tableName}\` WHERE \`${identifierColumnName}\`=?`;
|
||||
isMaster && !dbFullName ? "" : `\`${dbFullName}\`.`
|
||||
}\`${tableName}\` WHERE \`${identifierColumnName.toString()}\`=?`;
|
||||
|
||||
const deletedEntry = await connDbHandler(DB_CONN, query, [
|
||||
identifierValue,
|
||||
|
||||
@@ -126,7 +126,7 @@ export default async function runQuery({
|
||||
case "insert":
|
||||
result = await addDbEntry({
|
||||
dbContext,
|
||||
dbFullName: dbFullName,
|
||||
dbFullName,
|
||||
tableName: table,
|
||||
data: data,
|
||||
update,
|
||||
@@ -145,7 +145,7 @@ export default async function runQuery({
|
||||
case "update":
|
||||
result = await updateDbEntry({
|
||||
dbContext,
|
||||
dbFullName: dbFullName,
|
||||
dbFullName,
|
||||
tableName: table,
|
||||
data: data,
|
||||
identifierColumnName,
|
||||
@@ -158,7 +158,7 @@ export default async function runQuery({
|
||||
case "delete":
|
||||
result = await deleteDbEntry({
|
||||
dbContext,
|
||||
dbFullName: dbFullName,
|
||||
dbFullName,
|
||||
tableName: table,
|
||||
identifierColumnName,
|
||||
identifierValue,
|
||||
|
||||
@@ -4,7 +4,13 @@ import encrypt from "../../dsql/encrypt";
|
||||
import checkIfIsMaster from "../../../utils/check-if-is-master";
|
||||
import connDbHandler from "../../../utils/db/conn-db-handler";
|
||||
import { DbContextsArray } from "./runQuery";
|
||||
import { PostInsertReturn } from "../../../types";
|
||||
import {
|
||||
APIResponseObject,
|
||||
DSQL_TableSchemaType,
|
||||
PostInsertReturn,
|
||||
} from "../../../types";
|
||||
import _ from "lodash";
|
||||
import purgeDefaultFields from "../../../utils/purge-default-fields";
|
||||
|
||||
type Param<T extends { [k: string]: any } = any> = {
|
||||
dbContext?: (typeof DbContextsArray)[number];
|
||||
@@ -12,8 +18,8 @@ type Param<T extends { [k: string]: any } = any> = {
|
||||
tableName: string;
|
||||
encryptionKey?: string;
|
||||
encryptionSalt?: string;
|
||||
data: any;
|
||||
tableSchema?: import("../../../types").DSQL_TableSchemaType;
|
||||
data?: T;
|
||||
tableSchema?: DSQL_TableSchemaType;
|
||||
identifierColumnName: keyof T;
|
||||
identifierValue: string | number;
|
||||
forceLocal?: boolean;
|
||||
@@ -36,11 +42,17 @@ export default async function updateDbEntry<
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
forceLocal,
|
||||
}: Param<T>): Promise<PostInsertReturn | null> {
|
||||
}: Param<T>): Promise<APIResponseObject<PostInsertReturn>> {
|
||||
/**
|
||||
* Check if data is valid
|
||||
*/
|
||||
if (!data || !Object.keys(data).length) return null;
|
||||
if (!data || !Object.keys(data).length) {
|
||||
return {
|
||||
success: false,
|
||||
payload: undefined,
|
||||
msg: "No data provided",
|
||||
};
|
||||
}
|
||||
|
||||
const isMaster = forceLocal
|
||||
? true
|
||||
@@ -54,12 +66,15 @@ export default async function updateDbEntry<
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
let newData = _.cloneDeep(data);
|
||||
newData = purgeDefaultFields(newData);
|
||||
|
||||
/**
|
||||
* Declare variables
|
||||
*
|
||||
* @description Declare "results" variable
|
||||
*/
|
||||
const dataKeys = Object.keys(data);
|
||||
const dataKeys = Object.keys(newData);
|
||||
|
||||
let updateKeyValueArray = [];
|
||||
let updateValues = [];
|
||||
@@ -67,8 +82,7 @@ export default async function updateDbEntry<
|
||||
for (let i = 0; i < dataKeys.length; i++) {
|
||||
try {
|
||||
const dataKey = dataKeys[i];
|
||||
// @ts-ignore
|
||||
let value = data[dataKey];
|
||||
let value = newData[dataKey];
|
||||
|
||||
const targetFieldSchemaArray = tableSchema
|
||||
? tableSchema?.fields?.filter(
|
||||
@@ -159,7 +173,7 @@ export default async function updateDbEntry<
|
||||
////////////////////////////////////////
|
||||
|
||||
const query = `UPDATE ${
|
||||
isMaster ? "" : `\`${dbFullName}\`.`
|
||||
isMaster && !dbFullName ? "" : `\`${dbFullName}\`.`
|
||||
}\`${tableName}\` SET ${updateKeyValueArray.join(",")} WHERE \`${
|
||||
identifierColumnName as string
|
||||
}\`=?`;
|
||||
@@ -171,5 +185,12 @@ export default async function updateDbEntry<
|
||||
/**
|
||||
* Return statement
|
||||
*/
|
||||
return updatedEntry;
|
||||
return {
|
||||
success: Boolean(updatedEntry?.affectedRows),
|
||||
payload: updatedEntry,
|
||||
queryObject: {
|
||||
sql: query,
|
||||
params: updateValues,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,69 +1,63 @@
|
||||
import fs from "fs";
|
||||
import serverError from "./serverError";
|
||||
import grabDSQLConnection from "../../utils/grab-dsql-connection";
|
||||
import path from "path";
|
||||
import grabDSQLConnection from "../../utils/grab-dsql-connection";
|
||||
|
||||
type Param = {
|
||||
query: string;
|
||||
values?: string[] | object;
|
||||
noErrorLogs?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* # Main DB Handler Function
|
||||
* @requires DSQL_DB_CONN - Gobal Variable for Datasquirel Database
|
||||
*/
|
||||
export default async function dbHandler(...args: any[]) {
|
||||
process.env.NODE_ENV?.match(/dev/) &&
|
||||
fs.appendFileSync(
|
||||
"./.tmp/sqlQuery.sql",
|
||||
args[0] + "\n" + Date() + "\n\n\n",
|
||||
"utf8"
|
||||
);
|
||||
|
||||
export default async function dbHandler({
|
||||
query,
|
||||
values,
|
||||
noErrorLogs,
|
||||
}: Param): Promise<any[] | object | null> {
|
||||
const CONNECTION = grabDSQLConnection();
|
||||
|
||||
let results;
|
||||
|
||||
/**
|
||||
* Fetch from db
|
||||
*
|
||||
* @description Fetch data from db if no cache
|
||||
*/
|
||||
try {
|
||||
results = await new Promise((resolve, reject) => {
|
||||
CONNECTION.query(
|
||||
...args,
|
||||
(error: any, result: any, fields: any) => {
|
||||
if (error) {
|
||||
resolve({ error: error.message });
|
||||
} else {
|
||||
resolve(result);
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
if (query && values) {
|
||||
results = await CONNECTION.query(query, values);
|
||||
} else {
|
||||
results = await CONNECTION.query(query);
|
||||
}
|
||||
} catch (error: any) {
|
||||
const tmpFolder = path.resolve(process.cwd(), "./.tmp");
|
||||
if (!fs.existsSync(tmpFolder))
|
||||
fs.mkdirSync(tmpFolder, { recursive: true });
|
||||
if (!noErrorLogs) {
|
||||
global.ERROR_CALLBACK?.(`DB Handler Error...`, error as Error);
|
||||
}
|
||||
|
||||
fs.appendFileSync(
|
||||
path.resolve(tmpFolder, "./dbErrorLogs.txt"),
|
||||
JSON.stringify(error, null, 4) + "\n" + Date() + "\n\n\n",
|
||||
"utf8"
|
||||
);
|
||||
if (process.env.FIRST_RUN) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!noErrorLogs) {
|
||||
console.log("ERROR in dbHandler =>", error.message);
|
||||
console.log(error);
|
||||
console.log(CONNECTION.config());
|
||||
|
||||
const tmpFolder = path.resolve(process.cwd(), "./.tmp");
|
||||
|
||||
if (!fs.existsSync(tmpFolder))
|
||||
fs.mkdirSync(tmpFolder, { recursive: true });
|
||||
|
||||
fs.appendFileSync(
|
||||
path.resolve(tmpFolder, "./dbErrorLogs.txt"),
|
||||
JSON.stringify(error, null, 4) + "\n" + Date() + "\n\n\n",
|
||||
"utf8"
|
||||
);
|
||||
}
|
||||
|
||||
results = null;
|
||||
|
||||
global.ERROR_CALLBACK?.(`DB Handler Error`, error as Error);
|
||||
|
||||
serverError({
|
||||
component: "dbHandler",
|
||||
message: error.message,
|
||||
});
|
||||
} finally {
|
||||
await CONNECTION?.end();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return results
|
||||
*
|
||||
* @description Return results add to cache if "req" param is passed
|
||||
*/
|
||||
if (results) {
|
||||
return JSON.parse(JSON.stringify(results));
|
||||
} else {
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
/**
|
||||
* Regular expression to match default fields
|
||||
*
|
||||
* @description Regular expression to match default fields
|
||||
*/
|
||||
const defaultFieldsRegexp =
|
||||
/^id$|^uuid$|^date_created$|^date_created_code$|^date_created_timestamp$|^date_updated$|^date_updated_code$|^date_updated_timestamp$/;
|
||||
|
||||
export default defaultFieldsRegexp;
|
||||
@@ -0,0 +1,70 @@
|
||||
import { UserType } from "../../types";
|
||||
import dbHandler from "./dbHandler";
|
||||
import dsqlCrud from "../../utils/data-fetching/crud";
|
||||
import { DSQL_DATASQUIREL_USERS, DsqlTables } from "../../types/dsql";
|
||||
import decrypt from "../dsql/decrypt";
|
||||
import createUserSQLUser from "../../utils/create-user-sql-user";
|
||||
import grabUserMainSqlUserName from "../../utils/grab-user-main-sql-user-name";
|
||||
|
||||
type Params = {
|
||||
user: UserType;
|
||||
};
|
||||
|
||||
type Return = {
|
||||
fullName?: string;
|
||||
host?: string;
|
||||
username?: string;
|
||||
password?: string;
|
||||
};
|
||||
|
||||
export default async function grabMariadbMainUserForUser({
|
||||
user,
|
||||
}: Params): Promise<Return> {
|
||||
const {
|
||||
fullName,
|
||||
host,
|
||||
username: mariaDBUsername,
|
||||
webHost,
|
||||
} = grabUserMainSqlUserName({ user });
|
||||
|
||||
const existingWebAppUser = (await dbHandler({
|
||||
query: `SELECT * FROM mysql.user WHERE user=? AND host=?`,
|
||||
values: [mariaDBUsername, webHost],
|
||||
})) as any[];
|
||||
|
||||
if (!existingWebAppUser?.[0]) {
|
||||
return await createUserSQLUser(user);
|
||||
} else {
|
||||
const existingUserRecord = await dsqlCrud<
|
||||
DSQL_DATASQUIREL_USERS,
|
||||
(typeof DsqlTables)[number]
|
||||
>({
|
||||
action: "get",
|
||||
table: "users",
|
||||
query: {
|
||||
query: {
|
||||
id: {
|
||||
value: String(user.id),
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const targetUser = (
|
||||
existingUserRecord?.payload as DSQL_DATASQUIREL_USERS[] | undefined
|
||||
)?.[0];
|
||||
|
||||
if (!targetUser?.id) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return {
|
||||
fullName,
|
||||
host,
|
||||
username: mariaDBUsername,
|
||||
password: decrypt({
|
||||
encryptedString: targetUser.mariadb_pass || "",
|
||||
}),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
import { DSQL_DatabaseSchemaType, UserType } from "../../types";
|
||||
import serverError from "./serverError";
|
||||
import fs from "fs";
|
||||
import grabDirNames from "../../utils/backend/names/grab-dir-names";
|
||||
import EJSON from "../../utils/ejson";
|
||||
|
||||
type Params = {
|
||||
userId?: string | number;
|
||||
};
|
||||
|
||||
/**
|
||||
* # Grab User Schema Data
|
||||
*/
|
||||
export default function grabUserSchemaData({
|
||||
userId,
|
||||
}: Params): DSQL_DatabaseSchemaType[] | null {
|
||||
try {
|
||||
const { userSchemaMainJSONFilePath } = grabDirNames({ userId });
|
||||
const schemaJSON = fs.readFileSync(
|
||||
userSchemaMainJSONFilePath || "",
|
||||
"utf-8"
|
||||
);
|
||||
const schemaObj = EJSON.parse(schemaJSON) as DSQL_DatabaseSchemaType[];
|
||||
return schemaObj;
|
||||
} catch (error: any) {
|
||||
serverError({
|
||||
component: "grabUserSchemaData",
|
||||
message: error.message,
|
||||
});
|
||||
|
||||
global.ERROR_CALLBACK?.(
|
||||
`Error Grabbing User Schema Data`,
|
||||
error as Error
|
||||
);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
// @ts-check
|
||||
|
||||
import decrypt from "../dsql/decrypt";
|
||||
import defaultFieldsRegexp from "./defaultFieldsRegexp";
|
||||
import defaultFieldsRegexp from "../dsql/default-fields-regexp";
|
||||
|
||||
type Param = {
|
||||
unparsedResults: any[];
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
import serverError from "./serverError";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import { DSQL_DatabaseSchemaType } from "../../types";
|
||||
import grabDirNames from "../../utils/backend/names/grab-dir-names";
|
||||
|
||||
type Param = {
|
||||
userId: string | number;
|
||||
schemaData: DSQL_DatabaseSchemaType[];
|
||||
};
|
||||
|
||||
/**
|
||||
* # Set User Schema Data
|
||||
*/
|
||||
export default function setUserSchemaData({
|
||||
userId,
|
||||
schemaData,
|
||||
}: Param): boolean {
|
||||
try {
|
||||
const { userSchemaMainJSONFilePath } = grabDirNames({ userId });
|
||||
|
||||
if (!userSchemaMainJSONFilePath) {
|
||||
throw new Error(`No User Schema JSON found!`);
|
||||
}
|
||||
|
||||
fs.writeFileSync(
|
||||
userSchemaMainJSONFilePath,
|
||||
JSON.stringify(schemaData),
|
||||
"utf8"
|
||||
);
|
||||
|
||||
return true;
|
||||
} catch (error: any) {
|
||||
serverError({
|
||||
component: "/functions/backend/setUserSchemaData",
|
||||
message: error.message,
|
||||
});
|
||||
|
||||
global.ERROR_CALLBACK?.(`Error Setting User Schema`, error as Error);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import mysql from "serverless-mysql";
|
||||
import { UserType } from "../../types";
|
||||
import connDbHandler from "../../utils/db/conn-db-handler";
|
||||
|
||||
type Params = {
|
||||
query?: string;
|
||||
values?: any[];
|
||||
database?: string;
|
||||
user: UserType;
|
||||
};
|
||||
|
||||
export default async function suDbHandler({
|
||||
query,
|
||||
database,
|
||||
user,
|
||||
values,
|
||||
}: Params) {
|
||||
const 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",
|
||||
},
|
||||
});
|
||||
|
||||
const results = await connDbHandler(connection, query);
|
||||
|
||||
return results;
|
||||
}
|
||||
@@ -1,14 +1,17 @@
|
||||
import serverError from "./serverError";
|
||||
import grabUserSchemaData from "./grabUserSchemaData";
|
||||
import setUserSchemaData from "./setUserSchemaData";
|
||||
import createDbFromSchema from "../../shell/createDbFromSchema";
|
||||
import grabSchemaFieldsFromData from "./grabSchemaFieldsFromData";
|
||||
import {
|
||||
grabPrimaryRequiredDbSchema,
|
||||
writeUpdatedDbSchema,
|
||||
} from "../../shell/createDbFromSchema/grab-required-database-schemas";
|
||||
|
||||
type Param = {
|
||||
userId: number | string;
|
||||
database: string;
|
||||
newFields?: string[];
|
||||
newPayload?: { [s: string]: any };
|
||||
dbId: string | number;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -19,27 +22,25 @@ export default async function updateUsersTableSchema({
|
||||
database,
|
||||
newFields,
|
||||
newPayload,
|
||||
dbId,
|
||||
}: Param): Promise<any> {
|
||||
try {
|
||||
const dbFullName = database;
|
||||
|
||||
const userSchemaData = grabUserSchemaData({ userId });
|
||||
if (!userSchemaData) throw new Error("User schema data not found!");
|
||||
let targetDatabase = grabPrimaryRequiredDbSchema({
|
||||
dbId,
|
||||
userId,
|
||||
});
|
||||
|
||||
let targetDatabaseIndex = userSchemaData.findIndex(
|
||||
(db) => db.dbFullName === database
|
||||
);
|
||||
|
||||
if (targetDatabaseIndex < 0) {
|
||||
if (!targetDatabase) {
|
||||
throw new Error("Couldn't Find Target Database!");
|
||||
}
|
||||
|
||||
let existingTableIndex = userSchemaData[
|
||||
targetDatabaseIndex
|
||||
]?.tables.findIndex((table) => table.tableName === "users");
|
||||
let existingTableIndex = targetDatabase?.tables.findIndex(
|
||||
(table) => table.tableName === "users"
|
||||
);
|
||||
|
||||
const usersTable =
|
||||
userSchemaData[targetDatabaseIndex].tables[existingTableIndex];
|
||||
const usersTable = targetDatabase.tables[existingTableIndex];
|
||||
|
||||
if (!usersTable?.fields?.[0]) throw new Error("Users Table Not Found!");
|
||||
|
||||
@@ -56,7 +57,7 @@ export default async function updateUsersTableSchema({
|
||||
|
||||
usersTable.fields.splice(finalSpliceStartIndex, 0, ...additionalFields);
|
||||
|
||||
setUserSchemaData({ schemaData: userSchemaData, userId });
|
||||
writeUpdatedDbSchema({ dbSchema: targetDatabase, userId });
|
||||
|
||||
const dbShellUpdate = await createDbFromSchema({
|
||||
userId,
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
import mysql from "serverless-mysql";
|
||||
import { DSQL_TableSchemaType, UserType } from "../../types";
|
||||
import grabMariadbMainUserForUser from "./grab-mariadb-main-user-for-user";
|
||||
import connDbHandler from "../../utils/db/conn-db-handler";
|
||||
|
||||
type Params = {
|
||||
query?: string;
|
||||
values?: any[];
|
||||
database?: string;
|
||||
tableSchema?: DSQL_TableSchemaType;
|
||||
debug?: boolean;
|
||||
user: UserType;
|
||||
};
|
||||
|
||||
export default async function userDbHandler({
|
||||
query,
|
||||
user,
|
||||
database,
|
||||
debug,
|
||||
tableSchema,
|
||||
values,
|
||||
}: Params) {
|
||||
const { fullName, host, username, password } =
|
||||
await grabMariadbMainUserForUser({ user });
|
||||
|
||||
const connection = mysql({
|
||||
config: {
|
||||
host,
|
||||
user: username,
|
||||
password: password,
|
||||
database: database,
|
||||
charset: "utf8mb4",
|
||||
},
|
||||
});
|
||||
|
||||
const results = await connDbHandler(connection, query);
|
||||
|
||||
return results;
|
||||
}
|
||||
@@ -2,12 +2,13 @@ import parseDbResults from "./parseDbResults";
|
||||
import serverError from "./serverError";
|
||||
import grabDSQLConnection from "../../utils/grab-dsql-connection";
|
||||
import connDbHandler from "../../utils/db/conn-db-handler";
|
||||
import { DSQL_TableSchemaType } from "../../types";
|
||||
|
||||
type Param = {
|
||||
queryString: string;
|
||||
queryValuesArray?: any[];
|
||||
database?: string;
|
||||
tableSchema?: import("../../types").DSQL_TableSchemaType;
|
||||
tableSchema?: DSQL_TableSchemaType;
|
||||
debug?: boolean;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user