From bacb5ee2668ff9518ed176145127e49a30debd91 Mon Sep 17 00:00:00 2001 From: Tim Glasgow Date: Sun, 24 Jul 2022 17:16:38 -0500 Subject: [PATCH] syntax --- squarenotsquare/realm/dbInit.js | 90 ++++++++++--------- .../realm/migrations/Migrations.js | 16 ++++ .../redux/actions/SystemActions.js | 14 +-- squarenotsquare/redux/reducers/NavReducer.js | 2 +- squarenotsquare/services/Keystore.js | 2 +- 5 files changed, 72 insertions(+), 52 deletions(-) create mode 100644 squarenotsquare/realm/migrations/Migrations.js diff --git a/squarenotsquare/realm/dbInit.js b/squarenotsquare/realm/dbInit.js index 4ce8b06..8395352 100644 --- a/squarenotsquare/realm/dbInit.js +++ b/squarenotsquare/realm/dbInit.js @@ -11,11 +11,10 @@ import {SystemRepo} from './entities/System'; import {UserRepo} from './entities/User'; //Migrations -import {migrateV0} from './migrations/v0'; -import {migrateV1} from './migrations/v1'; +import * as Migrations from './migrations/Migrations'; //keys -import { generateKey } from "../libs/Random"; +import {generateKey} from '../libs/Random'; import * as KeyStore from '../services/Keystore'; let systemRepo = null; @@ -25,52 +24,55 @@ let userRepo = null; let realmDB = null; export async function initDB(){ - const dbKeyRef = 'squareDB'; - let fromStore = await KeyStore.getKey(dbKeyRef); - - if (fromStore === null) { - let newKey = generateKey(); - await KeyStore.setKey(newKey, dbKeyRef); - fromStore = await KeyStore.getKey(dbKeyRef); - } - - let dbKey = Uint8Array(64); - if (fromStore !== null){ - for (let i = 0; i < 64; ++i){ - dbKey[i] = fromStore.charAt(i); - } - } - - if (realmDB === 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; + 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); } - while (currentSchema < schemaList.length) { - if (realmDB !== null) { - realmDB.close(); + let dbKey = new Uint8Array(64); + if (fromStore !== null){ + for (let i = 0; i < 64; ++i){ + dbKey[i] = fromStore.charAt(i); } - - realmDB = new Realm(schemaList[currentSchema]); - currentSchema += 1; } - if (realmDB === null) { - realmDB = new Realm(schemaList[schemaList.length - 1]); - } - - systemRepo = new SystemRepo(realmDB); - userRepo = new UserRepo(realmDB); - scoreRepo = new ScoreRepo(realmDB) - } + if (realmDB === null){ + let schemaList = [ + {schemaVersion: 0, schema: [System, User, Score], migration: Migrations.migratev0, encryptionKey: dbKey}, + {schemaVersion: 1, schema: [System, User, Score], migration: Migrations.migratev1, encryptionKey: dbKey} + ] + + let currentSchema = Realm.schemaVersion(Realm.defaultPath, dbKey); + + if (currentSchema === -1) { + currentSchema = 0; + } + + while (currentSchema < schemaList.length) { + if (realmDB !== null) { + realmDB.close(); + } - return realmDB; + realmDB = new Realm(schemaList[currentSchema]); + currentSchema += 1; + } + + if (realmDB === null) { + realmDB = new Realm(schemaList[schemaList.length - 1]); + } + systemRepo = new SystemRepo(realmDB); + userRepo = new UserRepo(realmDB); + scoreRepo = new ScoreRepo(realmDB) + } + + return realmDB; + } catch (err) { + console.log(err); + } } \ No newline at end of file diff --git a/squarenotsquare/realm/migrations/Migrations.js b/squarenotsquare/realm/migrations/Migrations.js new file mode 100644 index 0000000..4f875c7 --- /dev/null +++ b/squarenotsquare/realm/migrations/Migrations.js @@ -0,0 +1,16 @@ +import {System} from '../entities/System'; +import uuid from 'uuid'; + +export function migratev0(oldRealm, newRealm) { + //start with empty db +} + +export function migratev1(oldRealm, newRealm) { + if (oldRealm.schemaVersion === 0) { + newRealm.create(System.name, { + id: uuid.v4(), + key: 'username', + value: 'changeme' + }); + } +} diff --git a/squarenotsquare/redux/actions/SystemActions.js b/squarenotsquare/redux/actions/SystemActions.js index ff3c838..5793adc 100644 --- a/squarenotsquare/redux/actions/SystemActions.js +++ b/squarenotsquare/redux/actions/SystemActions.js @@ -10,10 +10,12 @@ export function appInit() { } function onInit() { - dispatch({ - type: APP_INIT - //system: system - //user: user - //score: score - }); + return (dispatch) => { + dispatch({ + type: APP_INIT + //system: system + //user: user + //score: score + }); + } } \ No newline at end of file diff --git a/squarenotsquare/redux/reducers/NavReducer.js b/squarenotsquare/redux/reducers/NavReducer.js index 3be4a9a..7e204c1 100644 --- a/squarenotsquare/redux/reducers/NavReducer.js +++ b/squarenotsquare/redux/reducers/NavReducer.js @@ -5,7 +5,7 @@ import { APP_INIT } from '../types/SystemTypes'; export function navReducer(state = {}, action){ switch (action.type) { case APP_INIT: - SquareNav.navigate('Home'); + //SquareNav.navigate('Home'); break; default: break; diff --git a/squarenotsquare/services/Keystore.js b/squarenotsquare/services/Keystore.js index 3e5aa04..3ee94cc 100644 --- a/squarenotsquare/services/Keystore.js +++ b/squarenotsquare/services/Keystore.js @@ -6,7 +6,7 @@ export async function removeKey(key) { console.log('Removed key ' + res); }, (err) => { - console.tron(err.message); + console.log(err.message); }, ); }