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

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() {
dispatch({
type: APP_INIT
//system: system
//user: user
//score: score
});
return (dispatch) => {
dispatch({
type: APP_INIT
//system: system
//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){
switch (action.type) {
case APP_INIT:
SquareNav.navigate('Home');
//SquareNav.navigate('Home');
break;
default:
break;

2
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);
},
);
}

Loading…
Cancel
Save