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";
//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);

Loading…
Cancel
Save