diff --git a/squarenotsquare/__tests__/DB-test.js b/squarenotsquare/__tests__/DB-test.js index 19e2b2e..09c6dbe 100644 --- a/squarenotsquare/__tests__/DB-test.js +++ b/squarenotsquare/__tests__/DB-test.js @@ -1,10 +1,5 @@ import { initDB } from '../src/realm/dbInit'; -import { - getAllSystemValues, - createSystemValue, - deleteSystemValue, - getSystemValue - } from '../src/realm/dbAPI'; +import dbAPI from '../src/realm/dbAPI'; test('Realm DB inits', () => { return initDB('negTest', 'negTest') @@ -37,7 +32,7 @@ test('Realm DB returns null after exceptions', () => { test('dbAPI calls system repo getAllSystemValues()', () => { return initDB('isNull', 'isNull') .then( () => { - let returned = getAllSystemValues(); + let returned = dbAPI.getAllSystemValues(); expect(returned).toEqual({}); }); }); @@ -45,7 +40,7 @@ test('dbAPI calls system repo getAllSystemValues()', () => { test('dbAPI calls system repo createSystemValue()', () => { return initDB('isNull', 'isNull') .then( () => { - let returned = createSystemValue(); + let returned = dbAPI.createSystemValue(); expect(returned).toEqual(true); }); }); @@ -53,7 +48,7 @@ test('dbAPI calls system repo createSystemValue()', () => { test('dbAPI calls system repo deleteSystemValue()', () => { return initDB('isNull', 'isNull') .then( () => { - let returned = deleteSystemValue(); + let returned = dbAPI.deleteSystemValue(); expect(returned).toEqual(true); }); }); @@ -61,7 +56,7 @@ test('dbAPI calls system repo deleteSystemValue()', () => { test('dbAPI calls system repo getSystemValue()', () => { return initDB('isNull', 'isNull') .then( () => { - let returned = getSystemValue('notakey'); + let returned = dbAPI.getSystemValue('notakey'); expect(returned.id).toEqual(null); }); }); \ No newline at end of file diff --git a/squarenotsquare/src/realm/dbAPI.js b/squarenotsquare/src/realm/dbAPI.js index 7be9996..91a398f 100644 --- a/squarenotsquare/src/realm/dbAPI.js +++ b/squarenotsquare/src/realm/dbAPI.js @@ -1,19 +1,37 @@ -import {systemRepo, userRepo, scoreRepo} from './dbInit'; +import {initDB} from './dbInit'; -export function getAllSystemValues() { - return systemRepo.getAllSystemValues(); -} +class DbAPI { + constructor(){ + this.systemRepo = null; + this.scoreRepo = null; + this.userRepo = null; + } + + getAllSystemValues() { + return systemRepo.getAllSystemValues(); + } + + createSystemValue(key, value) { + systemRepo.createSystemValue(key, value); + return true; + } -export function createSystemValue(key, value) { - systemRepo.createSystemValue(key, value); - return true; -} + deleteSystemValue(key) { + systemRepo.deleteSystemValue(key); + return true; + } + + getSystemValue(key) { + return systemRepo.getSystemKeyValue(key); + } -export function deleteSystemValue(key) { - systemRepo.deleteSystemValue(key); - return true; + async initDB() { + let repos = await initDB(); + this.systemRepo = repos.systemRepo; + this.scoreRepo = repos.scoreRepo; + this.userRepo = repos.userRepo; + } } -export function getSystemValue(key) { - return systemRepo.getSystemKeyValue(key); -} \ No newline at end of file +const dbAPI = new DbAPI(); +export default dbAPI; \ No newline at end of file diff --git a/squarenotsquare/src/realm/dbInit.js b/squarenotsquare/src/realm/dbInit.js index a372780..d0ceb7a 100644 --- a/squarenotsquare/src/realm/dbInit.js +++ b/squarenotsquare/src/realm/dbInit.js @@ -18,10 +18,6 @@ import { migratev1 } from "./migrations/MigrateV1"; import {generateKey} from '../libs/Random'; import * as KeyStore from '../services/Keystore'; -export let systemRepo = null; -export let scoreRepo = null; -export let userRepo = null; - export async function initDB(dbKeyRef = 'squareDB', dbLocation = Realm.defaultPath){ try { let fromStore = await KeyStore.getKey(dbKeyRef); diff --git a/squarenotsquare/src/redux/actions/SystemActions.js b/squarenotsquare/src/redux/actions/SystemActions.js index b334d36..a6108a7 100644 --- a/squarenotsquare/src/redux/actions/SystemActions.js +++ b/squarenotsquare/src/redux/actions/SystemActions.js @@ -1,9 +1,10 @@ import { initDB } from "../../realm/dbInit"; +import dbAPI from "../../realm/dbAPI"; import { APP_INIT } from "../types/SystemTypes"; export function appInit() { return async (dispatch) => { - await initDB(); + await dbAPI.initDB(); dispatch(onInit()); } }