@ -87,9 +87,79 @@ const DEFAULT_SETTINGS: Settings = {
// Event handler to initialize the non-sensitive database with default settings
// Event handler to initialize the non-sensitive database with default settings
db . on ( "populate" , async ( ) = > {
db . on ( "populate" , async ( ) = > {
await db . settings . add ( DEFAULT_SETTINGS ) ;
try {
await db . settings . add ( DEFAULT_SETTINGS ) ;
} catch ( error ) {
console . error ( "Error populating the database with default settings:" , error ) ;
}
} ) ;
} ) ;
// Helper function to safely open the database with retries
async function safeOpenDatabase ( retries = 1 , delay = 500 ) : Promise < void > {
// console.log("Starting safeOpenDatabase with retries:", retries);
for ( let i = 0 ; i < retries ; i ++ ) {
try {
// console.log(`Attempt ${i + 1}: Checking if database is open...`);
if ( ! db . isOpen ( ) ) {
// console.log(`Attempt ${i + 1}: Database is closed, attempting to open...`);
// Create a promise that rejects after 5 seconds
const timeoutPromise = new Promise ( ( _ , reject ) = > {
setTimeout ( ( ) = > reject ( new Error ( 'Database open timed out' ) ) , 500 ) ;
} ) ;
// Race between the open operation and the timeout
const openPromise = db . open ( ) ;
// console.log(`Attempt ${i + 1}: Waiting for db.open() promise...`);
await Promise . race ( [ openPromise , timeoutPromise ] ) ;
// If we get here, the open succeeded
// console.log(`Attempt ${i + 1}: Database opened successfully`);
return ;
}
// console.log(`Attempt ${i + 1}: Database was already open`);
return ;
} catch ( error ) {
console . error ( ` Attempt ${ i + 1 } : Database open failed: ` , error ) ;
if ( i < retries - 1 ) {
console . log ( ` Attempt ${ i + 1 } : Waiting ${ delay } ms before retry... ` ) ;
await new Promise ( resolve = > setTimeout ( resolve , delay ) ) ;
} else {
throw error ;
}
}
}
}
export async function updateDefaultSettings (
settingsChanges : Settings ,
) : Promise < number > {
delete settingsChanges . accountDid ; // just in case
// ensure there is no "id" that would override the key
delete settingsChanges . id ;
try {
try {
// console.log("Database state before open:", db.isOpen() ? "open" : "closed");
// console.log("Database name:", db.name);
// console.log("Database version:", db.verno);
await safeOpenDatabase ( ) ;
} catch ( openError : unknown ) {
console . error ( "Failed to open database:" , openError ) ;
const errorMessage = openError instanceof Error ? openError.message : String ( openError ) ;
throw new Error ( ` Database connection failed: ${ errorMessage } . Please try again or restart the app. ` ) ;
}
const result = await db . settings . update ( MASTER_SETTINGS_KEY , settingsChanges ) ;
return result ;
} catch ( error ) {
console . error ( "Error updating default settings:" , error ) ;
if ( error instanceof Error ) {
throw error ; // Re-throw if it's already an Error with a message
} else {
throw new Error ( ` Failed to update settings: ${ error } ` ) ;
}
}
}
// Manage the encryption key.
// Manage the encryption key.
// It's not really secure to maintain the secret next to the user's data.
// It's not really secure to maintain the secret next to the user's data.
@ -183,15 +253,6 @@ export async function retrieveSettingsForActiveAccount(): Promise<Settings> {
}
}
}
}
export async function updateDefaultSettings (
settingsChanges : Settings ,
) : Promise < void > {
delete settingsChanges . accountDid ; // just in case
// ensure there is no "id" that would override the key
delete settingsChanges . id ;
await db . settings . update ( MASTER_SETTINGS_KEY , settingsChanges ) ;
}
export async function updateAccountSettings (
export async function updateAccountSettings (
accountDid : string ,
accountDid : string ,
settingsChanges : Settings ,
settingsChanges : Settings ,