IndexedDB:你可以使用数组元素作为键或索引吗? [英] IndexedDB: Can you use an array element as a key or an index?

查看:179
本文介绍了IndexedDB:你可以使用数组元素作为键或索引吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面的对象存储,将 domain 键设置为 keyPath

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 - single tag
    { domain: "youtube", 

      bold:["*",0],
      strikethrough:["-",0],
      italic:["_",0] 
    },  


    { domain: "stackoverflow", 

      bold:["<strong>",1], 
      italic:["<em>",1],
      strikethrough:["<del>",1],
      superscript:["<sup>",1],
      subscript:["<sub>",1],
      heading1:["<h1>",1],
      heading2:["<h2>",1],
      heading3:["<h3>",1],
      blockquote:["<blockquote>",1],
      code:["<code>",1],
      newline:["<br>",2],
      horizontal:["<hr>",2]
    }   
];  

上述代码工作正常,可以轻松高效地进行查找。但是,在许多情况下,除了 domain 属性,商店中的两个对象完全相同。

The above code works fine and lets me do look-ups easily and efficiently. However, there are many cases where two objects in the store are completely identical except for their domain attribute.

例如,我想将所有Stack Exchange站点的对象添加到存储,所有这些对象将等于StackOverflow的对象。

For example, I want to add objects for all of the Stack Exchange sites to the store, and all of those objects would be equal to the one for StackOverflow.

因此,我不想创建许多单独的对象,我想这样做:

So, rather than create many separate objects, I want to do something like this:

var tags  = [ 
    //codes: 0 - markdown wrap tag
    //       1 - HTML wrap tag 
    //       2 - single tag
    { domain: ["youtube"], 

      bold:["*",0],
      strikethrough:["-",0],
      italic:["_",0] 
    },  


    { domain: ["stackoverflow","stackexchange",...], 

      bold:["<strong>",1], 
      italic:["<em>",1],
      strikethrough:["<del>",1],
      superscript:["<sup>",1],
      subscript:["<sub>",1],
      heading1:["<h1>",1],
      heading2:["<h2>",1],
      heading3:["<h3>",1],
      blockquote:["<blockquote>",1],
      code:["<code>",1],
      newline:["<br>",2],
      horizontal:["<hr>",2]
    }   
];  

可以使用 KeyGen 而不是一个 keyPath ,并设置一些类型的索引取值并在 domain key?

Would it be possible to use a KeyGen rather than a keyPath and set up some kind of index that took a value and searched for it in the arrays pointed to by the domain key?

或者我每次要查找时都要使用光标?

Or would I have to use a cursor each time I want to do a look up?

一些有用的参考资料包括:

Some potentially helpful references are:

http:// www.w3.org/TR/IndexedDB/#key-path-construct

https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB

推荐答案

解决方案是使用 multiEntry 键属性设置为 true

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

请参阅此链接(感谢@kyaw Tun)

see this link (thanks @kyaw Tun)


每个索引也有一个multiEntry标志。当评估索引的键路径的结果产生一个数组时,此标志影响索引的行为。如果multiEntry标志为false,则将其键为数组的单个记录添加到索引。如果multiEntry标志为true,则将一个记录添加到数组中每个项目的索引。每个记录的键是数组中相应项的值。

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.

使用此 code>,不再需要特定的 keyPath ,因此为简单起见,您只需使用 keyGen 即可。

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

因此,要创建数据库:

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){...};

这篇关于IndexedDB:你可以使用数组元素作为键或索引吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆