index.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import * as SQLite from 'expo-sqlite/legacy';
  2. import * as FileSystem from 'expo-file-system';
  3. import { Asset } from 'expo-asset';
  4. import { API_HOST } from 'src/constants';
  5. import { storage } from 'src/storage';
  6. import { fetchLastDareDbUpdate, fetchLastRegionsDbUpdate } from '@api/app';
  7. let db1: SQLite.SQLiteDatabase | null = null;
  8. let db2: SQLite.SQLiteDatabase | null = null;
  9. let db3: SQLite.SQLiteDatabase | null = null;
  10. const nmRegionsDBname = 'nmRegions.db';
  11. const darePlacesDBname = 'darePlaces.db';
  12. const countriesDBname = 'nmCountries.db';
  13. const sqliteDirectory = 'SQLite';
  14. const sqliteFullPath = FileSystem.documentDirectory + sqliteDirectory;
  15. const DS = '/';
  16. async function copyDatabaseFile(dbName: string, dbAsset: Asset) {
  17. console.log('DB copy start - ' + dbName);
  18. await dbAsset.downloadAsync();
  19. await FileSystem.downloadAsync(dbAsset.uri, sqliteFullPath + DS + dbName);
  20. const dbUri = sqliteFullPath + DS + dbName;
  21. await FileSystem.copyAsync({
  22. from: dbAsset.localUri ?? '',
  23. to: dbUri
  24. });
  25. return dbUri;
  26. }
  27. export async function openDatabases() {
  28. try {
  29. const fileInfo = await FileSystem.getInfoAsync(sqliteFullPath);
  30. if (!fileInfo.exists) {
  31. await FileSystem.makeDirectoryAsync(sqliteFullPath, { intermediates: true });
  32. }
  33. const nmRegionsDB = await FileSystem.getInfoAsync(sqliteFullPath + DS + nmRegionsDBname, {
  34. size: true
  35. });
  36. if (!nmRegionsDB.exists) {
  37. await copyDatabaseFile(
  38. nmRegionsDBname,
  39. Asset.fromModule(require('../../assets/db/' + nmRegionsDBname))
  40. );
  41. }
  42. if (nmRegionsDB.exists && nmRegionsDB.size == 0) {
  43. await FileSystem.deleteAsync(sqliteFullPath + DS + nmRegionsDBname);
  44. await copyDatabaseFile(
  45. nmRegionsDBname,
  46. Asset.fromModule(require('../../assets/db/' + nmRegionsDBname))
  47. );
  48. }
  49. const darePlacesDB = await FileSystem.getInfoAsync(sqliteFullPath + DS + darePlacesDBname, {
  50. size: true
  51. });
  52. if (!darePlacesDB.exists) {
  53. await copyDatabaseFile(
  54. darePlacesDBname,
  55. Asset.fromModule(require('../../assets/db/' + darePlacesDBname))
  56. );
  57. }
  58. if (darePlacesDB.exists && darePlacesDB.size == 0) {
  59. await FileSystem.deleteAsync(sqliteFullPath + DS + darePlacesDBname);
  60. await copyDatabaseFile(
  61. darePlacesDBname,
  62. Asset.fromModule(require('../../assets/db/' + darePlacesDBname))
  63. );
  64. }
  65. const countriesDB = await FileSystem.getInfoAsync(sqliteFullPath + DS + countriesDBname, {
  66. size: true
  67. });
  68. if (!countriesDB.exists) {
  69. await copyDatabaseFile(
  70. countriesDBname,
  71. Asset.fromModule(require('../../assets/db/' + countriesDBname))
  72. );
  73. }
  74. if (countriesDB.exists && countriesDB.size == 0) {
  75. await FileSystem.deleteAsync(sqliteFullPath + DS + countriesDBname);
  76. await copyDatabaseFile(
  77. countriesDBname,
  78. Asset.fromModule(require('../../assets/db/' + countriesDBname))
  79. );
  80. }
  81. const openDatabase = (dbName: string) => SQLite.openDatabase(dbName);
  82. db1 = openDatabase(nmRegionsDBname);
  83. db2 = openDatabase(darePlacesDBname);
  84. db3 = openDatabase(countriesDBname);
  85. } catch (error) {
  86. console.error('openDatabases - Error:');
  87. console.error(JSON.stringify(error, null, 2));
  88. }
  89. }
  90. export async function refreshDatabases() {
  91. try {
  92. const fileInfo = await FileSystem.getInfoAsync(sqliteFullPath);
  93. if (!fileInfo.exists) {
  94. await FileSystem.makeDirectoryAsync(sqliteFullPath, { intermediates: true });
  95. }
  96. await refreshNmDatabase();
  97. await refreshDarePlacesDatabase();
  98. await refreshCountriesDatabase();
  99. } catch (error) {
  100. console.error('refreshDatabases - Error:');
  101. console.error(JSON.stringify(error, null, 2));
  102. }
  103. }
  104. export function getFirstDatabase() {
  105. return db1;
  106. }
  107. export function getSecondDatabase() {
  108. return db2;
  109. }
  110. export function getCountriesDatabase() {
  111. return db3;
  112. }
  113. const openDatabase = (dbName: string) => SQLite.openDatabase(dbName);
  114. async function refreshNmDatabase() {
  115. try {
  116. await FileSystem.deleteAsync(sqliteFullPath + DS + nmRegionsDBname, { idempotent: true });
  117. const nmUrl = `${API_HOST}/static/app/${nmRegionsDBname}`;
  118. let nmFileUri = sqliteFullPath + DS + nmRegionsDBname;
  119. const nmResponse = await FileSystem.downloadAsync(nmUrl, nmFileUri);
  120. if (nmResponse.status !== 200) {
  121. console.error(`Failed to download the nmDb file: Status code ${nmResponse.status}`);
  122. }
  123. db1 = null;
  124. db1 = openDatabase(nmRegionsDBname);
  125. } catch (error) {
  126. console.error('refreshDatabase nmRegions - Error:');
  127. console.error(JSON.stringify(error, null, 2));
  128. }
  129. }
  130. async function refreshDarePlacesDatabase() {
  131. try {
  132. await FileSystem.deleteAsync(sqliteFullPath + DS + darePlacesDBname, { idempotent: true });
  133. const dareUrl = `${API_HOST}/static/app/${darePlacesDBname}`;
  134. let dareFileUri = sqliteFullPath + DS + darePlacesDBname;
  135. const dareResponse = await FileSystem.downloadAsync(dareUrl, dareFileUri);
  136. if (dareResponse.status !== 200) {
  137. console.error(`Failed to download the dareDb file: Status code ${dareResponse.status}`);
  138. }
  139. db2 = null;
  140. db2 = openDatabase(darePlacesDBname);
  141. } catch (error) {
  142. console.error('refreshDatabase darePlaces - Error:');
  143. console.error(JSON.stringify(error, null, 2));
  144. }
  145. }
  146. async function refreshCountriesDatabase() {
  147. try {
  148. await FileSystem.deleteAsync(sqliteFullPath + DS + countriesDBname, { idempotent: true });
  149. const countriesUrl = `${API_HOST}/static/app/${countriesDBname}`;
  150. let countriesFileUri = sqliteFullPath + DS + countriesDBname;
  151. const countriesResponse = await FileSystem.downloadAsync(countriesUrl, countriesFileUri);
  152. if (countriesResponse.status !== 200) {
  153. console.error(
  154. `Failed to download the countriesDb file: Status code ${countriesResponse.status}`
  155. );
  156. }
  157. db3 = null;
  158. db3 = openDatabase(countriesDBname);
  159. } catch (error) {
  160. console.error('refreshDatabase nmCountries - Error:');
  161. console.error(JSON.stringify(error, null, 2));
  162. }
  163. }
  164. export async function updateNmRegionsDb(localLastDate: string) {
  165. const lastUpdate = await fetchLastRegionsDbUpdate(localLastDate);
  166. if (lastUpdate && lastUpdate.date !== localLastDate) {
  167. const dirInfo = await FileSystem.getInfoAsync(sqliteFullPath);
  168. if (!dirInfo.exists) {
  169. await FileSystem.makeDirectoryAsync(sqliteFullPath, { intermediates: true });
  170. }
  171. await refreshNmDatabase();
  172. storage.set('lastUpdateNmRegions', lastUpdate.date);
  173. }
  174. }
  175. export async function updateDarePlacesDb(localLastDate: string) {
  176. const lastUpdate = await fetchLastDareDbUpdate(localLastDate);
  177. if (lastUpdate && lastUpdate.date !== localLastDate) {
  178. const dirInfo = await FileSystem.getInfoAsync(sqliteFullPath);
  179. if (!dirInfo.exists) {
  180. await FileSystem.makeDirectoryAsync(sqliteFullPath, { intermediates: true });
  181. }
  182. await refreshDarePlacesDatabase();
  183. storage.set('lastUpdateDarePlaces', lastUpdate.date);
  184. }
  185. }