1- // версия с шифрованием - https://github.com/axsy-dev/react-native-sqlcipher-storage
21import { Alert } from 'react-native' ;
32import { getDBPath } from '../../../src/utils' ;
43
5- import SQLite from 'react-native-sqlite-storage' ; // sselect sqlite_version() - "3.22.0"
4+ import SQLite from 'react-native-sqlite-storage' ;
65
7- // SQLite.DEBUG(process.env.NODE_ENV !== 'production');
86SQLite . DEBUG ( false ) ;
97SQLite . enablePromise ( true ) ;
108
119/**
12- * Коннектор к базе данных
10+ * Database connector
1311 */
1412class Database {
1513 DB : SQLite . SQLiteDatabase | null = null ;
@@ -18,11 +16,9 @@ class Database {
1816 transaction : Function = ( callback : ( ) => Promise < void > ) =>
1917 this ?. DB ?. transaction ( callback ) ;
2018
21- // сделаю свое логирование ошибок на проде
2219 executeSql : Function = async ( sql : string , arg ?: any [ ] ) : Promise < any [ ] > => {
23- // если вдруг подключение пропало, попытаюсь его восстановить
2420 if ( ! this . DB ) {
25- Alert . alert ( ' executeSql: Не удалось переподключиться к базе' ) ;
21+ Alert . alert ( " executeSql: can't re-connect to database" ) ;
2622 return [ ] ;
2723 }
2824
@@ -52,7 +48,7 @@ class Database {
5248 return resolve ( this . DB ) ;
5349 }
5450
55- // не обязательно
51+ // not necessary
5652 this . basePath = await getDBPath ( baseName ) ;
5753
5854 const params = {
@@ -64,7 +60,6 @@ class Database {
6460 params ,
6561 ( DB ) => {
6662 this . DB = DB ;
67- // Нужно включить внешние ключи
6863 this . executeSql ( 'PRAGMA foreign_keys = ON' ) . then ( ( ) => {
6964 return resolve ( DB ) ;
7065 } ) ;
@@ -92,6 +87,13 @@ class Database {
9287 return resolve ( ) ;
9388 } ) ;
9489 } ;
90+
91+ isTableExist = async ( table : string = '' ) : Promise < boolean > => {
92+ const ifExist = await this . executeSql (
93+ `SELECT EXISTS(SELECT name FROM sqlite_master WHERE type='table' AND name='${ table } ') as exist`
94+ ) ;
95+ return ! ! ifExist ?. [ 0 ] ?. rows ?. item ( 0 ) ?. exist ?? false ;
96+ } ;
9597}
9698
9799export default new Database ( ) ;
0 commit comments