Skip to content Skip to sidebar Skip to footer

Indexeddb: Can You Use An Array Element As A Key Or An Index?

Consider the following object store, with the domain key set as the keyPath var tags = [ //codes: 0 - markdown wrap tag // 1 - HTML wrap tag // 2 - singl

Solution 1:

The solution is to use an index with the multiEntry key property set to true

see this link (thanks @kyaw Tun)

Each index also has a multiEntry flag. This flag affects how the index behaves when the result of evaluating the index's key path yields an Array. If the multiEntry flag is false, then a single record whose key is an Array is added to the index. If the multiEntry flag is true, then the one record is added to the index for each item in the Array. The key for each record is the value of respective item in the Array.

Armed with this index, a specific keyPath is no longer necessary, so you can just use a keyGen for simplicity.


So, to create the database:

request.onupgradeneeded = function(event) 
{
   var db = event.target.result;
   var objectStore = db.createObjectStore("domains", {autoIncrement: true });
   objectStore.createIndex("domain", "domain", { unique: true, multiEntry: true });
   for(var i in tags)
   {
       objectStore.add(tags[i]);
       console.log("added " + tags[i]["domain"] + " to the DB");
   }
};  

and an example of using a domain to query for an object:

var objectStore = db.transaction("domains").objectStore("domains");
    var query = objectStore.index("domain").get(queryURL);
    query.onsuccess = function(event){...};

Post a Comment for "Indexeddb: Can You Use An Array Element As A Key Or An Index?"