Make sqlite work (#771)

This commit is contained in:
Leendert de Borst
2025-04-11 16:41:15 +02:00
parent baf1f24379
commit eda0fb4d4e
2 changed files with 68 additions and 18 deletions

View File

@@ -7,9 +7,15 @@ import { IconSymbol } from '@/components/ui/IconSymbol';
import TabBarBackground from '@/components/ui/TabBarBackground';
import { Colors } from '@/constants/Colors';
import { useColorScheme } from '@/hooks/useColorScheme';
import { useAuth } from '@/context/AuthContext';
export default function TabLayout() {
const colorScheme = useColorScheme();
const { isLoggedIn, isInitialized } = useAuth();
if (!isInitialized) {
return null;
}
return (
<Tabs
@@ -18,13 +24,13 @@ export default function TabLayout() {
headerShown: false,
tabBarButton: HapticTab,
tabBarBackground: TabBarBackground,
tabBarStyle: Platform.select({
// Hide tab bar when not logged in
tabBarStyle: isLoggedIn ? Platform.select({
ios: {
// Use a transparent background on iOS to show the blur effect
position: 'absolute',
},
default: {},
}),
}) : { display: 'none' },
}}>
<Tabs.Screen
name="index"
@@ -40,6 +46,13 @@ export default function TabLayout() {
tabBarIcon: ({ color }) => <IconSymbol size={28} name="paperplane.fill" color={color} />,
}}
/>
<Tabs.Screen
name="settings"
options={{
title: 'Settings',
tabBarIcon: ({ color }) => <IconSymbol size={28} name="gear" color={color} />,
}}
/>
</Tabs>
);
}

View File

@@ -1,4 +1,5 @@
import * as SQLite from 'expo-sqlite';
import * as FileSystem from 'expo-file-system';
import { Credential } from './types/Credential';
import { EncryptionKey } from './types/EncryptionKey';
import { TotpCode } from './types/TotpCode';
@@ -30,25 +31,51 @@ class SqliteClient {
*/
public async initializeFromBase64(base64String: string): Promise<void> {
try {
// Open the database
this.db = SQLite.openDatabaseSync('aliasvault.db');
// For in-memory database, we need to create a temporary file first
const tempFileUri = `${FileSystem.documentDirectory}SQLite/temp.db`;
console.log('Writing database to temporary file');
await FileSystem.writeAsStringAsync(tempFileUri, base64String, {
encoding: FileSystem.EncodingType.Base64,
});
// Convert base64 to Uint8Array
const binaryString = atob(base64String);
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
console.log('Database written to temporary file');
console.log('tempFileUri', tempFileUri);
// Open the database in memory
console.log('Opening database from file');
this.db = SQLite.openDatabaseSync('temp.db');
console.log('Database opened from file');
// TODO: Finish implementation of in-memory database as we don't want to persist the database to the file system.
// Attach in-memory db
/*await this.executeUpdate(`ATTACH DATABASE ':memory:' AS target`);
await this.executeUpdate('BEGIN TRANSACTION');
console.log('Executing query to get tables');
// Copy all tables from source to memory
const tables = await this.executeQuery<{ name: string }>(
"SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'"
);
console.log('Tables copied');
for (const table of tables) {
await this.executeUpdate(`CREATE TABLE ${table.name} AS SELECT * FROM target.${table.name}`);
}
await this.executeUpdate('COMMIT');
await this.executeUpdate('DETACH DATABASE source');
// Clean up the temporary file
await FileSystem.deleteAsync(tempFileUri);*/
// Import the database
// Setup database pragmas to configure the database.
await this.executeUpdate('PRAGMA journal_mode = WAL');
await this.executeUpdate('PRAGMA synchronous = NORMAL');
await this.executeUpdate('PRAGMA foreign_keys = ON');
// TODO: Implement database import from bytes
// This is a complex operation that would require writing the bytes to a file
// and then importing that file into the SQLite database
console.warn('Database import from base64 not yet implemented');
} catch (error) {
console.error('Error initializing SQLite database:', error);
throw error;
@@ -84,10 +111,20 @@ class SqliteClient {
}
try {
const results = await this.db!.getAllAsync(
console.log('Executing query:', query);
// First do query to get all tables
const tables = await this.db.getAllAsync(
"SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'"
);
console.log('Tables:', tables);
const results = await this.db.getAllAsync(
query,
...params
);
console.log('Results:', results);
return results as T[];
} catch (error) {
console.error('Error executing query:', error);
@@ -104,7 +141,7 @@ class SqliteClient {
}
try {
const result = await this.db!.runAsync(
const result = await this.db.runAsync(
query,
...params
);