Files
crowd-funder-for-time-pwa/src/main.electron.ts
Matthew Raymer a5a9e15ece WIP: Refactor Electron SQLite initialization and database path handling
- Add logic in main process to resolve and create the correct database directory and file path using Electron's app, path, and fs modules
- Pass absolute dbPath to CapacitorSQLite plugin for reliable database creation
- Add extensive logging for debugging database location, permissions, and initialization
- Remove redundant open() call after createConnection in Electron platform service
- Add IPC handlers for essential SQLite operations (echo, createConnection, execute, query, closeConnection, isAvailable)
- Improve error handling and logging throughout initialization and IPC
- Still investigating database file creation and permissions issues
2025-05-30 08:16:31 +00:00

67 lines
2.2 KiB
TypeScript

import { initializeApp } from "./main.common";
import { logger } from "./utils/logger";
async function initializeSQLite() {
try {
// Wait for SQLite to be available in the main process
let retries = 0;
const maxRetries = 5;
const retryDelay = 1000; // 1 second
while (retries < maxRetries) {
try {
const isAvailable = await window.CapacitorSQLite.isAvailable();
if (isAvailable) {
logger.info("[Electron] SQLite plugin bridge initialized successfully");
return true;
}
} catch (error) {
logger.warn(`[Electron] SQLite not available yet (attempt ${retries + 1}/${maxRetries}):`, error);
}
retries++;
if (retries < maxRetries) {
await new Promise(resolve => setTimeout(resolve, retryDelay));
}
}
throw new Error("SQLite plugin not available after maximum retries");
} catch (error) {
logger.error("[Electron] Failed to initialize SQLite plugin bridge:", error);
throw error;
}
}
const platform = process.env.VITE_PLATFORM;
const pwa_enabled = process.env.VITE_PWA_ENABLED === "true";
logger.info("[Electron] Initializing app");
logger.info("[Electron] Platform:", { platform });
logger.info("[Electron] PWA enabled:", { pwa_enabled });
if (pwa_enabled) {
logger.warn("[Electron] PWA is enabled, but not supported in electron");
}
// Initialize app and SQLite
const app = initializeApp();
// Initialize SQLite first, then mount the app
initializeSQLite()
.then(() => {
logger.info("[Electron] SQLite initialized, mounting app...");
app.mount("#app");
})
.catch(error => {
logger.error("[Electron] Failed to initialize app:", error);
// Show error to user
const errorDiv = document.createElement("div");
errorDiv.style.cssText = "position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); background: #ffebee; color: #c62828; padding: 20px; border-radius: 4px; text-align: center; max-width: 80%;";
errorDiv.innerHTML = `
<h2>Failed to Initialize Database</h2>
<p>There was an error initializing the database. Please try restarting the application.</p>
<p>Error details: ${error.message}</p>
`;
document.body.appendChild(errorDiv);
});