10

I want to basic count the number of records in my indexedDB database.

Currently my code looks like

Javascript

var transaction = db.transaction(["data"], "readonly");
var objectStore = transaction.objectStore("data");
var cursor = objectStore.openCursor();  
    var count = objectStore.count();
    console.log(count); 

I would love for this to say output just 3, but instead i get.

Output

        IDBRequest {onerror: null, onsuccess: null, readyState: "pending", transaction: IDBTransaction, source: IDBObjectStore…}
    error: null
    onerror: null
    onsuccess: null
    readyState: "done"
    result: 3
    source: IDBObjectStore

transaction: IDBTransaction
__proto__: IDBRequest

Which is correct but I just want it to say 3 not loads of other stuff.

Josh
  • 17,834
  • 7
  • 50
  • 68
Brent
  • 2,385
  • 10
  • 39
  • 63
  • Just to clarify, you'd like to count the number of objects in your `objectstore` rather than the `database`? You can count the number of `objectstores` at the database-level but not objects themselves. – buley Mar 18 '14 at 15:54

3 Answers3

14

Bring back record count with a little less code:

var store = db.transaction(['trans']).objectStore('trans');
var count = store.count();
count.onsuccess = function() {
    console.log(count.result);
}
AssemblyX
  • 1,841
  • 1
  • 13
  • 15
6

Try something like this:

var transaction = db.transaction(["data"], "readonly");
var objectStore = transaction.objectStore("data"); 
var count = objectStore.count();

count.onsuccess = function() {
    console.log(count.result);
};
3

A little bit of introduction in order. From my personal docs on transactions:

Certain transactions return data, or "results", from the database. These transactions are called "requests" and with the exception of database opening, the values are always various combinations of object "keys" and "values" and instances of IDBRequest. Request transactions are just that: a transaction request," namely the act of asking for something rather than the getting of it. A programmer encounters them when dealing with IDBObjectStore, IDBIndex or IDBCursor objects.

What you're looking at is an IDBRequest object, which is returned by the count() method. That represents the request for data, and not the data itself.

The data itself is available after the complete event fires, and can be accessed via the IDBRequest.result property.

Here's a tested count method from my library, dash:

API.entries.count = function (count_ctx) {
  var request;
  if (API.exists(count_ctx.index)) {
    count_ctx.idx = count_ctx.objectstore.index(count_ctx.index);
    request = API.isEmpty(count_ctx.key) ? count_ctx.idx.count() : count_ctx.idx.count(count_ctx.key);
  } else {
    request = API.isEmpty(count_ctx.key) ? count_ctx.objectstore.count() : count_ctx.objectstore.count(count_ctx.key);
  }

  count_ctx.transaction.addEventListener('error', function (event) {
    count_ctx.error = event.target.error.message;
    API.error(count_ctx);
  });

  request.addEventListener('success', function () {
    count_ctx.total = request.result;
    API.success(count_ctx);
  });

I'll note that I probably should have used the complete event rather than the success event. I can't explain why but sometimes result values are not available in success callbacks.

buley
  • 28,032
  • 17
  • 85
  • 106
  • Could you explain how implement this in my code? I really thankfull you taking time to explain your answer – Brent Mar 18 '14 at 16:10
  • 1
    Of course! I believe you want to attach a callback to the transaction you've created and listen for its completion. Then reach into `event.result` and you should find an integer representing the count of objects: https://gist.github.com/editor/21aeefd96e6b853c5671 – buley Mar 18 '14 at 16:13
  • When trying this i get count undefined, will try debugging it – Brent Mar 18 '14 at 16:17
  • I goofed up on the gist. Try checking `count.result` (it should be updated now). It's the `IDBRequest ` that has a value, not the event. – buley Mar 18 '14 at 16:22
  • Boom, you god! Thanks. I've just had to recode a localstorage website and convert everything. IndexedDB seems really good just allot to learn I guess but with the amount of storage i guess its going to be use alot more! – Brent Mar 18 '14 at 16:24
  • Here's to hoping. [Follow me on Twitter](http://twitter.com/taylorbuley) if you want more IDB news. Trying to help create a community around the folks using the technology – buley Mar 18 '14 at 16:27
  • 1
    I'll make sure to keep my eyes open, will try keep active on this section. Followed :) – Brent Mar 18 '14 at 16:29
  • 1
    "sometimes result values are not available" may be related to this issue http://lists.w3.org/Archives/Public/public-webapps/2014JanMar/0575.html I prefer as current behaviour, i.e, you have to use transaction complete to get definite result. But performance gain is not ignorable. – Kyaw Tun Mar 18 '14 at 23:10
  • Voted this down because it appears more like a promo for the respondent's own library than a genuine answer to the OP's question. – Velojet Mar 03 '16 at 21:39