POWR/lib/db/db-service.ts

65 lines
1.5 KiB
TypeScript
Raw Normal View History

2025-02-16 22:47:47 -05:00
// lib/db/db-service.ts
import { SQLiteDatabase } from 'expo-sqlite';
import { createLogger } from '@/lib/utils/logger';
// Create database-specific logger
const logger = createLogger('SQLite');
2025-02-16 22:47:47 -05:00
export class DbService {
private db: SQLiteDatabase;
constructor(db: SQLiteDatabase) {
this.db = db;
}
async execAsync(sql: string): Promise<void> {
try {
logger.debug('Executing SQL:', sql);
2025-02-16 22:47:47 -05:00
await this.db.execAsync(sql);
} catch (error) {
logger.error('SQL Error:', error);
2025-02-16 22:47:47 -05:00
throw error;
}
}
async runAsync(sql: string, params: any[] = []) {
try {
logger.debug('Running SQL:', sql);
logger.debug('Parameters:', params);
2025-02-16 22:47:47 -05:00
return await this.db.runAsync(sql, params);
} catch (error) {
logger.error('SQL Error:', error);
2025-02-16 22:47:47 -05:00
throw error;
}
}
async getFirstAsync<T>(sql: string, params: any[] = []): Promise<T | null> {
try {
return await this.db.getFirstAsync<T>(sql, params);
} catch (error) {
logger.error('SQL Error:', error);
2025-02-16 22:47:47 -05:00
throw error;
}
}
async getAllAsync<T>(sql: string, params: any[] = []): Promise<T[]> {
try {
return await this.db.getAllAsync<T>(sql, params);
} catch (error) {
logger.error('SQL Error:', error);
2025-02-16 22:47:47 -05:00
throw error;
}
}
async withTransactionAsync(action: () => Promise<void>): Promise<void> {
try {
await this.db.withTransactionAsync(async () => {
await action();
});
} catch (error) {
logger.error('Transaction Error:', error);
2025-02-16 22:47:47 -05:00
throw error;
}
}
}