I have seen multiple JavaScript examples of using createIndex
to define an ObjectStore index directly after the ObjectStore has been created like this:
var objectStore = ixDb.createObjectStore(osName, { keyPath: pkName, autoIncrement: autoIncrement });
objectStore.createIndex("name", "name", { unique: false });
Can anyone show me how you would use createIndex
on a pre-existing table without calling createObjectStore
? I guess the real question here is how to get a reference to the objectStore without using createObjectStore
?
I tried several variations of the following with no luck:
var objectStore = window.IDBTransaction.objectStore(ObjectStoreName);
var index = objectStore.createIndex(ixName, fieldName, { unique: unique, multiEntry: multiEntry });
It lets you store just about anything in the user's browser. In addition to the usual search, get, and put actions, IndexedDB also supports transactions. Here is the definition of IndexedDB on MDN: "IndexedDB is a low-level API for client-side storage of significant amounts of structured data, including files/blobs.
IndexedDB follows a same-origin policy. So while you can access stored data within a domain, you cannot access data across different domains.
You do it during onupgradeneeded
, which should be the same place you are making the object store in the first place. In there, you can do whatever appropriate action is needed to upgrade it, such as creating a new index. Here is an example.
Chrome does not currently support the onupgradeneeded event. If you wanted to get a reference to an ObjectStore using the old or new W3 specs, this is one possible workaround via the onsuccess event using either the old setVersion command or via the new onupgradeneeded event:
var ixDb;
var ixDbRequest;
var ixDbVersionTansaction;
//Check to see if we have a browser that supports IndexedDB
if (window.indexedDB) {
ixDbRequest = window.indexedDB.open(dbName, dbVersion);
//For browsers like chrome that support the old set version method
ixDbRequest.onsuccess = function (e) {
ixDb = ixDbRequest.result || e.result;
if (typeof ixDb.setVersion === "function") {
//Put your version checking logic here
if (oldVersion < newVersion) {
var verRequest = ixDb.setVersion(newVersion);
verRequest.onerror = function (e) {
//handling error logic here
}
verRequest.onsuccess = function (e) {
//Get a reference to the version transaction
//from the old setVersion method.
ixDbVersionTansaction = verRequest.result;
//Create database using function provided by the user.
UserFunction();
}
}
}
};
ixDbRequest.onupgradeneeded = function (e) {
//FF uses this event to fire the transaction for upgrades.
//All browsers will eventually use this method. Per - W3C Working Draft 24 May 2012
ixDb = ixDbRequest.result || e.currentTarget.result;
//Get a reference to the version transaction via the onupgradeneeded event (e)
ixDbVersionTansaction = e.currentTarget.transaction;
//Create database using function provided by the user.
UserFunction();
};
UserFunction(){
//ObjectStore is accessed via ixDbVersionTansaction variable
// in either instance (transaction..objectStore("ObjectStoreName"))
var ObjectStore = ixDbVersionTansaction.objectStore("ObjectStoreName");
var index = ObjectStore.createIndex("ixName", "fieldName");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With