IndexedDB-什么是Key,keyPath和indexName? [英] IndexedDB - What is Key, keyPath and indexName?

查看:382
本文介绍了IndexedDB-什么是Key,keyPath和indexName?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自MySQL,我习惯了常规的数据库表方案.我在理解IndexedDB及其某些术语时遇到了麻烦.我在文档中查询了这些定义:

I am coming from MySQL and I am used to the conventional database table scheme. I am having trouble understanding IndexedDB and some of its terminology. I looked up these definitions in the documentation:

一个数据值,通过该数据值可以在对象存储中组织和检索存储的值.

Key A data value by which stored values are organized and retrieved in the object store.

indexName 要创建的索引的名称.

indexName The name of the index to create.

keyPath 要使用的索引的关键路径.

keyPath The key path for the index to use.

基本上,类似于MySQL中的主键,对吗? indexName 与列相同吗?而且我不知道 keyPath 是什么.

Basically, Key is like a Primary Key in MySQL, right? Is indexName the same thing as a column? And I don't understand what a keyPath is.

有人可以为我解释这些吗?再次感谢您的耐心等候:).

Can someone explain these for me? Thank you again for you patience :).

推荐答案

是的,key就像SQL中的主键一样.但是其他人似乎缺少解释您问题主要部分的示例,这就是indexNamekeyPath之间的区别.根据创建索引上的Mozilla页面

Yes, key is like a primary key in SQL. But others seem to be missing an example explaining the main part of your question, and that is the distinction between indexName and keyPath. Per the Mozilla page on creating an index,

indexName 要创建的索引的名称.请注意,可以使用空名称创建索引.

indexName The name of the index to create. Note that it is possible to create an index with an empty name.

keyPath 要使用的索引的关键路径.请注意,可以使用空的keyPath创建索引,也可以将序列(数组)作为keyPath传递.

keyPath The key path for the index to use. Note that it is possible to create an index with an empty keyPath, and also to pass in a sequence (array) as a keyPath.

indexName是您用来访问该索引的内容.索引用于在数据库中搜索该列". keyPath是列"的实际名称.有关keyPath可能采用的形式,请参见其他问题和解答. 请注意,列"在技术上并不正确,但是我正在使用它,因为那是您使用的.

The indexName is what you use to access that index. Indexes are used to search that "column" in the database. The keyPath is the actual name of the "column." See other questions and answers for what forms a keyPath may take. Note that "column" is not technically correct, but I'm using it because that's what you used.

例如,假设您的数据具有列hours,并且您希望能够在该列上搜索数据库.创建数据库时,您将为该列创建索引:

For example, suppose your data has the column hours and you want to be able to search your database on that column. When creating your database you would create an index for that column:

objectStore.createIndex(indexName, keyPath, { unique: false });

indexName可以是您想要的任何内容,例如 hoursColumn ,而keyPath可以是 hours .

where indexName can be anything you want, let's say hoursColumn, and keyPath would be hours.

objectStore.createIndex("hoursColumn", "hours", { unique: false });

unique: false仅表示小时中其他数据行可能具有相同的值.

unique: false just means that other rows of data may have the same value for hours.

我可以按如下方式将数据写入objectStore:

I can write data to the objectStore as follows:

db.transaction(storeName, "readwrite").objectStore(storeName).add({hours: 20, minutes: 30});

因此要在小时列上搜索数据,您可以编写:

So to search your data on the column hours, you could write:

var data = db.transaction(storeName).objectStore(storeName).index("hoursColumn").get(20)

,结果将是第一行数据,其中小时 = 20,例如{hours: 20, minutes: 30}

and the result would be the first row of data where hours = 20, e.g. {hours: 20, minutes: 30}

总而言之, indexName 就是您要创建的要搜索的索引,而 keyPath 是您在其上存储的数据的实际名称.要搜索.

So to summarize, indexName is just what you call the index you created that you want to search, and keyPath is the actual name of the stored data on which you want to search.

这篇关于IndexedDB-什么是Key,keyPath和indexName?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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