Browse Source

singleton dbAPI

pull/1/head
Tim Glasgow 2 years ago
parent
commit
5559a0e3dd
  1. 15
      squarenotsquare/__tests__/DB-test.js
  2. 46
      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 {
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);
});
});

46
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);
}
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 * 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);

3
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());
}
}

Loading…
Cancel
Save