Refactor types
This commit is contained in:
+10
-3
@@ -29,7 +29,7 @@ const runQuery = require("./utils/runQuery");
|
||||
*
|
||||
* @param {Object} params - Single object passed
|
||||
* @param {LocalQueryObject} params.options - SQL Query
|
||||
* @param {import("../../types/database-schema.td").DSQL_DatabaseSchemaType} [params.dbSchema] - Name of the table to query
|
||||
* @param {import("@/package-shared/types/database-schema.td").DSQL_DatabaseSchemaType | undefined} [params.dbSchema] - Name of the table to query
|
||||
*
|
||||
* @returns { Promise<LocalGetReturn> } - Return Object
|
||||
*/
|
||||
@@ -47,7 +47,13 @@ async function localGet({ options, dbSchema }) {
|
||||
*
|
||||
* @description Input Validation
|
||||
*/
|
||||
if (typeof query == "string" && (query.match(/^alter|^delete|information_schema|databases|^create/i) || !query.match(/^select/i))) {
|
||||
if (
|
||||
typeof query == "string" &&
|
||||
(query.match(
|
||||
/^alter|^delete|information_schema|databases|^create/i
|
||||
) ||
|
||||
!query.match(/^select/i))
|
||||
) {
|
||||
return { success: false, msg: "Wrong Input" };
|
||||
}
|
||||
|
||||
@@ -68,7 +74,8 @@ async function localGet({ options, dbSchema }) {
|
||||
});
|
||||
|
||||
if (error) throw error;
|
||||
if (!result) throw new Error("No Result received for query => " + query);
|
||||
if (!result)
|
||||
throw new Error("No Result received for query => " + query);
|
||||
if (result?.error) throw new Error(result.error);
|
||||
|
||||
results = result;
|
||||
|
||||
@@ -24,7 +24,7 @@ const runQuery = require("./utils/runQuery");
|
||||
*
|
||||
* @param {Object} params - Single object passed
|
||||
* @param {LocalPostQueryObject} params.options - SQL Query
|
||||
* @param {import("../../types/database-schema.td").DSQL_DatabaseSchemaType} [params.dbSchema] - Name of the table to query
|
||||
* @param {import("@/package-shared/types/database-schema.td").DSQL_DatabaseSchemaType | undefined} [params.dbSchema] - Name of the table to query
|
||||
*
|
||||
* @returns { Promise<LocalPostReturn> } - Return Object
|
||||
*/
|
||||
@@ -41,11 +41,17 @@ async function localPost({ options, dbSchema }) {
|
||||
*
|
||||
* @description Input Validation
|
||||
*/
|
||||
if (typeof query === "string" && query?.match(/^create |^alter |^drop /i)) {
|
||||
if (
|
||||
typeof query === "string" &&
|
||||
query?.match(/^create |^alter |^drop /i)
|
||||
) {
|
||||
return { success: false, msg: "Wrong Input" };
|
||||
}
|
||||
|
||||
if (typeof query === "object" && query?.action?.match(/^create |^alter |^drop /i)) {
|
||||
if (
|
||||
typeof query === "object" &&
|
||||
query?.action?.match(/^create |^alter |^drop /i)
|
||||
) {
|
||||
return { success: false, msg: "Wrong Input" };
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ const updateDbEntry = require("./updateDbEntry");
|
||||
* @param {string} params.dbFullName - Database full name
|
||||
* @param {string} params.tableName - Table name
|
||||
* @param {*} params.data - Data to add
|
||||
* @param {import("../../../types/database-schema.td").DSQL_TableSchemaType} [params.tableSchema] - Table schema
|
||||
* @param {import("@/package-shared/types/database-schema.td").DSQL_TableSchemaType} [params.tableSchema] - Table schema
|
||||
* @param {string} [params.duplicateColumnName] - Duplicate column name
|
||||
* @param {string} [params.duplicateColumnValue] - Duplicate column value
|
||||
* @param {boolean} [params.update] - Update this row if it exists
|
||||
@@ -28,7 +28,17 @@ const updateDbEntry = require("./updateDbEntry");
|
||||
*
|
||||
* @returns {Promise<*>}
|
||||
*/
|
||||
async function addDbEntry({ dbFullName, tableName, data, tableSchema, duplicateColumnName, duplicateColumnValue, update, encryptionKey, encryptionSalt }) {
|
||||
async function addDbEntry({
|
||||
dbFullName,
|
||||
tableName,
|
||||
data,
|
||||
tableSchema,
|
||||
duplicateColumnName,
|
||||
duplicateColumnValue,
|
||||
update,
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
}) {
|
||||
/**
|
||||
* Initialize variables
|
||||
*/
|
||||
@@ -79,8 +89,15 @@ async function addDbEntry({ dbFullName, tableName, data, tableSchema, duplicateC
|
||||
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] : null;
|
||||
const targetFieldSchemaArray = tableSchema
|
||||
? tableSchema?.fields?.filter(
|
||||
(field) => field.fieldName == dataKey
|
||||
)
|
||||
: null;
|
||||
const targetFieldSchema =
|
||||
targetFieldSchemaArray && targetFieldSchemaArray[0]
|
||||
? targetFieldSchemaArray[0]
|
||||
: null;
|
||||
|
||||
if (!value) continue;
|
||||
|
||||
@@ -90,7 +107,10 @@ async function addDbEntry({ dbFullName, tableName, data, tableSchema, duplicateC
|
||||
}
|
||||
|
||||
if (targetFieldSchema?.pattern) {
|
||||
const pattern = new RegExp(targetFieldSchema.pattern, targetFieldSchema.patternFlags || "");
|
||||
const pattern = new RegExp(
|
||||
targetFieldSchema.pattern,
|
||||
targetFieldSchema.patternFlags || ""
|
||||
);
|
||||
if (!value?.toString()?.match(pattern)) {
|
||||
console.log("DSQL: Pattern not matched =>", value);
|
||||
value = "";
|
||||
@@ -136,7 +156,9 @@ async function addDbEntry({ dbFullName, tableName, data, tableSchema, duplicateC
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
const query = `INSERT INTO \`${tableName}\` (${insertKeysArray.join(",")}) VALUES (${insertValuesArray.map(() => "?").join(",")})`;
|
||||
const query = `INSERT INTO \`${tableName}\` (${insertKeysArray.join(
|
||||
","
|
||||
)}) VALUES (${insertValuesArray.map(() => "?").join(",")})`;
|
||||
const queryValuesArray = insertValuesArray;
|
||||
|
||||
const newInsert = await dbHandler({
|
||||
|
||||
@@ -19,13 +19,20 @@ const dbHandler = require("../../engine/utils/dbHandler");
|
||||
* "Read only" or "Full Access"? Defaults to "Read Only"
|
||||
* @param {string} params.dbFullName - Database full name
|
||||
* @param {string} params.tableName - Table name
|
||||
* @param {import("../../../types/database-schema.td").DSQL_TableSchemaType} [params.tableSchema] - Table schema
|
||||
* @param {import("@/package-shared/types/database-schema.td").DSQL_TableSchemaType} [params.tableSchema] - Table schema
|
||||
* @param {string} params.identifierColumnName - Update row identifier column name
|
||||
* @param {string|number} params.identifierValue - Update row identifier column value
|
||||
*
|
||||
* @returns {Promise<object|null>}
|
||||
*/
|
||||
async function deleteDbEntry({ dbContext, paradigm, dbFullName, tableName, identifierColumnName, identifierValue }) {
|
||||
async function deleteDbEntry({
|
||||
dbContext,
|
||||
paradigm,
|
||||
dbFullName,
|
||||
tableName,
|
||||
identifierColumnName,
|
||||
identifierValue,
|
||||
}) {
|
||||
try {
|
||||
/**
|
||||
* Check if data is valid
|
||||
|
||||
@@ -31,13 +31,20 @@ const varDatabaseDbHandler = require("../../engine/utils/varDatabaseDbHandler");
|
||||
* @param {string} params.dbFullName - Database full name. Eg. "datasquire_user_2_test"
|
||||
* @param {*} params.query - Query string or object
|
||||
* @param {boolean} [params.readOnly] - Is this operation read only?
|
||||
* @param {import("../../../types/database-schema.td").DSQL_DatabaseSchemaType} [params.dbSchema] - Database schema
|
||||
* @param {import("@/package-shared/types/database-schema.td").DSQL_DatabaseSchemaType} [params.dbSchema] - Database schema
|
||||
* @param {string[]} [params.queryValuesArray] - An optional array of query values if "?" is used in the query string
|
||||
* @param {string} [params.tableName] - Table Name
|
||||
*
|
||||
* @return {Promise<{result: *, error?: *}>}
|
||||
*/
|
||||
async function runQuery({ dbFullName, query, readOnly, dbSchema, queryValuesArray, tableName }) {
|
||||
async function runQuery({
|
||||
dbFullName,
|
||||
query,
|
||||
readOnly,
|
||||
dbSchema,
|
||||
queryValuesArray,
|
||||
tableName,
|
||||
}) {
|
||||
/**
|
||||
* Declare variables
|
||||
*
|
||||
@@ -50,9 +57,17 @@ async function runQuery({ dbFullName, query, readOnly, dbSchema, queryValuesArra
|
||||
|
||||
if (dbSchema) {
|
||||
try {
|
||||
const table = tableName ? tableName : typeof query == "string" ? null : query ? query?.table : null;
|
||||
const table = tableName
|
||||
? tableName
|
||||
: typeof query == "string"
|
||||
? null
|
||||
: query
|
||||
? query?.table
|
||||
: null;
|
||||
if (!table) throw new Error("No table name provided");
|
||||
tableSchema = dbSchema.tables.filter((tb) => tb?.tableName === table)[0];
|
||||
tableSchema = dbSchema.tables.filter(
|
||||
(tb) => tb?.tableName === table
|
||||
)[0];
|
||||
} catch (_err) {}
|
||||
}
|
||||
|
||||
@@ -75,7 +90,16 @@ async function runQuery({ dbFullName, query, readOnly, dbSchema, queryValuesArra
|
||||
*
|
||||
* @description Declare "results" variable
|
||||
*/
|
||||
const { data, action, table, identifierColumnName, identifierValue, update, duplicateColumnName, duplicateColumnValue } = query;
|
||||
const {
|
||||
data,
|
||||
action,
|
||||
table,
|
||||
identifierColumnName,
|
||||
identifierValue,
|
||||
update,
|
||||
duplicateColumnName,
|
||||
duplicateColumnValue,
|
||||
} = query;
|
||||
|
||||
switch (action.toLowerCase()) {
|
||||
case "insert":
|
||||
|
||||
@@ -21,7 +21,7 @@ const dbHandler = require("../../engine/utils/dbHandler");
|
||||
* @param {string} params.dbFullName - Database full name
|
||||
* @param {string} params.tableName - Table name
|
||||
* @param {*} params.data - Data to add
|
||||
* @param {import("../../../types/database-schema.td").DSQL_TableSchemaType} [params.tableSchema] - Table schema
|
||||
* @param {import("@/package-shared/types/database-schema.td").DSQL_TableSchemaType} [params.tableSchema] - Table schema
|
||||
* @param {string} params.identifierColumnName - Update row identifier column name
|
||||
* @param {string | number} params.identifierValue - Update row identifier column value
|
||||
* @param {string} params.encryptionKey - Encryption key
|
||||
@@ -29,7 +29,18 @@ const dbHandler = require("../../engine/utils/dbHandler");
|
||||
*
|
||||
* @returns {Promise<object|null>}
|
||||
*/
|
||||
async function updateDbEntry({ dbContext, paradigm, dbFullName, tableName, data, tableSchema, identifierColumnName, identifierValue, encryptionKey, encryptionSalt }) {
|
||||
async function updateDbEntry({
|
||||
dbContext,
|
||||
paradigm,
|
||||
dbFullName,
|
||||
tableName,
|
||||
data,
|
||||
tableSchema,
|
||||
identifierColumnName,
|
||||
identifierValue,
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
}) {
|
||||
/**
|
||||
* Check if data is valid
|
||||
*/
|
||||
@@ -59,11 +70,23 @@ async function updateDbEntry({ dbContext, paradigm, dbFullName, tableName, data,
|
||||
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] : null;
|
||||
const targetFieldSchemaArray = tableSchema
|
||||
? tableSchema?.fields?.filter(
|
||||
(field) => field.fieldName === dataKey
|
||||
)
|
||||
: null;
|
||||
const targetFieldSchema =
|
||||
targetFieldSchemaArray && targetFieldSchemaArray[0]
|
||||
? targetFieldSchemaArray[0]
|
||||
: null;
|
||||
|
||||
if (typeof value == "undefined") continue;
|
||||
if (typeof value !== "string" && typeof value !== "number" && !value) continue;
|
||||
if (
|
||||
typeof value !== "string" &&
|
||||
typeof value !== "number" &&
|
||||
!value
|
||||
)
|
||||
continue;
|
||||
|
||||
if (targetFieldSchema?.encrypted) {
|
||||
value = encrypt({ data: value, encryptionKey, encryptionSalt });
|
||||
@@ -82,7 +105,10 @@ async function updateDbEntry({ dbContext, paradigm, dbFullName, tableName, data,
|
||||
}
|
||||
|
||||
if (targetFieldSchema?.pattern) {
|
||||
const pattern = new RegExp(targetFieldSchema.pattern, targetFieldSchema.patternFlags || "");
|
||||
const pattern = new RegExp(
|
||||
targetFieldSchema.pattern,
|
||||
targetFieldSchema.patternFlags || ""
|
||||
);
|
||||
if (!value?.toString()?.match(pattern)) {
|
||||
console.log("DSQL: Pattern not matched =>", value);
|
||||
value = "";
|
||||
@@ -108,7 +134,10 @@ async function updateDbEntry({ dbContext, paradigm, dbFullName, tableName, data,
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
console.log("DSQL: Error in parsing data keys in update function =>", error.message);
|
||||
console.log(
|
||||
"DSQL: Error in parsing data keys in update function =>",
|
||||
error.message
|
||||
);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@@ -122,7 +151,9 @@ async function updateDbEntry({ dbContext, paradigm, dbFullName, tableName, data,
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
const query = `UPDATE ${tableName} SET ${updateKeyValueArray.join(",")} WHERE \`${identifierColumnName}\`=?`;
|
||||
const query = `UPDATE ${tableName} SET ${updateKeyValueArray.join(
|
||||
","
|
||||
)} WHERE \`${identifierColumnName}\`=?`;
|
||||
|
||||
updateValues.push(identifierValue);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user