mnowotka
mnowotka

Reputation: 17218

indexeddb get all keys from object store

I'm new to indexeddb. Let's say I put several objects to indexed db:

transaction.objectStore("some_store").put(some_object, some_key);

Now I want to get all keys from that object store. Is that possible? If yes, how?

Upvotes: 4

Views: 6413

Answers (3)

jpoppe
jpoppe

Reputation: 2366

There is a IDBObjectStore.getAllKeys() method, which will return all keys from an object store.

For more information about this method see: https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/getAllKeys

You could use this together with the IDBObjectStore.getAll() method to combine the results.

The drawback is that no data should be added to the store between executing those methods.

Upvotes: 2

Kyaw Tun
Kyaw Tun

Reputation: 13131

Possible as Kristof said by using openCursor method. It is not efficient because requesting value cursor object might involve de-serialization.

You should also note that, your put method return primary key of the inserted object.

Currently, if you want very efficient keys retrive, index the keyPath for in-line key object store. For out-of-line object store you are out of luck. Using index, you can retrive keys as follow:

transaction.objectStore("some_store").index('id').openKeyCursor(); // here id is primary key path

There is a bug report for requesting openKeyCursor method directly object store. Hopefully next IndexedDB spec will have it.

Upvotes: 3

Kristof Degrave
Kristof Degrave

Reputation: 4180

You will need to use the openCursor method to retrieve al records 1 by 1. Only getting the keys isn't possible.

Upvotes: 1

Related Questions