Updates
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { DSQL_TableSchemaType, PostInsertReturn } from "../../../types";
|
||||
import { APIResponseObject, DSQL_TableSchemaType, DsqlCrudParamWhereClause, PostInsertReturn } from "../../../types";
|
||||
import { DbContextsArray } from "./runQuery";
|
||||
type Param<T extends {
|
||||
[k: string]: any;
|
||||
@@ -7,9 +7,10 @@ type Param<T extends {
|
||||
dbFullName?: string;
|
||||
tableName: K;
|
||||
tableSchema?: DSQL_TableSchemaType;
|
||||
identifierColumnName: keyof T;
|
||||
identifierValue: string | number;
|
||||
identifierColumnName?: keyof T;
|
||||
identifierValue?: string | number;
|
||||
forceLocal?: boolean;
|
||||
whereClauseObject?: DsqlCrudParamWhereClause;
|
||||
};
|
||||
/**
|
||||
* # Delete DB Entry Function
|
||||
@@ -17,5 +18,5 @@ type Param<T extends {
|
||||
*/
|
||||
export default function deleteDbEntry<T extends {
|
||||
[k: string]: any;
|
||||
} = any, K extends string = string>({ dbContext, dbFullName, tableName, identifierColumnName, identifierValue, forceLocal, }: Param<T, K>): Promise<PostInsertReturn | null>;
|
||||
} = any, K extends string = string>({ dbContext, dbFullName, tableName, identifierColumnName, identifierValue, forceLocal, whereClauseObject, }: Param<T, K>): Promise<APIResponseObject<PostInsertReturn>>;
|
||||
export {};
|
||||
|
||||
+32
-8
@@ -13,6 +13,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = deleteDbEntry;
|
||||
const sql_formatter_1 = require("sql-formatter");
|
||||
const check_if_is_master_1 = __importDefault(require("../../../utils/check-if-is-master"));
|
||||
const conn_db_handler_1 = __importDefault(require("../../../utils/db/conn-db-handler"));
|
||||
/**
|
||||
@@ -20,7 +21,7 @@ const conn_db_handler_1 = __importDefault(require("../../../utils/db/conn-db-han
|
||||
* @description
|
||||
*/
|
||||
function deleteDbEntry(_a) {
|
||||
return __awaiter(this, arguments, void 0, function* ({ dbContext, dbFullName, tableName, identifierColumnName, identifierValue, forceLocal, }) {
|
||||
return __awaiter(this, arguments, void 0, function* ({ dbContext, dbFullName, tableName, identifierColumnName, identifierValue, forceLocal, whereClauseObject, }) {
|
||||
try {
|
||||
const isMaster = forceLocal
|
||||
? true
|
||||
@@ -30,19 +31,42 @@ function deleteDbEntry(_a) {
|
||||
*
|
||||
* @description
|
||||
*/
|
||||
const query = `DELETE FROM ${isMaster && !dbFullName ? "" : `\`${dbFullName}\`.`}\`${tableName}\` WHERE \`${identifierColumnName.toString()}\`=?`;
|
||||
const deletedEntry = yield (0, conn_db_handler_1.default)({
|
||||
let query = `DELETE FROM ${isMaster && !dbFullName ? "" : `\`${dbFullName}\`.`}\`${tableName}\``;
|
||||
let values = [];
|
||||
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 = (yield (0, conn_db_handler_1.default)({
|
||||
query,
|
||||
values: [identifierValue],
|
||||
});
|
||||
values,
|
||||
}));
|
||||
/**
|
||||
* Return statement
|
||||
*/
|
||||
return deletedEntry;
|
||||
return {
|
||||
success: Boolean(deletedEntry.affectedRows),
|
||||
payload: deletedEntry,
|
||||
queryObject: {
|
||||
sql: (0, sql_formatter_1.format)(query),
|
||||
params: values,
|
||||
},
|
||||
};
|
||||
}
|
||||
catch (error) {
|
||||
console.log("Error Deleting Entry =>", error.message);
|
||||
return null;
|
||||
const errorMsg = `Error Deleting Entry =>, ${error.message}`;
|
||||
console.log(errorMsg);
|
||||
return {
|
||||
success: false,
|
||||
msg: errorMsg,
|
||||
};
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ function grabParsedValue({ value, tableSchema, encryptionKey, encryptionSalt, da
|
||||
}
|
||||
if (typeof newValue === "string" &&
|
||||
(newValue.match(/^null$/i) || !newValue.match(/./i))) {
|
||||
newValue = undefined;
|
||||
newValue = "";
|
||||
}
|
||||
if (typeof newValue === "boolean" ||
|
||||
((_d = targetFieldSchema === null || targetFieldSchema === void 0 ? void 0 : targetFieldSchema.dataType) === null || _d === void 0 ? void 0 : _d.match(/boolean/i))) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { DbContextsArray } from "./runQuery";
|
||||
import { APIResponseObject, DSQL_TableSchemaType, PostInsertReturn } from "../../../types";
|
||||
import { APIResponseObject, DSQL_TableSchemaType, DsqlCrudParamWhereClause, PostInsertReturn } from "../../../types";
|
||||
import { ConnectionConfig } from "mariadb";
|
||||
type Param<T extends {
|
||||
[k: string]: any;
|
||||
@@ -16,6 +16,7 @@ type Param<T extends {
|
||||
forceLocal?: boolean;
|
||||
debug?: boolean;
|
||||
dbConfig?: ConnectionConfig;
|
||||
whereClauseObject?: DsqlCrudParamWhereClause;
|
||||
};
|
||||
/**
|
||||
* # Update DB Function
|
||||
@@ -23,5 +24,5 @@ type Param<T extends {
|
||||
*/
|
||||
export default function updateDbEntry<T extends {
|
||||
[k: string]: any;
|
||||
} = any>({ dbContext, dbFullName, tableName, data, tableSchema, identifierColumnName, identifierValue, encryptionKey, encryptionSalt, forceLocal, debug, dbConfig, }: Param<T>): Promise<APIResponseObject<PostInsertReturn>>;
|
||||
} = any>({ dbContext, dbFullName, tableName, data, tableSchema, identifierColumnName, identifierValue, encryptionKey, encryptionSalt, forceLocal, debug, dbConfig, whereClauseObject, }: Param<T>): Promise<APIResponseObject<PostInsertReturn>>;
|
||||
export {};
|
||||
|
||||
+10
-3
@@ -24,7 +24,7 @@ const grab_parsed_value_1 = __importDefault(require("./grab-parsed-value"));
|
||||
* @description
|
||||
*/
|
||||
function updateDbEntry(_a) {
|
||||
return __awaiter(this, arguments, void 0, function* ({ dbContext, dbFullName, tableName, data, tableSchema, identifierColumnName, identifierValue, encryptionKey, encryptionSalt, forceLocal, debug, dbConfig, }) {
|
||||
return __awaiter(this, arguments, void 0, function* ({ dbContext, dbFullName, tableName, data, tableSchema, identifierColumnName, identifierValue, encryptionKey, encryptionSalt, forceLocal, debug, dbConfig, whereClauseObject, }) {
|
||||
var _b, _c;
|
||||
/**
|
||||
* Check if data is valid
|
||||
@@ -96,8 +96,15 @@ function updateDbEntry(_a) {
|
||||
updateKeyValueArray.push(`date_updated_code='${Date.now()}'`);
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
const query = `UPDATE ${isMaster && !dbFullName ? "" : `\`${dbFullName}\`.`}\`${tableName}\` SET ${updateKeyValueArray.join(",")} WHERE \`${identifierColumnName}\`=?`;
|
||||
updateValues.push(identifierValue);
|
||||
let query = `UPDATE ${isMaster && !dbFullName ? "" : `\`${dbFullName}\`.`}\`${tableName}\` SET ${updateKeyValueArray.join(",")}`;
|
||||
if (whereClauseObject) {
|
||||
query += ` ${whereClauseObject.clause}`;
|
||||
updateValues.push(...(whereClauseObject.params || []));
|
||||
}
|
||||
else {
|
||||
query += ` WHERE \`${identifierColumnName}\`=?`;
|
||||
updateValues.push(identifierValue);
|
||||
}
|
||||
const updatedEntry = yield (0, conn_db_handler_1.default)({
|
||||
query,
|
||||
values: updateValues,
|
||||
|
||||
Reference in New Issue
Block a user