Browse Source

singleton dbAPI

pull/1/head
Tim Glasgow 2 years ago
parent
commit
5559a0e3dd
  1. 15
      squarenotsquare/__tests__/DB-test.js
  2. 34
      squarenotsquare/src/realm/dbAPI.js
  3. 4
      squarenotsquare/src/realm/dbInit.js
  4. 3
      squarenotsquare/src/redux/actions/SystemActions.js

15
squarenotsquare/__tests__/DB-test.js

@ -1,10 +1,5 @@
import { initDB } from '../src/realm/dbInit'; import { initDB } from '../src/realm/dbInit';
import { import dbAPI from '../src/realm/dbAPI';
getAllSystemValues,
createSystemValue,
deleteSystemValue,
getSystemValue
} from '../src/realm/dbAPI';
test('Realm DB inits', () => { test('Realm DB inits', () => {
return initDB('negTest', 'negTest') return initDB('negTest', 'negTest')
@ -37,7 +32,7 @@ test('Realm DB returns null after exceptions', () => {
test('dbAPI calls system repo getAllSystemValues()', () => { test('dbAPI calls system repo getAllSystemValues()', () => {
return initDB('isNull', 'isNull') return initDB('isNull', 'isNull')
.then( () => { .then( () => {
let returned = getAllSystemValues(); let returned = dbAPI.getAllSystemValues();
expect(returned).toEqual({}); expect(returned).toEqual({});
}); });
}); });
@ -45,7 +40,7 @@ test('dbAPI calls system repo getAllSystemValues()', () => {
test('dbAPI calls system repo createSystemValue()', () => { test('dbAPI calls system repo createSystemValue()', () => {
return initDB('isNull', 'isNull') return initDB('isNull', 'isNull')
.then( () => { .then( () => {
let returned = createSystemValue(); let returned = dbAPI.createSystemValue();
expect(returned).toEqual(true); expect(returned).toEqual(true);
}); });
}); });
@ -53,7 +48,7 @@ test('dbAPI calls system repo createSystemValue()', () => {
test('dbAPI calls system repo deleteSystemValue()', () => { test('dbAPI calls system repo deleteSystemValue()', () => {
return initDB('isNull', 'isNull') return initDB('isNull', 'isNull')
.then( () => { .then( () => {
let returned = deleteSystemValue(); let returned = dbAPI.deleteSystemValue();
expect(returned).toEqual(true); expect(returned).toEqual(true);
}); });
}); });
@ -61,7 +56,7 @@ test('dbAPI calls system repo deleteSystemValue()', () => {
test('dbAPI calls system repo getSystemValue()', () => { test('dbAPI calls system repo getSystemValue()', () => {
return initDB('isNull', 'isNull') return initDB('isNull', 'isNull')
.then( () => { .then( () => {
let returned = getSystemValue('notakey'); let returned = dbAPI.getSystemValue('notakey');
expect(returned.id).toEqual(null); expect(returned.id).toEqual(null);
}); });
}); });

34
squarenotsquare/src/realm/dbAPI.js

@ -1,19 +1,37 @@
import {systemRepo, userRepo, scoreRepo} from './dbInit'; import {initDB} from './dbInit';
export function getAllSystemValues() { class DbAPI {
constructor(){
this.systemRepo = null;
this.scoreRepo = null;
this.userRepo = null;
}
getAllSystemValues() {
return systemRepo.getAllSystemValues(); return systemRepo.getAllSystemValues();
} }
export function createSystemValue(key, value) { createSystemValue(key, value) {
systemRepo.createSystemValue(key, value); systemRepo.createSystemValue(key, value);
return true; return true;
} }
export function deleteSystemValue(key) { deleteSystemValue(key) {
systemRepo.deleteSystemValue(key); systemRepo.deleteSystemValue(key);
return true; return true;
} }
export function getSystemValue(key) { getSystemValue(key) {
return systemRepo.getSystemKeyValue(key); return systemRepo.getSystemKeyValue(key);
}
async initDB() {
let repos = await initDB();
this.systemRepo = repos.systemRepo;
this.scoreRepo = repos.scoreRepo;
this.userRepo = repos.userRepo;
}
} }
const dbAPI = new DbAPI();
export default dbAPI;

4
squarenotsquare/src/realm/dbInit.js

@ -18,10 +18,6 @@ import { migratev1 } from "./migrations/MigrateV1";
import {generateKey} from '../libs/Random'; import {generateKey} from '../libs/Random';
import * as KeyStore from '../services/Keystore'; 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){ export async function initDB(dbKeyRef = 'squareDB', dbLocation = Realm.defaultPath){
try { try {
let fromStore = await KeyStore.getKey(dbKeyRef); let fromStore = await KeyStore.getKey(dbKeyRef);

3
squarenotsquare/src/redux/actions/SystemActions.js

@ -1,9 +1,10 @@
import { initDB } from "../../realm/dbInit"; import { initDB } from "../../realm/dbInit";
import dbAPI from "../../realm/dbAPI";
import { APP_INIT } from "../types/SystemTypes"; import { APP_INIT } from "../types/SystemTypes";
export function appInit() { export function appInit() {
return async (dispatch) => { return async (dispatch) => {
await initDB(); await dbAPI.initDB();
dispatch(onInit()); dispatch(onInit());
} }
} }

Loading…
Cancel
Save