Last active
August 8, 2019 09:39
-
-
Save Peelz/5a902779902445c4fa6be398d51f9de9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#### db.js #### | |
import Realm from 'realm' | |
import Log from './schema/Log'; | |
import Setting from './schema/Setting'; | |
import UserProfile from './schema/UserProfile'; | |
import { asyncGetEncyptionKey } from '../src/utils'; | |
class db { | |
static realmInstance = null | |
static schema = [ | |
Log, | |
Setting, | |
UserProfile | |
] | |
static async constructor(key=null) { | |
this.key = key | |
} | |
static async getDb() { | |
try { | |
console.log(this.realmInstance); | |
if (this.key === null) { | |
this.key = await asyncGetEncyptionKey() # to get EncryptionKey from asyncronouse process (ex. from key store) | |
} | |
if (this.realmInstance !== null) { | |
return this.realmInstance | |
} else { | |
this.realmInstance = new Realm({ | |
schema: this.schema, | |
encryptionKey: this.key | |
}) | |
return this.realmInstance | |
} | |
} catch(error) { | |
console.error("getDb", error); | |
} | |
} | |
} | |
export default db | |
### Screen.js ### | |
import React from 'react' | |
import db from './db' | |
class Screen extends React.Component { | |
async componentDidMount() { | |
const realm = await db.getDb() | |
let users = db.getDb().objects("User") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment