From 5b8c4111347b8cd9cd4474683260b513d73f8cca Mon Sep 17 00:00:00 2001 From: Joel Jeremy Marquez Date: Mon, 27 Jan 2025 22:56:55 -0800 Subject: [PATCH] [TypeScript] Make db.firstSync generic --- .../loot-core/src/server/budget/actions.ts | 47 +++++++++++-------- packages/loot-core/src/server/db/index.ts | 9 ++-- 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/packages/loot-core/src/server/budget/actions.ts b/packages/loot-core/src/server/budget/actions.ts index f3f12f277c..69b5292674 100644 --- a/packages/loot-core/src/server/budget/actions.ts +++ b/packages/loot-core/src/server/budget/actions.ts @@ -35,12 +35,14 @@ function calcBufferedAmount( return buffered + amount; } -function getBudgetTable(): string { +type BudgetTable = 'reflect_budgets' | 'zero_budgets'; + +function getBudgetTable(): BudgetTable { return isReflectBudget() ? 'reflect_budgets' : 'zero_budgets'; } export function isReflectBudget(): boolean { - const budgetType = db.firstSync( + const budgetType = db.firstSync>( `SELECT value FROM preferences WHERE id = ?`, ['budgetType'], ); @@ -91,7 +93,7 @@ export function getBudget({ month: string; }): number { const table = getBudgetTable(); - const existing = db.firstSync( + const existing = db.firstSync( `SELECT * FROM ${table} WHERE month = ? AND category = ?`, [dbMonth(month), category], ); @@ -110,10 +112,12 @@ export function setBudget({ amount = safeNumber(typeof amount === 'number' ? amount : 0); const table = getBudgetTable(); - const existing = db.firstSync( - `SELECT id FROM ${table} WHERE month = ? AND category = ?`, - [dbMonth(month), category], - ); + const existing = db.firstSync< + Pick + >(`SELECT id FROM ${table} WHERE month = ? AND category = ?`, [ + dbMonth(month), + category, + ]); if (existing) { return db.update(table, { id: existing.id, amount }); } @@ -127,10 +131,12 @@ export function setBudget({ export function setGoal({ month, category, goal, long_goal }): Promise { const table = getBudgetTable(); - const existing = db.firstSync( - `SELECT id FROM ${table} WHERE month = ? AND category = ?`, - [dbMonth(month), category], - ); + const existing = db.firstSync< + Pick + >(`SELECT id FROM ${table} WHERE month = ? AND category = ?`, [ + dbMonth(month), + category, + ]); if (existing) { return db.update(table, { id: existing.id, @@ -148,7 +154,7 @@ export function setGoal({ month, category, goal, long_goal }): Promise { } export function setBuffer(month: string, amount: unknown): Promise { - const existing = db.firstSync( + const existing = db.firstSync>( `SELECT id FROM zero_budget_months WHERE id = ?`, [month], ); @@ -167,10 +173,12 @@ function setCarryover( month: string, flag: boolean, ): Promise { - const existing = db.firstSync( - `SELECT id FROM ${table} WHERE month = ? AND category = ?`, - [month, category], - ); + const existing = db.firstSync< + Pick + >(`SELECT id FROM ${table} WHERE month = ? AND category = ?`, [ + month, + category, + ]); if (existing) { return db.update(table, { id: existing.id, carryover: flag ? 1 : 0 }); } @@ -547,9 +555,10 @@ async function addMovementNotes({ const monthBudgetNotesId = `budget-${month}`; const existingMonthBudgetNotes = addNewLine( - db.firstSync(`SELECT n.note FROM notes n WHERE n.id = ?`, [ - monthBudgetNotesId, - ])?.note, + db.firstSync>( + `SELECT n.note FROM notes n WHERE n.id = ?`, + [monthBudgetNotesId], + )?.note, ); const displayDay = monthUtils.format(monthUtils.currentDate(), 'MMMM dd'); diff --git a/packages/loot-core/src/server/db/index.ts b/packages/loot-core/src/server/db/index.ts index ad5262cebc..94dfbb5a24 100644 --- a/packages/loot-core/src/server/db/index.ts +++ b/packages/loot-core/src/server/db/index.ts @@ -174,12 +174,9 @@ export async function first(sql, params?: (string | number)[]) { // The underlying sql system is now sync, but we can't update `first` yet // without auditing all uses of it -export function firstSync(sql, params?: (string | number)[]) { - const arr = runQuery(sql, params, true); - // TODO: In the next phase, we will make this function generic - // and pass the type of the return type to `runQuery`. - // eslint-disable-next-line @typescript-eslint/no-explicit-any - return arr.length === 0 ? null : (arr[0] as any); +export function firstSync(sql, params?: (string | number)[]) { + const arr = runQuery(sql, params, true); + return arr.length === 0 ? null : arr[0]; } // This function is marked as async because `runQuery` is no longer