Updates
This commit is contained in:
@@ -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,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user