Even though indexedDB lacks findOne or find functions like MongoDB, my goal is to achieve a similar functionality. I have a data store in indexedDB with an index created on the stop_id
.
The objective is to retrieve all documents in the store that contain a specific stop_id
. It's possible for multiple objects to share the same stop_id
.
My Current Implementation:
I'm attempting a workaround method (feel free to suggest improvements)
// This function gets called from HTML using AngularJS
$scope.findOne = function(stop_id) {
var db;
var request = indexedDB.open("Trans");
request.onerror = function(event) {
alert("Could not establish connection to Database");
};
request.onsuccess = function(event) {
db = event.target.result;
var objectStore = db.transaction("times").objectStore("times");
var index = objectStore.index("stop_id");
var range = IDBKeyRange.only(stop_id);
// Need to add code here to retrieve
// either one or all documents with the stop_id provided from the HTML
}
}
To utilize this function in the HTML:
<div class="medium-6 columns" ng-repeat="stops in objects | orderBy: 'stop_name'">
<div class="card hoverable">
<div class="content">
<span class="title">{{stops.stop_name}}</span><small class="float-right">{{stops.stop_id}}</small>
<!-- The following function will search another object store and retrieve all documents matching the stop_id -->
{{ findOne(stops.stop_id)}}</p>
</div>
</div>
</div>
I am exploring this approach because indexedDB does not support joins by default, and I intend to use native workarounds of indexedDB to fetch additional data related to an id in another datastore dynamically. Performance is not a major concern as both data stores are expected to have less than 150 items each.