stream-chat-react-native-core
Version:
The official React Native and Expo components for Stream Chat, a service for building chat applications
41 lines (30 loc) • 1.06 kB
text/typescript
import Database, { type Database as DatabaseType } from 'better-sqlite3';
import { tables } from '../store/schema';
const testDbName = `foobar-${process.env.JEST_WORKER_ID ?? '0'}.db`;
export class BetterSqlite {
static db: DatabaseType | null = null;
static openDB = (): void => {
BetterSqlite.db = new Database(testDbName);
};
static closeDB = (): void => {
BetterSqlite.db?.close();
};
static getTables = async (): Promise<unknown> => {
const tablesInDb = await BetterSqlite.db?.pragma('table_list;');
return tablesInDb;
};
static dropAllTables = (): void => {
const tableNames = Object.keys(tables);
tableNames.forEach((name) => {
const stmt = BetterSqlite.db?.prepare(`DROP TABLE IF EXISTS ${name}`);
stmt?.run();
});
};
static selectFromTable = async <TRow = Record<string, unknown>>(
table: string,
): Promise<TRow[]> => {
const stmt = await BetterSqlite.db?.prepare(`SELECT * FROM ${table}`);
const result = (stmt?.all() ?? []) as TRow[];
return result;
};
}