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);
|
.get(routeId);
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateRoute(route) {
|
// Update `fields` of a row by id from a plain object. Returns the run info.
|
||||||
const fields = [
|
function update(table, fields, obj) {
|
||||||
"verb",
|
|
||||||
"path",
|
|
||||||
"structure_id",
|
|
||||||
"handler",
|
|
||||||
"updated_at",
|
|
||||||
"error",
|
|
||||||
];
|
|
||||||
const values = fields.map((field) => route[field]);
|
|
||||||
const placeholders = fields.map((field) => `${field} = ?`).join(", ");
|
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 = ?`;
|
function updateRoute(route) {
|
||||||
values.push(route.id); // Add routeId to the end for the WHERE clause
|
update("routes", ["verb", "path", "structure_id", "handler", "updated_at", "error"], route);
|
||||||
db.prepare(sql).run(...values);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateDb(appDb) {
|
function updateDb(appDb) {
|
||||||
const fields = ["name", "library"];
|
update("dbs", ["name", "library"], appDb);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateStruct(struct) {
|
function updateStruct(struct) {
|
||||||
const fields = ["name", "route_prefix", "head_injection"];
|
update("structures", ["name", "route_prefix", "head_injection"], struct);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateTemplate(template) {
|
function updateTemplate(template) {
|
||||||
const fields = ["content", "name", "test_object"];
|
update("templates", ["content", "name", "test_object"], template);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTemplates(structureId) {
|
function getTemplates(structureId) {
|
||||||
|
|
@ -508,17 +485,7 @@ function getLatestScaffoldPage(routeId) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateScaffoldPage(scaffoldPage) {
|
function updateScaffoldPage(scaffoldPage) {
|
||||||
const fields = ["content"];
|
return update("scaffold_pages", ["content"], scaffoldPage).changes > 0;
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function generatePostForm(endpoint) {
|
function generatePostForm(endpoint) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue