Skip to content

Latest commit

 

History

History
35 lines (29 loc) · 2.03 KB

how-to-do-a-count-in-indexeddb.md

File metadata and controls

35 lines (29 loc) · 2.03 KB
title authors intro types categories published updated status
How to do a Count in IndexedDB
thebeebs
I had a question from a customer today where he as...
shorts
indexeddb
internet-explorer
2011/11/08 12:00:00
2011/11/08 13:00:00
archived

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);
 };