|
|
@ -1,14 +1,14 @@ |
|
|
|
import Realm from "realm"; |
|
|
|
|
|
|
|
//Entities
|
|
|
|
import {Score} from './entities/Score'; |
|
|
|
import {System} from './entities/System'; |
|
|
|
import {User} from './entities/User'; |
|
|
|
import {ScoreEntity} from './entities/Score'; |
|
|
|
import {SystemEntity} from './entities/System'; |
|
|
|
import {UserEntity} from './entities/User'; |
|
|
|
|
|
|
|
//Repos
|
|
|
|
import {ScoreRepo} from './repos/ScoreRepo'; |
|
|
|
import {SystemRepo} from './entities/System'; |
|
|
|
import {UserRepo} from './entities/User'; |
|
|
|
import ScoreRepo from './repos/ScoreRepo'; |
|
|
|
import SystemRepo from './repos/SystemRepo'; |
|
|
|
import UserRepo from './repos/UserRepo'; |
|
|
|
|
|
|
|
//Migrations
|
|
|
|
import { migratev0 } from "./migrations/MigrateV0"; |
|
|
@ -22,9 +22,8 @@ let systemRepo = null; |
|
|
|
let scoreRepo = null; |
|
|
|
let userRepo = null; |
|
|
|
|
|
|
|
export async function initDB(){ |
|
|
|
export async function initDB(dbKeyRef = 'squareDB', dbLocation = Realm.defaultPath){ |
|
|
|
try { |
|
|
|
const dbKeyRef = 'squareDB'; |
|
|
|
let fromStore = await KeyStore.getKey(dbKeyRef); |
|
|
|
|
|
|
|
if (fromStore === null) { |
|
|
@ -42,13 +41,12 @@ export async function initDB(){ |
|
|
|
|
|
|
|
let dbRef = null; |
|
|
|
|
|
|
|
if (dbRef === 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: [SystemEntity, UserEntity, ScoreEntity], migration: migratev0, encryptionKey: dbKey}, |
|
|
|
{schemaVersion: 1, schema: [SystemEntity, UserEntity, ScoreEntity], migration: migratev1, encryptionKey: dbKey} |
|
|
|
] |
|
|
|
|
|
|
|
let currentSchema = Realm.schemaVersion(Realm.defaultPath, dbKey); |
|
|
|
let currentSchema = Realm.schemaVersion(dbLocation, dbKey); |
|
|
|
|
|
|
|
if (currentSchema === -1) { |
|
|
|
currentSchema = 0; |
|
|
@ -63,14 +61,9 @@ export async function initDB(){ |
|
|
|
currentSchema += 1; |
|
|
|
} |
|
|
|
|
|
|
|
if (dbRef === null) { |
|
|
|
dbRef = new Realm(schemaList[schemaList.length - 1]); |
|
|
|
} |
|
|
|
|
|
|
|
systemRepo = new SystemRepo(dbRef); |
|
|
|
userRepo = new UserRepo(dbRef); |
|
|
|
scoreRepo = new ScoreRepo(dbRef) |
|
|
|
} |
|
|
|
scoreRepo = new ScoreRepo(dbRef); |
|
|
|
|
|
|
|
return dbRef; |
|
|
|
} catch (err) { |
|
|
|