@ -17,6 +17,8 @@
* - Ultra - concise database interaction methods
* - Smart caching layer with TTL for performance optimization
* - Settings shortcuts for ultra - frequent update patterns
* - High - level entity operations ( insertContact , updateContact , etc . )
* - Result mapping helpers to eliminate verbose row processing
*
* Benefits :
* - Eliminates repeated PlatformServiceFactory . getInstance ( ) calls
@ -28,10 +30,13 @@
* - Ultra - concise method names for frequent operations
* - Automatic caching for settings and contacts ( massive performance gain )
* - Settings update shortcuts reduce 90 % of update boilerplate
* - Entity operations eliminate verbose SQL INSERT / UPDATE patterns
* - Result mapping helpers reduce row processing boilerplate by 75 %
*
* @author Matthew Raymer
* @version 4.0 . 0
* @version 4.1 . 0
* @since 2025 - 07 - 02
* @updated 2025 - 01 - 25 - Added high - level entity operations for code reduction
* /
import { PlatformServiceFactory } from "@/services/PlatformServiceFactory" ;
@ -645,6 +650,216 @@ export const PlatformServiceMixin = {
$clearAllCaches ( ) : void {
this . _clearCache ( ) ;
} ,
// =================================================
// HIGH-LEVEL ENTITY OPERATIONS (eliminate verbose SQL patterns)
// =================================================
/ * *
* Map SQL query results to typed objects - $mapResults ( )
* Eliminates verbose row mapping patterns
* @param results SQL query results
* @param mapper Function to map each row to an object
* @returns Array of mapped objects
* /
$mapResults < T > (
results : QueryExecResult | undefined ,
mapper : ( row : unknown [ ] ) = > T ,
) : T [ ] {
if ( ! results ? . values ) return [ ] ;
return results . values . map ( mapper ) ;
} ,
/ * *
* Insert or replace contact - $insertContact ( )
* Eliminates verbose INSERT OR REPLACE patterns
* @param contact Contact object to insert
* @returns Promise < boolean > Success status
* /
async $insertContact ( contact : Partial < Contact > ) : Promise < boolean > {
try {
// Convert undefined values to null for SQL.js compatibility
const safeContact = {
did : contact.did !== undefined ? contact.did : null ,
name : contact.name !== undefined ? contact.name : null ,
publicKeyBase64 :
contact . publicKeyBase64 !== undefined
? contact . publicKeyBase64
: null ,
seesMe : contact.seesMe !== undefined ? contact.seesMe : null ,
registered :
contact . registered !== undefined ? contact.registered : null ,
nextPubKeyHashB64 :
contact . nextPubKeyHashB64 !== undefined
? contact . nextPubKeyHashB64
: null ,
profileImageUrl :
contact . profileImageUrl !== undefined
? contact . profileImageUrl
: null ,
} ;
await this . $dbExec (
` INSERT OR REPLACE INTO contacts
( did , name , publicKeyBase64 , seesMe , registered , nextPubKeyHashB64 , profileImageUrl )
VALUES ( ? , ? , ? , ? , ? , ? , ? ) ` ,
[
safeContact . did ,
safeContact . name ,
safeContact . publicKeyBase64 ,
safeContact . seesMe ,
safeContact . registered ,
safeContact . nextPubKeyHashB64 ,
safeContact . profileImageUrl ,
] ,
) ;
// Invalidate contacts cache
this . _invalidateCache ( "contacts_all" ) ;
return true ;
} catch ( error ) {
logger . error ( "[PlatformServiceMixin] Error inserting contact:" , error ) ;
return false ;
}
} ,
/ * *
* Update contact - $updateContact ( )
* Eliminates verbose UPDATE patterns
* @param did Contact DID to update
* @param changes Partial contact changes
* @returns Promise < boolean > Success status
* /
async $updateContact (
did : string ,
changes : Partial < Contact > ,
) : Promise < boolean > {
try {
const setParts : string [ ] = [ ] ;
const params : unknown [ ] = [ ] ;
Object . entries ( changes ) . forEach ( ( [ key , value ] ) = > {
if ( value !== undefined ) {
setParts . push ( ` ${ key } = ? ` ) ;
params . push ( value ) ;
}
} ) ;
if ( setParts . length === 0 ) return true ;
params . push ( did ) ;
await this . $dbExec (
` UPDATE contacts SET ${ setParts . join ( ", " ) } WHERE did = ? ` ,
params ,
) ;
// Invalidate contacts cache
this . _invalidateCache ( "contacts_all" ) ;
return true ;
} catch ( error ) {
logger . error ( "[PlatformServiceMixin] Error updating contact:" , error ) ;
return false ;
}
} ,
/ * *
* Get all contacts as typed objects - $getAllContacts ( )
* Eliminates verbose query + mapping patterns
* @returns Promise < Contact [ ] > Array of contact objects
* /
async $getAllContacts ( ) : Promise < Contact [ ] > {
const results = await this . $dbQuery (
"SELECT did, name, publicKeyBase64, seesMe, registered, nextPubKeyHashB64, profileImageUrl FROM contacts ORDER BY name" ,
) ;
return this . $mapResults ( results , ( row : unknown [ ] ) = > ( {
did : row [ 0 ] as string ,
name : row [ 1 ] as string ,
publicKeyBase64 : row [ 2 ] as string ,
seesMe : Boolean ( row [ 3 ] ) ,
registered : Boolean ( row [ 4 ] ) ,
nextPubKeyHashB64 : row [ 5 ] as string ,
profileImageUrl : row [ 6 ] as string ,
} ) ) ;
} ,
/ * *
* Generic entity insertion - $insertEntity ( )
* Eliminates verbose INSERT patterns for any entity
* @param tableName Database table name
* @param entity Entity object to insert
* @param fields Array of field names to insert
* @returns Promise < boolean > Success status
* /
async $insertEntity (
tableName : string ,
entity : Record < string , unknown > ,
fields : string [ ] ,
) : Promise < boolean > {
try {
const placeholders = fields . map ( ( ) = > "?" ) . join ( ", " ) ;
// Convert undefined values to null for SQL.js compatibility
const values = fields . map ( ( field ) = >
entity [ field ] !== undefined ? entity [ field ] : null ,
) ;
await this . $dbExec (
` INSERT OR REPLACE INTO ${ tableName } ( ${ fields . join ( ", " ) } ) VALUES ( ${ placeholders } ) ` ,
values ,
) ;
return true ;
} catch ( error ) {
logger . error (
` [PlatformServiceMixin] Error inserting entity into ${ tableName } : ` ,
error ,
) ;
return false ;
}
} ,
/ * *
* Update settings with direct SQL - $updateSettings ( )
* Eliminates verbose settings update patterns
* @param changes Settings changes to apply
* @param did Optional DID for user - specific settings
* @returns Promise < boolean > Success status
* /
async $updateSettings (
changes : Partial < Settings > ,
did? : string ,
) : Promise < boolean > {
try {
// Use databaseUtil methods which handle the correct schema
if ( did ) {
return await databaseUtil . updateDidSpecificSettings ( did , changes ) ;
} else {
return await databaseUtil . updateDefaultSettings ( changes ) ;
}
} catch ( error ) {
logger . error ( "[PlatformServiceMixin] Error updating settings:" , error ) ;
return false ;
}
} ,
/ * *
* Get settings row as array - $getSettingsRow ( )
* Eliminates verbose settings retrieval patterns
* @param fields Array of field names to retrieve
* @param did Optional DID for user - specific settings
* @returns Promise < unknown [ ] > Settings row as array
* /
async $getSettingsRow (
fields : string [ ] ,
did? : string ,
) : Promise < unknown [ ] | undefined > {
// Use correct settings table schema
const whereClause = did ? "WHERE accountDid = ?" : "WHERE id = ?" ;
const params = did ? [ did ] : [ MASTER_SETTINGS_KEY ] ;
return await this . $one (
` SELECT ${ fields . join ( ", " ) } FROM settings ${ whereClause } ` ,
params ,
) ;
} ,
} ,
} ;
@ -677,6 +892,25 @@ export interface IPlatformServiceMixin {
isWeb : boolean ;
isElectron : boolean ;
capabilities : PlatformCapabilities ;
// High-level entity operations
$mapResults < T > (
results : QueryExecResult | undefined ,
mapper : ( row : unknown [ ] ) = > T ,
) : T [ ] ;
$insertContact ( contact : Partial < Contact > ) : Promise < boolean > ;
$updateContact ( did : string , changes : Partial < Contact > ) : Promise < boolean > ;
$getAllContacts ( ) : Promise < Contact [ ] > ;
$insertEntity (
tableName : string ,
entity : Record < string , unknown > ,
fields : string [ ] ,
) : Promise < boolean > ;
$updateSettings ( changes : Partial < Settings > , did? : string ) : Promise < boolean > ;
$getSettingsRow (
fields : string [ ] ,
did? : string ,
) : Promise < unknown [ ] | undefined > ;
}
// TypeScript declaration merging to eliminate (this as any) type assertions
@ -742,5 +976,24 @@ declare module "@vue/runtime-core" {
$refreshSettings ( ) : Promise < Settings > ;
$refreshContacts ( ) : Promise < Contact [ ] > ;
$clearAllCaches ( ) : void ;
// High-level entity operations (eliminate verbose SQL patterns)
$mapResults < T > (
results : QueryExecResult | undefined ,
mapper : ( row : unknown [ ] ) = > T ,
) : T [ ] ;
$insertContact ( contact : Partial < Contact > ) : Promise < boolean > ;
$updateContact ( did : string , changes : Partial < Contact > ) : Promise < boolean > ;
$getAllContacts ( ) : Promise < Contact [ ] > ;
$insertEntity (
tableName : string ,
entity : Record < string , unknown > ,
fields : string [ ] ,
) : Promise < boolean > ;
$updateSettings ( changes : Partial < Settings > , did? : string ) : Promise < boolean > ;
$getSettingsRow (
fields : string [ ] ,
did? : string ,
) : Promise < unknown [ ] | undefined > ;
}
}