From de94d43e486402ef28db156bbb7e9fc3e06a1ccb Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 2 Aug 2026 22:31:15 -0400 Subject: [PATCH] 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 --- db.js | 57 ++++++++++++--------------------------------------------- 1 file changed, 12 insertions(+), 45 deletions(-) diff --git a/db.js b/db.js index 343d2d3..dc80bf3 100644 --- a/db.js +++ b/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) {