Skip to content

Commit 438cbef

Browse files
committed
Add example db entity and readme images
1 parent 9bf67b0 commit 438cbef

11 files changed

Lines changed: 211 additions & 85 deletions

File tree

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# react-native-sqlite-explorer
22

3-
Explorer for react-native-sqlite-storage library database inside react native app
3+
Explorer for react-native-sqlite-storage inside react native app
44

55
## Installation
66

@@ -13,21 +13,27 @@ npm install react-native-sqlite-explorer
1313
```js
1414
import SQLiteExplorer from 'react-native-sqlite-explorer';
1515

16-
const DBExplorer = () => {
16+
const SQLiteExplorerScreen = () => {
1717
// initial baseName that you used in params or taken from openDatabase success callback:
1818
// SQLite.openDatabase({ name: baseName, location: 'default' }, DB => { DB.dbname <-- your baseName also here
1919
const baseName = '<your_base_name_here>';
2020

2121
return <SQLiteExplorer params={{ name: baseName, location: 'default' }} />;
2222
};
2323

24-
export default DBExplorer;
24+
export default SQLiteExplorerScreen;
2525
```
2626

2727
## Tips
2828

2929
Temporarily only for Android
3030

31+
![Example 1](/src/assets/example-1.png)
32+
![Example 2](/src/assets/example-2.png)
33+
34+
![Example 3](/src/assets/example-3.png)
35+
![Example 4](/src/assets/example-4.png)
36+
3137
## License
3238

3339
MIT

example/src/App.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { Alert, StyleSheet, Text, View } from 'react-native';
33
import { getDBPath } from '../../src/utils';
44
import DB from './models/DB';
55
import SQLiteExplorer from '../../src';
6+
import Document from './models/Document';
7+
import Record from './models/Record';
8+
import Setting from './models/Setting';
69

710
const defaultBasePrefix = 'defaultBasePrefix';
811
const defaultBasePostfix = '1';
@@ -22,16 +25,19 @@ export default function App() {
2225
!!pathString ? `Loading from ${pathString}...` : "Error! Can't find path"
2326
);
2427

25-
// инициализирую подключение к базе
28+
// init database
2629
try {
2730
await DB.open(baseName);
2831
setOpened(true);
2932
} catch (error: any) {
30-
Alert.alert(
31-
'Внимание',
32-
'Не удалось открыть базу данных: ' + error.message
33-
);
33+
Alert.alert('Error!', "Can't open database: " + error.message);
34+
return;
3435
}
36+
37+
// init models
38+
await Document.init();
39+
await Record.init();
40+
await Setting.init();
3541
};
3642

3743
return (

example/src/models/DB.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
// версия с шифрованием - https://github.com/axsy-dev/react-native-sqlcipher-storage
21
import { Alert } from 'react-native';
32
import { 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');
86
SQLite.DEBUG(false);
97
SQLite.enablePromise(true);
108

119
/**
12-
* Коннектор к базе данных
10+
* Database connector
1311
*/
1412
class 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

9799
export default new Database();

example/src/models/Document.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import DB from './DB';
2+
3+
class Document {
4+
init: () => Promise<void> = () => {
5+
return new Promise<void>(async (resolve): Promise<void> => {
6+
DB.isTableExist('Document').then((ifExists): void => {
7+
if (ifExists) {
8+
resolve();
9+
} else {
10+
DB.transaction(async (): Promise<void> => {
11+
await DB.executeSql(`
12+
CREATE TABLE IF NOT EXISTS Document (
13+
_id INTEGER PRIMARY KEY AUTOINCREMENT,
14+
name VARCHAR(255)
15+
)
16+
`);
17+
resolve();
18+
});
19+
}
20+
});
21+
});
22+
};
23+
24+
truncate: () => Promise<void> = () => {
25+
return new Promise(async (resolve): Promise<void> => {
26+
await DB.executeSql('DROP TABLE IF EXISTS Document');
27+
await this.init();
28+
resolve();
29+
});
30+
};
31+
}
32+
33+
export default new Document();

example/src/models/Record.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import DB from './DB';
2+
3+
class Record {
4+
init: () => Promise<void> = () => {
5+
return new Promise<void>(async (resolve): Promise<void> => {
6+
DB.isTableExist('Record').then((ifExists): void => {
7+
if (ifExists) {
8+
resolve();
9+
} else {
10+
DB.transaction(async (): Promise<void> => {
11+
await DB.executeSql(`
12+
CREATE TABLE IF NOT EXISTS Record (
13+
_id INTEGER PRIMARY KEY AUTOINCREMENT,
14+
name VARCHAR(255),
15+
description VARCHAR(255),
16+
type VARCHAR(100),
17+
code INTEGER,
18+
json TEXT NOT NULL
19+
)
20+
`);
21+
resolve();
22+
});
23+
}
24+
});
25+
});
26+
};
27+
28+
truncate: () => Promise<void> = () => {
29+
return new Promise(async (resolve): Promise<void> => {
30+
await DB.executeSql('DROP TABLE IF EXISTS Record');
31+
await this.init();
32+
resolve();
33+
});
34+
};
35+
}
36+
37+
export default new Record();

example/src/models/Setting.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import DB from './DB';
2+
3+
class Setting {
4+
init: () => Promise<void> = () => {
5+
return new Promise<void>(async (resolve): Promise<void> => {
6+
DB.isTableExist('Setting').then((ifExists): void => {
7+
if (ifExists) {
8+
resolve();
9+
} else {
10+
DB.transaction(async (): Promise<void> => {
11+
await DB.executeSql(`
12+
CREATE TABLE IF NOT EXISTS Setting (
13+
_id INTEGER PRIMARY KEY AUTOINCREMENT,
14+
name VARCHAR(255)
15+
)
16+
`);
17+
resolve();
18+
});
19+
}
20+
});
21+
});
22+
};
23+
24+
truncate: () => Promise<void> = () => {
25+
return new Promise(async (resolve): Promise<void> => {
26+
await DB.executeSql('DROP TABLE IF EXISTS Setting');
27+
await this.init();
28+
resolve();
29+
});
30+
};
31+
}
32+
33+
export default new Setting();

src/assets/example-1.png

31.4 KB
Loading

src/assets/example-2.png

40.1 KB
Loading

src/assets/example-3.png

88.9 KB
Loading

src/assets/example-4.png

179 KB
Loading

0 commit comments

Comments
 (0)