index.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import * as SQLite from 'expo-sqlite';
  2. import * as FileSystem from 'expo-file-system';
  3. import { Asset } from 'expo-asset';
  4. let db1: SQLite.SQLiteDatabase | null = null;
  5. let db2: SQLite.SQLiteDatabase | null = null;
  6. async function copyDatabaseFile(dbName: string, dbAsset: Asset) {
  7. await dbAsset.downloadAsync();
  8. await FileSystem.downloadAsync(
  9. dbAsset.uri,
  10. FileSystem.documentDirectory + "SQLite/" + dbName
  11. );
  12. const dbUri = FileSystem.documentDirectory + `SQLite/${dbName}`;
  13. await FileSystem.copyAsync({
  14. from: dbAsset.localUri ?? '',
  15. to: dbUri,
  16. });
  17. return dbUri;
  18. }
  19. export async function openDatabases() {
  20. try {
  21. const sqlDir = FileSystem.documentDirectory + "SQLite";
  22. const fileInfo = await FileSystem.getInfoAsync(sqlDir);
  23. if (!fileInfo.exists) {
  24. await FileSystem.makeDirectoryAsync(sqlDir, { intermediates: true });
  25. console.log('openDatabase - Downloading databases');
  26. await copyDatabaseFile('nmRegions.db', Asset.fromModule(require('../../assets/db/nmRegions.db')));
  27. await copyDatabaseFile('darePlaces.db', Asset.fromModule(require('../../assets/db/darePlaces.db')));
  28. console.log('openDatabase - Databases downloaded');
  29. }
  30. const openDatabase = (dbName: string) => SQLite.openDatabase(dbName);
  31. db1 = openDatabase("nmRegions.db");
  32. db2 = openDatabase("darePlaces.db");
  33. } catch (error) {
  34. console.error('openDatabase - Error:', error);
  35. }
  36. }
  37. export function getFirstDatabase() {
  38. return db1;
  39. }
  40. export function getSecondDatabase() {
  41. return db2;
  42. }