Browse Source

syntax

pull/1/head
Tim Glasgow 2 years ago
parent
commit
bacb5ee266
  1. 90
      squarenotsquare/realm/dbInit.js
  2. 16
      squarenotsquare/realm/migrations/Migrations.js
  3. 14
      squarenotsquare/redux/actions/SystemActions.js
  4. 2
      squarenotsquare/redux/reducers/NavReducer.js
  5. 2
      squarenotsquare/services/Keystore.js

90
squarenotsquare/realm/dbInit.js

@ -11,11 +11,10 @@ import {SystemRepo} from './entities/System';
import {UserRepo} from './entities/User'; import {UserRepo} from './entities/User';
//Migrations //Migrations
import {migrateV0} from './migrations/v0'; import * as Migrations from './migrations/Migrations';
import {migrateV1} from './migrations/v1';
//keys //keys
import { generateKey } from "../libs/Random"; import {generateKey} from '../libs/Random';
import * as KeyStore from '../services/Keystore'; import * as KeyStore from '../services/Keystore';
let systemRepo = null; let systemRepo = null;
@ -25,52 +24,55 @@ let userRepo = null;
let realmDB = null; let realmDB = null;
export async function initDB(){ export async function initDB(){
const dbKeyRef = 'squareDB'; try {
let fromStore = await KeyStore.getKey(dbKeyRef); const dbKeyRef = 'squareDB';
let fromStore = await KeyStore.getKey(dbKeyRef);
if (fromStore === null) {
let newKey = generateKey(); if (fromStore === null) {
await KeyStore.setKey(newKey, dbKeyRef); let newKey = generateKey();
fromStore = await KeyStore.getKey(dbKeyRef); await KeyStore.setKey(dbKeyRef, newKey);
} 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;
} }
while (currentSchema < schemaList.length) { let dbKey = new Uint8Array(64);
if (realmDB !== null) { if (fromStore !== null){
realmDB.close(); for (let i = 0; i < 64; ++i){
dbKey[i] = fromStore.charAt(i);
} }
realmDB = new Realm(schemaList[currentSchema]);
currentSchema += 1;
} }
if (realmDB === null) { if (realmDB === null){
realmDB = new Realm(schemaList[schemaList.length - 1]); let schemaList = [
} {schemaVersion: 0, schema: [System, User, Score], migration: Migrations.migratev0, encryptionKey: dbKey},
{schemaVersion: 1, schema: [System, User, Score], migration: Migrations.migratev1, encryptionKey: dbKey}
systemRepo = new SystemRepo(realmDB); ]
userRepo = new UserRepo(realmDB);
scoreRepo = new ScoreRepo(realmDB) 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);
}
} }

16
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'
});
}
}

14
squarenotsquare/redux/actions/SystemActions.js

@ -10,10 +10,12 @@ export function appInit() {
} }
function onInit() { function onInit() {
dispatch({ return (dispatch) => {
type: APP_INIT dispatch({
//system: system type: APP_INIT
//user: user //system: system
//score: score //user: user
}); //score: score
});
}
} }

2
squarenotsquare/redux/reducers/NavReducer.js

@ -5,7 +5,7 @@ import { APP_INIT } from '../types/SystemTypes';
export function navReducer(state = {}, action){ export function navReducer(state = {}, action){
switch (action.type) { switch (action.type) {
case APP_INIT: case APP_INIT:
SquareNav.navigate('Home'); //SquareNav.navigate('Home');
break; break;
default: default:
break; break;

2
squarenotsquare/services/Keystore.js

@ -6,7 +6,7 @@ export async function removeKey(key) {
console.log('Removed key ' + res); console.log('Removed key ' + res);
}, },
(err) => { (err) => {
console.tron(err.message); console.log(err.message);
}, },
); );
} }

Loading…
Cancel
Save