Last active
March 12, 2016 00:06
-
-
Save erichulburd/52d5a1964414730a4f27 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// databasable.js | |
import Loki from 'lokijs/src/lokijs'; | |
import LokiIndexedAdapter from 'lokijs/src/loki-indexed-adapter'; | |
const DEFAULTS = { | |
autosave: false | |
}; | |
var databasable = { | |
accessDb: function(db_name){ | |
var databasable = this, | |
opts = Object.assign({ | |
adapter: new LokiIndexedAdapter(db_name) | |
}, DEFAULTS, databasable.lokijs_options || {}), | |
has_adapter = !!opts.adapter; | |
return new Promise((fnResolve, fnReject)=>{ | |
if (!databasable.db) { | |
databasable.db = new Loki(db_name, opts); | |
if (has_adapter){ | |
databasable.db.loadDatabase({}, ()=>{ | |
fnResolve(databasable.db); | |
}); | |
} else { fnResolve(databasable.db); } | |
} else { fnResolve(databasable.db); } | |
}); | |
}, | |
closeDb: function(){ | |
var databasable = this; | |
if (databasable.db){ | |
databasable.db.save(); | |
databasable.db.close(); | |
databasable.db = undefined; | |
} | |
}, | |
collection: function(db_name, collection_name, options){ | |
var databasable = this; | |
options = options || {}; | |
return databasable.accessDb(db_name) | |
.then((db)=>{ | |
if (!db || db !== databasable.db){ | |
throw new Error('Databasable does not have db set.') | |
} | |
var collection = db.getCollection(collection_name) | |
if (!collection){ | |
collection = db.addCollection(collection_name, options); | |
if (options && options.unique_indices){ | |
options.unique_indices.forEach((field)=>{ | |
collection.ensureUniqueIndex(field); | |
}); | |
} | |
} | |
return collection; | |
}); | |
} | |
} | |
// house.js | |
class House { | |
// must be initiated with a dataset already in Loki database (not directly JSON). | |
constructor(data){ | |
var house = this; | |
house.data = data; | |
house.state = {}; | |
Object.assign(house, Databasable); | |
} | |
setEnergyData(energy_range){ | |
var house = this; | |
house.state.energy_range = energy_range; | |
return house.collection(house.scoped_id, EnergyDatum.NAME, EnergyDatum.COLLECTION_OPTIONS) | |
.then((energy_collection)=>{ | |
return house.ensureEnergyData() | |
.then((res)=>{ | |
console.log(res) // Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined} | |
var params = house.rangeToLokiParams('day', house.state.energy_range); | |
// and House#saveEnergyData has not finished - no data retrived from LokiJs database. | |
house.energy_data = energy_collection.find(params) | |
.sort((pd1, pd2)=>{ | |
if (pd1.day === pd2.day) return 0; | |
if (pd1.day > pd2.day) return 1; | |
if (pd1.day < pd2.day) return -1; | |
}) | |
.map((data)=>{ return new EnergyDatum(data, house); }); | |
}); | |
}); | |
} | |
ensureEnergyData(){ | |
var house = this, | |
query_ranges = DateRange.addRange(house.state.energy_range, house.data.energy_datum_ranges || []); | |
if (query_ranges.gaps_filled.length > 0){ | |
return house.getEnergyData({dates: query_ranges.gaps_filled}) | |
.then((energy_data)=>{ | |
return house.saveEnergyData(energy_data, query_ranges.new_ranges) | |
}); | |
} else { return Promise.resolve(); } | |
} | |
getEnergyData(params){ | |
return jQuery.ajax({url: 'yada.com/action', type: 'POST', data: params}) | |
.then((res)=>{ return res.data }); | |
} | |
// save new energy data to LokiJs Db, as well as | |
// the new energy data query ranges (ie house metadata). | |
saveEnergyData(energy_data, new_ranges){ | |
var house = this; | |
return house.collection(house.scoped_id, EnergyDatum.NAME, EnergyDatum.COLLECTION_OPTIONS) | |
.then((energy_collection)=>{ | |
energy_collection.insert(energy_data); | |
house.db.save(); | |
house.data.energy_datum_ranges = new_ranges; | |
house.save(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment