Skip to content
This repository was archived by the owner on Oct 24, 2025. It is now read-only.

Commit 35dd8fc

Browse files
committed
more examples
1 parent 531aa6a commit 35dd8fc

File tree

6 files changed

+185
-53
lines changed

6 files changed

+185
-53
lines changed

example/src/App.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,23 @@ import React, {useState} from 'react';
22
import {SafeAreaView, ScrollView, StatusBar, View} from 'react-native';
33
import useDatabase from './hooks/useDatabase';
44
import DatabaseSelector from './components/DatabaseSelector';
5-
import BasicCrud from './components/BasicCrud';
5+
import ExampleSelector, {Examples} from './components/ExampleSelector';
6+
import BasicCrudExample from './components/examples/BasicCrud';
7+
import UtilsExample from './components/examples/Utils';
8+
import type {AsyncStorageSqlite} from '@react-native-async-storage/sqlite-storage';
9+
10+
const ExamplesToComponents: {
11+
[key in Examples]: React.FC<{db: AsyncStorageSqlite}>;
12+
} = {
13+
[Examples.BasicCrud]: BasicCrudExample,
14+
[Examples.Utils]: UtilsExample,
15+
};
616

717
function App(): JSX.Element {
818
const [dbName, setDbName] = useState('example_db.db');
19+
const [example, setExample] = useState(Examples.BasicCrud);
920
const database = useDatabase(dbName);
21+
const Example = ExamplesToComponents[example];
1022
return (
1123
<SafeAreaView>
1224
<StatusBar barStyle={'dark-content'} />
@@ -16,7 +28,8 @@ function App(): JSX.Element {
1628
initialName={dbName}
1729
onCreateDatabase={newName => setDbName(newName)}
1830
/>
19-
<BasicCrud key={dbName} db={database} />
31+
<ExampleSelector selected={example} onSelect={setExample} />
32+
<Example db={database} key={database.name} />
2033
</View>
2134
</ScrollView>
2235
</SafeAreaView>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import React from 'react';
2+
import {Button, StyleSheet, View} from 'react-native';
3+
4+
type Props = {
5+
selected: Examples;
6+
onSelect: (selected: Examples) => void;
7+
};
8+
9+
export enum Examples {
10+
BasicCrud = 'Basic Crud',
11+
Utils = 'Utils',
12+
}
13+
14+
const ActiveColor = '#7DCE82';
15+
const DisabledColor = '#B5E3B8';
16+
17+
const ExampleSelector: React.FC<Props> = ({selected, onSelect}) => {
18+
return (
19+
<View style={styles.row}>
20+
<View style={styles.button}>
21+
<Button
22+
color={selected === Examples.BasicCrud ? ActiveColor : DisabledColor}
23+
title="Curd"
24+
onPress={() => onSelect(Examples.BasicCrud)}
25+
/>
26+
</View>
27+
<View style={styles.button}>
28+
<Button
29+
color={selected === Examples.Utils ? ActiveColor : DisabledColor}
30+
title="Utils"
31+
onPress={() => onSelect(Examples.Utils)}
32+
/>
33+
</View>
34+
</View>
35+
);
36+
};
37+
38+
const styles = StyleSheet.create({
39+
row: {
40+
padding: 8,
41+
flexDirection: 'row',
42+
justifyContent: 'flex-start',
43+
alignItems: 'center',
44+
},
45+
button: {
46+
paddingHorizontal: 8,
47+
},
48+
});
49+
50+
export default ExampleSelector;

example/src/components/BasicCrud.tsx renamed to example/src/components/examples/BasicCrud.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type {AsyncStorageSqlite} from '@react-native-async-storage/sqlite-storage';
2-
import React, {useState} from 'react';
2+
import React, {useEffect, useState} from 'react';
33
import {Button, StyleSheet, Text, View} from 'react-native';
44

55
type Props = {db: AsyncStorageSqlite};
@@ -23,6 +23,10 @@ const BasicCrud: React.FC<Props> = ({db}) => {
2323
await readValue();
2424
}
2525

26+
useEffect(() => {
27+
readValue();
28+
}, []);
29+
2630
return (
2731
<View style={styles.container}>
2832
<Text style={styles.header}>Basic crud example</Text>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import type {AsyncStorageSqlite} from '@react-native-async-storage/sqlite-storage';
2+
import React, {useEffect, useState} from 'react';
3+
import {Button, StyleSheet, Text, View} from 'react-native';
4+
5+
type Props = {db: AsyncStorageSqlite};
6+
7+
const Utils: React.FC<Props> = ({db}) => {
8+
const [dbPath, setDbPath] = useState('');
9+
const [dbSize, setDbSize] = useState('-');
10+
11+
async function readPath() {
12+
const path = await db.getDatabaseFilePath();
13+
setDbPath(path);
14+
}
15+
16+
async function readSize() {
17+
const size = await db.getDatabaseFileSize();
18+
setDbSize(`${size / 1000}MB`);
19+
}
20+
21+
useEffect(() => {
22+
console.log('refreshing!');
23+
readPath();
24+
readSize();
25+
}, []);
26+
27+
return (
28+
<View style={styles.container}>
29+
<Text style={styles.header}>Database utils</Text>
30+
<View style={styles.row}>
31+
<View style={styles.button}>
32+
<Button title="read size" onPress={readSize} />
33+
</View>
34+
<View style={styles.button}>
35+
<Button title="read path" onPress={readPath} />
36+
</View>
37+
</View>
38+
39+
<Text>{`Database size: ${dbSize}`}</Text>
40+
<Text>{`Database path: ${dbPath}`}</Text>
41+
</View>
42+
);
43+
};
44+
45+
const styles = StyleSheet.create({
46+
container: {
47+
padding: 8,
48+
},
49+
button: {
50+
paddingHorizontal: 8,
51+
},
52+
header: {
53+
fontSize: 18,
54+
},
55+
row: {
56+
paddingVertical: 18,
57+
flexDirection: 'row',
58+
justifyContent: 'center',
59+
alignItems: 'center',
60+
},
61+
});
62+
63+
export default Utils;

src/AsyncStorageSqlite.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import type { Key, Value, Entries } from "./types";
2+
import module from "./NativeModule";
3+
4+
export class AsyncStorageSqlite {
5+
constructor(private dbName: string) {}
6+
7+
get name() {
8+
return this.dbName;
9+
}
10+
11+
read = async (key: Key): Promise<Entries> => {
12+
return this.readMany([key]);
13+
};
14+
15+
readMany = async (keys: Key[]): Promise<Entries> => {
16+
return module.readMany(this.dbName, keys);
17+
};
18+
19+
save = async (key: Key, value: Value): Promise<void> => {
20+
return this.saveMany({ [key]: value });
21+
};
22+
23+
saveMany = async (entries: Entries): Promise<void> => {
24+
return module.saveMany(this.dbName, entries);
25+
};
26+
27+
delete = async (key: Key): Promise<void> => {
28+
return this.deleteMany([key]);
29+
};
30+
31+
deleteMany = async (keys: Key[]): Promise<void> => {
32+
return module.deleteMany(this.dbName, keys);
33+
};
34+
35+
merge = async (entry: Entries): Promise<Entries> => {
36+
return module.merge(this.dbName, entry);
37+
};
38+
39+
getKeys = async (): Promise<Key[]> => {
40+
return module.getKeys(this.dbName);
41+
};
42+
43+
getDatabaseFilePath = async () => {
44+
return module.getDatabasePath(this.dbName);
45+
};
46+
47+
getDatabaseFileSize = async () => {
48+
return module.getDatabaseSize(this.dbName);
49+
};
50+
}

src/index.ts

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,2 @@
1-
import type { Key, Value, Entries } from "./types";
2-
import module from "./NativeModule";
3-
4-
export class AsyncStorageSqlite {
5-
constructor(private dbName: string) {}
6-
7-
get name() {
8-
return this.dbName;
9-
}
10-
11-
read = async (key: Key): Promise<Entries> => {
12-
return this.readMany([key]);
13-
};
14-
15-
readMany = async (keys: Key[]): Promise<Entries> => {
16-
return module.readMany(this.dbName, keys);
17-
};
18-
19-
save = async (key: Key, value: Value): Promise<void> => {
20-
return this.saveMany({ [key]: value });
21-
};
22-
23-
saveMany = async (entries: Entries): Promise<void> => {
24-
return module.saveMany(this.dbName, entries);
25-
};
26-
27-
delete = async (key: Key): Promise<void> => {
28-
return this.deleteMany([key]);
29-
};
30-
31-
deleteMany = async (keys: Key[]): Promise<void> => {
32-
return module.deleteMany(this.dbName, keys);
33-
};
34-
35-
merge = async (entry: Entries): Promise<Entries> => {
36-
return module.merge(this.dbName, entry);
37-
};
38-
39-
getKeys = async (): Promise<Key[]> => {
40-
return module.getKeys(this.dbName);
41-
};
42-
43-
getDatabaseFilePath = async () => {
44-
return module.getDatabasePath(this.dbName);
45-
};
46-
47-
getDatabaseFileSize = async () => {
48-
return module.getDatabaseSize(this.dbName);
49-
};
50-
}
1+
export { AsyncStorageSqlite } from "./AsyncStorageSqlite";
2+
export type { Key, Entries, Value } from "./types";

0 commit comments

Comments
 (0)