Commit 6630ca24 authored by Romain Courteaud's avatar Romain Courteaud

WIP [indexedDB] Add pseudo index support in allDocs

parent 20b66153
...@@ -65,7 +65,7 @@ ...@@ -65,7 +65,7 @@
} }
IndexedDBStorage.prototype.hasCapacity = function (name) { IndexedDBStorage.prototype.hasCapacity = function (name) {
return ((name === "list") || (name === "include")); return ((name === "list") || (name === "include") || (name === "index"));
}; };
function buildKeyPath(key_list) { function buildKeyPath(key_list) {
...@@ -270,7 +270,9 @@ ...@@ -270,7 +270,9 @@
IndexedDBStorage.prototype.buildQuery = function (options) { IndexedDBStorage.prototype.buildQuery = function (options) {
var result_list = [], var result_list = [],
context = this; context = this,
key = "_id",
value;
function pushIncludedMetadata(cursor) { function pushIncludedMetadata(cursor) {
result_list.push({ result_list.push({
...@@ -287,20 +289,30 @@ ...@@ -287,20 +289,30 @@
}); });
} }
if (options.index) {
if (context._index_key_list.indexOf(options.index.key) === -1) {
throw new jIO.util.jIOError(
"IndexedDB: unsupported index '" + options.index.key + "'",
400
);
}
key = INDEX_PREFIX + options.index.key;
value = options.index.value;
}
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return waitForOpenIndexedDB(context, function (db) { return waitForOpenIndexedDB(context, function (db) {
return waitForTransaction(db, ["metadata"], "readonly", return waitForTransaction(db, ["metadata"], "readonly",
function (tx) { function (tx) {
var key = "_id";
if (options.include_docs === true) { if (options.include_docs === true) {
return waitForAllSynchronousCursor( return waitForAllSynchronousCursor(
tx.objectStore("metadata").index(key).openCursor(), tx.objectStore("metadata").index(key).openCursor(value),
pushIncludedMetadata pushIncludedMetadata
); );
} }
return waitForAllSynchronousCursor( return waitForAllSynchronousCursor(
tx.objectStore("metadata").index(key).openKeyCursor(), tx.objectStore("metadata").index(key).openKeyCursor(value),
pushMetadata pushMetadata
); );
}); });
......
...@@ -710,6 +710,78 @@ ...@@ -710,6 +710,78 @@
}); });
}); });
test("Unhandled index", function () {
var context = this;
stop();
expect(3);
deleteIndexedDB(context.jio)
.then(function () {
return context.jio.allDocs({index: {key: 'a', value: '3'}});
})
.fail(function (error) {
ok(error instanceof jIO.util.jIOError);
equal(
error.message,
"IndexedDB: unsupported index 'a'"
);
equal(error.status_code, 400);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("Handled index", function () {
var context = this;
this.jio = jIO.createJIO({
type: "indexeddb",
database: "qunit",
index_key_list: ['foo']
});
stop();
expect(1);
deleteIndexedDB(context.jio)
.then(function () {
return RSVP.all([
context.jio.put("1", {"foo": "bar"}),
context.jio.put("2", {"foo": "bar2"}),
context.jio.put("3", {"foo2": "bar"}),
context.jio.put("4", {"foo": "bar"})
]);
})
.then(function () {
return context.jio.allDocs({index: {key: 'foo', value: 'bar'}});
})
.then(function (result) {
deepEqual(result, {
"data": {
"rows": [
{
"id": "1",
"value": {}
},
{
"id": "4",
"value": {}
}
],
"total_rows": 2
}
});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// indexeddbStorage.get // indexeddbStorage.get
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment