index.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. const nmRegionsDBname = 'nmRegions.db';
  7. const darePlacesDBname = 'darePlaces.db';
  8. const sqliteDirectory = 'SQLite';
  9. const sqliteFullPath = FileSystem.documentDirectory + sqliteDirectory;
  10. const DS = '/';
  11. async function copyDatabaseFile(dbName: string, dbAsset: Asset) {
  12. console.log("DB copy start - " + dbName);
  13. await dbAsset.downloadAsync();
  14. await FileSystem.downloadAsync(
  15. dbAsset.uri,
  16. sqliteFullPath + DS + dbName
  17. );
  18. const dbUri = sqliteFullPath + DS + dbName;
  19. await FileSystem.copyAsync({
  20. from: dbAsset.localUri ?? '',
  21. to: dbUri,
  22. });
  23. return dbUri;
  24. }
  25. export async function openDatabases() {
  26. try {
  27. const fileInfo = await FileSystem.getInfoAsync(sqliteFullPath);
  28. if (!fileInfo.exists) {
  29. await FileSystem.makeDirectoryAsync(sqliteFullPath, { intermediates: true });
  30. }
  31. const nmRegionsDB = await FileSystem.getInfoAsync(sqliteFullPath + DS + nmRegionsDBname, { size: true });
  32. if (!nmRegionsDB.exists) {
  33. await copyDatabaseFile(nmRegionsDBname, Asset.fromModule(require('../../assets/db/' + nmRegionsDBname)));
  34. }
  35. if (nmRegionsDB.size == 0) {
  36. await FileSystem.deleteAsync(sqliteFullPath + DS + nmRegionsDBname);
  37. await copyDatabaseFile(nmRegionsDBname, Asset.fromModule(require('../../assets/db/' + nmRegionsDBname)));
  38. }
  39. const darePlacesDB = await FileSystem.getInfoAsync(sqliteFullPath + DS + nmRegionsDBname, { size: true });
  40. if (!darePlacesDB.exists) {
  41. await copyDatabaseFile(darePlacesDBname, Asset.fromModule(require('../../assets/db/' + darePlacesDBname)));
  42. }
  43. if (darePlacesDB.size == 0) {
  44. await FileSystem.deleteAsync(sqliteFullPath + DS + darePlacesDBname);
  45. await copyDatabaseFile(darePlacesDBname, Asset.fromModule(require('../../assets/db/' + darePlacesDBname)));
  46. }
  47. const openDatabase = (dbName: string) => SQLite.openDatabase(dbName);
  48. db1 = openDatabase(nmRegionsDBname);
  49. db2 = openDatabase(darePlacesDBname);
  50. } catch (error) {
  51. console.error('openDatabases - Error:');
  52. console.error(JSON.stringify(error, null, 2));
  53. }
  54. }
  55. export async function refreshDatabases() {
  56. try {
  57. const fileInfo = await FileSystem.getInfoAsync(sqliteFullPath);
  58. if (!fileInfo.exists) {
  59. await FileSystem.makeDirectoryAsync(sqliteFullPath, { intermediates: true });
  60. }
  61. await FileSystem.deleteAsync(sqliteFullPath + DS + nmRegionsDBname, {idempotent: true});
  62. await copyDatabaseFile(nmRegionsDBname, Asset.fromModule(require('../../assets/db/' + nmRegionsDBname)));
  63. await FileSystem.deleteAsync(sqliteFullPath + DS + darePlacesDBname , {idempotent: true} );
  64. await copyDatabaseFile(darePlacesDBname, Asset.fromModule(require('../../assets/db/' + darePlacesDBname)));
  65. const openDatabase = (dbName: string) => SQLite.openDatabase(dbName);
  66. db1 = null;
  67. db2 = null;
  68. db1 = openDatabase(nmRegionsDBname);
  69. db2 = openDatabase(darePlacesDBname);
  70. } catch (error) {
  71. console.error('refreshDatabases - Error:');
  72. console.error(JSON.stringify(error, null, 2));
  73. }
  74. }
  75. export function getFirstDatabase() {
  76. return db1;
  77. }
  78. export function getSecondDatabase() {
  79. return db2;
  80. }