A local-first personal finance app
[TypeScript] Make `db.firstSync` generic to make it easy to type DB query results (#4249)
Joel Jeremy Marquez84e4cda6ad4c
diff --git a/packages/loot-core/src/server/budget/actions.ts b/packages/loot-core/src/server/budget/actions.tsindex 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<Pick<db.DbPreference, 'value'>>(`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<db.DbZeroBudget | db.DbReflectBudget>(`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<db.DbZeroBudget | db.DbReflectBudget, 'id'>+ >(`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<void> {const table = getBudgetTable();- const existing = db.firstSync(- `SELECT id FROM ${table} WHERE month = ? AND category = ?`,- [dbMonth(month), category],- );+ const existing = db.firstSync<+ Pick<db.DbZeroBudget | db.DbReflectBudget, 'id'>+ >(`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<void> {}export function setBuffer(month: string, amount: unknown): Promise<void> {- const existing = db.firstSync(+ const existing = db.firstSync<Pick<db.DbZeroBudget, 'id'>>(`SELECT id FROM zero_budget_months WHERE id = ?`,[month],);@@ -167,10 +173,12 @@ function setCarryover(month: string,flag: boolean,): Promise<void> {- const existing = db.firstSync(- `SELECT id FROM ${table} WHERE month = ? AND category = ?`,- [month, category],- );+ const existing = db.firstSync<+ Pick<db.DbZeroBudget | db.DbReflectBudget, 'id'>+ >(`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<Pick<db.DbNote, 'note'>>(+ `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.tsindex 43316d2194..3bdcdd5371 100644--- a/packages/loot-core/src/server/db/index.ts+++ b/packages/loot-core/src/server/db/index.ts@@ -175,12 +175,9 @@ export async function first<T>(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<T>(sql, params?: (string | number)[]) {+ const arr = runQuery<T>(sql, params, true);+ return arr.length === 0 ? null : arr[0];}// This function is marked as async because `runQuery` is no longer@@ -423,7 +420,7 @@ export async function insertCategory(}if (atEnd) {- const lastCat = await first<Pick<DbCategoryGroup, 'sort_order'>>(`+ const lastCat = await first<Pick<DbCategory, 'sort_order'>>(`SELECT sort_order FROM categories WHERE tombstone = 0 ORDER BY sort_order DESC, id DESC LIMIT 1`);sort_order = (lastCat ? lastCat.sort_order : 0) + SORT_INCREMENT;diff --git a/upcoming-release-notes/4249.md b/upcoming-release-notes/4249.mdnew file mode 100644index 0000000000..981ff496fe--- /dev/null+++ b/upcoming-release-notes/4249.md@@ -0,0 +1,6 @@+---+category: Maintenance+authors: [joel-jeremy]+---++[TypeScript] Make `db.firstSync` generic to make it easy to type DB query results.\ No newline at end of file