|
| 1 | +import { useStore } from '@nanostores/react' |
| 2 | +import { Button, StyleSheet, Text, View } from 'react-native' |
| 3 | + |
| 4 | +import { expoDriver } from '../../expo/index.js' |
| 5 | +import { migrateIfNeeded, openDb } from '../../index.js' |
| 6 | + |
| 7 | +let db = openDb(expoDriver('expo-demo.sqlite')) |
| 8 | + |
| 9 | +let $migration = migrateIfNeeded(db, 1, async prevVersion => { |
| 10 | + if (prevVersion < 1) { |
| 11 | + await db.exec`CREATE TABLE counters |
| 12 | + (id INTEGER PRIMARY KEY AUTOINCREMENT, value INTEGER NOT NULL DEFAULT 0)` |
| 13 | + } |
| 14 | +}) |
| 15 | + |
| 16 | +function CounterList() { |
| 17 | + let $counters = db.store`SELECT * FROM counters ORDER BY id` |
| 18 | + let state = useStore($counters) |
| 19 | + |
| 20 | + if (state.isLoading) { |
| 21 | + return <Text>Loading…</Text> |
| 22 | + } |
| 23 | + |
| 24 | + let counters = state.value ?? [] |
| 25 | + |
| 26 | + return ( |
| 27 | + <View> |
| 28 | + {counters.map(({ id, value }) => ( |
| 29 | + <View key={id} style={styles.counter}> |
| 30 | + <Text style={styles.id}>#{id}</Text> |
| 31 | + <Button |
| 32 | + title="−" |
| 33 | + onPress={() => |
| 34 | + db.exec`UPDATE counters SET value = value - 1 WHERE id = ${id}` |
| 35 | + } |
| 36 | + /> |
| 37 | + <Text style={styles.value}>{value}</Text> |
| 38 | + <Button |
| 39 | + title="+" |
| 40 | + onPress={() => |
| 41 | + db.exec`UPDATE counters SET value = value + 1 WHERE id = ${id}` |
| 42 | + } |
| 43 | + /> |
| 44 | + <Button |
| 45 | + title="Delete" |
| 46 | + onPress={() => db.exec`DELETE FROM counters WHERE id = ${id}`} |
| 47 | + /> |
| 48 | + </View> |
| 49 | + ))} |
| 50 | + <Button |
| 51 | + title="Add counter" |
| 52 | + onPress={() => db.exec`INSERT INTO counters (value) VALUES (0)`} |
| 53 | + /> |
| 54 | + </View> |
| 55 | + ) |
| 56 | +} |
| 57 | + |
| 58 | +function renderContent(migration) { |
| 59 | + if ('applying' in migration) return <Text>Running migrations…</Text> |
| 60 | + if ('outdated' in migration) return <Text>Page outdated, please reload.</Text> |
| 61 | + return <CounterList /> |
| 62 | +} |
| 63 | + |
| 64 | +export default function App() { |
| 65 | + let migration = useStore($migration) |
| 66 | + |
| 67 | + return ( |
| 68 | + <View style={styles.container}> |
| 69 | + <Text style={styles.title}>Nano Stores SQL Demo (Expo)</Text> |
| 70 | + {renderContent(migration)} |
| 71 | + </View> |
| 72 | + ) |
| 73 | +} |
| 74 | + |
| 75 | +let styles = StyleSheet.create({ |
| 76 | + container: { |
| 77 | + flex: 1, |
| 78 | + padding: 40, |
| 79 | + maxWidth: 480 |
| 80 | + }, |
| 81 | + title: { |
| 82 | + fontSize: 24, |
| 83 | + fontWeight: 'bold', |
| 84 | + marginBottom: 20 |
| 85 | + }, |
| 86 | + counter: { |
| 87 | + flexDirection: 'row', |
| 88 | + alignItems: 'center', |
| 89 | + gap: 8, |
| 90 | + marginVertical: 4 |
| 91 | + }, |
| 92 | + id: { |
| 93 | + minWidth: 40 |
| 94 | + }, |
| 95 | + value: { |
| 96 | + minWidth: 40, |
| 97 | + textAlign: 'center' |
| 98 | + } |
| 99 | +}) |
0 commit comments