IndexedDB getAll() ordering
Asked Answered
S

1

6

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

Shockley answered 31/1, 2017 at 23:8 Comment(0)
M
12

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.

Meridional answered 1/2, 2017 at 8:37 Comment(1)
Not only index. ObjectStore is also sorted.Hillock

© 2022 - 2024 — McMap. All rights reserved.