diff --git a/src/interfaces/database.ts b/src/interfaces/database.ts index 7f491e6b..eeedb428 100644 --- a/src/interfaces/database.ts +++ b/src/interfaces/database.ts @@ -5,11 +5,23 @@ export interface QueryExecResult { values: Array>; } +export interface DatabaseExecResult { + changes: number; + lastId?: number; +} + export interface DatabaseService { initialize(): Promise; query(sql: string, params?: unknown[]): Promise; - run( - sql: string, - params?: unknown[], - ): Promise<{ changes: number; lastId?: number }>; + run(sql: string, params?: unknown[]): Promise; } + +/** + * Generic database result type for mapped query results + */ +export type DatabaseResult> = T; + +/** + * Database query result that can be either a single result or an array + */ +export type DatabaseQueryResult> = T | T[] | null; diff --git a/src/services/PlatformService.ts b/src/services/PlatformService.ts index 2eecf949..f8b65853 100644 --- a/src/services/PlatformService.ts +++ b/src/services/PlatformService.ts @@ -1,4 +1,4 @@ -import { QueryExecResult } from "@/interfaces/database"; +import { QueryExecResult, DatabaseExecResult } from "@/interfaces/database"; /** * Represents the result of an image capture or selection operation. @@ -145,10 +145,7 @@ export interface PlatformService { * @param params - The parameters to pass to the statement * @returns Promise resolving to the result of the statement */ - dbExec( - sql: string, - params?: unknown[], - ): Promise<{ changes: number; lastId?: number }>; + dbExec(sql: string, params?: unknown[]): Promise; /** * Executes a SQL query and returns the first row as an array. diff --git a/src/utils/PlatformServiceMixin.ts b/src/utils/PlatformServiceMixin.ts index c074a2da..2f8422c6 100644 --- a/src/utils/PlatformServiceMixin.ts +++ b/src/utils/PlatformServiceMixin.ts @@ -40,6 +40,8 @@ import { mapColumnsToValues, parseJsonField } from "@/db/databaseUtil"; import { MASTER_SETTINGS_KEY, type Settings } from "@/db/tables/settings"; import * as databaseUtil from "@/db/databaseUtil"; import { logger } from "@/utils/logger"; +import { Contact } from "@/db/tables/contacts"; +import { QueryExecResult, DatabaseExecResult } from "@/interfaces/database"; // ================================================= // CACHING INFRASTRUCTURE @@ -385,7 +387,10 @@ export const PlatformServiceMixin = { * @param sql SQL query string * @param params Query parameters */ - async $db(sql: string, params: unknown[] = []): Promise { + async $db( + sql: string, + params: unknown[] = [], + ): Promise { return await (this as any).platformService.dbQuery(sql, params); }, @@ -394,7 +399,10 @@ export const PlatformServiceMixin = { * @param sql SQL statement string * @param params Statement parameters */ - async $exec(sql: string, params: unknown[] = []): Promise { + async $exec( + sql: string, + params: unknown[] = [], + ): Promise { return await (this as any).platformService.dbExec(sql, params); }, @@ -403,7 +411,10 @@ export const PlatformServiceMixin = { * @param sql SQL query string * @param params Query parameters */ - async $one(sql: string, params: unknown[] = []): Promise { + async $one( + sql: string, + params: unknown[] = [], + ): Promise { return await (this as any).platformService.dbGetOneRow(sql, params); }, @@ -418,12 +429,16 @@ export const PlatformServiceMixin = { * @param params Query parameters * @returns Mapped array of results */ - async $query(sql: string, params: unknown[] = []): Promise { + async $query>( + sql: string, + params: unknown[] = [], + ): Promise { const result = await (this as any).platformService.dbQuery(sql, params); if (!result?.columns || !result?.values) { return []; } - return mapColumnsToValues(result.columns, result.values) || []; + const mappedResults = mapColumnsToValues(result.columns, result.values); + return mappedResults as T[]; }, /** @@ -432,9 +447,12 @@ export const PlatformServiceMixin = { * @param params Query parameters * @returns First mapped result or null */ - async $first(sql: string, params: unknown[] = []): Promise { + async $first>( + sql: string, + params: unknown[] = [], + ): Promise { const results = await (this as any).$query(sql, params); - return results.length > 0 ? results[0] : null; + return results.length > 0 ? (results[0] as T) : null; }, // ================================================= @@ -446,9 +464,9 @@ export const PlatformServiceMixin = { * Ultra-concise shortcut with 60s TTL for performance * @returns Cached mapped array of all contacts */ - async $contacts(): Promise { + async $contacts(): Promise { const cacheKey = "contacts_all"; - const cached = this._getCached(cacheKey); + const cached = this._getCached(cacheKey); if (cached) { return cached; } @@ -456,7 +474,11 @@ export const PlatformServiceMixin = { const contacts = await this.$query( "SELECT * FROM contacts ORDER BY name", ); - return this._setCached(cacheKey, contacts, CACHE_DEFAULTS.contacts); + return this._setCached( + cacheKey, + contacts as Contact[], + CACHE_DEFAULTS.contacts, + ); }, /** @@ -656,13 +678,19 @@ declare module "@vue/runtime-core" { capabilities: any; // Ultra-concise database methods (shortest possible names) - $db(sql: string, params?: unknown[]): Promise; - $exec(sql: string, params?: unknown[]): Promise; - $one(sql: string, params?: unknown[]): Promise; + $db(sql: string, params?: unknown[]): Promise; + $exec(sql: string, params?: unknown[]): Promise; + $one(sql: string, params?: unknown[]): Promise; // Query + mapping combo methods - $query(sql: string, params?: unknown[]): Promise; - $first(sql: string, params?: unknown[]): Promise; + $query>( + sql: string, + params?: unknown[], + ): Promise; + $first>( + sql: string, + params?: unknown[], + ): Promise; // Enhanced utility methods $dbQuery(sql: string, params?: unknown[]): Promise; @@ -680,7 +708,7 @@ declare module "@vue/runtime-core" { $withTransaction(fn: () => Promise): Promise; // Cached specialized shortcuts (massive performance boost) - $contacts(): Promise; + $contacts(): Promise; $settings(defaults?: Settings): Promise; $accountSettings(did?: string, defaults?: Settings): Promise;