|
| 1 | +import '@azure/core-asynciterator-polyfill'; |
| 2 | +import React, { useEffect } from 'react'; |
| 3 | +import type {PropsWithChildren} from 'react'; |
| 4 | +import { |
| 5 | + SafeAreaView, |
| 6 | + ScrollView, |
| 7 | + StatusBar, |
| 8 | + StyleSheet, |
| 9 | + Text, |
| 10 | + useColorScheme, |
| 11 | + View, |
| 12 | +} from 'react-native'; |
| 13 | + |
| 14 | +import { |
| 15 | + Colors, |
| 16 | + DebugInstructions, |
| 17 | + Header, |
| 18 | + LearnMoreLinks, |
| 19 | + ReloadInstructions, |
| 20 | +} from 'react-native/Libraries/NewAppScreen'; |
| 21 | +import { OPSqliteOpenFactory } from '@powersync/op-sqlite'; |
| 22 | +import { column, Schema, Table, PowerSyncDatabase } from '@powersync/react-native'; |
| 23 | + |
| 24 | +/** |
| 25 | + * A placeholder connector which doesn't do anything but used to confirm connect can run. |
| 26 | + */ |
| 27 | +class DummyConnector { |
| 28 | + async fetchCredentials() { |
| 29 | + return { |
| 30 | + endpoint: '', |
| 31 | + token: '', |
| 32 | + }; |
| 33 | + } |
| 34 | + |
| 35 | + async uploadData() {} |
| 36 | +} |
| 37 | + |
| 38 | +const customers = new Table({ name: column.text }); |
| 39 | + |
| 40 | +const schema = new Schema({ customers }); |
| 41 | + |
| 42 | +let powerSync: PowerSyncDatabase | null = null; |
| 43 | + |
| 44 | +const setupDatabase = async () => { |
| 45 | + if (powerSync) return powerSync; |
| 46 | + |
| 47 | + const factory = new OPSqliteOpenFactory({ |
| 48 | + dbFilename: 'powersync.db', |
| 49 | + }); |
| 50 | + |
| 51 | + powerSync = new PowerSyncDatabase({ |
| 52 | + schema, |
| 53 | + database: factory, |
| 54 | + }); |
| 55 | + |
| 56 | + await powerSync.init(); |
| 57 | + return powerSync; |
| 58 | +}; |
| 59 | + |
| 60 | +type SectionProps = PropsWithChildren<{ |
| 61 | + title: string; |
| 62 | +}>; |
| 63 | + |
| 64 | +function Section({children, title}: SectionProps): React.JSX.Element { |
| 65 | + const isDarkMode = useColorScheme() === 'dark'; |
| 66 | + return ( |
| 67 | + <View style={styles.sectionContainer}> |
| 68 | + <Text |
| 69 | + style={[ |
| 70 | + styles.sectionTitle, |
| 71 | + { |
| 72 | + color: isDarkMode ? Colors.white : Colors.black, |
| 73 | + }, |
| 74 | + ]}> |
| 75 | + {title} |
| 76 | + </Text> |
| 77 | + <Text |
| 78 | + style={[ |
| 79 | + styles.sectionDescription, |
| 80 | + { |
| 81 | + color: isDarkMode ? Colors.light : Colors.dark, |
| 82 | + }, |
| 83 | + ]}> |
| 84 | + {children} |
| 85 | + </Text> |
| 86 | + </View> |
| 87 | + ); |
| 88 | +} |
| 89 | + |
| 90 | +function App(): React.JSX.Element { |
| 91 | + const isDarkMode = useColorScheme() === 'dark'; |
| 92 | + |
| 93 | + const backgroundStyle = { |
| 94 | + backgroundColor: isDarkMode ? Colors.darker : Colors.lighter, |
| 95 | + }; |
| 96 | + |
| 97 | + useEffect(() => { |
| 98 | + const initDB = async () => { |
| 99 | + try { |
| 100 | + const db = await setupDatabase(); |
| 101 | + |
| 102 | + // Test database operations |
| 103 | + await db.execute('INSERT INTO customers(id, name) VALUES(uuid(), ?)', ['Fred']); |
| 104 | + const result = await db.getAll('SELECT * FROM customers'); |
| 105 | + console.log('Customers:', result); |
| 106 | + |
| 107 | + // Connect with dummy connector |
| 108 | + await db.connect(new DummyConnector()); |
| 109 | + } catch (error) { |
| 110 | + console.error('Database initialization error:', error); |
| 111 | + } |
| 112 | + }; |
| 113 | + |
| 114 | + initDB(); |
| 115 | + }, []); |
| 116 | + |
| 117 | + return ( |
| 118 | + <SafeAreaView style={backgroundStyle}> |
| 119 | + <StatusBar |
| 120 | + barStyle={isDarkMode ? 'light-content' : 'dark-content'} |
| 121 | + backgroundColor={backgroundStyle.backgroundColor} |
| 122 | + /> |
| 123 | + <ScrollView |
| 124 | + contentInsetAdjustmentBehavior="automatic" |
| 125 | + style={backgroundStyle}> |
| 126 | + <Header /> |
| 127 | + <View |
| 128 | + style={{ |
| 129 | + backgroundColor: isDarkMode ? Colors.black : Colors.white, |
| 130 | + }}> |
| 131 | + <Section title="Step One"> |
| 132 | + Edit <Text style={styles.highlight}>App.tsx</Text> to change this |
| 133 | + screen and then come back to see your edits. |
| 134 | + </Section> |
| 135 | + <Section title="See Your Changes"> |
| 136 | + <ReloadInstructions /> |
| 137 | + </Section> |
| 138 | + <Section title="Debug"> |
| 139 | + <DebugInstructions /> |
| 140 | + </Section> |
| 141 | + <Section title="Learn More"> |
| 142 | + Read the docs to discover what to do next: |
| 143 | + </Section> |
| 144 | + <LearnMoreLinks /> |
| 145 | + </View> |
| 146 | + </ScrollView> |
| 147 | + </SafeAreaView> |
| 148 | + ); |
| 149 | +} |
| 150 | + |
| 151 | +const styles = StyleSheet.create({ |
| 152 | + sectionContainer: { |
| 153 | + marginTop: 32, |
| 154 | + paddingHorizontal: 24, |
| 155 | + }, |
| 156 | + sectionTitle: { |
| 157 | + fontSize: 24, |
| 158 | + fontWeight: '600', |
| 159 | + }, |
| 160 | + sectionDescription: { |
| 161 | + marginTop: 8, |
| 162 | + fontSize: 18, |
| 163 | + fontWeight: '400', |
| 164 | + }, |
| 165 | + highlight: { |
| 166 | + fontWeight: '700', |
| 167 | + }, |
| 168 | +}); |
| 169 | + |
| 170 | +export default App; |
0 commit comments