147 lines
4.0 KiB
TypeScript
147 lines
4.0 KiB
TypeScript
import checkIfIsMaster from "../../../utils/check-if-is-master";
|
|
import connDbHandler from "../../../utils/db/conn-db-handler";
|
|
import { DbContextsArray } from "./runQuery";
|
|
import {
|
|
APIResponseObject,
|
|
DSQL_TableSchemaType,
|
|
PostInsertReturn,
|
|
} from "../../../types";
|
|
import _ from "lodash";
|
|
import purgeDefaultFields from "../../../utils/purge-default-fields";
|
|
import grabParsedValue from "./grab-parsed-value";
|
|
|
|
type Param<T extends { [k: string]: any } = any> = {
|
|
dbContext?: (typeof DbContextsArray)[number];
|
|
dbFullName?: string;
|
|
tableName: string;
|
|
encryptionKey?: string;
|
|
encryptionSalt?: string;
|
|
data?: T;
|
|
tableSchema?: DSQL_TableSchemaType;
|
|
identifierColumnName: keyof T;
|
|
identifierValue: string | number;
|
|
forceLocal?: boolean;
|
|
debug?: boolean;
|
|
};
|
|
|
|
/**
|
|
* # Update DB Function
|
|
* @description
|
|
*/
|
|
export default async function updateDbEntry<
|
|
T extends { [k: string]: any } = any
|
|
>({
|
|
dbContext,
|
|
dbFullName,
|
|
tableName,
|
|
data,
|
|
tableSchema,
|
|
identifierColumnName,
|
|
identifierValue,
|
|
encryptionKey,
|
|
encryptionSalt,
|
|
forceLocal,
|
|
debug,
|
|
}: Param<T>): Promise<APIResponseObject<PostInsertReturn>> {
|
|
/**
|
|
* Check if data is valid
|
|
*/
|
|
if (!data || !Object.keys(data).length) {
|
|
return {
|
|
success: false,
|
|
payload: undefined,
|
|
msg: "No data provided",
|
|
};
|
|
}
|
|
|
|
const isMaster = forceLocal
|
|
? true
|
|
: checkIfIsMaster({ dbContext, dbFullName });
|
|
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
|
|
let newData = _.cloneDeep(data);
|
|
newData = purgeDefaultFields(newData);
|
|
|
|
/**
|
|
* Declare variables
|
|
*
|
|
* @description Declare "results" variable
|
|
*/
|
|
const dataKeys = Object.keys(newData);
|
|
|
|
let updateKeyValueArray = [];
|
|
let updateValues = [];
|
|
|
|
for (let i = 0; i < dataKeys.length; i++) {
|
|
try {
|
|
const dataKey = dataKeys[i];
|
|
let value = newData[dataKey];
|
|
|
|
const parsedValue = grabParsedValue({
|
|
dataKey,
|
|
encryptionKey,
|
|
encryptionSalt,
|
|
tableSchema,
|
|
value,
|
|
});
|
|
|
|
if (typeof parsedValue == "undefined") continue;
|
|
|
|
updateKeyValueArray.push(`\`${dataKey}\`=?`);
|
|
|
|
if (typeof parsedValue == "number") {
|
|
updateValues.push(String(parsedValue));
|
|
} else {
|
|
updateValues.push(parsedValue);
|
|
}
|
|
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
} catch (/** @type {any} */ error: any) {
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
|
|
console.log(
|
|
"DSQL: Error in parsing data keys in update function =>",
|
|
error.message
|
|
);
|
|
continue;
|
|
}
|
|
}
|
|
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
|
|
updateKeyValueArray.push(`date_updated='${Date()}'`);
|
|
updateKeyValueArray.push(`date_updated_code='${Date.now()}'`);
|
|
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
|
|
const query = `UPDATE ${
|
|
isMaster && !dbFullName ? "" : `\`${dbFullName}\`.`
|
|
}\`${tableName}\` SET ${updateKeyValueArray.join(",")} WHERE \`${
|
|
identifierColumnName as string
|
|
}\`=?`;
|
|
|
|
updateValues.push(identifierValue);
|
|
|
|
const updatedEntry = await connDbHandler(null, query, updateValues);
|
|
|
|
/**
|
|
* Return statement
|
|
*/
|
|
return {
|
|
success: Boolean(updatedEntry?.affectedRows),
|
|
payload: updatedEntry,
|
|
queryObject: {
|
|
sql: query,
|
|
params: updateValues,
|
|
},
|
|
debug: debug ? { data, newData } : undefined,
|
|
};
|
|
}
|