Skip to content

Instantly share code, notes, and snippets.

@codeopensrc
Last active June 10, 2016 19:50
Show Gist options
  • Save codeopensrc/7d0d820508a7df1828be213b9120d104 to your computer and use it in GitHub Desktop.
Save codeopensrc/7d0d820508a7df1828be213b9120d104 to your computer and use it in GitHub Desktop.
Examples using my RealmWrapper.js and RealmSchema.js

Examples for my RealmWrapper.js and RealmSchema.js

You can find the RealmWrapper here and RealmSchema here

//addRealmListener(fn)
this.realm.addRealmListener(this.updateImgs.bind(this));


//removeRealmListener(fn)
this.realm.removeRealmListener(this.updateImgs.bind(this));


//create(objectType, properties)  -- This just provides the native realm.create method for use, wrapped in a write
this.realm.create("SomeRealmObj", {prop1: true, prop2: 5})


//getOne(objectType, query) and write(transactions)
//Provided a fuller example for this one
let kv = {
  realmType: this.props.realmType, //SalesInfo in this case
  key: this.props.title,
  value: this.state.val
  kv.id = this.state.id
}
const queryString = {
  'SalesInfo' : `id == '${kv.id}' AND parentId == '${this.state.dev.survey.id}'`
}
let objectToUpdate = this.realm.getOne(kv.realmType, queryString[kv.realmType]);
this.realm.write(() => { objectToUpdate[kv.key] = String(kv.value) })


//delete(objectType, query) and updateProp(objectType, properties)
let lockedDev = {
  id: parseInt(dev.idnum),
  name: dev.name,
  county: dev.county,
  address: dev.address,
  latlng: dev.latlng
}
this.realm.delete('Assigned', `id == ${lockedDev.id}`);
this.realm.updateProp('Locked', lockedDev)


//updateList(objectType, objId, listName, realmListProps)
this.realm.updateList('Development', dev.idnum, 'images', { id: img.imgId, parentId: dev.idnum, data: img.data })


//deleteAll()
// Wipes the DB

//queryList(realmObj, query)
//I don't have a regular use case for using the Realm query result itself

//queryToArray(objectType, query)
// I use this method the most
let rows = this.realm.queryToArray('Assigned');
let rows2 = this.realm.queryToArray('Locked');


//listToArray(object, listName)
let realmSurvey = this.realm.getOne('Survey', `id == '${dev.survey.id}' AND parentId == '${dev.id}'`);
//Converting to JSON to pass to another server in this particular case
// Allows us to manipulate the data before sendoff, why we need a .toJSON method mentioned in the RealmWrapper
development.survey = JSON.parse(JSON.stringify(realmSurvey)); 
development.survey.salesinfo = this.realm.listToArray(realmSurvey, "salesinfo");


//======= Internal method examples (used soley in the RealmWrapper file) ====
//=========================================================================

//construct(objectType, properties)
// Used internally -- see updateList() in RealmWrapper
let newRealmObject = this.construct(objectType, {id: objId})


//deconstruct(objectType, query)
// Used internally in the RealmWrapper to access the Realm Objects children -- see query() below
let listChildren = this.deconstruct(objectType, query)


//query(objectType, query)
//  Used internally. This is actually copy paste of the delete(objectType, query) method
let listChildren = this.deconstruct(objectType, query)
let results = this.query(objectType, query);
 // this.realm in this case is the node module "realm" as this example is used in the wrapper.
 // Confusing I know
this.realm.write(() => {
    listChildren.forEach((child) => { this.realm.delete(child) })
    this.realm.delete(results);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment