- isQueue table option adds queue columns and a (status, available_at) index; BunSQLite.queue exposes add, claim, complete, fail and release_stale with atomic claims - Set busy_timeout on connections so workers in separate processes wait instead of failing with SQLITE_BUSY - Quote table and column names with backticks in generated SQL, so reserved words (to, order, group) work and unknown columns error instead of matching as string literals - Tests for queue ops, multi-process claims and reserved-word names Co-Authored-By: Claude Opus 5.5 <[email protected]>
96 lines
3.1 KiB
JavaScript
96 lines
3.1 KiB
JavaScript
import dbHandler from "../db-handler";
|
|
import _ from "lodash";
|
|
import init from "../../functions/init";
|
|
import sqlGenerator from "../../utils/sql-generator";
|
|
import { sqlValue } from "../../utils/sql-insert-generator";
|
|
import { sqlName } from "../../utils/sql-safe";
|
|
export default async function DbUpdate({ table, data, query, targetId, returning = true, config, }) {
|
|
let sqlObj = {
|
|
string: "",
|
|
values: [],
|
|
};
|
|
try {
|
|
const finalQuery = query || {};
|
|
const { dbSchema } = init();
|
|
const isVectorTable = dbSchema.tables.find((t) => t.tableName === table)?.isVector ===
|
|
true;
|
|
const useReturning = returning && !isVectorTable;
|
|
const sqlQueryObj = sqlGenerator({
|
|
tableName: table,
|
|
genObject: _.pick(finalQuery, ["query", "searchOperator"]),
|
|
targetId,
|
|
});
|
|
let values = [];
|
|
const whereClause = sqlQueryObj.where.string;
|
|
if (whereClause) {
|
|
let sql = `UPDATE ${sqlName(table, "table name")} SET`;
|
|
const finalData = {
|
|
updated_at: Date.now(),
|
|
...data,
|
|
};
|
|
const keys = Object.keys(finalData);
|
|
for (let i = 0; i < keys.length; i++) {
|
|
const key = keys[i];
|
|
if (!key)
|
|
continue;
|
|
const isLast = i == keys.length - 1;
|
|
sql += ` ${sqlName(key, "column name")}=?`;
|
|
values.push(sqlValue(finalData[key]));
|
|
if (!isLast) {
|
|
sql += `,`;
|
|
}
|
|
}
|
|
sql += ` ${whereClause}`;
|
|
if (useReturning) {
|
|
sql += " RETURNING *";
|
|
}
|
|
values = [...values, ...sqlQueryObj.where.values];
|
|
sqlObj.string = sql;
|
|
sqlObj.values = values;
|
|
const res = dbHandler({ query: sql, values, config });
|
|
if (!res.success) {
|
|
return { ...res, debug: { sqlObj } };
|
|
}
|
|
if (!useReturning) {
|
|
return {
|
|
success: Boolean(res.postInsertReturn?.affectedRows),
|
|
postInsertReturn: res.postInsertReturn,
|
|
debug: {
|
|
sqlObj,
|
|
},
|
|
};
|
|
}
|
|
const rows = res.payload || [];
|
|
return {
|
|
success: rows.length > 0,
|
|
payload: rows,
|
|
singleRes: rows[0] ?? null,
|
|
postInsertReturn: {
|
|
affectedRows: rows.length,
|
|
insertId: rows.length
|
|
? Number(rows[rows.length - 1]?.id)
|
|
: undefined,
|
|
},
|
|
debug: {
|
|
sqlObj,
|
|
},
|
|
};
|
|
}
|
|
else {
|
|
return {
|
|
success: false,
|
|
msg: `No WHERE clause`,
|
|
};
|
|
}
|
|
}
|
|
catch (error) {
|
|
return {
|
|
success: false,
|
|
error: error.message,
|
|
debug: {
|
|
sqlObj,
|
|
},
|
|
};
|
|
}
|
|
}
|