Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IndexedDB getAll() ordering

I'm using getAll() method to get all items from db.

db.transaction('history', 'readonly').objectStore('history').getAll().onsuccess = ...

My ObjectStore is defined as:

db.createObjectStore('history', { keyPath: 'id', autoIncrement: true });

Can I count on the ordering of the items I get? Will they always be sorted by primary key id?
(or is there a way to specify sort explicitly?)

I could not find any info about ordering in official docs

like image 806
icl7126 Avatar asked Jan 31 '17 23:01

icl7126


1 Answers

If the docs don't help, consult the specs:

  1. getAll refers to "steps for retrieving multiple referenced values"
  2. the retrieval steps refer to "first count records in index"
  3. the specification of index contains the following paragraph:

    The records in an index are always sorted according to the record's key. However unlike object stores, a given index can contain multiple records with the same key. Such records are additionally sorted according to the index's record's value (meaning the key of the record in the referenced object store).

Reading backwards: An index is sorted. getAll retrieves the first N of an index, i.e. it is order-preserving. Therefore the result itself should retain the sort order.

like image 90
the8472 Avatar answered Oct 14 '22 02:10

the8472