Browse Source

DB init changes

pull/1/head
Tim Glasgow 2 years ago
parent
commit
adc37102c2
  1. 71
      squarenotsquare/src/realm/dbInit.js

71
squarenotsquare/src/realm/dbInit.js

@ -1,14 +1,14 @@
import Realm from "realm"; import Realm from "realm";
//Entities //Entities
import {Score} from './entities/Score'; import {ScoreEntity} from './entities/Score';
import {System} from './entities/System'; import {SystemEntity} from './entities/System';
import {User} from './entities/User'; import {UserEntity} from './entities/User';
//Repos //Repos
import {ScoreRepo} from './repos/ScoreRepo'; import ScoreRepo from './repos/ScoreRepo';
import {SystemRepo} from './entities/System'; import SystemRepo from './repos/SystemRepo';
import {UserRepo} from './entities/User'; import UserRepo from './repos/UserRepo';
//Migrations //Migrations
import { migratev0 } from "./migrations/MigrateV0"; import { migratev0 } from "./migrations/MigrateV0";
@ -22,17 +22,16 @@ let systemRepo = null;
let scoreRepo = null; let scoreRepo = null;
let userRepo = null; let userRepo = null;
export async function initDB(){ export async function initDB(dbKeyRef = 'squareDB', dbLocation = Realm.defaultPath){
try { try {
const dbKeyRef = 'squareDB';
let fromStore = await KeyStore.getKey(dbKeyRef); let fromStore = await KeyStore.getKey(dbKeyRef);
if (fromStore === null) { if (fromStore === null) {
let newKey = generateKey(); let newKey = generateKey();
await KeyStore.setKey(dbKeyRef, newKey); await KeyStore.setKey(dbKeyRef, newKey);
fromStore = await KeyStore.getKey(dbKeyRef); fromStore = await KeyStore.getKey(dbKeyRef);
} }
let dbKey = new Uint8Array(64); let dbKey = new Uint8Array(64);
if (fromStore !== null){ if (fromStore !== null){
for (let i = 0; i < 64; ++i){ for (let i = 0; i < 64; ++i){
@ -42,36 +41,30 @@ export async function initDB(){
let dbRef = null; let dbRef = null;
if (dbRef === null){ let schemaList = [
let schemaList = [ {schemaVersion: 0, schema: [SystemEntity, UserEntity, ScoreEntity], migration: migratev0, encryptionKey: dbKey},
{schemaVersion: 0, schema: [System, User, Score], migration: migratev0, encryptionKey: dbKey}, {schemaVersion: 1, schema: [SystemEntity, UserEntity, ScoreEntity], migration: migratev1, encryptionKey: dbKey}
{schemaVersion: 1, schema: [System, User, Score], migration: migratev1, encryptionKey: dbKey} ]
]
let currentSchema = Realm.schemaVersion(dbLocation, dbKey);
let currentSchema = Realm.schemaVersion(Realm.defaultPath, dbKey);
if (currentSchema === -1) {
if (currentSchema === -1) { currentSchema = 0;
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)
} }
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; return dbRef;
} catch (err) { } catch (err) {
console.log(err); console.log(err);

Loading…
Cancel
Save