How to do a Count in IndexedDB


I had a question from a customer today where he asked How can I count the records in an IndexedDb database.

The current IndexedDB prototype for IE8 and IE9 and the IE10 platform preview 3 do not include a count() implementation

The latest Version (from April) of the IndexedDb working draft over at the W3C doesn’t include a count property on the objectStore object.

However the he latest version of the editors draft does. After a little bit of reading it appears that the proposed method for performing a count with the new count would be along the lines of:

  var keyRange = IDBKeyRange.lowerBound(0);
  var theCount = store.count(keyRange);

As far as I know no browser vendor yet supports this and so you’d need to do something along the lines of:

var keyRange = IDBKeyRange.lowerBound(0);
var cursorRequest = store.openCursor(keyRange);
var count = 0;

cursorRequest.onsuccess = function(e) {        
	var result = e.target.result;
	result ? ++count && result.continue() : console.log(count);
 };

	
Comments (0)

Skip to main content