From c2ca42a6ad4e1a50a3f4d38fbe12cd29b9dba212 Mon Sep 17 00:00:00 2001
From: Tristan Cavelier <tristan.cavelier@tiolive.com>
Date: Tue, 1 Apr 2014 17:31:42 +0200
Subject: [PATCH] allDocs prototype added for indexeddbstorage

---
 src/jio.storage/indexeddbstorage.js | 56 +++++++++++++++++++++++++++--
 1 file changed, 54 insertions(+), 2 deletions(-)

diff --git a/src/jio.storage/indexeddbstorage.js b/src/jio.storage/indexeddbstorage.js
index e057eb7..4dc2b25 100644
--- a/src/jio.storage/indexeddbstorage.js
+++ b/src/jio.storage/indexeddbstorage.js
@@ -88,10 +88,11 @@
         // If we reach this point, the database is created.
         // There is no way to cancel the operation from here.
         // So let's continue.
-        var db = request.result;
-        db.createObjectStore("metadata", {
+        var db = request.result,
+         store = db.createObjectStore("metadata", {
           "keyPath": "_id"
         });
+        store.createIndex("_id", "_id");
         status = "created";
       };
       request.onerror = function () {
@@ -227,6 +228,57 @@
       }, command.error, command.notify);
   };
 
+  IndexedDBStorage.prototype.getList = function () {
+    var rows = [], onCancel, request = indexedDB.open(this._database_name);
+    return new Promise(function (resolve, reject) {
+      request.onsuccess = function () {
+        var db, tx, store, index, indexrequest;
+        db = request.result;
+        tx = db.transaction("metadata", "readonly");
+        onCancel = function () {
+          tx.abort();
+          db.close();
+        };
+        store = tx.objectStore("metadata");
+        index = store.index("_id");
+
+        indexrequest = index.openCursor();
+        indexrequest.onsuccess = function () {
+          var cursor = indexrequest.result;
+          if (cursor) {
+            // Called for each matching record.
+            rows.push({
+              "id": cursor.value._id,
+              "doc": cursor.value,
+              "values": {}
+            });
+            cursor.continue();
+          } else {
+            // No more matching records.
+            resolve({"data": {"rows": rows, "total_rows": rows.length}});
+            db.close();
+          }
+        };
+      };
+      request.onerror = function () {
+        reject(request.error);
+        var db = request.result;
+        if (db) { db.close(); }
+      };
+    }, function () {
+      if (typeof onCancel === "function") {
+        onCancel();
+      }
+    });
+  };
+
+  IndexedDBStorage.prototype.allDocs = function (command, param, option) {
+    /*jslint unparam: true */
+    this.createDBIfNecessary().
+      then(this.getList.bind(this)).
+      then(command.success, command.error, command.notify);
+  };
+
   jIO.addStorage("indexeddb", IndexedDBStorage);
 
 }));
-- 
2.30.9