|
|
@ -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,16 +24,17 @@ let userRepo = null; |
|
|
|
let realmDB = null; |
|
|
|
|
|
|
|
export async function initDB(){ |
|
|
|
try { |
|
|
|
const dbKeyRef = 'squareDB'; |
|
|
|
let fromStore = await KeyStore.getKey(dbKeyRef); |
|
|
|
|
|
|
|
if (fromStore === null) { |
|
|
|
let newKey = generateKey(); |
|
|
|
await KeyStore.setKey(newKey, dbKeyRef); |
|
|
|
await KeyStore.setKey(dbKeyRef, newKey); |
|
|
|
fromStore = await KeyStore.getKey(dbKeyRef); |
|
|
|
} |
|
|
|
|
|
|
|
let dbKey = Uint8Array(64); |
|
|
|
let dbKey = new Uint8Array(64); |
|
|
|
if (fromStore !== null){ |
|
|
|
for (let i = 0; i < 64; ++i){ |
|
|
|
dbKey[i] = fromStore.charAt(i); |
|
|
@ -43,8 +43,8 @@ export async function initDB(){ |
|
|
|
|
|
|
|
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} |
|
|
|
{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); |
|
|
@ -72,5 +72,7 @@ export async function initDB(){ |
|
|
|
} |
|
|
|
|
|
|
|
return realmDB; |
|
|
|
|
|
|
|
} catch (err) { |
|
|
|
console.log(err); |
|
|
|
} |
|
|
|
} |