Bugfix
This commit is contained in:
parent
b73309e9d5
commit
ff5d8082b7
13
dist/lib/sqlite/db-update.js
vendored
13
dist/lib/sqlite/db-update.js
vendored
@ -2,7 +2,7 @@ import DbClient from ".";
|
|||||||
import _ from "lodash";
|
import _ from "lodash";
|
||||||
import sqlGenerator from "../../utils/sql-generator";
|
import sqlGenerator from "../../utils/sql-generator";
|
||||||
export default async function DbUpdate({ table, data, query, targetId, }) {
|
export default async function DbUpdate({ table, data, query, targetId, }) {
|
||||||
let sqlObj = null;
|
let sqlObj = { string: "", values: [] };
|
||||||
try {
|
try {
|
||||||
let finalQuery = query || {};
|
let finalQuery = query || {};
|
||||||
if (targetId) {
|
if (targetId) {
|
||||||
@ -14,12 +14,12 @@ export default async function DbUpdate({ table, data, query, targetId, }) {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
sqlObj = sqlGenerator({
|
const sqlQueryObj = sqlGenerator({
|
||||||
tableName: table,
|
tableName: table,
|
||||||
genObject: finalQuery,
|
genObject: finalQuery,
|
||||||
});
|
});
|
||||||
let values = [];
|
let values = [];
|
||||||
const whereClause = sqlObj.string.match(/WHERE .*/)?.[0];
|
const whereClause = sqlQueryObj.string.match(/WHERE .*/)?.[0];
|
||||||
if (whereClause) {
|
if (whereClause) {
|
||||||
let sql = `UPDATE ${table} SET`;
|
let sql = `UPDATE ${table} SET`;
|
||||||
const finalData = {
|
const finalData = {
|
||||||
@ -33,13 +33,16 @@ export default async function DbUpdate({ table, data, query, targetId, }) {
|
|||||||
continue;
|
continue;
|
||||||
const isLast = i == keys.length - 1;
|
const isLast = i == keys.length - 1;
|
||||||
sql += ` ${key}=?`;
|
sql += ` ${key}=?`;
|
||||||
values.push(String(finalData[key]));
|
const value = finalData[key];
|
||||||
|
values.push(value || null);
|
||||||
if (!isLast) {
|
if (!isLast) {
|
||||||
sql += `,`;
|
sql += `,`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sql += ` ${whereClause}`;
|
sql += ` ${whereClause}`;
|
||||||
values = [...values, ...sqlObj.values];
|
values = [...values, ...sqlQueryObj.values];
|
||||||
|
sqlObj.string = sql;
|
||||||
|
sqlObj.values = values;
|
||||||
const res = DbClient.run(sql, values);
|
const res = DbClient.run(sql, values);
|
||||||
return {
|
return {
|
||||||
success: Boolean(res.changes),
|
success: Boolean(res.changes),
|
||||||
|
|||||||
@ -1,6 +1,10 @@
|
|||||||
import DbClient from ".";
|
import DbClient from ".";
|
||||||
import _ from "lodash";
|
import _ from "lodash";
|
||||||
import type { APIResponseObject, ServerQueryParam } from "../../types";
|
import type {
|
||||||
|
APIResponseObject,
|
||||||
|
SQLInsertGenValueType,
|
||||||
|
ServerQueryParam,
|
||||||
|
} from "../../types";
|
||||||
import sqlGenerator from "../../utils/sql-generator";
|
import sqlGenerator from "../../utils/sql-generator";
|
||||||
|
|
||||||
type Params<
|
type Params<
|
||||||
@ -22,7 +26,7 @@ export default async function DbUpdate<
|
|||||||
query,
|
query,
|
||||||
targetId,
|
targetId,
|
||||||
}: Params<Schema, Table>): Promise<APIResponseObject> {
|
}: Params<Schema, Table>): Promise<APIResponseObject> {
|
||||||
let sqlObj: ReturnType<typeof sqlGenerator> | null = null;
|
let sqlObj: ReturnType<typeof sqlGenerator> = { string: "", values: [] };
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let finalQuery = query || {};
|
let finalQuery = query || {};
|
||||||
@ -40,19 +44,19 @@ export default async function DbUpdate<
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
sqlObj = sqlGenerator({
|
const sqlQueryObj = sqlGenerator({
|
||||||
tableName: table,
|
tableName: table,
|
||||||
genObject: finalQuery,
|
genObject: finalQuery,
|
||||||
});
|
});
|
||||||
|
|
||||||
let values: (string | number)[] = [];
|
let values: SQLInsertGenValueType[] = [];
|
||||||
|
|
||||||
const whereClause = sqlObj.string.match(/WHERE .*/)?.[0];
|
const whereClause = sqlQueryObj.string.match(/WHERE .*/)?.[0];
|
||||||
|
|
||||||
if (whereClause) {
|
if (whereClause) {
|
||||||
let sql = `UPDATE ${table} SET`;
|
let sql = `UPDATE ${table} SET`;
|
||||||
|
|
||||||
const finalData: { [k: string]: any } = {
|
const finalData: { [k: string]: SQLInsertGenValueType } = {
|
||||||
...data,
|
...data,
|
||||||
updated_at: Date.now(),
|
updated_at: Date.now(),
|
||||||
};
|
};
|
||||||
@ -66,9 +70,8 @@ export default async function DbUpdate<
|
|||||||
const isLast = i == keys.length - 1;
|
const isLast = i == keys.length - 1;
|
||||||
|
|
||||||
sql += ` ${key}=?`;
|
sql += ` ${key}=?`;
|
||||||
values.push(
|
const value = finalData[key];
|
||||||
String(finalData[key as keyof { [k: string]: any }]),
|
values.push(value || null);
|
||||||
);
|
|
||||||
|
|
||||||
if (!isLast) {
|
if (!isLast) {
|
||||||
sql += `,`;
|
sql += `,`;
|
||||||
@ -76,7 +79,10 @@ export default async function DbUpdate<
|
|||||||
}
|
}
|
||||||
|
|
||||||
sql += ` ${whereClause}`;
|
sql += ` ${whereClause}`;
|
||||||
values = [...values, ...sqlObj.values];
|
values = [...values, ...sqlQueryObj.values];
|
||||||
|
|
||||||
|
sqlObj.string = sql;
|
||||||
|
sqlObj.values = values as any[];
|
||||||
|
|
||||||
const res = DbClient.run(sql, values);
|
const res = DbClient.run(sql, values);
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user