最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - indexeddb get all keys from object store - Stack Overflow

programmeradmin7浏览0评论

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?

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?

Share Improve this question asked Mar 14, 2013 at 16:05 mnowotkamnowotka 17.3k20 gold badges91 silver badges139 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

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.

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/en-US/docs/Web/API/IDBObjectStore/getAllKeys

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

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

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

发布评论

评论列表(0)

  1. 暂无评论