@afriapps/fs-client
Version:
React Native FileServer Client SDK for file synchronization and data management. Requires React Native environment with SQLite and File System support.
175 lines (155 loc) • 4.67 kB
text/typescript
import SQLite, {SQLiteDatabase} from 'react-native-sqlite-storage';
import {logger} from '../utils/logger';
import {createTablesSQL} from './DatabaseCommon';
import {IDatabase} from './IDatabase';
export class ReactNativeDatabase implements IDatabase<SQLiteDatabase> {
private dbName: string;
private db?: SQLiteDatabase;
constructor(dbName: string) {
this.dbName = dbName;
SQLite.enablePromise(true);
}
public getSQLiteDatabase(): SQLiteDatabase {
if (!this.db) {
throw new Error('Database not initialized');
}
return this.db;
}
public async initializeDB(force = false): Promise<void> {
try {
this.db = await SQLite.openDatabase({name: this.dbName, location: 'default'});
logger.debug('✅ Database opened.');
if (force) {
logger.debug('Force re-initialization requested. Dropping existing database...');
try {
await this.db.executeSql(`DROP DATABASE IF EXISTS ${this.dbName}`, []);
await this.createTables();
} catch (error) {
logger.error('Error dropping and recreating database:', error);
throw error;
}
}
logger.debug('Creating tables...');
await this.createTables();
logger.debug('Tables created or already exists.');
} catch (err) {
logger.error('Error opening database:', err);
throw err;
}
}
private async createTables(): Promise<void> {
if (!this.db) {
throw new Error('Database not initialized');
}
for (const sql of createTablesSQL) {
this.db.transaction(tx => {
tx.executeSql(sql, []);
});
}
}
public async query(sql: string, params: any[] = []): Promise<any> {
if (!this.db) {
throw new Error('Database not initialized');
}
return new Promise((resolve, reject) => {
this.db!.transaction((tx: any) => {
tx.executeSql(
sql,
params,
(_: any, results: any) => resolve(results.rows.raw()),
(_: any, error: any) => {
logger.error('Query error:', error);
reject(error);
return false;
},
);
});
});
}
public async close(): Promise<void> {
if (this.db) {
await this.db.close();
logger.debug('Database closed');
} else {
logger.debug('Database was not open');
}
}
public async all(sql: string, ...params: any[]): Promise<any[]> {
if (!this.db) {
throw new Error('Database not initialized');
}
return new Promise((resolve, reject) => {
this.db?.transaction((tx: any) => {
tx.executeSql(
sql,
params,
(_: any, results: any) => {
resolve(results.rows.raw());
},
(_: any, error: any) => {
logger.error('Query error:', error);
reject(error);
return false;
},
);
});
});
}
public async get(sql: string, ...params: any[]): Promise<any> {
if (!this.db) {
throw new Error('Database not initialized');
}
return new Promise((resolve, reject) => {
this.db!.transaction((tx: any) => {
tx.executeSql(
sql,
params,
(_: any, results: any) => resolve(results.rows.item(0)),
(_: any, error: any) => {
logger.error('Query error:', error);
reject(error);
return false;
},
);
});
});
}
public async run(sql: any, ...params: any[]): Promise<any> {
if (!this.db) {
throw new Error('Database not initialized');
}
return new Promise((resolve, reject) => {
this.db!.transaction((tx: any) => {
tx.executeSql(
sql,
params,
(_: any, results: any) => resolve(results),
(_: any, error: any) => {
logger.error('Query error:', error);
reject(error);
return false;
},
);
});
});
}
public async prepare(sql: string): Promise<any> {
if (!this.db) {
throw new Error('Database not initialized');
}
return new Promise((resolve, reject) => {
this.db!.transaction((tx: any) => {
tx.executeSql(
sql,
[],
() => resolve({}),
(_: any, error: any) => {
logger.error('Query error:', error);
reject(error);
return false;
},
);
});
});
}
}