From adc37102c2c9412d5737ac3e40f9eae31596e8a0 Mon Sep 17 00:00:00 2001 From: Tim Glasgow Date: Sat, 30 Jul 2022 14:42:26 -0500 Subject: [PATCH] DB init changes --- squarenotsquare/src/realm/dbInit.js | 71 +++++++++++++---------------- 1 file changed, 32 insertions(+), 39 deletions(-) diff --git a/squarenotsquare/src/realm/dbInit.js b/squarenotsquare/src/realm/dbInit.js index 06ea0d8..2bceb17 100644 --- a/squarenotsquare/src/realm/dbInit.js +++ b/squarenotsquare/src/realm/dbInit.js @@ -1,14 +1,14 @@ import Realm from "realm"; //Entities -import {Score} from './entities/Score'; -import {System} from './entities/System'; -import {User} from './entities/User'; +import {ScoreEntity} from './entities/Score'; +import {SystemEntity} from './entities/System'; +import {UserEntity} from './entities/User'; //Repos -import {ScoreRepo} from './repos/ScoreRepo'; -import {SystemRepo} from './entities/System'; -import {UserRepo} from './entities/User'; +import ScoreRepo from './repos/ScoreRepo'; +import SystemRepo from './repos/SystemRepo'; +import UserRepo from './repos/UserRepo'; //Migrations import { migratev0 } from "./migrations/MigrateV0"; @@ -22,17 +22,16 @@ let systemRepo = null; let scoreRepo = null; let userRepo = null; -export async function initDB(){ +export async function initDB(dbKeyRef = 'squareDB', dbLocation = Realm.defaultPath){ try { - const dbKeyRef = 'squareDB'; let fromStore = await KeyStore.getKey(dbKeyRef); - + if (fromStore === null) { let newKey = generateKey(); await KeyStore.setKey(dbKeyRef, newKey); fromStore = await KeyStore.getKey(dbKeyRef); } - + let dbKey = new Uint8Array(64); if (fromStore !== null){ for (let i = 0; i < 64; ++i){ @@ -42,36 +41,30 @@ export async function initDB(){ let dbRef = null; - if (dbRef === null){ - let schemaList = [ - {schemaVersion: 0, schema: [System, User, Score], migration: migratev0, encryptionKey: dbKey}, - {schemaVersion: 1, schema: [System, User, Score], migration: migratev1, encryptionKey: dbKey} - ] - - let currentSchema = Realm.schemaVersion(Realm.defaultPath, dbKey); - - if (currentSchema === -1) { - currentSchema = 0; - } - - while (currentSchema < schemaList.length) { - if (dbRef !== null) { - dbRef.close(); - } - - dbRef = new Realm(schemaList[currentSchema]); - currentSchema += 1; - } - - if (dbRef === null) { - dbRef = new Realm(schemaList[schemaList.length - 1]); - } - - systemRepo = new SystemRepo(dbRef); - userRepo = new UserRepo(dbRef); - scoreRepo = new ScoreRepo(dbRef) + let schemaList = [ + {schemaVersion: 0, schema: [SystemEntity, UserEntity, ScoreEntity], migration: migratev0, encryptionKey: dbKey}, + {schemaVersion: 1, schema: [SystemEntity, UserEntity, ScoreEntity], migration: migratev1, encryptionKey: dbKey} + ] + + let currentSchema = Realm.schemaVersion(dbLocation, dbKey); + + if (currentSchema === -1) { + currentSchema = 0; } - + + while (currentSchema < schemaList.length) { + if (dbRef !== null) { + dbRef.close(); + } + + dbRef = new Realm(schemaList[currentSchema]); + currentSchema += 1; + } + + systemRepo = new SystemRepo(dbRef); + userRepo = new UserRepo(dbRef); + scoreRepo = new ScoreRepo(dbRef); + return dbRef; } catch (err) { console.log(err);