From fc03fafab91c44cae6aadfdb054638652b1872ff Mon Sep 17 00:00:00 2001 From: KernelDeimos <7225168+KernelDeimos@users.noreply.github.com> Date: Tue, 30 Dec 2025 12:59:15 -0500 Subject: [PATCH] dev(data-access): misc improvements to `.update()` DRY some redundant code and use guard clauses where applicable. --- .../src/modules/data-access/AppService.js | 29 ++++++++----------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/src/backend/src/modules/data-access/AppService.js b/src/backend/src/modules/data-access/AppService.js index dd80d211f..0c80384f7 100644 --- a/src/backend/src/modules/data-access/AppService.js +++ b/src/backend/src/modules/data-access/AppService.js @@ -531,19 +531,12 @@ export default class AppService extends BaseService { values.push(value); } - if ( set_clauses.length === 0 ) { - // Nothing to update in the main table - // Fetch the internal ID for file associations - const rows = await this.db.read('SELECT id FROM apps WHERE uid = ?', - [old_app.uid]); - return { insert_id: rows[0]?.id }; + if ( set_clauses.length > 0 ) { + values.push(old_app.uid); + const stmt = `UPDATE apps SET ${set_clauses.join(', ')} WHERE uid = ? LIMIT 1`; + await this.db_write.write(stmt, values); } - values.push(old_app.uid); - - const stmt = `UPDATE apps SET ${set_clauses.join(', ')} WHERE uid = ?`; - await this.db_write.write(stmt, values); - // Fetch the internal ID const rows = await this.db.read('SELECT id FROM apps WHERE uid = ?', [old_app.uid]); @@ -556,13 +549,15 @@ export default class AppService extends BaseService { [app_id]); // Add new file associations - if ( filetype_associations && filetype_associations.length > 0 ) { - const stmt = - `INSERT INTO app_filetype_association (app_id, type) VALUES ${ - filetype_associations.map(() => '(?, ?)').join(', ')}`; - const values = filetype_associations.flatMap(ft => [app_id, ft.toLowerCase()]); - await this.db_write.write(stmt, values); + if ( !filetype_associations || !(filetype_associations.length > 0) ) { + return; } + + const stmt = + `INSERT INTO app_filetype_association (app_id, type) VALUES ${ + filetype_associations.map(() => '(?, ?)').join(', ')}`; + const values = filetype_associations.flatMap(ft => [app_id, ft.toLowerCase()]); + await this.db_write.write(stmt, values); } async #emit_change_events (object, old_app) {