First working version.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import dbHandler from "../db-handler";
|
||||
import _ from "lodash";
|
||||
import type {
|
||||
APIResponseObject,
|
||||
DBResponseObject,
|
||||
SQLInsertGenValueType,
|
||||
ServerQueryParam,
|
||||
} from "../../types";
|
||||
@@ -29,7 +29,7 @@ export default async function DbUpdate<
|
||||
data,
|
||||
query,
|
||||
targetId,
|
||||
}: Params<Schema, Table>): Promise<APIResponseObject> {
|
||||
}: Params<Schema, Table>): Promise<DBResponseObject> {
|
||||
let sqlObj: ReturnType<typeof sqlGenerator> = { string: "", values: [] };
|
||||
|
||||
try {
|
||||
@@ -63,7 +63,8 @@ export default async function DbUpdate<
|
||||
}
|
||||
|
||||
let values: SQLInsertGenValueType[] = [];
|
||||
let sql = `UPDATE ${quoteIdentifier(table)} SET`;
|
||||
let sql = ``;
|
||||
sql += `UPDATE ${quoteIdentifier(table)} SET`;
|
||||
|
||||
const finalData: { [k: string]: SQLInsertGenValueType } = {
|
||||
updated_at: Date.now(),
|
||||
@@ -89,31 +90,47 @@ export default async function DbUpdate<
|
||||
sql += ` ${whereClause}`;
|
||||
values = [...values, ...sqlQueryObj.values];
|
||||
|
||||
sqlObj.string = sql;
|
||||
sqlObj.values = values as any[];
|
||||
|
||||
const res = await dbHandler({
|
||||
query: sql,
|
||||
values: values as any,
|
||||
});
|
||||
|
||||
if (!res.success) {
|
||||
return {
|
||||
success: false,
|
||||
msg: "Database update failed",
|
||||
debug: {
|
||||
sqlObj,
|
||||
},
|
||||
};
|
||||
sqlObj.string = sql;
|
||||
sqlObj.values = values as any[];
|
||||
|
||||
let updated_sql = ``;
|
||||
let updated_sql_values: any[] = [];
|
||||
|
||||
updated_sql += `SELECT * FROM ${quoteIdentifier(table)} ${whereClause}`;
|
||||
updated_sql_values = [...updated_sql_values, ...sqlQueryObj.values];
|
||||
updated_sql += ` AND `;
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
const key = keys[i];
|
||||
if (!key) continue;
|
||||
if (key == "updated_at") continue;
|
||||
|
||||
const isLast = i == keys.length - 1;
|
||||
|
||||
updated_sql += ` ${quoteIdentifier(key)}=?`;
|
||||
updated_sql_values.push(finalData[key] ?? null);
|
||||
|
||||
if (!isLast) {
|
||||
updated_sql += ` AND `;
|
||||
}
|
||||
}
|
||||
|
||||
const singleRes = res.single_res as any;
|
||||
const updated_res = await dbHandler({
|
||||
query: updated_sql,
|
||||
values: updated_sql_values,
|
||||
});
|
||||
|
||||
const affected_rows = updated_res.payload?.length;
|
||||
|
||||
return {
|
||||
success: Boolean(singleRes?.affectedRows || singleRes?.changedRows),
|
||||
postInsertReturn: {
|
||||
affectedRows: Number(singleRes?.affectedRows),
|
||||
insertId: Number(singleRes?.insertId),
|
||||
...res,
|
||||
success: Boolean(affected_rows),
|
||||
insert_return: {
|
||||
affected_rows,
|
||||
},
|
||||
debug: {
|
||||
sqlObj,
|
||||
|
||||
Reference in New Issue
Block a user