Skip to content

Instantly share code, notes, and snippets.

@Peelz
Last active August 8, 2019 09:39
Show Gist options
  • Save Peelz/5a902779902445c4fa6be398d51f9de9 to your computer and use it in GitHub Desktop.
Save Peelz/5a902779902445c4fa6be398d51f9de9 to your computer and use it in GitHub Desktop.
#### 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