@ -6,11 +6,11 @@ import { runMigrations } from "../db-sql/migration";
import type { DatabaseService , QueryExecResult } from "../interfaces/database" ;
import type { DatabaseService , QueryExecResult } from "../interfaces/database" ;
import { logger } from "@/utils/logger" ;
import { logger } from "@/utils/logger" ;
interface QueuedOperation < T = unknown > {
interface QueuedOperation {
type : "run" | "query" | "getOneRow" | "getAll" ;
type : "run" | "query" | "getOneRow" | "getAll" ;
sql : string ;
sql : string ;
params : unknown [ ] ;
params : unknown [ ] ;
resolve : ( value : T ) = > void ;
resolve : ( value : unknown ) = > void ;
reject : ( reason : unknown ) = > void ;
reject : ( reason : unknown ) = > void ;
}
}
@ -22,12 +22,12 @@ interface AbsurdSqlDatabase {
) = > Promise < { changes : number ; lastId? : number } > ;
) = > Promise < { changes : number ; lastId? : number } > ;
}
}
class AbsurdSqlDatabaseService < T > implements DatabaseService {
class AbsurdSqlDatabaseService implements DatabaseService {
private static instance : AbsurdSqlDatabaseService | null = null ;
private static instance : AbsurdSqlDatabaseService | null = null ;
private db : AbsurdSqlDatabase | null ;
private db : AbsurdSqlDatabase | null ;
private initialized : boolean ;
private initialized : boolean ;
private initializationPromise : Promise < void > | null = null ;
private initializationPromise : Promise < void > | null = null ;
private operationQueue : Array < QueuedOperation < T > > = [ ] ;
private operationQueue : Array < QueuedOperation > = [ ] ;
private isProcessingQueue : boolean = false ;
private isProcessingQueue : boolean = false ;
private constructor ( ) {
private constructor ( ) {
@ -123,9 +123,7 @@ class AbsurdSqlDatabaseService<T> implements DatabaseService {
if ( ! operation ) continue ;
if ( ! operation ) continue ;
try {
try {
let result ;
let result : unknown ;
let queryResult ;
let allResult ;
switch ( operation . type ) {
switch ( operation . type ) {
case "run" :
case "run" :
result = await this . db . run ( operation . sql , operation . params ) ;
result = await this . db . run ( operation . sql , operation . params ) ;
@ -134,11 +132,11 @@ class AbsurdSqlDatabaseService<T> implements DatabaseService {
result = await this . db . exec ( operation . sql , operation . params ) ;
result = await this . db . exec ( operation . sql , operation . params ) ;
break ;
break ;
case "getOneRow" :
case "getOneRow" :
queryResult = await this . db . exec ( operation . sql , operation . params ) ;
const queryResult = await this . db . exec ( operation . sql , operation . params ) ;
result = queryResult [ 0 ] ? . values [ 0 ] ;
result = queryResult [ 0 ] ? . values [ 0 ] ;
break ;
break ;
case "getAll" :
case "getAll" :
allResult = await this . db . exec ( operation . sql , operation . params ) ;
const allResult = await this . db . exec ( operation . sql , operation . params ) ;
result = allResult [ 0 ] ? . values || [ ] ;
result = allResult [ 0 ] ? . values || [ ] ;
break ;
break ;
}
}
@ -151,17 +149,17 @@ class AbsurdSqlDatabaseService<T> implements DatabaseService {
this . isProcessingQueue = false ;
this . isProcessingQueue = false ;
}
}
private async queueOperation < T > (
private async queueOperation < R > (
type : QueuedOperation < T > [ "type" ] ,
type : QueuedOperation [ "type" ] ,
sql : string ,
sql : string ,
params : unknown [ ] = [ ] ,
params : unknown [ ] = [ ] ,
) : Promise < T > {
) : Promise < R > {
return new Promise < T > ( ( resolve , reject ) = > {
return new Promise < R > ( ( resolve , reject ) = > {
const operation : QueuedOperation < T > = {
const operation : QueuedOperation = {
type ,
type ,
sql ,
sql ,
params ,
params ,
resolve ,
resolve : ( value : unknown ) = > resolve ( value as R ) ,
reject ,
reject ,
} ;
} ;
this . operationQueue . push ( operation ) ;
this . operationQueue . push ( operation ) ;