A local-first personal finance app
[TypeScript] Make db.all generic to make it easy to type DB query results (#4542)
Joel Jeremy Marquez4b48f09112bf
diff --git a/packages/loot-core/src/client/accounts/accountsSlice.ts b/packages/loot-core/src/client/accounts/accountsSlice.tsindex 0b713d1b13..425d39994d 100644--- a/packages/loot-core/src/client/accounts/accountsSlice.ts+++ b/packages/loot-core/src/client/accounts/accountsSlice.ts@@ -249,7 +249,11 @@ export const syncAccounts = createAppAsyncThunk(const { setAccountsSyncing } = accountsSlice.actions;dispatch(setAccountsSyncing({ ids: accountIdsToSync }));- const accountsData: AccountEntity[] = await send('accounts-get');+ // TODO: Force cast to AccountEntity.+ // Server is currently returning the DB model it should return the entity model instead.+ const accountsData = (await send(+ 'accounts-get',+ )) as unknown as AccountEntity[];const simpleFinAccounts = accountsData.filter(a => a.account_sync_source === 'simpleFin',);diff --git a/packages/loot-core/src/client/queries/queriesSlice.ts b/packages/loot-core/src/client/queries/queriesSlice.tsindex 1722e60745..7c0ee453ed 100644--- a/packages/loot-core/src/client/queries/queriesSlice.ts+++ b/packages/loot-core/src/client/queries/queriesSlice.ts@@ -227,7 +227,9 @@ export const updateAccount = createAppAsyncThunk(export const getAccounts = createAppAsyncThunk(`${sliceName}/getAccounts`,async () => {- const accounts: AccountEntity[] = await send('accounts-get');+ // TODO: Force cast to AccountEntity.+ // Server is currently returning the DB model it should return the entity model instead.+ const accounts = (await send('accounts-get')) as AccountEntity[];return accounts;},);diff --git a/packages/loot-core/src/server/accounts/sync.test.ts b/packages/loot-core/src/server/accounts/sync.test.tsindex f4514ca4e1..d0bcb9fe4b 100644--- a/packages/loot-core/src/server/accounts/sync.test.ts+++ b/packages/loot-core/src/server/accounts/sync.test.ts@@ -24,7 +24,9 @@ beforeEach(async () => {});function getAllTransactions() {- return db.all(+ return db.all<+ db.DbViewTransactionInternal & { payee_name: db.DbPayee['name'] }+ >(`SELECT t.*, p.name as payee_nameFROM v_transactions_internal tLEFT JOIN payees p ON p.id = t.payeediff --git a/packages/loot-core/src/server/accounts/sync.ts b/packages/loot-core/src/server/accounts/sync.tsindex 5f5cf79ccf..f49095de23 100644--- a/packages/loot-core/src/server/accounts/sync.ts+++ b/packages/loot-core/src/server/accounts/sync.ts@@ -557,7 +557,7 @@ export async function reconcileTransactions(}if (existing.is_parent && existing.cleared !== updates.cleared) {- const children = await db.all(+ const children = await db.all<Pick<db.DbViewTransaction, 'id'>>('SELECT id FROM v_transactions WHERE parent_id = ?',[existing.id],);@@ -670,7 +670,22 @@ export async function matchTransactions(// strictIdChecking has the added behaviour of only matching on transactions with no import ID// if the transaction being imported has an import ID.if (strictIdChecking) {- fuzzyDataset = await db.all(+ fuzzyDataset = await db.all<+ Pick<+ db.DbViewTransaction,+ | 'id'+ | 'is_parent'+ | 'date'+ | 'imported_id'+ | 'payee'+ | 'imported_payee'+ | 'category'+ | 'notes'+ | 'reconciled'+ | 'cleared'+ | 'amount'+ >+ >(`SELECT id, is_parent, date, imported_id, payee, imported_payee, category, notes, reconciled, cleared, amountFROM v_transactionsWHERE@@ -687,7 +702,22 @@ export async function matchTransactions(],);} else {- fuzzyDataset = await db.all(+ fuzzyDataset = await db.all<+ Pick<+ db.DbViewTransaction,+ | 'id'+ | 'is_parent'+ | 'date'+ | 'imported_id'+ | 'payee'+ | 'imported_payee'+ | 'category'+ | 'notes'+ | 'reconciled'+ | 'cleared'+ | 'amount'+ >+ >(`SELECT id, is_parent, date, imported_id, payee, imported_payee, category, notes, reconciled, cleared, amountFROM v_transactionsWHERE date >= ? AND date <= ? AND amount = ? AND account = ?`,diff --git a/packages/loot-core/src/server/api.ts b/packages/loot-core/src/server/api.tsindex 32b78ee719..e03039f278 100644--- a/packages/loot-core/src/server/api.ts+++ b/packages/loot-core/src/server/api.ts@@ -17,6 +17,7 @@ import {} from '../shared/transactions';import { integerToAmount } from '../shared/util';import { Handlers } from '../types/handlers';+import { AccountEntity, CategoryGroupEntity } from '../types/models';import { ServerHandlers } from '../types/server-handlers';import { addTransactions } from './accounts/sync';@@ -53,7 +54,7 @@ function withMutation<Params extends Array<unknown>, ReturnType>(const latestTimestamp = getClock().timestamp.toString();const result = await handler(...args);- const rows = await db.all(+ const rows = await db.all<Pick<db.DbCrdtMessage, 'dataset'>>('SELECT DISTINCT dataset FROM messages_crdt WHERE timestamp > ?',[latestTimestamp],);@@ -355,7 +356,9 @@ handlers['api/budget-month'] = async function ({ month }) {checkFileOpen();await validateMonth(month);- const groups = await db.getCategoriesGrouped();+ // TODO: Force cast to CategoryGroupEntity. This should be updated to an AQL query.+ const groups =+ (await db.getCategoriesGrouped()) as unknown as CategoryGroupEntity[];const sheetName = monthUtils.sheetForMonth(month);function value(name) {@@ -556,7 +559,8 @@ handlers['api/transaction-delete'] = withMutation(async function ({ id }) {handlers['api/accounts-get'] = async function () {checkFileOpen();- const accounts = await db.getAccounts();+ // TODO: Force cast to AccountEntity. This should be updated to an AQL query.+ const accounts = (await db.getAccounts()) as AccountEntity[];return accounts.map(account => accountModel.toExternal(account));};diff --git a/packages/loot-core/src/server/aql/exec.test.ts b/packages/loot-core/src/server/aql/exec.test.tsindex 0a7bdf48cd..c16f3af1aa 100644--- a/packages/loot-core/src/server/aql/exec.test.ts+++ b/packages/loot-core/src/server/aql/exec.test.ts@@ -261,7 +261,9 @@ describe('runQuery', () => {it('fetches all data required for $oneof', async () => {await insertTransactions();- const rows = await db.all('SELECT id FROM transactions WHERE amount < -50');+ const rows = await db.all<Pick<db.DbTransaction, 'id'>>(+ 'SELECT id FROM transactions WHERE amount < -50',+ );const ids = rows.slice(0, 3).map(row => row.id);ids.sort();diff --git a/packages/loot-core/src/server/aql/schema/executors.ts b/packages/loot-core/src/server/aql/schema/executors.tsindex d0180aad6f..6d35fe0ea2 100644--- a/packages/loot-core/src/server/aql/schema/executors.ts+++ b/packages/loot-core/src/server/aql/schema/executors.ts@@ -125,7 +125,7 @@ async function execTransactionsGrouped(${sql.limit != null ? `LIMIT ${sql.limit}` : ''}${sql.offset != null ? `OFFSET ${sql.offset}` : ''}`;- rows = await db.all(rowSql, params);+ rows = await db.all<db.DbViewTransactionInternal>(rowSql, params);} else {// TODO: phew, what a doozy. write docs why it works this way//@@ -151,7 +151,7 @@ async function execTransactionsGrouped(${sql.offset != null ? `OFFSET ${sql.offset}` : ''}`;- rows = await db.all(rowSql, params);+ rows = await db.all<db.DbViewTransactionInternal>(rowSql, params);matched = new Set([].concat.apply([],@@ -171,7 +171,11 @@ async function execTransactionsGrouped(${sql.orderBy}`;- const allRows = await db.all(finalSql);+ const allRows = await db.all<+ db.DbViewTransactionInternal & {+ _parent_id: db.DbViewTransactionInternal['parent_id'];+ }+ >(finalSql);// Group the parents and children upconst { parents, children } = allRows.reduce(diff --git a/packages/loot-core/src/server/aql/schema/index.test.ts b/packages/loot-core/src/server/aql/schema/index.test.tsindex ce0e26a2c2..1b519dc28b 100644--- a/packages/loot-core/src/server/aql/schema/index.test.ts+++ b/packages/loot-core/src/server/aql/schema/index.test.ts@@ -9,29 +9,59 @@ beforeEach(global.emptyDatabase());describe('schema', () => {test('never returns transactions without a date', async () => {- expect((await db.all('SELECT * FROM transactions')).length).toBe(0);- expect((await db.all('SELECT * FROM v_transactions')).length).toBe(0);+ expect(+ (await db.all<db.DbTransaction>('SELECT * FROM transactions')).length,+ ).toBe(0);+ expect(+ (await db.all<db.DbViewTransaction>('SELECT * FROM v_transactions'))+ .length,+ ).toBe(0);await db.runQuery('INSERT INTO transactions (acct) VALUES (?)', ['foo']);- expect((await db.all('SELECT * FROM transactions')).length).toBe(1);- expect((await db.all('SELECT * FROM v_transactions')).length).toBe(0);+ expect(+ (await db.all<db.DbTransaction>('SELECT * FROM transactions')).length,+ ).toBe(1);+ expect(+ (await db.all<db.DbViewTransaction>('SELECT * FROM v_transactions'))+ .length,+ ).toBe(0);});test('never returns transactions without an account', async () => {- expect((await db.all('SELECT * FROM transactions')).length).toBe(0);- expect((await db.all('SELECT * FROM v_transactions')).length).toBe(0);+ expect(+ (await db.all<db.DbTransaction>('SELECT * FROM transactions')).length,+ ).toBe(0);+ expect(+ (await db.all<db.DbViewTransaction>('SELECT * FROM v_transactions'))+ .length,+ ).toBe(0);await db.runQuery('INSERT INTO transactions (date) VALUES (?)', [20200101]);- expect((await db.all('SELECT * FROM transactions')).length).toBe(1);- expect((await db.all('SELECT * FROM v_transactions')).length).toBe(0);+ expect(+ (await db.all<db.DbTransaction>('SELECT * FROM transactions')).length,+ ).toBe(1);+ expect(+ (await db.all<db.DbViewTransaction>('SELECT * FROM v_transactions'))+ .length,+ ).toBe(0);});test('never returns child transactions without a parent', async () => {- expect((await db.all('SELECT * FROM transactions')).length).toBe(0);- expect((await db.all('SELECT * FROM v_transactions')).length).toBe(0);+ expect(+ (await db.all<db.DbTransaction>('SELECT * FROM transactions')).length,+ ).toBe(0);+ expect(+ (await db.all<db.DbViewTransaction>('SELECT * FROM v_transactions'))+ .length,+ ).toBe(0);await db.runQuery('INSERT INTO transactions (date, acct, isChild) VALUES (?, ?, ?)',[20200101, 'foo', 1],);- expect((await db.all('SELECT * FROM transactions')).length).toBe(1);- expect((await db.all('SELECT * FROM v_transactions')).length).toBe(0);+ expect(+ (await db.all<db.DbTransaction>('SELECT * FROM transactions')).length,+ ).toBe(1);+ expect(+ (await db.all<db.DbViewTransaction>('SELECT * FROM v_transactions'))+ .length,+ ).toBe(0);});});diff --git a/packages/loot-core/src/server/budget/actions.ts b/packages/loot-core/src/server/budget/actions.tsindex 27bd6838cd..cd8f30c98e 100644--- a/packages/loot-core/src/server/budget/actions.ts+++ b/packages/loot-core/src/server/budget/actions.ts@@ -63,8 +63,14 @@ type BudgetData = {amount: number;};-function getBudgetData(table: string, month: string): Promise<BudgetData[]> {- return db.all(+function getBudgetData<T extends BudgetTable>(+ table: T,+ month: string,+): Promise<BudgetData[]> {+ return db.all<+ (db.DbReflectBudget | db.DbZeroBudget) &+ Pick<db.DbViewCategory, 'is_income'>+ >(`SELECT b.*, c.is_income FROM v_categories cLEFT JOIN ${table} b ON b.category = c.id@@ -235,7 +241,7 @@ export async function copySinglePreviousMonth({}export async function setZero({ month }: { month: string }): Promise<void> {- const categories = await db.all(+ const categories = await db.all<db.DbViewCategory>('SELECT * FROM v_categories WHERE tombstone = 0',);@@ -254,7 +260,7 @@ export async function set3MonthAvg({}: {month: string;}): Promise<void> {- const categories = await db.all(+ const categories = await db.all<db.DbViewCategory>('SELECT * FROM v_categories WHERE tombstone = 0',);@@ -297,7 +303,7 @@ export async function set12MonthAvg({}: {month: string;}): Promise<void> {- const categories = await db.all(+ const categories = await db.all<db.DbViewCategory>('SELECT * FROM v_categories WHERE tombstone = 0',);@@ -316,7 +322,7 @@ export async function set6MonthAvg({}: {month: string;}): Promise<void> {- const categories = await db.all(+ const categories = await db.all<db.DbViewCategory>('SELECT * FROM v_categories WHERE tombstone = 0',);diff --git a/packages/loot-core/src/server/budget/cleanup-template.ts b/packages/loot-core/src/server/budget/cleanup-template.tsindex b0dbc66535..098e8778d5 100644--- a/packages/loot-core/src/server/budget/cleanup-template.ts+++ b/packages/loot-core/src/server/budget/cleanup-template.ts@@ -132,7 +132,7 @@ async function processCleanup(month: string): Promise<Notification> {const db_month = parseInt(month.replace('-', ''));const category_templates = await getCategoryTemplates();- const categories = await db.all(+ const categories = await db.all<db.DbViewCategory>('SELECT * FROM v_categories WHERE tombstone = 0',);const sheetName = monthUtils.sheetForMonth(month);@@ -369,7 +369,7 @@ const TEMPLATE_PREFIX = '#cleanup ';async function getCategoryTemplates() {const templates = {};- const notes = await db.all(+ const notes = await db.all<db.DbNote>(`SELECT * FROM notes WHERE lower(note) like '%${TEMPLATE_PREFIX}%'`,);diff --git a/packages/loot-core/src/server/budget/goaltemplates.ts b/packages/loot-core/src/server/budget/goaltemplates.tsindex 5dacdfb01e..c68427d9dc 100644--- a/packages/loot-core/src/server/budget/goaltemplates.ts+++ b/packages/loot-core/src/server/budget/goaltemplates.ts@@ -1,7 +1,6 @@// @ts-strict-ignoreimport { Notification } from '../../client/notifications/notificationsSlice';import * as monthUtils from '../../shared/months';-import { CategoryEntity } from '../../types/models';import * as db from '../db';import { batchMessages } from '../sync';@@ -26,7 +25,7 @@ export async function overwriteTemplate({ month }): Promise<Notification> {export async function applyMultipleCategoryTemplates({ month, categoryIds }) {const placeholders = categoryIds.map(() => '?').join(', ');const query = `SELECT * FROM v_categories WHERE id IN (${placeholders})`;- const categories = await db.all(query, categoryIds);+ const categories = await db.all<db.DbViewCategory>(query, categoryIds);await storeTemplates();const categoryTemplates = await getTemplates(categories);const ret = await processTemplate(month, true, categoryTemplates, categories);@@ -34,9 +33,10 @@ export async function applyMultipleCategoryTemplates({ month, categoryIds }) {}export async function applySingleCategoryTemplate({ month, category }) {- const categories = await db.all(`SELECT * FROM v_categories WHERE id = ?`, [- category,- ]);+ const categories = await db.all<db.DbViewCategory>(+ `SELECT * FROM v_categories WHERE id = ?`,+ [category],+ );await storeTemplates();const categoryTemplates = await getTemplates(categories[0]);const ret = await processTemplate(month, true, categoryTemplates, categories);@@ -47,8 +47,8 @@ export function runCheckTemplates() {return checkTemplates();}-async function getCategories(): Promise<CategoryEntity[]> {- return await db.all(+async function getCategories(): Promise<db.DbCategory[]> {+ return await db.all<db.DbCategory>(`SELECT categories.* FROM categoriesINNER JOIN category_groups on categories.cat_group = category_groups.id@@ -60,7 +60,7 @@ async function getCategories(): Promise<CategoryEntity[]> {async function getTemplates(category) {//retrieves template definitions from the database- const goalDef = await db.all(+ const goalDef = await db.all<db.DbCategory>('SELECT * FROM categories WHERE goal_def IS NOT NULL',);diff --git a/packages/loot-core/src/server/budget/statements.ts b/packages/loot-core/src/server/budget/statements.tsindex 13e4707a5f..4547c5cdc2 100644--- a/packages/loot-core/src/server/budget/statements.ts+++ b/packages/loot-core/src/server/budget/statements.ts@@ -28,7 +28,9 @@ export type CategoryWithTemplateNote = {export async function getCategoriesWithTemplateNotes(): Promise<CategoryWithTemplateNote[]> {- return await db.all(+ return await db.all<+ Pick<db.DbCategory, 'id' | 'name'> & Pick<db.DbNote, 'note'>+ >(`SELECT c.id AS id, c.name as name, n.note AS noteFROM notes n@@ -41,8 +43,19 @@ export async function getCategoriesWithTemplateNotes(): Promise<);}-export async function getActiveSchedules(): Promise<DbSchedule[]> {- return await db.all(+export async function getActiveSchedules() {+ return await db.all<+ Pick<+ DbSchedule,+ | 'id'+ | 'rule'+ | 'active'+ | 'completed'+ | 'posts_transaction'+ | 'tombstone'+ | 'name'+ >+ >('SELECT id, rule, active, completed, posts_transaction, tombstone, name from schedules WHERE name NOT NULL AND tombstone = 0',);}diff --git a/packages/loot-core/src/server/dashboard/app.ts b/packages/loot-core/src/server/dashboard/app.tsindex d1299069b5..096977d5c9 100644--- a/packages/loot-core/src/server/dashboard/app.ts+++ b/packages/loot-core/src/server/dashboard/app.ts@@ -5,7 +5,6 @@ import * as fs from '../../platform/server/fs';import { DEFAULT_DASHBOARD_STATE } from '../../shared/dashboard';import { q } from '../../shared/query';import {- type CustomReportEntity,type ExportImportDashboard,type ExportImportDashboardWidget,type ExportImportCustomReportWidget,@@ -178,7 +177,7 @@ async function importDashboard({ filepath }: { filepath: string }) {exportModel.validate(parsedContent);- const customReportIds: CustomReportEntity[] = await db.all(+ const customReportIds = await db.all<Pick<db.DbCustomReport, 'id'>>('SELECT id from custom_reports',);const customReportIdSet = new Set(customReportIds.map(({ id }) => id));diff --git a/packages/loot-core/src/server/db/index.ts b/packages/loot-core/src/server/db/index.tsindex 0e330c2af2..0e66abd85d 100644--- a/packages/loot-core/src/server/db/index.ts+++ b/packages/loot-core/src/server/db/index.ts@@ -15,7 +15,6 @@ import * as fs from '../../platform/server/fs';import * as sqlite from '../../platform/server/sqlite';import * as monthUtils from '../../shared/months';import { groupById } from '../../shared/util';-import { CategoryEntity, CategoryGroupEntity } from '../../types/models';import { WithRequired } from '../../types/util';import {schema,@@ -36,12 +35,16 @@ import { sendMessages, batchMessages } from '../sync';import { shoveSortOrders, SORT_INCREMENT } from './sort';import {DbAccount,+ DbBank,DbCategory,DbCategoryGroup,+ DbCategoryMapping,DbClockMessage,DbPayee,+ DbPayeeMapping,DbTransaction,DbViewTransaction,+ DbViewTransactionInternalAlive,} from './types';export * from './types';@@ -161,11 +164,8 @@ export function asyncTransaction(fn: () => Promise<void>) {// This function is marked as async because `runQuery` is no longer// async. We return a promise here until we've audited all the code to// make sure nothing calls `.then` on this.-export async function all(sql, params?: (string | number)[]) {- // 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 runQuery(sql, params, true) as any[];+export async function all<T>(sql: string, params?: (string | number)[]) {+ return runQuery<T>(sql, params, true);}export async function first<T>(sql, params?: (string | number)[]) {@@ -265,7 +265,7 @@ export async function delete_(table, id) {}export async function deleteAll(table: string) {- const rows: Array<{ id: string }> = await all(`+ const rows = await all<{ id: string }>(`SELECT id FROM ${table} WHERE tombstone = 0`);await Promise.all(rows.map(({ id }) => delete_(table, id)));@@ -306,19 +306,17 @@ export function updateWithSchema(table, fields) {// Data-specific functions. Ideally this would be split up into// different files-// TODO: Fix return type. This should returns a DbCategory[].-export async function getCategories(- ids?: Array<DbCategory['id']>,-): Promise<CategoryEntity[]> {+export async function getCategories(ids?: Array<DbCategory['id']>) {const whereIn = ids ? `c.id IN (${toSqlQueryParameters(ids)}) AND` : '';const query = `SELECT c.* FROM categories c WHERE ${whereIn} c.tombstone = 0 ORDER BY c.sort_order, c.id`;- return ids ? await all(query, [...ids]) : await all(query);+ return ids+ ? await all<DbCategory>(query, [...ids])+ : await all<DbCategory>(query);}-// TODO: Fix return type. This should returns a [DbCategoryGroup, ...DbCategory].export async function getCategoriesGrouped(ids?: Array<DbCategoryGroup['id']>,-): Promise<Array<CategoryGroupEntity>> {+): Promise<Array<DbCategoryGroup & { categories: DbCategory[] }>> {const categoryGroupWhereIn = ids? `cg.id IN (${toSqlQueryParameters(ids)}) AND`: '';@@ -332,19 +330,17 @@ export async function getCategoriesGrouped(ORDER BY c.sort_order, c.id`;const groups = ids- ? await all(categoryGroupQuery, [...ids])- : await all(categoryGroupQuery);+ ? await all<DbCategoryGroup>(categoryGroupQuery, [...ids])+ : await all<DbCategoryGroup>(categoryGroupQuery);const categories = ids- ? await all(categoryQuery, [...ids])- : await all(categoryQuery);+ ? await all<DbCategory>(categoryQuery, [...ids])+ : await all<DbCategory>(categoryQuery);- return groups.map(group => {- return {- ...group,- categories: categories.filter(c => c.cat_group === group.id),- };- });+ return groups.map(group => ({+ ...group,+ categories: categories.filter(c => c.cat_group === group.id),+ }));}export async function insertCategoryGroup(group) {@@ -379,7 +375,7 @@ export function updateCategoryGroup(group) {}export async function moveCategoryGroup(id, targetId) {- const groups = await all(+ const groups = await all<Pick<DbCategoryGroup, 'id' | 'sort_order'>>(`SELECT id, sort_order FROM category_groups WHERE tombstone = 0 ORDER BY sort_order, id`,);@@ -391,9 +387,10 @@ export async function moveCategoryGroup(id, targetId) {}export async function deleteCategoryGroup(group, transferId?: string) {- const categories = await all('SELECT * FROM categories WHERE cat_group = ?', [- group.id,- ]);+ const categories = await all<DbCategory>(+ 'SELECT * FROM categories WHERE cat_group = ?',+ [group.id],+ );// Delete all the categories within a groupawait Promise.all(categories.map(cat => deleteCategory(cat, transferId)));@@ -427,7 +424,7 @@ export async function insertCategory(} else {// Unfortunately since we insert at the beginning, we need to shove// the sort orders to make sure there's room for it- const categories = await all(+ const categories = await all<Pick<DbCategory, 'id' | 'sort_order'>>(`SELECT id, sort_order FROM categories WHERE cat_group = ? AND tombstone = 0 ORDER BY sort_order, id`,[category.cat_group],);@@ -469,7 +466,7 @@ export async function moveCategory(throw new Error('moveCategory: groupId is required');}- const categories = await all(+ const categories = await all<Pick<DbCategory, 'id' | 'sort_order'>>(`SELECT id, sort_order FROM categories WHERE cat_group = ? AND tombstone = 0 ORDER BY sort_order, id`,[groupId],);@@ -489,7 +486,7 @@ export async function deleteCategory(// We need to update all the deleted categories that currently// point to the one we're about to delete so they all are// "forwarded" to the new transferred category.- const existingTransfers = await all(+ const existingTransfers = await all<DbCategoryMapping>('SELECT * FROM category_mapping WHERE transferId = ?',[category.id],);@@ -559,7 +556,7 @@ export async function mergePayees(ids: Array<DbPayee['id']>,) {// Load in payees so we can check some stuff- const dbPayees: DbPayee[] = await all('SELECT * FROM payees');+ const dbPayees: DbPayee[] = await all<DbPayee>('SELECT * FROM payees');const payees = groupById(dbPayees);// Filter out any transfer payees@@ -571,7 +568,7 @@ export async function mergePayees(await batchMessages(async () => {await Promise.all(ids.map(async id => {- const mappings = await all(+ const mappings = await all<DbPayeeMapping>('SELECT id FROM payee_mapping WHERE targetId = ?',[id],);@@ -594,8 +591,8 @@ export async function mergePayees(});}-export function getPayees(): Promise<DbPayee[]> {- return all(`+export function getPayees() {+ return all<DbPayee & { name: DbAccount['name'] | DbPayee['name'] }>(`SELECT p.*, COALESCE(a.name, p.name) AS name FROM payees pLEFT JOIN accounts a ON (p.transfer_acct = a.id AND a.tombstone = 0)WHERE p.tombstone = 0 AND (p.transfer_acct IS NULL OR a.id IS NOT NULL)@@ -603,12 +600,19 @@ export function getPayees(): Promise<DbPayee[]> {`);}-export function getCommonPayees(): Promise<DbPayee[]> {+export function getCommonPayees() {const twelveWeeksAgo = toDateRepr(monthUtils.subWeeks(monthUtils.currentDate(), 12),);const limit = 10;- return all(`+ return all<+ DbPayee & {+ common: true;+ transfer_acct: null;+ c: number;+ latest: DbViewTransactionInternalAlive['date'];+ }+ >(`SELECT p.id as id, p.name as name, p.favorite as favorite,p.category as category, TRUE as common, NULL as transfer_acct,count(*) as c,@@ -645,12 +649,12 @@ const orphanedPayeesQuery = ``;/* eslint-enable rulesdir/typography */-export function syncGetOrphanedPayees(): Promise<Array<Pick<DbPayee, 'id'>>> {- return all(orphanedPayeesQuery);+export function syncGetOrphanedPayees() {+ return all<Pick<DbPayee, 'id'>>(orphanedPayeesQuery);}-export async function getOrphanedPayees(): Promise<Array<DbPayee['id']>> {- const rows = await all(orphanedPayeesQuery);+export async function getOrphanedPayees() {+ const rows = await all<Pick<DbPayee, 'id'>>(orphanedPayeesQuery);return rows.map(row => row.id);}@@ -662,7 +666,12 @@ export async function getPayeeByName(name: DbPayee['name']) {}export function getAccounts() {- return all(+ return all<+ DbAccount & {+ bankName: DbBank['name'];+ bankId: DbBank['id'];+ }+ >(`SELECT a.*, b.name as bankName, b.id as bankId FROM accounts aLEFT JOIN banks b ON a.bank = b.idWHERE a.tombstone = 0@@ -671,7 +680,7 @@ export function getAccounts() {}export async function insertAccount(account) {- const accounts = await all(+ const accounts = await all<DbAccount>('SELECT * FROM accounts WHERE offbudget = ? ORDER BY sort_order, name',[account.offbudget ? 1 : 0],);@@ -702,11 +711,11 @@ export async function moveAccount();let accounts;if (account.closed) {- accounts = await all(+ accounts = await all<Pick<DbAccount, 'id' | 'sort_order'>>(`SELECT id, sort_order FROM accounts WHERE closed = 1 ORDER BY sort_order, name`,);} else {- accounts = await all(+ accounts = await all<Pick<DbAccount, 'id' | 'sort_order'>>(`SELECT id, sort_order FROM accounts WHERE tombstone = 0 AND offbudget = ? ORDER BY sort_order, name`,[account.offbudget ? 1 : 0],);diff --git a/packages/loot-core/src/server/db/mappings.ts b/packages/loot-core/src/server/db/mappings.tsindex f7423cab5f..eaacf93db1 100644--- a/packages/loot-core/src/server/db/mappings.ts+++ b/packages/loot-core/src/server/db/mappings.ts@@ -22,12 +22,12 @@ let unlistenSync;export async function loadMappings() {// The mappings are separated into tables specific to the type of// data. But you know, we really could keep a global mapping table.- const categories = (await db.all('SELECT * FROM category_mapping')).map(- r => [r.id, r.transferId] as const,- );- const payees = (await db.all('SELECT * FROM payee_mapping')).map(- r => [r.id, r.targetId] as const,- );+ const categories = (+ await db.all<db.DbCategoryMapping>('SELECT * FROM category_mapping')+ ).map(r => [r.id, r.transferId] as const);+ const payees = (+ await db.all<db.DbPayeeMapping>('SELECT * FROM payee_mapping')+ ).map(r => [r.id, r.targetId] as const);// All ids are unique, so we can just keep a global table of mappingsallMappings = new Map(categories.concat(payees));diff --git a/packages/loot-core/src/server/main.test.ts b/packages/loot-core/src/server/main.test.tsindex 0100c8d92f..911e5278d3 100644--- a/packages/loot-core/src/server/main.test.ts+++ b/packages/loot-core/src/server/main.test.ts@@ -136,7 +136,7 @@ describe('Accounts', () => {date: '2017-01-01',});const differ = expectSnapshotWithDiffer(- await db.all('SELECT * FROM transactions'),+ await db.all<db.DbTransaction>('SELECT * FROM transactions'),);let transaction = await db.getTransaction(id);@@ -145,11 +145,15 @@ describe('Accounts', () => {payee: 'transfer-three',date: '2017-01-03',});- differ.expectToMatchDiff(await db.all('SELECT * FROM transactions'));+ differ.expectToMatchDiff(+ await db.all<db.DbTransaction>('SELECT * FROM transactions'),+ );transaction = await db.getTransaction(id);await runHandler(handlers['transaction-delete'], transaction);- differ.expectToMatchDiff(await db.all('SELECT * FROM transactions'));+ differ.expectToMatchDiff(+ await db.all<db.DbTransaction>('SELECT * FROM transactions'),+ );});});diff --git a/packages/loot-core/src/server/main.ts b/packages/loot-core/src/server/main.tsindex d8a366225b..9510766824 100644--- a/packages/loot-core/src/server/main.ts+++ b/packages/loot-core/src/server/main.ts@@ -16,6 +16,7 @@ import * as monthUtils from '../shared/months';import { q } from '../shared/query';import { type Budget } from '../types/budget';import { Handlers } from '../types/handlers';+import { CategoryEntity, CategoryGroupEntity } from '../types/models';import { OpenIdConfig } from '../types/models/openid';import { app as accountsApp } from './accounts/app';@@ -103,9 +104,12 @@ handlers['redo'] = mutator(function () {});handlers['get-categories'] = async function () {+ // TODO: Force cast to CategoryGroupEntity and CategoryEntity.+ // This should be updated to AQL queries. The server should not directly return DB models.return {- grouped: await db.getCategoriesGrouped(),- list: await db.getCategories(),+ grouped:+ (await db.getCategoriesGrouped()) as unknown as CategoryGroupEntity[],+ list: (await db.getCategories()) as unknown as CategoryEntity[],};};@@ -336,7 +340,7 @@ handlers['category-group-delete'] = mutator(async function ({transferId,}) {return withUndo(async () => {- const groupCategories = await db.all(+ const groupCategories = await db.all<Pick<db.DbCategory, 'id'>>('SELECT id FROM categories WHERE cat_group = ? AND tombstone = 0',[id],);diff --git a/packages/loot-core/src/server/payees/app.ts b/packages/loot-core/src/server/payees/app.tsindex 7fa95935da..f0fa717598 100644--- a/packages/loot-core/src/server/payees/app.ts+++ b/packages/loot-core/src/server/payees/app.ts@@ -43,13 +43,13 @@ async function createPayee({ name }: { name: PayeeEntity['name'] }) {return db.insertPayee({ name });}-// Server must return AQL entities not the raw DB dataasync function getCommonPayees(): Promise<PayeeEntity[]> {+ // TODO: Update to an AQL query. Server must return AQL entities not the raw DB data.return (await db.getCommonPayees()).map(payeeModel.fromDb);}-// Server must return AQL entities not the raw DB dataasync function getPayees(): Promise<PayeeEntity[]> {+ // TODO: Update to an AQL query. Server must return AQL entities not the raw DB data.return (await db.getPayees()).map(payeeModel.fromDb);}diff --git a/packages/loot-core/src/server/preferences/app.ts b/packages/loot-core/src/server/preferences/app.tsindex a174da7e79..1cea369a2f 100644--- a/packages/loot-core/src/server/preferences/app.ts+++ b/packages/loot-core/src/server/preferences/app.ts@@ -47,10 +47,9 @@ async function saveSyncedPrefs({}async function getSyncedPrefs(): Promise<SyncedPrefs> {- const prefs = (await db.all('SELECT id, value FROM preferences')) as Array<{- id: string;- value: string;- }>;+ const prefs = await db.all<Pick<db.DbPreference, 'id' | 'value'>>(+ 'SELECT id, value FROM preferences',+ );return prefs.reduce<SyncedPrefs>((carry, { value, id }) => {carry[id as keyof SyncedPrefs] = value;diff --git a/packages/loot-core/src/server/sheet.ts b/packages/loot-core/src/server/sheet.tsindex 3259c99162..8a3b2efa75 100644--- a/packages/loot-core/src/server/sheet.ts+++ b/packages/loot-core/src/server/sheet.ts@@ -5,7 +5,12 @@ import { captureBreadcrumb } from '../platform/exceptions';import * as sqlite from '../platform/server/sqlite';import { sheetForMonth } from '../shared/months';-import { DbPreference } from './db';+import {+ DbPreference,+ DbReflectBudget,+ DbZeroBudget,+ DbZeroBudgetMonth,+} from './db';import * as Platform from './platform';import { Spreadsheet } from './spreadsheet/spreadsheet';import { resolveName } from './spreadsheet/util';@@ -205,7 +210,7 @@ export async function loadUserBudgets()) ?? {};const table = budgetType === 'report' ? 'reflect_budgets' : 'zero_budgets';- const budgets = await db.all(`+ const budgets = await db.all<DbReflectBudget | DbZeroBudget>(`SELECT * FROM ${table} bLEFT JOIN categories c ON c.id = b.categoryWHERE c.tombstone = 0@@ -229,7 +234,9 @@ export async function loadUserBudgets(// For zero-based budgets, load the buffered amountsif (budgetType !== 'report') {- const budgetMonths = await db.all('SELECT * FROM zero_budget_months');+ const budgetMonths = await db.all<DbZeroBudgetMonth>(+ 'SELECT * FROM zero_budget_months',+ );for (const budgetMonth of budgetMonths) {const sheetName = sheetForMonth(budgetMonth.id);sheet.set(`${sheetName}!buffered`, budgetMonth.buffered);diff --git a/packages/loot-core/src/server/sync/index.ts b/packages/loot-core/src/server/sync/index.tsindex 5888fb768a..8fa8976790 100644--- a/packages/loot-core/src/server/sync/index.ts+++ b/packages/loot-core/src/server/sync/index.ts@@ -744,11 +744,13 @@ async function _fullSync(if (rebuiltMerkle.trie.hash === res.merkle.hash) {// Rebuilding the merkle worked... but why?- const clocks = await db.all('SELECT * FROM messages_clock');+ const clocks = await db.all<db.DbClockMessage>(+ 'SELECT * FROM messages_clock',+ );if (clocks.length !== 1) {console.log('Bad number of clocks:', clocks.length);}- const hash = deserializeClock(clocks[0]).merkle.hash;+ const hash = deserializeClock(clocks[0].clock).merkle.hash;console.log('Merkle hash in db:', hash);}diff --git a/packages/loot-core/src/server/sync/migrate.test.ts b/packages/loot-core/src/server/sync/migrate.test.tsindex 7fec886fcd..bbdb7dd99e 100644--- a/packages/loot-core/src/server/sync/migrate.test.ts+++ b/packages/loot-core/src/server/sync/migrate.test.ts@@ -117,7 +117,10 @@ describe('sync migrations', () => {await sendMessages(msgs);await tracer.expect('applied');- const transactions = await db.all('SELECT * FROM transactions', []);+ const transactions = await db.all<db.DbTransaction>(+ 'SELECT * FROM transactions',+ [],+ );for (const trans of transactions) {const transMsgs = msgs.filter(msg => msg.row === trans.id)diff --git a/packages/loot-core/src/server/sync/sync.test.ts b/packages/loot-core/src/server/sync/sync.test.tsindex a04c57b491..459912ee99 100644--- a/packages/loot-core/src/server/sync/sync.test.ts+++ b/packages/loot-core/src/server/sync/sync.test.ts@@ -54,8 +54,12 @@ describe('Sync', () => {expect(getClock().timestamp.toString()).toEqual(timestamp.toString());expect(mockSyncServer.getClock().merkle).toEqual(getClock().merkle);- expect(await db.all('SELECT * FROM messages_crdt')).toMatchSnapshot();- expect(await db.all('SELECT * FROM messages_clock')).toMatchSnapshot();+ expect(+ await db.all<db.DbCrdtMessage>('SELECT * FROM messages_crdt'),+ ).toMatchSnapshot();+ expect(+ await db.all<db.DbClockMessage>('SELECT * FROM messages_clock'),+ ).toMatchSnapshot();});it('should resend old messages to the server', async () => {diff --git a/packages/loot-core/src/server/tools/app.ts b/packages/loot-core/src/server/tools/app.tsindex 1ca92e4f94..7b6736c078 100644--- a/packages/loot-core/src/server/tools/app.ts+++ b/packages/loot-core/src/server/tools/app.ts@@ -13,7 +13,11 @@ export const app = createApp<ToolsHandlers>();app.method('tools/fix-split-transactions', async () => {// 1. Check for child transactions that have a blank payee, and set// the payee to whatever the parent has- const blankPayeeRows = await db.all(`+ const blankPayeeRows = await db.all<+ db.DbViewTransactionInternal & {+ parentPayee: db.DbViewTransactionInternal['payee'];+ }+ >(`SELECT t.*, p.payee AS parentPayee FROM v_transactions_internal tLEFT JOIN v_transactions_internal p ON t.parent_id = p.idWHERE t.is_child = 1 AND t.payee IS NULL AND p.payee IS NOT NULL@@ -29,7 +33,10 @@ app.method('tools/fix-split-transactions', async () => {// 2. Make sure the "cleared" flag is synced up with the parent// transactions- const clearedRows = await db.all(`+ const clearedRows = await db.all<+ Pick<db.DbViewTransactionInternal, 'id'> &+ Pick<db.DbViewTransactionInternal, 'cleared'>+ >(`SELECT t.id, p.cleared FROM v_transactions_internal tLEFT JOIN v_transactions_internal p ON t.parent_id = p.idWHERE t.is_child = 1 AND t.cleared != p.cleared@@ -45,7 +52,7 @@ app.method('tools/fix-split-transactions', async () => {// 3. Mark the `tombstone` field as true on any child transactions// that have a dead parent- const deletedRows = await db.all(`+ const deletedRows = await db.all<db.DbViewTransactionInternal>(`SELECT t.* FROM v_transactions_internal tLEFT JOIN v_transactions_internal p ON t.parent_id = p.idWHERE t.is_child = 1 AND t.tombstone = 0 AND (p.tombstone = 1 OR p.id IS NULL)@@ -74,7 +81,9 @@ app.method('tools/fix-split-transactions', async () => {});// 5. Fix transfers that should not have categories- const brokenTransfers = await db.all(`+ const brokenTransfers = await db.all<+ Pick<db.DbViewTransactionInternal, 'id'>+ >(`SELECT t1.idFROM v_transactions_internal t1JOIN accounts a1 ON t1.account = a1.id@@ -93,7 +102,7 @@ app.method('tools/fix-split-transactions', async () => {});// 6. Remove transaction errors from non-parent transactions- const errorRows = await db.all(`+ const errorRows = await db.all<Pick<db.DbViewTransactionInternal, 'id'>>(`SELECT id FROM v_transactions_internal WHERE error IS NOT NULL AND is_parent = 0`);diff --git a/packages/loot-core/src/server/transactions/index.ts b/packages/loot-core/src/server/transactions/index.tsindex e306d74913..4d719f994f 100644--- a/packages/loot-core/src/server/transactions/index.ts+++ b/packages/loot-core/src/server/transactions/index.ts@@ -12,7 +12,7 @@ import * as transfer from './transfer';async function idsWithChildren(ids: string[]) {const whereIds = whereIn(ids, 'parent_id');- const rows = await db.all(+ const rows = await db.all<Pick<db.DbViewTransactionInternal, 'id'>>(`SELECT id FROM v_transactions_internal WHERE ${whereIds}`,);const set = new Set(ids);@@ -57,7 +57,9 @@ export async function batchUpdateTransactions({: [];const oldPayees = new Set<PayeeEntity['id']>();- const accounts = await db.all('SELECT * FROM accounts WHERE tombstone = 0');+ const accounts = await db.all<db.DbAccount>(+ 'SELECT * FROM accounts WHERE tombstone = 0',+ );// We need to get all the payees of updated transactions _before_// making changesdiff --git a/packages/loot-core/src/server/transactions/transaction-rules.test.ts b/packages/loot-core/src/server/transactions/transaction-rules.test.tsindex 13a0abf04a..53895eb062 100644--- a/packages/loot-core/src/server/transactions/transaction-rules.test.ts+++ b/packages/loot-core/src/server/transactions/transaction-rules.test.ts@@ -97,7 +97,7 @@ describe('Transaction rules', () => {conditions: [],actions: [],});- expect((await db.all('SELECT * FROM rules')).length).toBe(1);+ expect((await db.all<db.DbRule>('SELECT * FROM rules')).length).toBe(1);// Make sure it was projectedexpect(getRules().length).toBe(1);@@ -110,7 +110,7 @@ describe('Transaction rules', () => {{ op: 'set', field: 'category', value: 'food' },],});- expect((await db.all('SELECT * FROM rules')).length).toBe(2);+ expect((await db.all<db.DbRule>('SELECT * FROM rules')).length).toBe(2);expect(getRules().length).toBe(2);const spy = jest.spyOn(console, 'warn').mockImplementation();@@ -119,7 +119,7 @@ describe('Transaction rules', () => {// that will validate the input)await db.insertWithUUID('rules', { conditions: '{', actions: '}' });// It will be in the database- expect((await db.all('SELECT * FROM rules')).length).toBe(3);+ expect((await db.all<db.DbRule>('SELECT * FROM rules')).length).toBe(3);// But it will be ignoredexpect(getRules().length).toBe(2);diff --git a/packages/loot-core/src/server/transactions/transaction-rules.ts b/packages/loot-core/src/server/transactions/transaction-rules.tsindex b9cefd8a0a..71138df78a 100644--- a/packages/loot-core/src/server/transactions/transaction-rules.ts+++ b/packages/loot-core/src/server/transactions/transaction-rules.ts@@ -175,7 +175,7 @@ export function makeRule(data) {export async function loadRules() {resetState();- const rules = await db.all(`+ const rules = await db.all<db.DbRule>(`SELECT * FROM rulesWHERE conditions IS NOT NULL AND actions IS NOT NULL AND tombstone = 0`);@@ -800,7 +800,7 @@ export async function updateCategoryRules(transactions) {// Also look 180 days in the future to get any future transactions// (this might change when we think about scheduled transactions)- const register: TransactionEntity[] = await db.all(+ const register = await db.all<db.DbViewTransaction>(`SELECT t.* FROM v_transactions tLEFT JOIN accounts a ON a.id = t.accountLEFT JOIN payees p ON p.id = t.payeediff --git a/packages/loot-core/src/server/transactions/transfer.test.ts b/packages/loot-core/src/server/transactions/transfer.test.tsindex 81701d6673..85fe960dee 100644--- a/packages/loot-core/src/server/transactions/transfer.test.ts+++ b/packages/loot-core/src/server/transactions/transfer.test.ts@@ -7,7 +7,7 @@ import * as transfer from './transfer';beforeEach(global.emptyDatabase());function getAllTransactions() {- return db.all(+ return db.all<db.DbViewTransaction & { payee_name: db.DbPayee['name'] }>(`SELECT t.*, p.name as payee_nameFROM v_transactions tLEFT JOIN payees p ON p.id = t.payeediff --git a/upcoming-release-notes/4542.md b/upcoming-release-notes/4542.mdnew file mode 100644index 0000000000..9be8134fe2--- /dev/null+++ b/upcoming-release-notes/4542.md@@ -0,0 +1,6 @@+---+category: Maintenance+authors: [joel-jeremy]+---++[TypeScript] Make `db.all` generic to make it easy to type DB query