| 
						
						
							
								
							
						
						
					 | 
				
				 | 
				
					@ -8,10 +8,10 @@ import { SQLiteFS } from "absurd-sql"; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					import IndexedDBBackend from "absurd-sql/dist/indexeddb-backend"; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					import { runMigrations } from "../db-sql/migration"; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					import type { QueryExecResult } from "../interfaces/database"; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					import type { DatabaseService, QueryExecResult } from "../interfaces/database"; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					import { logger } from "@/utils/logger"; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					interface SQLDatabase { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					interface AbsurdSqlDatabase { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					  exec: (sql: string, params?: unknown[]) => Promise<QueryExecResult[]>; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					  run: ( | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    sql: string, | 
				
			
			
		
	
	
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
				
				 | 
				
					@ -19,9 +19,9 @@ interface SQLDatabase { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					  ) => Promise<{ changes: number; lastId?: number }>; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					} | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					class DatabaseService { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					  private static instance: DatabaseService | null = null; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					  private db: SQLDatabase | null; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					class AbsurdSqlDatabaseService implements DatabaseService { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					  private static instance: AbsurdSqlDatabaseService | null = null; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					  private db: AbsurdSqlDatabase | null; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					  private initialized: boolean; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					  private initializationPromise: Promise<void> | null = null; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
	
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
				
				 | 
				
					@ -30,11 +30,11 @@ class DatabaseService { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    this.initialized = false; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					  } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					  static getInstance(): DatabaseService { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    if (!DatabaseService.instance) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      DatabaseService.instance = new DatabaseService(); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					  static getInstance(): AbsurdSqlDatabaseService { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    if (!AbsurdSqlDatabaseService.instance) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      AbsurdSqlDatabaseService.instance = new AbsurdSqlDatabaseService(); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    return DatabaseService.instance; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    return AbsurdSqlDatabaseService.instance; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					  } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					  async initialize(): Promise<void> { | 
				
			
			
		
	
	
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
				
				 | 
				
					@ -53,7 +53,7 @@ class DatabaseService { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    try { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      await this.initializationPromise; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    } catch (error) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      logger.error(`DatabaseService initialize method failed:`, error); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      logger.error(`AbsurdSqlDatabaseService initialize method failed:`, error); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      this.initializationPromise = null; // Reset on failure
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      throw error; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    } | 
				
			
			
		
	
	
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
				
				 | 
				
					@ -79,7 +79,7 @@ class DatabaseService { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    SQL.FS.mkdir("/sql"); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    SQL.FS.mount(sqlFS, {}, "/sql"); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    const path = "/sql/db.sqlite"; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    const path = "/sql/timesafari.sqlite"; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    if (typeof SharedArrayBuffer === "undefined") { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      const stream = SQL.FS.open(path, "a+"); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      await stream.node.contents.readIfFallback(); | 
				
			
			
		
	
	
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
				
				 | 
				
					@ -93,6 +93,7 @@ class DatabaseService { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      ); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    // An error is thrown without this pragma: "File has invalid page size. (the first block of a new file must be written first)"
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    await this.db.exec(`PRAGMA journal_mode=MEMORY;`); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    const sqlExec = this.db.exec.bind(this.db); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
	
		
			
				
					| 
						
							
								
							
						
						
							
								
							
						
						
					 | 
				
				 | 
				
					@ -150,7 +151,7 @@ class DatabaseService { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    return result[0]?.values[0]; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					  } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					  async all(sql: string, params: unknown[] = []): Promise<unknown[][]> { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					  async getAll(sql: string, params: unknown[] = []): Promise<unknown[][]> { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    await this.waitForInitialization(); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    const result = await this.db!.exec(sql, params); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    return result[0]?.values || []; | 
				
			
			
		
	
	
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
				
				 | 
				
					@ -158,6 +159,6 @@ class DatabaseService { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					} | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					// Create a singleton instance
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					const databaseService = DatabaseService.getInstance(); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					const databaseService = AbsurdSqlDatabaseService.getInstance(); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					export default databaseService; |