feat: implement database initialization in StorageService

This commit is contained in:
shuaiplus
2026-02-09 23:00:23 +08:00
parent 866ffb8390
commit 70a58aeb04
3 changed files with 143 additions and 2 deletions
+16 -1
View File
@@ -1,9 +1,24 @@
import { Env } from './types';
import { handleRequest } from './router';
import { StorageService } from './services/storage';
// Global flag to track if database has been initialized in this worker instance
let dbInitialized = false;
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
// Auto-initialize database on first request
if (!dbInitialized) {
try {
const storage = new StorageService(env.DB);
await storage.initializeDatabase();
dbInitialized = true;
} catch (error) {
console.error('Failed to initialize database:', error);
// Continue anyway - the error will surface when actual DB operations are attempted
}
}
return handleRequest(request, env);
return handleRequest(request, env);
},
};