From 466b239153bf630f0f47a2fef0d10e51ee05a3e1 Mon Sep 17 00:00:00 2001 From: stfrigerio Date: Sun, 6 Apr 2025 18:06:55 +0200 Subject: [PATCH] added updatedAt --- .../services/BooleanDataService.ts | 25 +++++++++---------- .../services/HabitDataService.ts | 24 ++++++++++-------- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/webcomponents/services/BooleanDataService.ts b/src/webcomponents/services/BooleanDataService.ts index 737f96c..edf3ea5 100644 --- a/src/webcomponents/services/BooleanDataService.ts +++ b/src/webcomponents/services/BooleanDataService.ts @@ -43,9 +43,9 @@ export class BooleanSwitchDataService { const safeHabitIdCol = quoteSqlIdentifier(args.habitIdCol); const safeValueCol = quoteSqlIdentifier(args.valueCol); const safeDateCol = quoteSqlIdentifier(args.dateCol); + const updatedAtCol = quoteSqlIdentifier("updatedAt"); //! 🔒 Hardcoded - let sql = ""; - let params: (string | number)[] = []; + const now = new Date().toISOString(); //~ Current timestamp for updatedAt let existingUuid: string | undefined; @@ -65,22 +65,21 @@ export class BooleanSwitchDataService { } if (existingUuid) { - sql = `UPDATE ${safeTable} SET ${safeValueCol} = ? WHERE uuid = ?`; - params = [args.newValue, existingUuid]; + const updateSql = `UPDATE ${safeTable} SET ${safeValueCol} = ?, ${updatedAtCol} = ? WHERE uuid = ?`; + const updateParams = [args.newValue, now, existingUuid]; + await this.dbService.runQuery(updateSql, updateParams); } else { - // Standard UPSERT path (no UUID column configured) - sql = ` - INSERT INTO ${safeTable} (${safeHabitIdCol}, ${safeDateCol}, ${safeValueCol}) - VALUES (?, ?, ?) + const insertSql = ` + INSERT INTO ${safeTable} (${safeHabitIdCol}, ${safeDateCol}, ${safeValueCol}, ${updatedAtCol}) + VALUES (?, ?, ?, ?) ON CONFLICT(${safeHabitIdCol}, ${safeDateCol}) DO UPDATE - SET ${safeValueCol} = excluded.${safeValueCol}; + SET ${safeValueCol} = excluded.${safeValueCol}, + ${updatedAtCol} = excluded.${updatedAtCol}; `; - //! Requires UNIQUE constraint on (keyIdCol, dateCol) - params = [args.habitKey, args.date, args.newValue]; + const insertParams = [args.habitKey, args.date, args.newValue, now]; + await this.dbService.runQuery(insertSql, insertParams); } - await this.dbService.runQuery(sql, params); - } catch (error) { console.error(`[BoolDataService] Error in upsertBooleanValue for ${args.habitKey}:`, error); if (error instanceof Error && error.message.includes("ON CONFLICT clause does not match")) { diff --git a/src/webcomponents/services/HabitDataService.ts b/src/webcomponents/services/HabitDataService.ts index 73db124..5f46649 100644 --- a/src/webcomponents/services/HabitDataService.ts +++ b/src/webcomponents/services/HabitDataService.ts @@ -47,6 +47,9 @@ export class HabitDataService { const safeHabitIdCol = quoteSqlIdentifier(args.habitIdCol); const safeValueCol = quoteSqlIdentifier(args.valueCol); const safeDateCol = quoteSqlIdentifier(args.dateCol); + const updatedAtCol = quoteSqlIdentifier("updatedAt"); //! 🔒 Hardcoded + + const now = new Date().toISOString(); //~ Current timestamp for updatedAt let existingUuid: string | undefined; @@ -58,29 +61,28 @@ export class HabitDataService { existingUuid = uuidResult?.[0]?.uuid; } catch (error) { if (error instanceof Error && /no such column|does not exist/i.test(error.message) && error.message.includes('uuid')) { - //^ It's expected that 'uuid' might not exist. Log for info and proceed without UUID. console.log(`[HabitDataService] Info: Optional 'uuid' column not found in table '${args.table}'. Proceeding with standard upsert.`); existingUuid = undefined; // Ensure it's undefined so the INSERT path is taken } else { - //^ A different, unexpected error occurred during the UUID fetch, re-throw it. console.error(`[HabitDataService] Error fetching potential UUID for ${args.habitKey} on ${args.date}:`, error); throw error; // Re-throw unexpected errors } } if (existingUuid && typeof existingUuid === 'string') { - const updateSql = `UPDATE ${safeTable} SET ${safeValueCol} = ? WHERE uuid = ?`; - const updateParams = [args.newValue, existingUuid]; + const updateSql = `UPDATE ${safeTable} SET ${safeValueCol} = ?, ${updatedAtCol} = ? WHERE uuid = ?`; + const updateParams = [args.newValue, now, existingUuid]; await this.dbService.runQuery(updateSql, updateParams); } else { - //^ Use specified columns in INSERT, ON CONFLICT target, and UPDATE SET without uuid - const sql = ` - INSERT INTO ${safeTable} (${safeHabitIdCol}, ${safeDateCol}, ${safeValueCol}) VALUES (?, ?, ?) - ON CONFLICT(${safeHabitIdCol}, ${safeDateCol}) DO UPDATE SET ${safeValueCol} = excluded.${safeValueCol}; + const insertSql = ` + INSERT INTO ${safeTable} (${safeHabitIdCol}, ${safeDateCol}, ${safeValueCol}, ${updatedAtCol}) + VALUES (?, ?, ?, ?) + ON CONFLICT(${safeHabitIdCol}, ${safeDateCol}) DO UPDATE SET + ${safeValueCol} = excluded.${safeValueCol}, + ${updatedAtCol} = excluded.${updatedAtCol}; `; - //! Requires UNIQUE constraint on the specified (habitIdCol, dateCol) - const params = [args.habitKey, args.date, args.newValue]; - await this.dbService.runQuery(sql, params); + const insertParams = [args.habitKey, args.date, args.newValue, now]; + await this.dbService.runQuery(insertSql, insertParams); } } catch (error) {