Updates
This commit is contained in:
@@ -1,4 +1,10 @@
|
||||
import { DSQL_TableSchemaType, PostInsertReturn } from "../../../types";
|
||||
import { format } from "sql-formatter";
|
||||
import {
|
||||
APIResponseObject,
|
||||
DSQL_TableSchemaType,
|
||||
DsqlCrudParamWhereClause,
|
||||
PostInsertReturn,
|
||||
} from "../../../types";
|
||||
import checkIfIsMaster from "../../../utils/check-if-is-master";
|
||||
import connDbHandler from "../../../utils/db/conn-db-handler";
|
||||
import { DbContextsArray } from "./runQuery";
|
||||
@@ -8,9 +14,10 @@ type Param<T extends { [k: string]: any } = any, K extends string = string> = {
|
||||
dbFullName?: string;
|
||||
tableName: K;
|
||||
tableSchema?: DSQL_TableSchemaType;
|
||||
identifierColumnName: keyof T;
|
||||
identifierValue: string | number;
|
||||
identifierColumnName?: keyof T;
|
||||
identifierValue?: string | number;
|
||||
forceLocal?: boolean;
|
||||
whereClauseObject?: DsqlCrudParamWhereClause;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -27,7 +34,8 @@ export default async function deleteDbEntry<
|
||||
identifierColumnName,
|
||||
identifierValue,
|
||||
forceLocal,
|
||||
}: Param<T, K>): Promise<PostInsertReturn | null> {
|
||||
whereClauseObject,
|
||||
}: Param<T, K>): Promise<APIResponseObject<PostInsertReturn>> {
|
||||
try {
|
||||
const isMaster = forceLocal
|
||||
? true
|
||||
@@ -38,21 +46,46 @@ export default async function deleteDbEntry<
|
||||
*
|
||||
* @description
|
||||
*/
|
||||
const query = `DELETE FROM ${
|
||||
let query = `DELETE FROM ${
|
||||
isMaster && !dbFullName ? "" : `\`${dbFullName}\`.`
|
||||
}\`${tableName}\` WHERE \`${identifierColumnName.toString()}\`=?`;
|
||||
}\`${tableName}\``;
|
||||
|
||||
const deletedEntry = await connDbHandler({
|
||||
let values: any[] = [];
|
||||
|
||||
if (whereClauseObject) {
|
||||
query += ` ${whereClauseObject.clause}`;
|
||||
values.push(...(whereClauseObject.params || []));
|
||||
} else if (identifierColumnName && identifierValue) {
|
||||
query += ` WHERE \`${identifierColumnName.toString()}\`=?`;
|
||||
values = [identifierValue];
|
||||
} else {
|
||||
throw new Error(
|
||||
`Delete operation has no specified rows! Can't delete everything in this table!`
|
||||
);
|
||||
}
|
||||
|
||||
const deletedEntry = (await connDbHandler({
|
||||
query,
|
||||
values: [identifierValue],
|
||||
});
|
||||
values,
|
||||
})) as PostInsertReturn;
|
||||
|
||||
/**
|
||||
* Return statement
|
||||
*/
|
||||
return deletedEntry;
|
||||
return {
|
||||
success: Boolean(deletedEntry.affectedRows),
|
||||
payload: deletedEntry,
|
||||
queryObject: {
|
||||
sql: format(query),
|
||||
params: values,
|
||||
},
|
||||
};
|
||||
} catch (error: any) {
|
||||
console.log("Error Deleting Entry =>", error.message);
|
||||
return null;
|
||||
const errorMsg = `Error Deleting Entry =>, ${error.message}`;
|
||||
console.log(errorMsg);
|
||||
return {
|
||||
success: false,
|
||||
msg: errorMsg,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ export default function grabParsedValue({
|
||||
if (typeof newValue == "undefined") {
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
typeof newValue !== "string" &&
|
||||
typeof newValue !== "number" &&
|
||||
@@ -94,7 +95,7 @@ export default function grabParsedValue({
|
||||
typeof newValue === "string" &&
|
||||
(newValue.match(/^null$/i) || !newValue.match(/./i))
|
||||
) {
|
||||
newValue = undefined;
|
||||
newValue = "";
|
||||
}
|
||||
|
||||
if (
|
||||
|
||||
@@ -5,6 +5,7 @@ import { DbContextsArray } from "./runQuery";
|
||||
import {
|
||||
APIResponseObject,
|
||||
DSQL_TableSchemaType,
|
||||
DsqlCrudParamWhereClause,
|
||||
PostInsertReturn,
|
||||
} from "../../../types";
|
||||
import _ from "lodash";
|
||||
@@ -25,6 +26,7 @@ type Param<T extends { [k: string]: any } = any> = {
|
||||
forceLocal?: boolean;
|
||||
debug?: boolean;
|
||||
dbConfig?: ConnectionConfig;
|
||||
whereClauseObject?: DsqlCrudParamWhereClause;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -46,6 +48,7 @@ export default async function updateDbEntry<
|
||||
forceLocal,
|
||||
debug,
|
||||
dbConfig,
|
||||
whereClauseObject,
|
||||
}: Param<T>): Promise<APIResponseObject<PostInsertReturn>> {
|
||||
/**
|
||||
* Check if data is valid
|
||||
@@ -135,13 +138,17 @@ export default async function updateDbEntry<
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
const query = `UPDATE ${
|
||||
let query = `UPDATE ${
|
||||
isMaster && !dbFullName ? "" : `\`${dbFullName}\`.`
|
||||
}\`${tableName}\` SET ${updateKeyValueArray.join(",")} WHERE \`${
|
||||
identifierColumnName as string
|
||||
}\`=?`;
|
||||
}\`${tableName}\` SET ${updateKeyValueArray.join(",")}`;
|
||||
|
||||
updateValues.push(identifierValue);
|
||||
if (whereClauseObject) {
|
||||
query += ` ${whereClauseObject.clause}`;
|
||||
updateValues.push(...(whereClauseObject.params || []));
|
||||
} else {
|
||||
query += ` WHERE \`${identifierColumnName as string}\`=?`;
|
||||
updateValues.push(identifierValue);
|
||||
}
|
||||
|
||||
const updatedEntry = await connDbHandler({
|
||||
query,
|
||||
|
||||
Reference in New Issue
Block a user