40 changed files with 664 additions and 659 deletions
@ -1,3 +1,3 @@ |
|||||
export async function initDB() { |
export async function initDB() { |
||||
return null; |
return null; |
||||
} |
} |
@ -1,135 +1,135 @@ |
|||||
// https://github.com/realm/realm-js/issues/370#issuecomment-270849466
|
// https://github.com/realm/realm-js/issues/370#issuecomment-270849466
|
||||
export default class MockRealm { |
export default class MockRealm { |
||||
constructor(params) { |
constructor(params) { |
||||
this.schema = {}; |
this.schema = {}; |
||||
this.schemaVersion = params.schemaVersion; |
this.schemaVersion = params.schemaVersion; |
||||
this.callbackList = []; |
this.callbackList = []; |
||||
this.data = {}; |
this.data = {}; |
||||
this.schemaCallbackList = {}; |
this.schemaCallbackList = {}; |
||||
params.schema.forEach((schema) => { |
params.schema.forEach((schema) => { |
||||
this.data[schema.name] = {}; |
this.data[schema.name] = {}; |
||||
}); |
}); |
||||
params.schema.forEach((schema) => { |
params.schema.forEach((schema) => { |
||||
this.schema[schema.name] = schema; |
this.schema[schema.name] = schema; |
||||
}); |
}); |
||||
this.lastLookedUpModel = null; |
this.lastLookedUpModel = null; |
||||
} |
} |
||||
|
|
||||
objects(schemaName) { |
objects(schemaName) { |
||||
this.lastLookedUpModel = schemaName; |
this.lastLookedUpModel = schemaName; |
||||
const objects = Object.values(this.data[schemaName]); |
const objects = Object.values(this.data[schemaName]); |
||||
objects.values = () => objects; |
objects.values = () => objects; |
||||
objects.sorted = () => this.compareFunc ? objects.sort(this.compareFunc) : objects.sort(); |
objects.sorted = () => this.compareFunc ? objects.sort(this.compareFunc) : objects.sort(); |
||||
objects.addListener = (cb) => { |
objects.addListener = (cb) => { |
||||
if (this.schemaCallbackList[schemaName]) { |
if (this.schemaCallbackList[schemaName]) { |
||||
this.schemaCallbackList[schemaName].push(cb); |
this.schemaCallbackList[schemaName].push(cb); |
||||
} else { |
} else { |
||||
this.schemaCallbackList[schemaName] = [cb]; |
this.schemaCallbackList[schemaName] = [cb]; |
||||
} |
|
||||
}; |
|
||||
objects.removeListener = () => {}; |
|
||||
objects.filtered = this.filtered ? this.filtered.bind(this, schemaName) : () => objects; |
|
||||
return objects; |
|
||||
} |
} |
||||
|
}; |
||||
|
objects.removeListener = () => {}; |
||||
|
objects.filtered = this.filtered ? this.filtered.bind(this, schemaName) : () => objects; |
||||
|
return objects; |
||||
|
} |
||||
|
|
||||
write(fn) { |
write(fn) { |
||||
this.writing = true; |
this.writing = true; |
||||
fn(); |
fn(); |
||||
this.writing = false; |
this.writing = false; |
||||
} |
} |
||||
|
|
||||
create(schemaName, object) { |
create(schemaName, object) { |
||||
const modelObject = object; |
const modelObject = object; |
||||
const properties = this.schema[schemaName].schema.properties; |
const properties = this.schema[schemaName].schema.properties; |
||||
Object.keys(properties).forEach((key) => { |
Object.keys(properties).forEach((key) => { |
||||
if (modelObject[key] && modelObject[key].model) { |
if (modelObject[key] && modelObject[key].model) { |
||||
this.data[modelObject[key].model][modelObject[key].id] = this.create( |
this.data[modelObject[key].model][modelObject[key].id] = this.create( |
||||
modelObject[key].model, modelObject[key], |
modelObject[key].model, modelObject[key], |
||||
); |
); |
||||
} else if (modelObject[key] && modelObject[key].length && modelObject[key][0].model) { |
} else if (modelObject[key] && modelObject[key].length && modelObject[key][0].model) { |
||||
modelObject[key].forEach((obj) => { |
modelObject[key].forEach((obj) => { |
||||
this.data[modelObject[key][0].model][obj.id] = obj; |
this.data[modelObject[key][0].model][obj.id] = obj; |
||||
}); |
|
||||
modelObject[key].filtered = this.filtered ? this.filtered : () => modelObject[key]; |
|
||||
modelObject[key].sorted = () => modelObject[key].sort(); |
|
||||
} else if (modelObject[key] === undefined) { |
|
||||
if (typeof properties[key] === 'object' && properties[key].optional) { |
|
||||
modelObject[key] = null; |
|
||||
} |
|
||||
if (typeof properties[key] === 'object' && ['list', 'linkingObjects'].includes(properties[key].type)) { |
|
||||
modelObject[key] = []; |
|
||||
modelObject[key].filtered = () => []; |
|
||||
modelObject[key].sorted = () => []; |
|
||||
} |
|
||||
} |
|
||||
}); |
}); |
||||
|
modelObject[key].filtered = this.filtered ? this.filtered : () => modelObject[key]; |
||||
this.data[schemaName][modelObject.id] = modelObject; |
modelObject[key].sorted = () => modelObject[key].sort(); |
||||
if (this.writing) { |
} else if (modelObject[key] === undefined) { |
||||
if (this.schemaCallbackList[schemaName]) { |
if (typeof properties[key] === 'object' && properties[key].optional) { |
||||
this.schemaCallbackList[schemaName].forEach(cb => cb(schemaName, { |
modelObject[key] = null; |
||||
insertions: { length: 1 }, |
} |
||||
modifications: { length: 0 }, |
if (typeof properties[key] === 'object' && ['list', 'linkingObjects'].includes(properties[key].type)) { |
||||
deletions: { length: 0 }, |
modelObject[key] = []; |
||||
})); |
modelObject[key].filtered = () => []; |
||||
} |
modelObject[key].sorted = () => []; |
||||
this.callbackList.forEach((cb) => { cb(); }); |
|
||||
} |
} |
||||
return modelObject; |
|
||||
} |
} |
||||
|
}); |
||||
|
|
||||
objectForPrimaryKey(model, id) { |
this.data[schemaName][modelObject.id] = modelObject; |
||||
this.lastLookedUpModel = model; |
if (this.writing) { |
||||
return this.data[model][id]; |
if (this.schemaCallbackList[schemaName]) { |
||||
|
this.schemaCallbackList[schemaName].forEach(cb => cb(schemaName, { |
||||
|
insertions: { length: 1 }, |
||||
|
modifications: { length: 0 }, |
||||
|
deletions: { length: 0 }, |
||||
|
})); |
||||
} |
} |
||||
|
this.callbackList.forEach((cb) => { cb(); }); |
||||
delete(object) { |
|
||||
if (this.lastLookedUpModel || object.model) { |
|
||||
const model = object.model ? object.model : this.lastLookedUpModel |
|
||||
if (Array.isArray(object)) { |
|
||||
object.forEach((item) => { |
|
||||
delete this.data[model][item.id]; |
|
||||
}); |
|
||||
} |
|
||||
delete this.data[model][object.id]; |
|
||||
if (this.writing) { |
|
||||
if (this.schemaCallbackList[model]) { |
|
||||
this.schemaCallbackList[model].forEach(cb => cb(model, { |
|
||||
insertions: { length: 0 }, |
|
||||
modifications: { length: 0 }, |
|
||||
deletions: { length: 1 }, |
|
||||
})); |
|
||||
} |
|
||||
this.callbackList.forEach((cb) => { cb(); }); |
|
||||
} |
|
||||
} |
|
||||
} |
} |
||||
|
return modelObject; |
||||
|
} |
||||
|
|
||||
deleteAll() { |
objectForPrimaryKey(model, id) { |
||||
Object.keys(this.schema).forEach((key) => { |
this.lastLookedUpModel = model; |
||||
if (this.writing && this.schemaCallbackList[this.schema[key].name]) { |
return this.data[model][id]; |
||||
this.schemaCallbackList[this.schema[key].name].forEach(cb => cb(key, { |
} |
||||
insertions: { length: 0 }, |
|
||||
modifications: { length: 0 }, |
delete(object) { |
||||
deletions: { length: Object.values(this.data[this.schema[key].name]).length }, |
if (this.lastLookedUpModel || object.model) { |
||||
})); |
const model = object.model ? object.model : this.lastLookedUpModel |
||||
} |
if (Array.isArray(object)) { |
||||
this.data[this.schema[key].name] = {}; |
object.forEach((item) => { |
||||
|
delete this.data[model][item.id]; |
||||
}); |
}); |
||||
if (this.writing) this.callbackList.forEach((cb) => { cb(); }); |
|
||||
} |
} |
||||
|
delete this.data[model][object.id]; |
||||
addListener(event, callback) { |
if (this.writing) { |
||||
this.callbackList.push(callback); |
if (this.schemaCallbackList[model]) { |
||||
|
this.schemaCallbackList[model].forEach(cb => cb(model, { |
||||
|
insertions: { length: 0 }, |
||||
|
modifications: { length: 0 }, |
||||
|
deletions: { length: 1 }, |
||||
|
})); |
||||
|
} |
||||
|
this.callbackList.forEach((cb) => { cb(); }); |
||||
|
} |
||||
} |
} |
||||
|
} |
||||
|
|
||||
prepareData(schemaName, objects) { |
deleteAll() { |
||||
objects.forEach((object) => { |
Object.keys(this.schema).forEach((key) => { |
||||
this.create(schemaName, object); |
if (this.writing && this.schemaCallbackList[this.schema[key].name]) { |
||||
}); |
this.schemaCallbackList[this.schema[key].name].forEach(cb => cb(key, { |
||||
|
insertions: { length: 0 }, |
||||
|
modifications: { length: 0 }, |
||||
|
deletions: { length: Object.values(this.data[this.schema[key].name]).length }, |
||||
|
})); |
||||
} |
} |
||||
|
this.data[this.schema[key].name] = {}; |
||||
|
}); |
||||
|
if (this.writing) this.callbackList.forEach((cb) => { cb(); }); |
||||
|
} |
||||
|
|
||||
|
addListener(event, callback) { |
||||
|
this.callbackList.push(callback); |
||||
|
} |
||||
|
|
||||
|
prepareData(schemaName, objects) { |
||||
|
objects.forEach((object) => { |
||||
|
this.create(schemaName, object); |
||||
|
}); |
||||
|
} |
||||
} |
} |
||||
|
|
||||
MockRealm.Object = class Object { |
MockRealm.Object = class Object { |
||||
isValid() { return true; } |
isValid() { return true; } |
||||
}; |
}; |
@ -0,0 +1,7 @@ |
|||||
|
import { initDB } from "../src/realm/dbInit"; |
||||
|
|
||||
|
test('Realm DB inits', async () => { |
||||
|
let realmDB = await initDB(); |
||||
|
|
||||
|
expect |
||||
|
}); |
@ -1,16 +1,16 @@ |
|||||
module.exports = { |
module.exports = { |
||||
// setupFiles: ['./jestSetup.js'],
|
// setupFiles: ['./jestSetup.js'],
|
||||
preset: 'react-native', |
preset: 'react-native', |
||||
verbose: true, |
verbose: true, |
||||
setupFiles: ['./node_modules/react-native-gesture-handler/jestSetup.js'], |
setupFiles: ['./node_modules/react-native-gesture-handler/jestSetup.js'], |
||||
moduleDirectories: ['node_modules', 'src'], |
moduleDirectories: ['node_modules', 'src'], |
||||
moduleNameMapper: { |
moduleNameMapper: { |
||||
// '@react-native-firebase/messaging: '<rootDir>/__mock__/mockFirebase.js',
|
// '@react-native-firebase/messaging: '<rootDir>/__mock__/mockFirebase.js',
|
||||
'../services/Keystore': '<rootDir>/__mock__/mockKeyStore.js', |
'../services/Keystore': '<rootDir>/__mock__/mockKeyStore.js', |
||||
'\\.(jpg|ico|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': '<rootDir>/__mock__/file.js' |
'\\.(jpg|ico|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': '<rootDir>/__mock__/file.js' |
||||
}, |
}, |
||||
transform: { |
transform: { |
||||
'^.+\\.(ts|tsx)?$': 'ts-jest', |
'^.+\\.(ts|tsx)?$': 'ts-jest', |
||||
'^.+\\.(js|jsx)$': 'babel-jest', |
'^.+\\.(js|jsx)$': 'babel-jest', |
||||
} |
} |
||||
} |
} |
@ -1,13 +1,13 @@ |
|||||
export function generateKey() { |
export function generateKey() { |
||||
let result = []; |
let result = []; |
||||
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; |
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; |
||||
let charactersLength = characters.length; |
let charactersLength = characters.length; |
||||
|
|
||||
for (let i = 0; i < 64; i++) { |
for (let i = 0; i < 64; i++) { |
||||
result.push( |
result.push( |
||||
characters.charAt(Math.floor(Math.random() * charactersLength)), |
characters.charAt(Math.floor(Math.random() * charactersLength)), |
||||
); |
); |
||||
} |
} |
||||
|
|
||||
return result.join(''); |
return result.join(''); |
||||
} |
} |
@ -1,29 +1,29 @@ |
|||||
import {systemRepo, userRepo, scoreRepo} from './dbInit'; |
import {systemRepo, userRepo, scoreRepo} from './dbInit'; |
||||
|
|
||||
class realmAPI { |
class realmAPI { |
||||
constructor(){ |
constructor(){ |
||||
if (realmDB === null) { |
if (realmDB === null) { |
||||
realmDB = this; |
realmDB = this; |
||||
} |
|
||||
|
|
||||
return realmDB; |
|
||||
} |
} |
||||
|
|
||||
getAllSystemValues() { |
return realmDB; |
||||
return systemRepo.getAllSystemValues(); |
} |
||||
} |
|
||||
|
|
||||
createSystemValue(key, value) { |
getAllSystemValues() { |
||||
systemRepo.createSystemValue(key, value); |
return systemRepo.getAllSystemValues(); |
||||
} |
} |
||||
|
|
||||
deleteSystemValue(key) { |
createSystemValue(key, value) { |
||||
systemRepo.deleteSystemValue(key); |
systemRepo.createSystemValue(key, value); |
||||
} |
} |
||||
|
|
||||
getSystemValue(key) { |
deleteSystemValue(key) { |
||||
return systemRepo.getSystemKeyValue(key); |
systemRepo.deleteSystemValue(key); |
||||
} |
} |
||||
|
|
||||
|
getSystemValue(key) { |
||||
|
return systemRepo.getSystemKeyValue(key); |
||||
|
} |
||||
} |
} |
||||
|
|
||||
export let realmDB = new realmAPI(); |
export let realmDB = new realmAPI(); |
@ -1,9 +1,9 @@ |
|||||
export const Score = { |
export const Score = { |
||||
name: 'Score', |
name: 'Score', |
||||
primaryKey: 'id', |
primaryKey: 'id', |
||||
properties: { |
properties: { |
||||
id: 'string', |
id: 'string', |
||||
user: 'string', |
user: 'string', |
||||
value: 'string' |
value: 'string' |
||||
} |
} |
||||
}; |
}; |
@ -1,9 +1,9 @@ |
|||||
export const System = { |
export const System = { |
||||
name: 'System', |
name: 'System', |
||||
primaryKey: 'id', |
primaryKey: 'id', |
||||
properties: { |
properties: { |
||||
id: 'string', |
id: 'string', |
||||
key: 'string', |
key: 'string', |
||||
value: 'string' |
value: 'string' |
||||
} |
} |
||||
}; |
}; |
@ -1,8 +1,8 @@ |
|||||
export const User = { |
export const User = { |
||||
name: 'User', |
name: 'User', |
||||
primaryKey: 'id', |
primaryKey: 'id', |
||||
properties: { |
properties: { |
||||
id: 'string', |
id: 'string', |
||||
name: 'string' |
name: 'string' |
||||
} |
} |
||||
} |
} |
@ -1,3 +1,3 @@ |
|||||
export function migratev0(oldRealm, newRealm) { |
export function migratev0(oldRealm, newRealm) { |
||||
Function.prototype |
Function.prototype |
||||
} |
} |
@ -1,14 +1,14 @@ |
|||||
import { APP_INIT } from "../types/SystemTypes"; |
import { APP_INIT } from "../types/SystemTypes"; |
||||
|
|
||||
function sys(state = {}, action) { |
function sys(state = {}, action) { |
||||
switch (action.type) { |
switch (action.type) { |
||||
case APP_INIT: |
case APP_INIT: |
||||
return {...state, ...action.system}; |
return {...state, ...action.system}; |
||||
default: |
default: |
||||
return state; |
return state; |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
export const systemReducer = { |
export const systemReducer = { |
||||
system: sys |
system: sys |
||||
}; |
}; |
@ -1,31 +1,31 @@ |
|||||
export const material = { |
export const material = { |
||||
dark: '#212121', |
dark: '#212121', |
||||
light: '#ffffff', |
light: '#ffffff', |
||||
|
|
||||
//200
|
//200
|
||||
blue200: '#90caf9', |
blue200: '#90caf9', |
||||
|
|
||||
//400
|
//400
|
||||
blue400: '#5c6bc0', |
blue400: '#5c6bc0', |
||||
grey400: '#bdbdbd', |
grey400: '#bdbdbd', |
||||
|
|
||||
//600
|
//600
|
||||
grey600: '#757575', |
grey600: '#757575', |
||||
|
|
||||
//700
|
//700
|
||||
grey700: '#616161', |
grey700: '#616161', |
||||
|
|
||||
//800
|
//800
|
||||
amber800: '#ff8f00', |
amber800: '#ff8f00', |
||||
blue800: '#1565c0', |
blue800: '#1565c0', |
||||
red800: '#c62828', |
red800: '#c62828', |
||||
red800dark: '#7f0000', |
red800dark: '#7f0000', |
||||
grey800: '#424242', |
grey800: '#424242', |
||||
grey800dark: '#1b1b1b', |
grey800dark: '#1b1b1b', |
||||
green800: '#2e7d32', |
green800: '#2e7d32', |
||||
} |
} |
||||
|
|
||||
export const fonts = { |
export const fonts = { |
||||
dark: '#000000', |
dark: '#000000', |
||||
light: '#ffffff' |
light: '#ffffff' |
||||
}; |
}; |
@ -1,5 +1,5 @@ |
|||||
export const squareIcons = { |
export const squareIcons = { |
||||
settings: 'cog-outline', |
settings: 'cog-outline', |
||||
toggleOn: 'toggle-switch-on', |
toggleOn: 'toggle-switch-on', |
||||
toggleOfff: 'toggle-switch-off' |
toggleOfff: 'toggle-switch-off' |
||||
}; |
}; |
Loading…
Reference in new issue