@@ -415,14 +415,29 @@ export class DBCacheManager {
415415 const result : Record < string , string [ ] > = { } ;
416416
417417 try {
418- for ( const app of apps ) {
419- const permissions = await this . getCachedPermissions (
420- app . appId ,
421- app . version ,
422- ) ;
423- if ( permissions ) {
424- result [ app . appId ] = permissions ;
425- }
418+ if ( apps . length === 0 ) return result ;
419+
420+ // Build a single query with placeholders for all (app_id, version) pairs
421+ const placeholders = apps
422+ . map ( ( ) => "(?, ?)" )
423+ . join ( ", " ) ;
424+
425+ const values = apps . flatMap ( ( app ) => [ app . appId , app . version ] ) ;
426+
427+ const rows = await this . db . select <
428+ Array < {
429+ app_id : string ;
430+ permissions : string ;
431+ outdated : number ;
432+ } >
433+ > (
434+ `SELECT app_id, permissions, outdated FROM app_permissions
435+ WHERE (app_id, version) IN (VALUES ${ placeholders } ) AND outdated = 0` ,
436+ values ,
437+ ) ;
438+
439+ for ( const row of rows ) {
440+ result [ row . app_id ] = JSON . parse ( row . permissions ) ;
426441 }
427442 } catch ( error ) {
428443 console . error ( "Error reading cached permissions batch:" , error ) ;
@@ -437,27 +452,18 @@ export class DBCacheManager {
437452 await this . initialize ( ) ;
438453 if ( ! this . db ) throw new Error ( "Database not initialized" ) ;
439454
440- try {
441- const entries = Object . entries ( permissionsMap ) ;
442- if ( entries . length === 0 ) return ;
443-
444- // Use explicit transaction for batch inserts (10-100x faster)
445- await this . db . execute ( "BEGIN TRANSACTION" ) ;
455+ const entries = Object . entries ( permissionsMap ) ;
456+ if ( entries . length === 0 ) return ;
446457
447- try {
448- for ( const [ appId , data ] of entries ) {
449- const permissionsStr = JSON . stringify ( data . permissions ) ;
450- await this . db . execute (
451- `INSERT OR REPLACE INTO app_permissions (app_id, version, permissions, outdated, cached_at)
452- VALUES (?, ?, ?, 0, CURRENT_TIMESTAMP)` ,
453- [ appId , data . version , permissionsStr ] ,
454- ) ;
455- }
456-
457- await this . db . execute ( "COMMIT" ) ;
458- } catch ( error ) {
459- await this . db . execute ( "ROLLBACK" ) ;
460- throw error ;
458+ try {
459+ // Insert each permission sequentially (no transaction to avoid locks)
460+ for ( const [ appId , data ] of entries ) {
461+ const permissionsStr = JSON . stringify ( data . permissions ) ;
462+ await this . db . execute (
463+ `INSERT OR REPLACE INTO app_permissions (app_id, version, permissions, outdated, cached_at)
464+ VALUES (?, ?, ?, 0, CURRENT_TIMESTAMP)` ,
465+ [ appId , data . version , permissionsStr ] ,
466+ ) ;
461467 }
462468 } catch ( error ) {
463469 console . error ( "Error caching permissions batch:" , error ) ;
0 commit comments