refactor: collapse five identical update builders into one update()
updateRoute/updateDb/updateStruct/updateTemplate/updateScaffoldPage were the same UPDATE-by-id builder with a different table and field list. Extract a single update(table, fields, obj) and express each as a one-liner. (updateScaffoldPage's two stray debug console.logs went away with the rewrite; it keeps its changes>0 return.) Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
d34c917ef9
commit
de94d43e48
1 changed files with 12 additions and 45 deletions
57
db.js
57
db.js
|
|
@ -167,51 +167,28 @@ function getRoute(routeId) {
|
|||
.get(routeId);
|
||||
}
|
||||
|
||||
function updateRoute(route) {
|
||||
const fields = [
|
||||
"verb",
|
||||
"path",
|
||||
"structure_id",
|
||||
"handler",
|
||||
"updated_at",
|
||||
"error",
|
||||
];
|
||||
const values = fields.map((field) => route[field]);
|
||||
// Update `fields` of a row by id from a plain object. Returns the run info.
|
||||
function update(table, fields, obj) {
|
||||
const placeholders = fields.map((field) => `${field} = ?`).join(", ");
|
||||
const values = fields.map((field) => obj[field]);
|
||||
values.push(obj.id);
|
||||
return db.prepare(`UPDATE ${table} SET ${placeholders} WHERE id = ?`).run(...values);
|
||||
}
|
||||
|
||||
const sql = `UPDATE routes SET ${placeholders} WHERE id = ?`;
|
||||
values.push(route.id); // Add routeId to the end for the WHERE clause
|
||||
db.prepare(sql).run(...values);
|
||||
function updateRoute(route) {
|
||||
update("routes", ["verb", "path", "structure_id", "handler", "updated_at", "error"], route);
|
||||
}
|
||||
|
||||
function updateDb(appDb) {
|
||||
const fields = ["name", "library"];
|
||||
const values = fields.map((field) => appDb[field]);
|
||||
const placeholders = fields.map((field) => `${field} = ?`).join(", ");
|
||||
|
||||
const sql = `UPDATE dbs SET ${placeholders} WHERE id = ?`;
|
||||
values.push(appDb.id); // Add routeId to the end for the WHERE clause
|
||||
db.prepare(sql).run(...values);
|
||||
update("dbs", ["name", "library"], appDb);
|
||||
}
|
||||
|
||||
function updateStruct(struct) {
|
||||
const fields = ["name", "route_prefix", "head_injection"];
|
||||
const values = fields.map((field) => struct[field]);
|
||||
const placeholders = fields.map((field) => `${field} = ?`).join(", ");
|
||||
|
||||
const sql = `UPDATE structures SET ${placeholders} WHERE id = ?`;
|
||||
values.push(struct.id); // Add routeId to the end for the WHERE clause
|
||||
db.prepare(sql).run(...values);
|
||||
update("structures", ["name", "route_prefix", "head_injection"], struct);
|
||||
}
|
||||
|
||||
function updateTemplate(template) {
|
||||
const fields = ["content", "name", "test_object"];
|
||||
const values = fields.map((field) => template[field]);
|
||||
const placeholders = fields.map((field) => `${field} = ?`).join(", ");
|
||||
|
||||
const sql = `UPDATE templates SET ${placeholders} WHERE id = ?`;
|
||||
values.push(template.id);
|
||||
db.prepare(sql).run(...values);
|
||||
update("templates", ["content", "name", "test_object"], template);
|
||||
}
|
||||
|
||||
function getTemplates(structureId) {
|
||||
|
|
@ -508,17 +485,7 @@ function getLatestScaffoldPage(routeId) {
|
|||
}
|
||||
|
||||
function updateScaffoldPage(scaffoldPage) {
|
||||
const fields = ["content"];
|
||||
const values = fields.map((field) => scaffoldPage[field]);
|
||||
const placeholders = fields.map((field) => `${field} = ?`).join(", ");
|
||||
console.log(scaffoldPage, "gabababab")
|
||||
|
||||
const sql = `UPDATE scaffold_pages SET ${placeholders} WHERE id = ?`;
|
||||
values.push(scaffoldPage.id); // Add scaffoldPageId to the end for the WHERE clause
|
||||
|
||||
const info = db.prepare(sql).run(...values);
|
||||
console.log(info.changes)
|
||||
return info.changes > 0; // Returns true if a row was updated, false otherwise
|
||||
return update("scaffold_pages", ["content"], scaffoldPage).changes > 0;
|
||||
}
|
||||
|
||||
function generatePostForm(endpoint) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue