I have set up an index in my database called time
on the created_at
field:
var os = thisDB.createObjectStore(name, { keyPath : "id" });
os.createIndex("time", "created_at", {unique: false });
Instead of storing the time as a standard date format, I convert it to a unix integer:
entry['created_at'] = Date.new(entry['created_at']).unix();
var request = sto.add(entry);
This results in the data appearing like this in the console:
My issue lies in retrieving the data in either ascending (asc
) or descending (desc
) order based on the created at time:
When I use a cursor to loop through the data:
function open_cursor(){
var objectStore = ENTRYDB.transaction('entries').objectStore('entries').index('time');
var request = objectStore.openCursor();
request.onsuccess = function(event) {handle_open(event)};
}
function handle_open(evt){
var cursor = evt.target.result;
if (cursor) {
console.log(cursor.value);
cursor.continue();
}
}
The data is not sorted by the created at time as expected, and appears random instead! Any assistance on this matter would be greatly appreciated. THANKS