forked from trent_larson/crowd-funder-for-time-pwa
fix: resolve TypeScript errors in database service implementation
- Remove unnecessary generic type parameter from AbsurdSqlDatabaseService - Fix type handling in operation queue and result processing - Correct WebPlatformService dbGetOneRow implementation to use imported databaseService - Add proper type annotations for database operation results This commit improves type safety and fixes several TypeScript errors that were preventing proper type checking in the database service layer.
This commit is contained in:
@@ -6,11 +6,11 @@ import { runMigrations } from "../db-sql/migration";
|
||||
import type { DatabaseService, QueryExecResult } from "../interfaces/database";
|
||||
import { logger } from "@/utils/logger";
|
||||
|
||||
interface QueuedOperation<T = unknown> {
|
||||
interface QueuedOperation {
|
||||
type: "run" | "query" | "getOneRow" | "getAll";
|
||||
sql: string;
|
||||
params: unknown[];
|
||||
resolve: (value: T) => void;
|
||||
resolve: (value: unknown) => void;
|
||||
reject: (reason: unknown) => void;
|
||||
}
|
||||
|
||||
@@ -22,12 +22,12 @@ interface AbsurdSqlDatabase {
|
||||
) => Promise<{ changes: number; lastId?: number }>;
|
||||
}
|
||||
|
||||
class AbsurdSqlDatabaseService<T> implements DatabaseService {
|
||||
class AbsurdSqlDatabaseService implements DatabaseService {
|
||||
private static instance: AbsurdSqlDatabaseService | null = null;
|
||||
private db: AbsurdSqlDatabase | null;
|
||||
private initialized: boolean;
|
||||
private initializationPromise: Promise<void> | null = null;
|
||||
private operationQueue: Array<QueuedOperation<T>> = [];
|
||||
private operationQueue: Array<QueuedOperation> = [];
|
||||
private isProcessingQueue: boolean = false;
|
||||
|
||||
private constructor() {
|
||||
@@ -123,9 +123,7 @@ class AbsurdSqlDatabaseService<T> implements DatabaseService {
|
||||
if (!operation) continue;
|
||||
|
||||
try {
|
||||
let result;
|
||||
let queryResult;
|
||||
let allResult;
|
||||
let result: unknown;
|
||||
switch (operation.type) {
|
||||
case "run":
|
||||
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);
|
||||
break;
|
||||
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];
|
||||
break;
|
||||
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 || [];
|
||||
break;
|
||||
}
|
||||
@@ -151,17 +149,17 @@ class AbsurdSqlDatabaseService<T> implements DatabaseService {
|
||||
this.isProcessingQueue = false;
|
||||
}
|
||||
|
||||
private async queueOperation<T>(
|
||||
type: QueuedOperation<T>["type"],
|
||||
private async queueOperation<R>(
|
||||
type: QueuedOperation["type"],
|
||||
sql: string,
|
||||
params: unknown[] = [],
|
||||
): Promise<T> {
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
const operation: QueuedOperation<T> = {
|
||||
): Promise<R> {
|
||||
return new Promise<R>((resolve, reject) => {
|
||||
const operation: QueuedOperation = {
|
||||
type,
|
||||
sql,
|
||||
params,
|
||||
resolve,
|
||||
resolve: (value: unknown) => resolve(value as R),
|
||||
reject,
|
||||
};
|
||||
this.operationQueue.push(operation);
|
||||
|
||||
@@ -381,4 +381,8 @@ export class WebPlatformService implements PlatformService {
|
||||
): Promise<{ changes: number; lastId?: number }> {
|
||||
return databaseService.run(sql, params);
|
||||
}
|
||||
|
||||
async dbGetOneRow(sql: string, params?: unknown[]): Promise<unknown[] | undefined> {
|
||||
return databaseService.query(sql, params).then((result: QueryExecResult[]) => result[0]?.values[0]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user