|
@@ -4,43 +4,86 @@ import { Asset } from 'expo-asset';
|
|
|
|
|
|
let db1: SQLite.SQLiteDatabase | null = null;
|
|
let db1: SQLite.SQLiteDatabase | null = null;
|
|
let db2: SQLite.SQLiteDatabase | null = null;
|
|
let db2: SQLite.SQLiteDatabase | null = null;
|
|
|
|
+const nmRegionsDBname = 'nmRegions.db';
|
|
|
|
+const darePlacesDBname = 'darePlaces.db';
|
|
|
|
+const sqliteDirectory = 'SQLite';
|
|
|
|
+const sqliteFullPath = FileSystem.documentDirectory + sqliteDirectory;
|
|
|
|
+const DS = '/';
|
|
|
|
|
|
async function copyDatabaseFile(dbName: string, dbAsset: Asset) {
|
|
async function copyDatabaseFile(dbName: string, dbAsset: Asset) {
|
|
|
|
+ console.log("DB copy start - " + dbName);
|
|
await dbAsset.downloadAsync();
|
|
await dbAsset.downloadAsync();
|
|
await FileSystem.downloadAsync(
|
|
await FileSystem.downloadAsync(
|
|
dbAsset.uri,
|
|
dbAsset.uri,
|
|
- FileSystem.documentDirectory + "SQLite/" + dbName
|
|
|
|
|
|
+ FileSystem.documentDirectory + DS + dbName
|
|
);
|
|
);
|
|
-
|
|
|
|
- const dbUri = FileSystem.documentDirectory + `SQLite/${dbName}`;
|
|
|
|
|
|
+
|
|
|
|
+ const dbUri = sqliteFullPath + DS + dbName;
|
|
|
|
|
|
await FileSystem.copyAsync({
|
|
await FileSystem.copyAsync({
|
|
from: dbAsset.localUri ?? '',
|
|
from: dbAsset.localUri ?? '',
|
|
to: dbUri,
|
|
to: dbUri,
|
|
});
|
|
});
|
|
-
|
|
|
|
|
|
+ console.log("DB copy OK - " + dbUri);
|
|
return dbUri;
|
|
return dbUri;
|
|
}
|
|
}
|
|
|
|
|
|
export async function openDatabases() {
|
|
export async function openDatabases() {
|
|
try {
|
|
try {
|
|
- const sqlDir = FileSystem.documentDirectory + "SQLite";
|
|
|
|
- const fileInfo = await FileSystem.getInfoAsync(sqlDir);
|
|
|
|
-
|
|
|
|
|
|
+ const fileInfo = await FileSystem.getInfoAsync(sqliteFullPath);
|
|
if (!fileInfo.exists) {
|
|
if (!fileInfo.exists) {
|
|
- await FileSystem.makeDirectoryAsync(sqlDir, { intermediates: true });
|
|
|
|
- console.log('openDatabase - Downloading databases');
|
|
|
|
- await copyDatabaseFile('nmRegions.db', Asset.fromModule(require('../../assets/db/nmRegions.db')));
|
|
|
|
- await copyDatabaseFile('darePlaces.db', Asset.fromModule(require('../../assets/db/darePlaces.db')));
|
|
|
|
|
|
+ await FileSystem.makeDirectoryAsync(sqliteFullPath, { intermediates: true });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ const nmRegionsDB = await FileSystem.getInfoAsync(sqliteFullPath + DS + nmRegionsDBname, { size: true });
|
|
|
|
+ if (!nmRegionsDB.exists) {
|
|
|
|
+ await copyDatabaseFile(nmRegionsDBname, Asset.fromModule(require('../../assets/db/' + nmRegionsDBname)));
|
|
|
|
+ }
|
|
|
|
+ if (nmRegionsDB.size == 0) {
|
|
|
|
+ await FileSystem.deleteAsync(sqliteFullPath + DS + nmRegionsDBname);
|
|
|
|
+ await copyDatabaseFile(nmRegionsDBname, Asset.fromModule(require('../../assets/db/' + nmRegionsDBname)));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
|
|
- console.log('openDatabase - Databases downloaded');
|
|
|
|
|
|
+ const darePlacesDB = await FileSystem.getInfoAsync(sqliteFullPath + DS + nmRegionsDBname, { size: true });
|
|
|
|
+ if (!darePlacesDB.exists) {
|
|
|
|
+ await copyDatabaseFile(darePlacesDBname, Asset.fromModule(require('../../assets/db/' + darePlacesDBname)));
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ if (darePlacesDB.size == 0) {
|
|
|
|
+ await FileSystem.deleteAsync(sqliteFullPath + DS + darePlacesDBname);
|
|
|
|
+ await copyDatabaseFile(darePlacesDBname, Asset.fromModule(require('../../assets/db/' + darePlacesDBname)));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ const openDatabase = (dbName: string) => SQLite.openDatabase(dbName);
|
|
|
|
+ db1 = openDatabase(nmRegionsDBname);
|
|
|
|
+ db2 = openDatabase(darePlacesDBname);
|
|
|
|
+ } catch (error) {
|
|
|
|
+ console.error('openDatabases - Error:');
|
|
|
|
+ console.error(JSON.stringify(error, null, 2));
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+export async function refreshDatabases() {
|
|
|
|
+ try {
|
|
|
|
+ const fileInfo = await FileSystem.getInfoAsync(sqliteFullPath);
|
|
|
|
+ if (!fileInfo.exists) {
|
|
|
|
+ await FileSystem.makeDirectoryAsync(sqliteFullPath, { intermediates: true });
|
|
|
|
+ }
|
|
|
|
+ await FileSystem.deleteAsync(sqliteFullPath + DS + nmRegionsDBname, {idempotent: true});
|
|
|
|
+ await copyDatabaseFile(nmRegionsDBname, Asset.fromModule(require('../../assets/db/' + nmRegionsDBname)));
|
|
|
|
+
|
|
|
|
+ await FileSystem.deleteAsync(sqliteFullPath + DS + darePlacesDBname , {idempotent: true} );
|
|
|
|
+ await copyDatabaseFile(darePlacesDBname, Asset.fromModule(require('../../assets/db/' + darePlacesDBname)));
|
|
|
|
|
|
const openDatabase = (dbName: string) => SQLite.openDatabase(dbName);
|
|
const openDatabase = (dbName: string) => SQLite.openDatabase(dbName);
|
|
- db1 = openDatabase("nmRegions.db");
|
|
|
|
- db2 = openDatabase("darePlaces.db");
|
|
|
|
|
|
+ db1 = null;
|
|
|
|
+ db2 = null;
|
|
|
|
+ db1 = openDatabase(nmRegionsDBname);
|
|
|
|
+ db2 = openDatabase(darePlacesDBname);
|
|
} catch (error) {
|
|
} catch (error) {
|
|
- console.error('openDatabase - Error:', error);
|
|
|
|
|
|
+ console.error('refreshDatabases - Error:');
|
|
|
|
+ console.error(JSON.stringify(error, null, 2));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|