Commit b07c5b5b authored by Tristan Cavelier's avatar Tristan Cavelier

indices added to indexedDB description

parent 17965461
...@@ -13,12 +13,14 @@ ...@@ -13,12 +13,14 @@
* *
* { * {
* "type": "indexeddb", * "type": "indexeddb",
* "database": <string> * "database": <string>,
* "version": <number>, (default is `1`)
* "indices": <array of strings>
* } * }
* *
* The database name will be prefixed by "jio:", so if the database property is * The database name will be prefixed by "jio:", so if the database property is
* "hello", then you can manually reach this database with * "hello", then you can manually reach this database with
* `indexedDB.open("jio:hello");`. (Or * `indexedDB.open("jio:hello", <version>);`. (Or
* `indexedDB.deleteDatabase("jio:hello");`.) * `indexedDB.deleteDatabase("jio:hello");`.)
* *
* For more informations: * For more informations:
...@@ -78,25 +80,73 @@ ...@@ -78,25 +80,73 @@
"must be a non-empty string"); "must be a non-empty string");
} }
this._database_name = "jio:" + description.database; this._database_name = "jio:" + description.database;
if (Array.isArray(description.indices)) {
this._indices = description.indices;
} else {
this._indices = [];
}
var i, l, index;
for (i = 0, l = this._indices.length; i < l; i += 1) {
index = this._indices[i];
if (typeof index !== "string" || index === "") {
throw new TypeError("IndexedDBStorage 'indices' must be an " +
"array of non empty strings.");
}
}
} }
// XXX doc string // XXX doc string
function openRequestOnUpgradeNeeded(shared, event) { function openRequestOnUpgradeNeeded(shared, event) {
if (shared.aborted) { return; } if (shared.aborted) { return; }
shared.db = event.target.result; shared.db = event.target.result;
try {
try { try {
shared.store = shared.db.createObjectStore("metadata", { shared.store = shared.db.createObjectStore("metadata", {
"keyPath": "_id" "keyPath": "_id"
//"autoIncrement": true //"autoIncrement": true
}); });
} catch (e0) {
// store already exists
shared.store = event.target.transaction.objectStore("metadata");
}
// `createObjectStore` can throw InvalidStateError - open_req.onerror // `createObjectStore` can throw InvalidStateError - open_req.onerror
// and db.onerror won't be called. // and db.onerror won't be called.
try {
shared.store.createIndex("_id", "_id"); shared.store.createIndex("_id", "_id");
// `store.createIndex` can throw an error // `store.createIndex` can throw an error
shared.db_created = true; } catch (ignore) {
// index already exists
}
var i, l, index;
for (i = 0, l = shared.connector._indices.length; i < l; i += 1) {
index = shared.connector._indices[i];
try {
shared.store.createIndex(index, index);
} catch (ignore) {
// index already exist
}
}
try {
shared.store = shared.db.createObjectStore("attachment", {
"keyPath": ["id", "attachment"]
});
} catch (ignore) {
// store already exists
}
try {
shared.store.createIndex("id,attachment", ["id", "attachment"]);
} catch (ignore) {
// index already exist
}
shared.store.transaction.oncomplete = function () { shared.store.transaction.oncomplete = function () {
delete shared.store; delete shared.store;
}; };
shared.db_created = true;
} catch (e) { } catch (e) {
shared.reject(e); shared.reject(e);
shared.db.close(); shared.db.close();
......
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