|
@ -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); |
|
|
let dbKey = new Uint8Array(64); |
|
|
if (fromStore !== null){ |
|
|
if (fromStore !== null){ |
|
|
for (let i = 0; i < 64; ++i){ |
|
|
for (let i = 0; i < 64; ++i){ |
|
|
dbKey[i] = fromStore.charAt(i); |
|
|
dbKey[i] = fromStore.charAt(i); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (realmDB === null){ |
|
|
if (realmDB === null){ |
|
|
let schemaList = [ |
|
|
let schemaList = [ |
|
|
{schemaVersion: 0, schema: [System, User, Score], migration: migrateV0, encryptionKey: dbKey}, |
|
|
{schemaVersion: 0, schema: [System, User, Score], migration: Migrations.migratev0, encryptionKey: dbKey}, |
|
|
{schemaVersion: 1, schema: [System, User, Score], migration: migrateV1, encryptionKey: dbKey} |
|
|
{schemaVersion: 1, schema: [System, User, Score], migration: Migrations.migratev1, encryptionKey: dbKey} |
|
|
] |
|
|
] |
|
|
|
|
|
|
|
|
let currentSchema = Realm.schemaVersion(Realm.defaultPath, dbKey); |
|
|
let currentSchema = Realm.schemaVersion(Realm.defaultPath, dbKey); |
|
|
|
|
|
|
|
|
if (currentSchema === -1) { |
|
|
if (currentSchema === -1) { |
|
|
currentSchema = 0; |
|
|
currentSchema = 0; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
while (currentSchema < schemaList.length) { |
|
|
|
|
|
if (realmDB !== null) { |
|
|
|
|
|
realmDB.close(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
while (currentSchema < schemaList.length) { |
|
|
realmDB = new Realm(schemaList[currentSchema]); |
|
|
if (realmDB !== null) { |
|
|
currentSchema += 1; |
|
|
realmDB.close(); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
realmDB = new Realm(schemaList[currentSchema]); |
|
|
if (realmDB === null) { |
|
|
currentSchema += 1; |
|
|
realmDB = new Realm(schemaList[schemaList.length - 1]); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (realmDB === null) { |
|
|
systemRepo = new SystemRepo(realmDB); |
|
|
realmDB = new Realm(schemaList[schemaList.length - 1]); |
|
|
userRepo = new UserRepo(realmDB); |
|
|
|
|
|
scoreRepo = new ScoreRepo(realmDB) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
systemRepo = new SystemRepo(realmDB); |
|
|
return realmDB; |
|
|
userRepo = new UserRepo(realmDB); |
|
|
} catch (err) { |
|
|
scoreRepo = new ScoreRepo(realmDB) |
|
|
console.log(err); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return realmDB; |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |